summaryrefslogtreecommitdiffstats
path: root/plugins/check_nt.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_nt.c')
-rw-r--r--plugins/check_nt.c527
1 files changed, 258 insertions, 269 deletions
diff --git a/plugins/check_nt.c b/plugins/check_nt.c
index dec0b668..7dd23e5c 100644
--- a/plugins/check_nt.c
+++ b/plugins/check_nt.c
@@ -13,7 +13,7 @@
13 * This plugin collects data from the NSClient service running on a 13 * This plugin collects data from the NSClient service running on a
14 * Windows NT/2000/XP/2003 server. 14 * Windows NT/2000/XP/2003 server.
15 * This plugin requires NSClient software to run on NT 15 * This plugin requires NSClient software to run on NT
16 * (http://nsclient.ready2run.nl/) 16 * (https://nsclient.org/)
17 * 17 *
18 * 18 *
19 * This program is free software: you can redistribute it and/or modify 19 * This program is free software: you can redistribute it and/or modify
@@ -39,82 +39,28 @@ const char *email = "devel@monitoring-plugins.org";
39#include "common.h" 39#include "common.h"
40#include "netutils.h" 40#include "netutils.h"
41#include "utils.h" 41#include "utils.h"
42 42#include "check_nt.d/config.h"
43enum checkvars {
44 CHECK_NONE,
45 CHECK_CLIENTVERSION,
46 CHECK_CPULOAD,
47 CHECK_UPTIME,
48 CHECK_USEDDISKSPACE,
49 CHECK_SERVICESTATE,
50 CHECK_PROCSTATE,
51 CHECK_MEMUSE,
52 CHECK_COUNTER,
53 CHECK_FILEAGE,
54 CHECK_INSTANCES
55};
56 43
57enum { 44enum {
58 MAX_VALUE_LIST = 30, 45 MAX_VALUE_LIST = 30,
59 PORT = 1248
60}; 46};
61 47
62static char *server_address = NULL;
63static int server_port = PORT;
64static char *value_list = NULL;
65static char *req_password = NULL;
66static unsigned long lvalue_list[MAX_VALUE_LIST];
67static unsigned long warning_value = 0L;
68static unsigned long critical_value = 0L;
69static bool check_warning_value = false;
70static bool check_critical_value = false;
71static enum checkvars vars_to_check = CHECK_NONE;
72static bool show_all = false;
73
74static char recv_buffer[MAX_INPUT_BUFFER]; 48static char recv_buffer[MAX_INPUT_BUFFER];
75 49
76static void fetch_data(const char *address, int port, const char *sendb); 50static void fetch_data(const char *address, int port, const char *sendb);
77static int process_arguments(int /*argc*/, char ** /*argv*/); 51
52typedef struct {
53 int errorcode;
54 check_nt_config config;
55} check_nt_config_wrapper;
56static check_nt_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
57
78static void preparelist(char *string); 58static void preparelist(char *string);
79static bool strtoularray(unsigned long *array, char *string, const char *delim); 59static bool strtoularray(unsigned long *array, char *string, const char *delim);
80static void print_help(void); 60static void print_help(void);
81void print_usage(void); 61void print_usage(void);
82 62
83int main(int argc, char **argv) { 63int main(int argc, char **argv) {
84
85 /* should be int result = STATE_UNKNOWN; */
86
87 int return_code = STATE_UNKNOWN;
88 char *send_buffer = NULL;
89 char *output_message = NULL;
90 char *perfdata = NULL;
91 char *temp_string = NULL;
92 char *temp_string_perf = NULL;
93 char *description = NULL, *counter_unit = NULL;
94 char *minval = NULL, *maxval = NULL, *errcvt = NULL;
95 char *fds = NULL, *tds = NULL;
96 char *numstr;
97
98 double total_disk_space = 0;
99 double free_disk_space = 0;
100 double percent_used_space = 0;
101 double warning_used_space = 0;
102 double critical_used_space = 0;
103 double mem_commitLimit = 0;
104 double mem_commitByte = 0;
105 double fminval = 0, fmaxval = 0;
106 unsigned long utilization;
107 unsigned long uptime;
108 unsigned long age_in_minutes;
109 double counter_value = 0.0;
110 int offset = 0;
111 int updays = 0;
112 int uphours = 0;
113 int upminutes = 0;
114
115 bool isPercent = false;
116 bool allRight = false;
117
118 setlocale(LC_ALL, ""); 64 setlocale(LC_ALL, "");
119 bindtextdomain(PACKAGE, LOCALEDIR); 65 bindtextdomain(PACKAGE, LOCALEDIR);
120 textdomain(PACKAGE); 66 textdomain(PACKAGE);
@@ -122,8 +68,12 @@ int main(int argc, char **argv) {
122 /* Parse extra opts if any */ 68 /* Parse extra opts if any */
123 argv = np_extra_opts(&argc, argv, progname); 69 argv = np_extra_opts(&argc, argv, progname);
124 70
125 if (process_arguments(argc, argv) == ERROR) 71 check_nt_config_wrapper tmp_config = process_arguments(argc, argv);
72 if (tmp_config.errorcode == ERROR) {
126 usage4(_("Could not parse arguments")); 73 usage4(_("Could not parse arguments"));
74 }
75
76 const check_nt_config config = tmp_config.config;
127 77
128 /* initialize alarm signal handling */ 78 /* initialize alarm signal handling */
129 signal(SIGALRM, socket_timeout_alarm_handler); 79 signal(SIGALRM, socket_timeout_alarm_handler);
@@ -131,49 +81,57 @@ int main(int argc, char **argv) {
131 /* set socket timeout */ 81 /* set socket timeout */
132 alarm(socket_timeout); 82 alarm(socket_timeout);
133 83
134 switch (vars_to_check) { 84 int return_code = STATE_UNKNOWN;
135 85 char *send_buffer = NULL;
86 char *output_message = NULL;
87 char *perfdata = NULL;
88 char *temp_string = NULL;
89 char *temp_string_perf = NULL;
90 char *description = NULL;
91 char *counter_unit = NULL;
92 char *errcvt = NULL;
93 unsigned long lvalue_list[MAX_VALUE_LIST];
94 switch (config.vars_to_check) {
136 case CHECK_CLIENTVERSION: 95 case CHECK_CLIENTVERSION:
137 96 xasprintf(&send_buffer, "%s&1", config.req_password);
138 xasprintf(&send_buffer, "%s&1", req_password); 97 fetch_data(config.server_address, config.server_port, send_buffer);
139 fetch_data(server_address, server_port, send_buffer); 98 if (config.value_list != NULL && strcmp(recv_buffer, config.value_list) != 0) {
140 if (value_list != NULL && strcmp(recv_buffer, value_list) != 0) { 99 xasprintf(&output_message, _("Wrong client version - running: %s, required: %s"), recv_buffer, config.value_list);
141 xasprintf(&output_message, _("Wrong client version - running: %s, required: %s"), recv_buffer, value_list);
142 return_code = STATE_WARNING; 100 return_code = STATE_WARNING;
143 } else { 101 } else {
144 xasprintf(&output_message, "%s", recv_buffer); 102 xasprintf(&output_message, "%s", recv_buffer);
145 return_code = STATE_OK; 103 return_code = STATE_OK;
146 } 104 }
147 break; 105 break;
148
149 case CHECK_CPULOAD: 106 case CHECK_CPULOAD:
150 107 if (config.value_list == NULL) {
151 if (value_list == NULL)
152 output_message = strdup(_("missing -l parameters")); 108 output_message = strdup(_("missing -l parameters"));
153 else if (!strtoularray(lvalue_list, value_list, ",")) 109 } else if (!strtoularray(lvalue_list, config.value_list, ",")) {
154 output_message = strdup(_("wrong -l parameter.")); 110 output_message = strdup(_("wrong -l parameter."));
155 else { 111 } else {
156 /* -l parameters is present with only integers */ 112 /* -l parameters is present with only integers */
157 return_code = STATE_OK; 113 return_code = STATE_OK;
158 temp_string = strdup(_("CPU Load")); 114 temp_string = strdup(_("CPU Load"));
159 temp_string_perf = strdup(" "); 115 temp_string_perf = strdup(" ");
160 116
161 /* loop until one of the parameters is wrong or not present */ 117 /* loop until one of the parameters is wrong or not present */
118 int offset = 0;
162 while (lvalue_list[0 + offset] > (unsigned long)0 && lvalue_list[0 + offset] <= (unsigned long)17280 && 119 while (lvalue_list[0 + offset] > (unsigned long)0 && lvalue_list[0 + offset] <= (unsigned long)17280 &&
163 lvalue_list[1 + offset] > (unsigned long)0 && lvalue_list[1 + offset] <= (unsigned long)100 && 120 lvalue_list[1 + offset] > (unsigned long)0 && lvalue_list[1 + offset] <= (unsigned long)100 &&
164 lvalue_list[2 + offset] > (unsigned long)0 && lvalue_list[2 + offset] <= (unsigned long)100) { 121 lvalue_list[2 + offset] > (unsigned long)0 && lvalue_list[2 + offset] <= (unsigned long)100) {
165 122
166 /* Send request and retrieve data */ 123 /* Send request and retrieve data */
167 xasprintf(&send_buffer, "%s&2&%lu", req_password, lvalue_list[0 + offset]); 124 xasprintf(&send_buffer, "%s&2&%lu", config.req_password, lvalue_list[0 + offset]);
168 fetch_data(server_address, server_port, send_buffer); 125 fetch_data(config.server_address, config.server_port, send_buffer);
169 126
170 utilization = strtoul(recv_buffer, NULL, 10); 127 unsigned long utilization = strtoul(recv_buffer, NULL, 10);
171 128
172 /* Check if any of the request is in a warning or critical state */ 129 /* Check if any of the request is in a warning or critical state */
173 if (utilization >= lvalue_list[2 + offset]) 130 if (utilization >= lvalue_list[2 + offset]) {
174 return_code = STATE_CRITICAL; 131 return_code = STATE_CRITICAL;
175 else if (utilization >= lvalue_list[1 + offset] && return_code < STATE_WARNING) 132 } else if (utilization >= lvalue_list[1 + offset] && return_code < STATE_WARNING) {
176 return_code = STATE_WARNING; 133 return_code = STATE_WARNING;
134 }
177 135
178 xasprintf(&output_message, _(" %lu%% (%lu min average)"), utilization, lvalue_list[0 + offset]); 136 xasprintf(&output_message, _(" %lu%% (%lu min average)"), utilization, lvalue_list[0 + offset]);
179 xasprintf(&temp_string, "%s%s", temp_string, output_message); 137 xasprintf(&temp_string, "%s%s", temp_string, output_message);
@@ -186,82 +144,87 @@ int main(int argc, char **argv) {
186 if (strlen(temp_string) > 10) { /* we had at least one loop */ 144 if (strlen(temp_string) > 10) { /* we had at least one loop */
187 output_message = strdup(temp_string); 145 output_message = strdup(temp_string);
188 perfdata = temp_string_perf; 146 perfdata = temp_string_perf;
189 } else 147 } else {
190 output_message = strdup(_("not enough values for -l parameters")); 148 output_message = strdup(_("not enough values for -l parameters"));
149 }
191 } 150 }
192 break; 151 break;
193 152 case CHECK_UPTIME: {
194 case CHECK_UPTIME: 153 char *tmp_value_list = config.value_list;
195 154 if (config.value_list == NULL) {
196 if (value_list == NULL) { 155 tmp_value_list = "minutes";
197 value_list = "minutes";
198 } 156 }
199 if (strncmp(value_list, "seconds", strlen("seconds") + 1) && strncmp(value_list, "minutes", strlen("minutes") + 1) && 157 if (strncmp(tmp_value_list, "seconds", strlen("seconds") + 1) && strncmp(tmp_value_list, "minutes", strlen("minutes") + 1) &&
200 strncmp(value_list, "hours", strlen("hours") + 1) && strncmp(value_list, "days", strlen("days") + 1)) { 158 strncmp(config.value_list, "hours", strlen("hours") + 1) && strncmp(tmp_value_list, "days", strlen("days") + 1)) {
201 159
202 output_message = strdup(_("wrong -l argument")); 160 output_message = strdup(_("wrong -l argument"));
203 } else { 161 } else {
204 xasprintf(&send_buffer, "%s&3", req_password); 162 xasprintf(&send_buffer, "%s&3", config.req_password);
205 fetch_data(server_address, server_port, send_buffer); 163 fetch_data(config.server_address, config.server_port, send_buffer);
206 uptime = strtoul(recv_buffer, NULL, 10); 164 unsigned long uptime = strtoul(recv_buffer, NULL, 10);
207 updays = uptime / 86400; 165 int updays = uptime / 86400;
208 uphours = (uptime % 86400) / 3600; 166 int uphours = (uptime % 86400) / 3600;
209 upminutes = ((uptime % 86400) % 3600) / 60; 167 int upminutes = ((uptime % 86400) % 3600) / 60;
210 168
211 if (!strncmp(value_list, "minutes", strlen("minutes"))) 169 if (!strncmp(tmp_value_list, "minutes", strlen("minutes"))) {
212 uptime = uptime / 60; 170 uptime = uptime / 60;
213 else if (!strncmp(value_list, "hours", strlen("hours"))) 171 } else if (!strncmp(tmp_value_list, "hours", strlen("hours"))) {
214 uptime = uptime / 3600; 172 uptime = uptime / 3600;
215 else if (!strncmp(value_list, "days", strlen("days"))) 173 } else if (!strncmp(tmp_value_list, "days", strlen("days"))) {
216 uptime = uptime / 86400; 174 uptime = uptime / 86400;
175 }
217 /* else uptime in seconds, nothing to do */ 176 /* else uptime in seconds, nothing to do */
218 177
219 xasprintf(&output_message, _("System Uptime - %u day(s) %u hour(s) %u minute(s) |uptime=%lu"), updays, uphours, upminutes, 178 xasprintf(&output_message, _("System Uptime - %u day(s) %u hour(s) %u minute(s) |uptime=%lu"), updays, uphours, upminutes,
220 uptime); 179 uptime);
221 180
222 if (check_critical_value && uptime <= critical_value) 181 if (config.check_critical_value && uptime <= config.critical_value) {
223 return_code = STATE_CRITICAL; 182 return_code = STATE_CRITICAL;
224 else if (check_warning_value && uptime <= warning_value) 183 } else if (config.check_warning_value && uptime <= config.warning_value) {
225 return_code = STATE_WARNING; 184 return_code = STATE_WARNING;
226 else 185 } else {
227 return_code = STATE_OK; 186 return_code = STATE_OK;
187 }
228 } 188 }
229 break; 189 } break;
230
231 case CHECK_USEDDISKSPACE: 190 case CHECK_USEDDISKSPACE:
232 191 if (config.value_list == NULL) {
233 if (value_list == NULL)
234 output_message = strdup(_("missing -l parameters")); 192 output_message = strdup(_("missing -l parameters"));
235 else if (strlen(value_list) != 1) 193 } else if (strlen(config.value_list) != 1) {
236 output_message = strdup(_("wrong -l argument")); 194 output_message = strdup(_("wrong -l argument"));
237 else { 195 } else {
238 xasprintf(&send_buffer, "%s&4&%s", req_password, value_list); 196 xasprintf(&send_buffer, "%s&4&%s", config.req_password, config.value_list);
239 fetch_data(server_address, server_port, send_buffer); 197 fetch_data(config.server_address, config.server_port, send_buffer);
240 fds = strtok(recv_buffer, "&"); 198 char *fds = strtok(recv_buffer, "&");
241 tds = strtok(NULL, "&"); 199 char *tds = strtok(NULL, "&");
242 if (fds != NULL) 200 double total_disk_space = 0;
201 double free_disk_space = 0;
202 if (fds != NULL) {
243 free_disk_space = atof(fds); 203 free_disk_space = atof(fds);
244 if (tds != NULL) 204 }
205 if (tds != NULL) {
245 total_disk_space = atof(tds); 206 total_disk_space = atof(tds);
207 }
246 208
247 if (total_disk_space > 0 && free_disk_space >= 0) { 209 if (total_disk_space > 0 && free_disk_space >= 0) {
248 percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100; 210 double percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100;
249 warning_used_space = ((float)warning_value / 100) * total_disk_space; 211 double warning_used_space = ((float)config.warning_value / 100) * total_disk_space;
250 critical_used_space = ((float)critical_value / 100) * total_disk_space; 212 double critical_used_space = ((float)config.critical_value / 100) * total_disk_space;
251 213
252 xasprintf(&temp_string, _("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"), value_list, 214 xasprintf(&temp_string, _("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"), config.value_list,
253 total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space, 215 total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space,
254 free_disk_space / 1073741824, (free_disk_space / total_disk_space) * 100); 216 free_disk_space / 1073741824, (free_disk_space / total_disk_space) * 100);
255 xasprintf(&temp_string_perf, _("'%s:\\ Used Space'=%.2fGb;%.2f;%.2f;0.00;%.2f"), value_list, 217 xasprintf(&temp_string_perf, _("'%s:\\ Used Space'=%.2fGb;%.2f;%.2f;0.00;%.2f"), config.value_list,
256 (total_disk_space - free_disk_space) / 1073741824, warning_used_space / 1073741824, 218 (total_disk_space - free_disk_space) / 1073741824, warning_used_space / 1073741824,
257 critical_used_space / 1073741824, total_disk_space / 1073741824); 219 critical_used_space / 1073741824, total_disk_space / 1073741824);
258 220
259 if (check_critical_value && percent_used_space >= critical_value) 221 if (config.check_critical_value && percent_used_space >= config.critical_value) {
260 return_code = STATE_CRITICAL; 222 return_code = STATE_CRITICAL;
261 else if (check_warning_value && percent_used_space >= warning_value) 223 } else if (config.check_warning_value && percent_used_space >= config.warning_value) {
262 return_code = STATE_WARNING; 224 return_code = STATE_WARNING;
263 else 225 } else {
264 return_code = STATE_OK; 226 return_code = STATE_OK;
227 }
265 228
266 output_message = strdup(temp_string); 229 output_message = strdup(temp_string);
267 perfdata = temp_string_perf; 230 perfdata = temp_string_perf;
@@ -271,41 +234,40 @@ int main(int argc, char **argv) {
271 } 234 }
272 } 235 }
273 break; 236 break;
274
275 case CHECK_SERVICESTATE: 237 case CHECK_SERVICESTATE:
276 case CHECK_PROCSTATE: 238 case CHECK_PROCSTATE:
277 239 if (config.value_list == NULL) {
278 if (value_list == NULL)
279 output_message = strdup(_("No service/process specified")); 240 output_message = strdup(_("No service/process specified"));
280 else { 241 } else {
281 preparelist(value_list); /* replace , between services with & to send the request */ 242 preparelist(config.value_list); /* replace , between services with & to send the request */
282 xasprintf(&send_buffer, "%s&%u&%s&%s", req_password, (vars_to_check == CHECK_SERVICESTATE) ? 5 : 6, 243 xasprintf(&send_buffer, "%s&%u&%s&%s", config.req_password, (config.vars_to_check == CHECK_SERVICESTATE) ? 5 : 6,
283 (show_all) ? "ShowAll" : "ShowFail", value_list); 244 (config.show_all) ? "ShowAll" : "ShowFail", config.value_list);
284 fetch_data(server_address, server_port, send_buffer); 245 fetch_data(config.server_address, config.server_port, send_buffer);
285 numstr = strtok(recv_buffer, "&"); 246 char *numstr = strtok(recv_buffer, "&");
286 if (numstr == NULL) 247 if (numstr == NULL) {
287 die(STATE_UNKNOWN, _("could not fetch information from server\n")); 248 die(STATE_UNKNOWN, _("could not fetch information from server\n"));
249 }
288 return_code = atoi(numstr); 250 return_code = atoi(numstr);
289 temp_string = strtok(NULL, "&"); 251 temp_string = strtok(NULL, "&");
290 output_message = strdup(temp_string); 252 output_message = strdup(temp_string);
291 } 253 }
292 break; 254 break;
293
294 case CHECK_MEMUSE: 255 case CHECK_MEMUSE:
295 256 xasprintf(&send_buffer, "%s&7", config.req_password);
296 xasprintf(&send_buffer, "%s&7", req_password); 257 fetch_data(config.server_address, config.server_port, send_buffer);
297 fetch_data(server_address, server_port, send_buffer); 258 char *numstr = strtok(recv_buffer, "&");
298 numstr = strtok(recv_buffer, "&"); 259 if (numstr == NULL) {
299 if (numstr == NULL)
300 die(STATE_UNKNOWN, _("could not fetch information from server\n")); 260 die(STATE_UNKNOWN, _("could not fetch information from server\n"));
301 mem_commitLimit = atof(numstr); 261 }
262 double mem_commitLimit = atof(numstr);
302 numstr = strtok(NULL, "&"); 263 numstr = strtok(NULL, "&");
303 if (numstr == NULL) 264 if (numstr == NULL) {
304 die(STATE_UNKNOWN, _("could not fetch information from server\n")); 265 die(STATE_UNKNOWN, _("could not fetch information from server\n"));
305 mem_commitByte = atof(numstr); 266 }
306 percent_used_space = (mem_commitByte / mem_commitLimit) * 100; 267 double mem_commitByte = atof(numstr);
307 warning_used_space = ((float)warning_value / 100) * mem_commitLimit; 268 double percent_used_space = (mem_commitByte / mem_commitLimit) * 100;
308 critical_used_space = ((float)critical_value / 100) * mem_commitLimit; 269 double warning_used_space = ((float)config.warning_value / 100) * mem_commitLimit;
270 double critical_used_space = ((float)config.critical_value / 100) * mem_commitLimit;
309 271
310 /* Divisor should be 1048567, not 3044515, as we are measuring "Commit Charge" here, 272 /* Divisor should be 1048567, not 3044515, as we are measuring "Commit Charge" here,
311 which equals RAM + Pagefiles. */ 273 which equals RAM + Pagefiles. */
@@ -316,15 +278,14 @@ int main(int argc, char **argv) {
316 critical_used_space / 1048567, mem_commitLimit / 1048567); 278 critical_used_space / 1048567, mem_commitLimit / 1048567);
317 279
318 return_code = STATE_OK; 280 return_code = STATE_OK;
319 if (check_critical_value && percent_used_space >= critical_value) 281 if (config.check_critical_value && percent_used_space >= config.critical_value) {
320 return_code = STATE_CRITICAL; 282 return_code = STATE_CRITICAL;
321 else if (check_warning_value && percent_used_space >= warning_value) 283 } else if (config.check_warning_value && percent_used_space >= config.warning_value) {
322 return_code = STATE_WARNING; 284 return_code = STATE_WARNING;
285 }
323 286
324 break; 287 break;
325 288 case CHECK_COUNTER: {
326 case CHECK_COUNTER:
327
328 /* 289 /*
329 CHECK_COUNTER has been modified to provide extensive perfdata information. 290 CHECK_COUNTER has been modified to provide extensive perfdata information.
330 In order to do this, some modifications have been done to the code 291 In order to do this, some modifications have been done to the code
@@ -346,26 +307,32 @@ int main(int argc, char **argv) {
346 strange things will happen when you make graphs of your data. 307 strange things will happen when you make graphs of your data.
347 */ 308 */
348 309
349 if (value_list == NULL) 310 double counter_value = 0.0;
311 if (config.value_list == NULL) {
350 output_message = strdup(_("No counter specified")); 312 output_message = strdup(_("No counter specified"));
351 else { 313 } else {
352 preparelist(value_list); /* replace , between services with & to send the request */ 314 preparelist(config.value_list); /* replace , between services with & to send the request */
353 isPercent = (strchr(value_list, '%') != NULL); 315 bool isPercent = (strchr(config.value_list, '%') != NULL);
354 316
355 strtok(value_list, "&"); /* burn the first parameters */ 317 strtok(config.value_list, "&"); /* burn the first parameters */
356 description = strtok(NULL, "&"); 318 description = strtok(NULL, "&");
357 counter_unit = strtok(NULL, "&"); 319 counter_unit = strtok(NULL, "&");
358 xasprintf(&send_buffer, "%s&8&%s", req_password, value_list); 320 xasprintf(&send_buffer, "%s&8&%s", config.req_password, config.value_list);
359 fetch_data(server_address, server_port, send_buffer); 321 fetch_data(config.server_address, config.server_port, send_buffer);
360 counter_value = atof(recv_buffer); 322 counter_value = atof(recv_buffer);
361 323
362 if (description == NULL) 324 bool allRight = false;
325 if (description == NULL) {
363 xasprintf(&output_message, "%.f", counter_value); 326 xasprintf(&output_message, "%.f", counter_value);
364 else if (isPercent) { 327 } else if (isPercent) {
365 counter_unit = strdup("%"); 328 counter_unit = strdup("%");
366 allRight = true; 329 allRight = true;
367 } 330 }
368 331
332 char *minval = NULL;
333 char *maxval = NULL;
334 double fminval = 0;
335 double fmaxval = 0;
369 if ((counter_unit != NULL) && (!allRight)) { 336 if ((counter_unit != NULL) && (!allRight)) {
370 minval = strtok(NULL, "&"); 337 minval = strtok(NULL, "&");
371 maxval = strtok(NULL, "&"); 338 maxval = strtok(NULL, "&");
@@ -375,16 +342,18 @@ int main(int argc, char **argv) {
375 fminval = (minval != NULL) ? strtod(minval, &errcvt) : -1; 342 fminval = (minval != NULL) ? strtod(minval, &errcvt) : -1;
376 fmaxval = (minval != NULL) ? strtod(maxval, &errcvt) : -1; 343 fmaxval = (minval != NULL) ? strtod(maxval, &errcvt) : -1;
377 344
378 if ((fminval == 0) && (minval == errcvt)) 345 if ((fminval == 0) && (minval == errcvt)) {
379 output_message = strdup(_("Minimum value contains non-numbers")); 346 output_message = strdup(_("Minimum value contains non-numbers"));
380 else { 347 } else {
381 if ((fmaxval == 0) && (maxval == errcvt)) 348 if ((fmaxval == 0) && (maxval == errcvt)) {
382 output_message = strdup(_("Maximum value contains non-numbers")); 349 output_message = strdup(_("Maximum value contains non-numbers"));
383 else 350 } else {
384 allRight = true; /* Everything is OK. */ 351 allRight = true; /* Everything is OK. */
352 }
385 } 353 }
386 } else if ((counter_unit == NULL) && (description != NULL)) 354 } else if ((counter_unit == NULL) && (description != NULL)) {
387 output_message = strdup(_("No unit counter specified")); 355 output_message = strdup(_("No unit counter specified"));
356 }
388 357
389 if (allRight) { 358 if (allRight) {
390 /* Let's format the output string, finally... */ 359 /* Let's format the output string, finally... */
@@ -396,63 +365,65 @@ int main(int argc, char **argv) {
396 } 365 }
397 xasprintf(&output_message, "%s |", output_message); 366 xasprintf(&output_message, "%s |", output_message);
398 xasprintf(&output_message, "%s %s", output_message, 367 xasprintf(&output_message, "%s %s", output_message,
399 fperfdata(description, counter_value, counter_unit, 1, warning_value, 1, critical_value, 368 fperfdata(description, counter_value, counter_unit, 1, config.warning_value, 1, config.critical_value,
400 (!(isPercent) && (minval != NULL)), fminval, (!(isPercent) && (minval != NULL)), fmaxval)); 369 (!(isPercent) && (minval != NULL)), fminval, (!(isPercent) && (minval != NULL)), fmaxval));
401 } 370 }
402 } 371 }
403 372
404 if (critical_value > warning_value) { /* Normal thresholds */ 373 if (config.critical_value > config.warning_value) { /* Normal thresholds */
405 if (check_critical_value && counter_value >= critical_value) 374 if (config.check_critical_value && counter_value >= config.critical_value) {
406 return_code = STATE_CRITICAL; 375 return_code = STATE_CRITICAL;
407 else if (check_warning_value && counter_value >= warning_value) 376 } else if (config.check_warning_value && counter_value >= config.warning_value) {
408 return_code = STATE_WARNING; 377 return_code = STATE_WARNING;
409 else 378 } else {
410 return_code = STATE_OK; 379 return_code = STATE_OK;
380 }
411 } else { /* inverse thresholds */ 381 } else { /* inverse thresholds */
412 return_code = STATE_OK; 382 return_code = STATE_OK;
413 if (check_critical_value && counter_value <= critical_value) 383 if (config.check_critical_value && counter_value <= config.critical_value) {
414 return_code = STATE_CRITICAL; 384 return_code = STATE_CRITICAL;
415 else if (check_warning_value && counter_value <= warning_value) 385 } else if (config.check_warning_value && counter_value <= config.warning_value) {
416 return_code = STATE_WARNING; 386 return_code = STATE_WARNING;
387 }
417 } 388 }
418 break; 389 } break;
419
420 case CHECK_FILEAGE: 390 case CHECK_FILEAGE:
421 391 if (config.value_list == NULL) {
422 if (value_list == NULL)
423 output_message = strdup(_("No counter specified")); 392 output_message = strdup(_("No counter specified"));
424 else { 393 } else {
425 preparelist(value_list); /* replace , between services with & to send the request */ 394 preparelist(config.value_list); /* replace , between services with & to send the request */
426 xasprintf(&send_buffer, "%s&9&%s", req_password, value_list); 395 xasprintf(&send_buffer, "%s&9&%s", config.req_password, config.value_list);
427 fetch_data(server_address, server_port, send_buffer); 396 fetch_data(config.server_address, config.server_port, send_buffer);
428 age_in_minutes = atoi(strtok(recv_buffer, "&")); 397 unsigned long age_in_minutes = atoi(strtok(recv_buffer, "&"));
429 description = strtok(NULL, "&"); 398 description = strtok(NULL, "&");
430 output_message = strdup(description); 399 output_message = strdup(description);
431 400
432 if (critical_value > warning_value) { /* Normal thresholds */ 401 if (config.critical_value > config.warning_value) { /* Normal thresholds */
433 if (check_critical_value && age_in_minutes >= critical_value) 402 if (config.check_critical_value && age_in_minutes >= config.critical_value) {
434 return_code = STATE_CRITICAL; 403 return_code = STATE_CRITICAL;
435 else if (check_warning_value && age_in_minutes >= warning_value) 404 } else if (config.check_warning_value && age_in_minutes >= config.warning_value) {
436 return_code = STATE_WARNING; 405 return_code = STATE_WARNING;
437 else 406 } else {
438 return_code = STATE_OK; 407 return_code = STATE_OK;
408 }
439 } else { /* inverse thresholds */ 409 } else { /* inverse thresholds */
440 if (check_critical_value && age_in_minutes <= critical_value) 410 if (config.check_critical_value && age_in_minutes <= config.critical_value) {
441 return_code = STATE_CRITICAL; 411 return_code = STATE_CRITICAL;
442 else if (check_warning_value && age_in_minutes <= warning_value) 412 } else if (config.check_warning_value && age_in_minutes <= config.warning_value) {
443 return_code = STATE_WARNING; 413 return_code = STATE_WARNING;
444 else 414 } else {
445 return_code = STATE_OK; 415 return_code = STATE_OK;
416 }
446 } 417 }
447 } 418 }
448 break; 419 break;
449 420
450 case CHECK_INSTANCES: 421 case CHECK_INSTANCES:
451 if (value_list == NULL) 422 if (config.value_list == NULL) {
452 output_message = strdup(_("No counter specified")); 423 output_message = strdup(_("No counter specified"));
453 else { 424 } else {
454 xasprintf(&send_buffer, "%s&10&%s", req_password, value_list); 425 xasprintf(&send_buffer, "%s&10&%s", config.req_password, config.value_list);
455 fetch_data(server_address, server_port, send_buffer); 426 fetch_data(config.server_address, config.server_port, send_buffer);
456 if (!strncmp(recv_buffer, "ERROR", 5)) { 427 if (!strncmp(recv_buffer, "ERROR", 5)) {
457 printf("NSClient - %s\n", recv_buffer); 428 printf("NSClient - %s\n", recv_buffer);
458 exit(STATE_UNKNOWN); 429 exit(STATE_UNKNOWN);
@@ -471,18 +442,16 @@ int main(int argc, char **argv) {
471 /* reset timeout */ 442 /* reset timeout */
472 alarm(0); 443 alarm(0);
473 444
474 if (perfdata == NULL) 445 if (perfdata == NULL) {
475 printf("%s\n", output_message); 446 printf("%s\n", output_message);
476 else 447 } else {
477 printf("%s | %s\n", output_message, perfdata); 448 printf("%s | %s\n", output_message, perfdata);
449 }
478 return return_code; 450 return return_code;
479} 451}
480 452
481/* process command-line arguments */ 453/* process command-line arguments */
482int process_arguments(int argc, char **argv) { 454check_nt_config_wrapper process_arguments(int argc, char **argv) {
483 int c;
484
485 int option = 0;
486 static struct option longopts[] = {{"port", required_argument, 0, 'p'}, 455 static struct option longopts[] = {{"port", required_argument, 0, 'p'},
487 {"timeout", required_argument, 0, 't'}, 456 {"timeout", required_argument, 0, 't'},
488 {"critical", required_argument, 0, 'c'}, 457 {"critical", required_argument, 0, 'c'},
@@ -497,34 +466,44 @@ int process_arguments(int argc, char **argv) {
497 {"help", no_argument, 0, 'h'}, 466 {"help", no_argument, 0, 'h'},
498 {0, 0, 0, 0}}; 467 {0, 0, 0, 0}};
499 468
469 check_nt_config_wrapper result = {
470 .errorcode = OK,
471 .config = check_nt_config_init(),
472 };
473
500 /* no options were supplied */ 474 /* no options were supplied */
501 if (argc < 2) 475 if (argc < 2) {
502 return ERROR; 476 result.errorcode = ERROR;
477 return result;
478 }
503 479
504 /* backwards compatibility */ 480 /* backwards compatibility */
505 if (!is_option(argv[1])) { 481 if (!is_option(argv[1])) {
506 server_address = strdup(argv[1]); 482 result.config.server_address = strdup(argv[1]);
507 argv[1] = argv[0]; 483 argv[1] = argv[0];
508 argv = &argv[1]; 484 argv = &argv[1];
509 argc--; 485 argc--;
510 } 486 }
511 487
512 for (c = 1; c < argc; c++) { 488 for (int index = 1; index < argc; index++) {
513 if (strcmp("-to", argv[c]) == 0) 489 if (strcmp("-to", argv[index]) == 0) {
514 strcpy(argv[c], "-t"); 490 strcpy(argv[index], "-t");
515 else if (strcmp("-wv", argv[c]) == 0) 491 } else if (strcmp("-wv", argv[index]) == 0) {
516 strcpy(argv[c], "-w"); 492 strcpy(argv[index], "-w");
517 else if (strcmp("-cv", argv[c]) == 0) 493 } else if (strcmp("-cv", argv[index]) == 0) {
518 strcpy(argv[c], "-c"); 494 strcpy(argv[index], "-c");
495 }
519 } 496 }
520 497
521 while (1) { 498 int option = 0;
522 c = getopt_long(argc, argv, "+hVH:t:c:w:p:v:l:s:d:u", longopts, &option); 499 while (true) {
500 int option_index = getopt_long(argc, argv, "+hVH:t:c:w:p:v:l:s:d:u", longopts, &option);
523 501
524 if (c == -1 || c == EOF || c == 1) 502 if (option_index == -1 || option_index == EOF || option_index == 1) {
525 break; 503 break;
504 }
526 505
527 switch (c) { 506 switch (option_index) {
528 case '?': /* print short usage statement if args not parsable */ 507 case '?': /* print short usage statement if args not parsable */
529 usage5(); 508 usage5();
530 case 'h': /* help */ 509 case 'h': /* help */
@@ -534,118 +513,128 @@ int process_arguments(int argc, char **argv) {
534 print_revision(progname, NP_VERSION); 513 print_revision(progname, NP_VERSION);
535 exit(STATE_UNKNOWN); 514 exit(STATE_UNKNOWN);
536 case 'H': /* hostname */ 515 case 'H': /* hostname */
537 server_address = optarg; 516 result.config.server_address = optarg;
538 break; 517 break;
539 case 's': /* password */ 518 case 's': /* password */
540 req_password = optarg; 519 result.config.req_password = optarg;
541 break; 520 break;
542 case 'p': /* port */ 521 case 'p': /* port */
543 if (is_intnonneg(optarg)) 522 if (is_intnonneg(optarg)) {
544 server_port = atoi(optarg); 523 result.config.server_port = atoi(optarg);
545 else 524 } else {
546 die(STATE_UNKNOWN, _("Server port must be an integer\n")); 525 die(STATE_UNKNOWN, _("Server port must be an integer\n"));
526 }
547 break; 527 break;
548 case 'v': 528 case 'v':
549 if (strlen(optarg) < 4) 529 if (strlen(optarg) < 4) {
550 return ERROR; 530 result.errorcode = ERROR;
551 if (!strcmp(optarg, "CLIENTVERSION")) 531 return result;
552 vars_to_check = CHECK_CLIENTVERSION; 532 }
553 else if (!strcmp(optarg, "CPULOAD")) 533 if (!strcmp(optarg, "CLIENTVERSION")) {
554 vars_to_check = CHECK_CPULOAD; 534 result.config.vars_to_check = CHECK_CLIENTVERSION;
555 else if (!strcmp(optarg, "UPTIME")) 535 } else if (!strcmp(optarg, "CPULOAD")) {
556 vars_to_check = CHECK_UPTIME; 536 result.config.vars_to_check = CHECK_CPULOAD;
557 else if (!strcmp(optarg, "USEDDISKSPACE")) 537 } else if (!strcmp(optarg, "UPTIME")) {
558 vars_to_check = CHECK_USEDDISKSPACE; 538 result.config.vars_to_check = CHECK_UPTIME;
559 else if (!strcmp(optarg, "SERVICESTATE")) 539 } else if (!strcmp(optarg, "USEDDISKSPACE")) {
560 vars_to_check = CHECK_SERVICESTATE; 540 result.config.vars_to_check = CHECK_USEDDISKSPACE;
561 else if (!strcmp(optarg, "PROCSTATE")) 541 } else if (!strcmp(optarg, "SERVICESTATE")) {
562 vars_to_check = CHECK_PROCSTATE; 542 result.config.vars_to_check = CHECK_SERVICESTATE;
563 else if (!strcmp(optarg, "MEMUSE")) 543 } else if (!strcmp(optarg, "PROCSTATE")) {
564 vars_to_check = CHECK_MEMUSE; 544 result.config.vars_to_check = CHECK_PROCSTATE;
565 else if (!strcmp(optarg, "COUNTER")) 545 } else if (!strcmp(optarg, "MEMUSE")) {
566 vars_to_check = CHECK_COUNTER; 546 result.config.vars_to_check = CHECK_MEMUSE;
567 else if (!strcmp(optarg, "FILEAGE")) 547 } else if (!strcmp(optarg, "COUNTER")) {
568 vars_to_check = CHECK_FILEAGE; 548 result.config.vars_to_check = CHECK_COUNTER;
569 else if (!strcmp(optarg, "INSTANCES")) 549 } else if (!strcmp(optarg, "FILEAGE")) {
570 vars_to_check = CHECK_INSTANCES; 550 result.config.vars_to_check = CHECK_FILEAGE;
571 else 551 } else if (!strcmp(optarg, "INSTANCES")) {
572 return ERROR; 552 result.config.vars_to_check = CHECK_INSTANCES;
553 } else {
554 result.errorcode = ERROR;
555 return result;
556 }
573 break; 557 break;
574 case 'l': /* value list */ 558 case 'l': /* value list */
575 value_list = optarg; 559 result.config.value_list = optarg;
576 break; 560 break;
577 case 'w': /* warning threshold */ 561 case 'w': /* warning threshold */
578 warning_value = strtoul(optarg, NULL, 10); 562 result.config.warning_value = strtoul(optarg, NULL, 10);
579 check_warning_value = true; 563 result.config.check_warning_value = true;
580 break; 564 break;
581 case 'c': /* critical threshold */ 565 case 'c': /* critical threshold */
582 critical_value = strtoul(optarg, NULL, 10); 566 result.config.critical_value = strtoul(optarg, NULL, 10);
583 check_critical_value = true; 567 result.config.check_critical_value = true;
584 break; 568 break;
585 case 'd': /* Display select for services */ 569 case 'd': /* Display select for services */
586 if (!strcmp(optarg, "SHOWALL")) 570 if (!strcmp(optarg, "SHOWALL")) {
587 show_all = true; 571 result.config.show_all = true;
572 }
588 break; 573 break;
589 case 'u': 574 case 'u':
590 socket_timeout_state = STATE_UNKNOWN; 575 socket_timeout_state = STATE_UNKNOWN;
591 break; 576 break;
592 case 't': /* timeout */ 577 case 't': /* timeout */
593 socket_timeout = atoi(optarg); 578 socket_timeout = atoi(optarg);
594 if (socket_timeout <= 0) 579 if (socket_timeout <= 0) {
595 return ERROR; 580 result.errorcode = ERROR;
581 return result;
582 }
596 } 583 }
597 } 584 }
598 if (server_address == NULL) 585 if (result.config.server_address == NULL) {
599 usage4(_("You must provide a server address or host name")); 586 usage4(_("You must provide a server address or host name"));
587 }
600 588
601 if (vars_to_check == CHECK_NONE) 589 if (result.config.vars_to_check == CHECK_NONE) {
602 return ERROR; 590 result.errorcode = ERROR;
591 return result;
592 }
603 593
604 if (req_password == NULL) 594 if (result.config.req_password == NULL) {
605 req_password = strdup(_("None")); 595 result.config.req_password = strdup(_("None"));
596 }
606 597
607 return OK; 598 return result;
608} 599}
609 600
610void fetch_data(const char *address, int port, const char *sendb) { 601void fetch_data(const char *address, int port, const char *sendb) {
611 int result; 602 int result = process_tcp_request(address, port, sendb, recv_buffer, sizeof(recv_buffer));
612
613 result = process_tcp_request(address, port, sendb, recv_buffer, sizeof(recv_buffer));
614 603
615 if (result != STATE_OK) 604 if (result != STATE_OK) {
616 die(result, _("could not fetch information from server\n")); 605 die(result, _("could not fetch information from server\n"));
606 }
617 607
618 if (!strncmp(recv_buffer, "ERROR", 5)) 608 if (!strncmp(recv_buffer, "ERROR", 5)) {
619 die(STATE_UNKNOWN, "NSClient - %s\n", recv_buffer); 609 die(STATE_UNKNOWN, "NSClient - %s\n", recv_buffer);
610 }
620} 611}
621 612
622bool strtoularray(unsigned long *array, char *string, const char *delim) { 613bool strtoularray(unsigned long *array, char *string, const char *delim) {
623 /* split a <delim> delimited string into a long array */ 614 /* split a <delim> delimited string into a long array */
624 int idx = 0; 615 for (int idx = 0; idx < MAX_VALUE_LIST; idx++) {
625 char *t1;
626
627 for (idx = 0; idx < MAX_VALUE_LIST; idx++)
628 array[idx] = 0; 616 array[idx] = 0;
617 }
629 618
630 idx = 0; 619 int idx = 0;
631 for (t1 = strtok(string, delim); t1 != NULL; t1 = strtok(NULL, delim)) { 620 for (char *t1 = strtok(string, delim); t1 != NULL; t1 = strtok(NULL, delim)) {
632 if (is_numeric(t1) && idx < MAX_VALUE_LIST) { 621 if (is_numeric(t1) && idx < MAX_VALUE_LIST) {
633 array[idx] = strtoul(t1, NULL, 10); 622 array[idx] = strtoul(t1, NULL, 10);
634 idx++; 623 idx++;
635 } else 624 } else {
636 return false; 625 return false;
626 }
637 } 627 }
638 return true; 628 return true;
639} 629}
640 630
641void preparelist(char *string) { 631void preparelist(char *string) {
642 /* Replace all , with & which is the delimiter for the request */ 632 /* Replace all , with & which is the delimiter for the request */
643 int i; 633 for (int i = 0; (size_t)i < strlen(string); i++) {
644
645 for (i = 0; (size_t)i < strlen(string); i++)
646 if (string[i] == ',') { 634 if (string[i] == ',') {
647 string[i] = '&'; 635 string[i] = '&';
648 } 636 }
637 }
649} 638}
650 639
651void print_help(void) { 640void print_help(void) {