summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/check_nt.c148
1 files changed, 54 insertions, 94 deletions
diff --git a/plugins/check_nt.c b/plugins/check_nt.c
index 4b9f8c48..7897558b 100644
--- a/plugins/check_nt.c
+++ b/plugins/check_nt.c
@@ -81,40 +81,6 @@ static void print_help(void);
81void print_usage(void); 81void print_usage(void);
82 82
83int main(int argc, char **argv) { 83int 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, ""); 84 setlocale(LC_ALL, "");
119 bindtextdomain(PACKAGE, LOCALEDIR); 85 bindtextdomain(PACKAGE, LOCALEDIR);
120 textdomain(PACKAGE); 86 textdomain(PACKAGE);
@@ -132,10 +98,17 @@ int main(int argc, char **argv) {
132 /* set socket timeout */ 98 /* set socket timeout */
133 alarm(socket_timeout); 99 alarm(socket_timeout);
134 100
101 int return_code = STATE_UNKNOWN;
102 char *send_buffer = NULL;
103 char *output_message = NULL;
104 char *perfdata = NULL;
105 char *temp_string = NULL;
106 char *temp_string_perf = NULL;
107 char *description = NULL;
108 char *counter_unit = NULL;
109 char *errcvt = NULL;
135 switch (vars_to_check) { 110 switch (vars_to_check) {
136
137 case CHECK_CLIENTVERSION: 111 case CHECK_CLIENTVERSION:
138
139 xasprintf(&send_buffer, "%s&1", req_password); 112 xasprintf(&send_buffer, "%s&1", req_password);
140 fetch_data(server_address, server_port, send_buffer); 113 fetch_data(server_address, server_port, send_buffer);
141 if (value_list != NULL && strcmp(recv_buffer, value_list) != 0) { 114 if (value_list != NULL && strcmp(recv_buffer, value_list) != 0) {
@@ -146,9 +119,7 @@ int main(int argc, char **argv) {
146 return_code = STATE_OK; 119 return_code = STATE_OK;
147 } 120 }
148 break; 121 break;
149
150 case CHECK_CPULOAD: 122 case CHECK_CPULOAD:
151
152 if (value_list == NULL) { 123 if (value_list == NULL) {
153 output_message = strdup(_("missing -l parameters")); 124 output_message = strdup(_("missing -l parameters"));
154 } else if (!strtoularray(lvalue_list, value_list, ",")) { 125 } else if (!strtoularray(lvalue_list, value_list, ",")) {
@@ -160,6 +131,7 @@ int main(int argc, char **argv) {
160 temp_string_perf = strdup(" "); 131 temp_string_perf = strdup(" ");
161 132
162 /* loop until one of the parameters is wrong or not present */ 133 /* loop until one of the parameters is wrong or not present */
134 int offset = 0;
163 while (lvalue_list[0 + offset] > (unsigned long)0 && lvalue_list[0 + offset] <= (unsigned long)17280 && 135 while (lvalue_list[0 + offset] > (unsigned long)0 && lvalue_list[0 + offset] <= (unsigned long)17280 &&
164 lvalue_list[1 + offset] > (unsigned long)0 && lvalue_list[1 + offset] <= (unsigned long)100 && 136 lvalue_list[1 + offset] > (unsigned long)0 && lvalue_list[1 + offset] <= (unsigned long)100 &&
165 lvalue_list[2 + offset] > (unsigned long)0 && lvalue_list[2 + offset] <= (unsigned long)100) { 137 lvalue_list[2 + offset] > (unsigned long)0 && lvalue_list[2 + offset] <= (unsigned long)100) {
@@ -168,7 +140,7 @@ int main(int argc, char **argv) {
168 xasprintf(&send_buffer, "%s&2&%lu", req_password, lvalue_list[0 + offset]); 140 xasprintf(&send_buffer, "%s&2&%lu", req_password, lvalue_list[0 + offset]);
169 fetch_data(server_address, server_port, send_buffer); 141 fetch_data(server_address, server_port, send_buffer);
170 142
171 utilization = strtoul(recv_buffer, NULL, 10); 143 unsigned long utilization = strtoul(recv_buffer, NULL, 10);
172 144
173 /* Check if any of the request is in a warning or critical state */ 145 /* Check if any of the request is in a warning or critical state */
174 if (utilization >= lvalue_list[2 + offset]) { 146 if (utilization >= lvalue_list[2 + offset]) {
@@ -193,9 +165,7 @@ int main(int argc, char **argv) {
193 } 165 }
194 } 166 }
195 break; 167 break;
196
197 case CHECK_UPTIME: 168 case CHECK_UPTIME:
198
199 if (value_list == NULL) { 169 if (value_list == NULL) {
200 value_list = "minutes"; 170 value_list = "minutes";
201 } 171 }
@@ -206,10 +176,10 @@ int main(int argc, char **argv) {
206 } else { 176 } else {
207 xasprintf(&send_buffer, "%s&3", req_password); 177 xasprintf(&send_buffer, "%s&3", req_password);
208 fetch_data(server_address, server_port, send_buffer); 178 fetch_data(server_address, server_port, send_buffer);
209 uptime = strtoul(recv_buffer, NULL, 10); 179 unsigned long uptime = strtoul(recv_buffer, NULL, 10);
210 updays = uptime / 86400; 180 int updays = uptime / 86400;
211 uphours = (uptime % 86400) / 3600; 181 int uphours = (uptime % 86400) / 3600;
212 upminutes = ((uptime % 86400) % 3600) / 60; 182 int upminutes = ((uptime % 86400) % 3600) / 60;
213 183
214 if (!strncmp(value_list, "minutes", strlen("minutes"))) { 184 if (!strncmp(value_list, "minutes", strlen("minutes"))) {
215 uptime = uptime / 60; 185 uptime = uptime / 60;
@@ -232,9 +202,7 @@ int main(int argc, char **argv) {
232 } 202 }
233 } 203 }
234 break; 204 break;
235
236 case CHECK_USEDDISKSPACE: 205 case CHECK_USEDDISKSPACE:
237
238 if (value_list == NULL) { 206 if (value_list == NULL) {
239 output_message = strdup(_("missing -l parameters")); 207 output_message = strdup(_("missing -l parameters"));
240 } else if (strlen(value_list) != 1) { 208 } else if (strlen(value_list) != 1) {
@@ -242,8 +210,10 @@ int main(int argc, char **argv) {
242 } else { 210 } else {
243 xasprintf(&send_buffer, "%s&4&%s", req_password, value_list); 211 xasprintf(&send_buffer, "%s&4&%s", req_password, value_list);
244 fetch_data(server_address, server_port, send_buffer); 212 fetch_data(server_address, server_port, send_buffer);
245 fds = strtok(recv_buffer, "&"); 213 char *fds = strtok(recv_buffer, "&");
246 tds = strtok(NULL, "&"); 214 char *tds = strtok(NULL, "&");
215 double total_disk_space = 0;
216 double free_disk_space = 0;
247 if (fds != NULL) { 217 if (fds != NULL) {
248 free_disk_space = atof(fds); 218 free_disk_space = atof(fds);
249 } 219 }
@@ -252,9 +222,9 @@ int main(int argc, char **argv) {
252 } 222 }
253 223
254 if (total_disk_space > 0 && free_disk_space >= 0) { 224 if (total_disk_space > 0 && free_disk_space >= 0) {
255 percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100; 225 double percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100;
256 warning_used_space = ((float)warning_value / 100) * total_disk_space; 226 double warning_used_space = ((float)warning_value / 100) * total_disk_space;
257 critical_used_space = ((float)critical_value / 100) * total_disk_space; 227 double critical_used_space = ((float)critical_value / 100) * total_disk_space;
258 228
259 xasprintf(&temp_string, _("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"), value_list, 229 xasprintf(&temp_string, _("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"), value_list,
260 total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space, 230 total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space,
@@ -279,10 +249,8 @@ int main(int argc, char **argv) {
279 } 249 }
280 } 250 }
281 break; 251 break;
282
283 case CHECK_SERVICESTATE: 252 case CHECK_SERVICESTATE:
284 case CHECK_PROCSTATE: 253 case CHECK_PROCSTATE:
285
286 if (value_list == NULL) { 254 if (value_list == NULL) {
287 output_message = strdup(_("No service/process specified")); 255 output_message = strdup(_("No service/process specified"));
288 } else { 256 } else {
@@ -290,7 +258,7 @@ int main(int argc, char **argv) {
290 xasprintf(&send_buffer, "%s&%u&%s&%s", req_password, (vars_to_check == CHECK_SERVICESTATE) ? 5 : 6, 258 xasprintf(&send_buffer, "%s&%u&%s&%s", req_password, (vars_to_check == CHECK_SERVICESTATE) ? 5 : 6,
291 (show_all) ? "ShowAll" : "ShowFail", value_list); 259 (show_all) ? "ShowAll" : "ShowFail", value_list);
292 fetch_data(server_address, server_port, send_buffer); 260 fetch_data(server_address, server_port, send_buffer);
293 numstr = strtok(recv_buffer, "&"); 261 char *numstr = strtok(recv_buffer, "&");
294 if (numstr == NULL) { 262 if (numstr == NULL) {
295 die(STATE_UNKNOWN, _("could not fetch information from server\n")); 263 die(STATE_UNKNOWN, _("could not fetch information from server\n"));
296 } 264 }
@@ -299,24 +267,22 @@ int main(int argc, char **argv) {
299 output_message = strdup(temp_string); 267 output_message = strdup(temp_string);
300 } 268 }
301 break; 269 break;
302
303 case CHECK_MEMUSE: 270 case CHECK_MEMUSE:
304
305 xasprintf(&send_buffer, "%s&7", req_password); 271 xasprintf(&send_buffer, "%s&7", req_password);
306 fetch_data(server_address, server_port, send_buffer); 272 fetch_data(server_address, server_port, send_buffer);
307 numstr = strtok(recv_buffer, "&"); 273 char *numstr = strtok(recv_buffer, "&");
308 if (numstr == NULL) { 274 if (numstr == NULL) {
309 die(STATE_UNKNOWN, _("could not fetch information from server\n")); 275 die(STATE_UNKNOWN, _("could not fetch information from server\n"));
310 } 276 }
311 mem_commitLimit = atof(numstr); 277 double mem_commitLimit = atof(numstr);
312 numstr = strtok(NULL, "&"); 278 numstr = strtok(NULL, "&");
313 if (numstr == NULL) { 279 if (numstr == NULL) {
314 die(STATE_UNKNOWN, _("could not fetch information from server\n")); 280 die(STATE_UNKNOWN, _("could not fetch information from server\n"));
315 } 281 }
316 mem_commitByte = atof(numstr); 282 double mem_commitByte = atof(numstr);
317 percent_used_space = (mem_commitByte / mem_commitLimit) * 100; 283 double percent_used_space = (mem_commitByte / mem_commitLimit) * 100;
318 warning_used_space = ((float)warning_value / 100) * mem_commitLimit; 284 double warning_used_space = ((float)warning_value / 100) * mem_commitLimit;
319 critical_used_space = ((float)critical_value / 100) * mem_commitLimit; 285 double critical_used_space = ((float)critical_value / 100) * mem_commitLimit;
320 286
321 /* Divisor should be 1048567, not 3044515, as we are measuring "Commit Charge" here, 287 /* Divisor should be 1048567, not 3044515, as we are measuring "Commit Charge" here,
322 which equals RAM + Pagefiles. */ 288 which equals RAM + Pagefiles. */
@@ -334,9 +300,7 @@ int main(int argc, char **argv) {
334 } 300 }
335 301
336 break; 302 break;
337
338 case CHECK_COUNTER: 303 case CHECK_COUNTER:
339
340 /* 304 /*
341 CHECK_COUNTER has been modified to provide extensive perfdata information. 305 CHECK_COUNTER has been modified to provide extensive perfdata information.
342 In order to do this, some modifications have been done to the code 306 In order to do this, some modifications have been done to the code
@@ -358,11 +322,12 @@ int main(int argc, char **argv) {
358 strange things will happen when you make graphs of your data. 322 strange things will happen when you make graphs of your data.
359 */ 323 */
360 324
325 double counter_value = 0.0;
361 if (value_list == NULL) { 326 if (value_list == NULL) {
362 output_message = strdup(_("No counter specified")); 327 output_message = strdup(_("No counter specified"));
363 } else { 328 } else {
364 preparelist(value_list); /* replace , between services with & to send the request */ 329 preparelist(value_list); /* replace , between services with & to send the request */
365 isPercent = (strchr(value_list, '%') != NULL); 330 bool isPercent = (strchr(value_list, '%') != NULL);
366 331
367 strtok(value_list, "&"); /* burn the first parameters */ 332 strtok(value_list, "&"); /* burn the first parameters */
368 description = strtok(NULL, "&"); 333 description = strtok(NULL, "&");
@@ -371,6 +336,7 @@ int main(int argc, char **argv) {
371 fetch_data(server_address, server_port, send_buffer); 336 fetch_data(server_address, server_port, send_buffer);
372 counter_value = atof(recv_buffer); 337 counter_value = atof(recv_buffer);
373 338
339 bool allRight = false;
374 if (description == NULL) { 340 if (description == NULL) {
375 xasprintf(&output_message, "%.f", counter_value); 341 xasprintf(&output_message, "%.f", counter_value);
376 } else if (isPercent) { 342 } else if (isPercent) {
@@ -378,6 +344,10 @@ int main(int argc, char **argv) {
378 allRight = true; 344 allRight = true;
379 } 345 }
380 346
347 char *minval = NULL;
348 char *maxval = NULL;
349 double fminval = 0;
350 double fmaxval = 0;
381 if ((counter_unit != NULL) && (!allRight)) { 351 if ((counter_unit != NULL) && (!allRight)) {
382 minval = strtok(NULL, "&"); 352 minval = strtok(NULL, "&");
383 maxval = strtok(NULL, "&"); 353 maxval = strtok(NULL, "&");
@@ -434,14 +404,13 @@ int main(int argc, char **argv) {
434 break; 404 break;
435 405
436 case CHECK_FILEAGE: 406 case CHECK_FILEAGE:
437
438 if (value_list == NULL) { 407 if (value_list == NULL) {
439 output_message = strdup(_("No counter specified")); 408 output_message = strdup(_("No counter specified"));
440 } else { 409 } else {
441 preparelist(value_list); /* replace , between services with & to send the request */ 410 preparelist(value_list); /* replace , between services with & to send the request */
442 xasprintf(&send_buffer, "%s&9&%s", req_password, value_list); 411 xasprintf(&send_buffer, "%s&9&%s", req_password, value_list);
443 fetch_data(server_address, server_port, send_buffer); 412 fetch_data(server_address, server_port, send_buffer);
444 age_in_minutes = atoi(strtok(recv_buffer, "&")); 413 unsigned long age_in_minutes = atoi(strtok(recv_buffer, "&"));
445 description = strtok(NULL, "&"); 414 description = strtok(NULL, "&");
446 output_message = strdup(description); 415 output_message = strdup(description);
447 416
@@ -499,9 +468,6 @@ int main(int argc, char **argv) {
499 468
500/* process command-line arguments */ 469/* process command-line arguments */
501int process_arguments(int argc, char **argv) { 470int process_arguments(int argc, char **argv) {
502 int c;
503
504 int option = 0;
505 static struct option longopts[] = {{"port", required_argument, 0, 'p'}, 471 static struct option longopts[] = {{"port", required_argument, 0, 'p'},
506 {"timeout", required_argument, 0, 't'}, 472 {"timeout", required_argument, 0, 't'},
507 {"critical", required_argument, 0, 'c'}, 473 {"critical", required_argument, 0, 'c'},
@@ -529,24 +495,25 @@ int process_arguments(int argc, char **argv) {
529 argc--; 495 argc--;
530 } 496 }
531 497
532 for (c = 1; c < argc; c++) { 498 for (int index = 1; index < argc; index++) {
533 if (strcmp("-to", argv[c]) == 0) { 499 if (strcmp("-to", argv[index]) == 0) {
534 strcpy(argv[c], "-t"); 500 strcpy(argv[index], "-t");
535 } else if (strcmp("-wv", argv[c]) == 0) { 501 } else if (strcmp("-wv", argv[index]) == 0) {
536 strcpy(argv[c], "-w"); 502 strcpy(argv[index], "-w");
537 } else if (strcmp("-cv", argv[c]) == 0) { 503 } else if (strcmp("-cv", argv[index]) == 0) {
538 strcpy(argv[c], "-c"); 504 strcpy(argv[index], "-c");
539 } 505 }
540 } 506 }
541 507
542 while (1) { 508 int option = 0;
543 c = getopt_long(argc, argv, "+hVH:t:c:w:p:v:l:s:d:u", longopts, &option); 509 while (true) {
510 int option_index = getopt_long(argc, argv, "+hVH:t:c:w:p:v:l:s:d:u", longopts, &option);
544 511
545 if (c == -1 || c == EOF || c == 1) { 512 if (option_index == -1 || option_index == EOF || option_index == 1) {
546 break; 513 break;
547 } 514 }
548 515
549 switch (c) { 516 switch (option_index) {
550 case '?': /* print short usage statement if args not parsable */ 517 case '?': /* print short usage statement if args not parsable */
551 usage5(); 518 usage5();
552 case 'h': /* help */ 519 case 'h': /* help */
@@ -638,9 +605,7 @@ int process_arguments(int argc, char **argv) {
638} 605}
639 606
640void fetch_data(const char *address, int port, const char *sendb) { 607void fetch_data(const char *address, int port, const char *sendb) {
641 int result; 608 int result = process_tcp_request(address, port, sendb, recv_buffer, sizeof(recv_buffer));
642
643 result = process_tcp_request(address, port, sendb, recv_buffer, sizeof(recv_buffer));
644 609
645 if (result != STATE_OK) { 610 if (result != STATE_OK) {
646 die(result, _("could not fetch information from server\n")); 611 die(result, _("could not fetch information from server\n"));
@@ -653,15 +618,12 @@ void fetch_data(const char *address, int port, const char *sendb) {
653 618
654bool strtoularray(unsigned long *array, char *string, const char *delim) { 619bool strtoularray(unsigned long *array, char *string, const char *delim) {
655 /* split a <delim> delimited string into a long array */ 620 /* split a <delim> delimited string into a long array */
656 int idx = 0; 621 for (int idx = 0; idx < MAX_VALUE_LIST; idx++) {
657 char *t1;
658
659 for (idx = 0; idx < MAX_VALUE_LIST; idx++) {
660 array[idx] = 0; 622 array[idx] = 0;
661 } 623 }
662 624
663 idx = 0; 625 int idx = 0;
664 for (t1 = strtok(string, delim); t1 != NULL; t1 = strtok(NULL, delim)) { 626 for (char *t1 = strtok(string, delim); t1 != NULL; t1 = strtok(NULL, delim)) {
665 if (is_numeric(t1) && idx < MAX_VALUE_LIST) { 627 if (is_numeric(t1) && idx < MAX_VALUE_LIST) {
666 array[idx] = strtoul(t1, NULL, 10); 628 array[idx] = strtoul(t1, NULL, 10);
667 idx++; 629 idx++;
@@ -674,9 +636,7 @@ bool strtoularray(unsigned long *array, char *string, const char *delim) {
674 636
675void preparelist(char *string) { 637void preparelist(char *string) {
676 /* Replace all , with & which is the delimiter for the request */ 638 /* Replace all , with & which is the delimiter for the request */
677 int i; 639 for (int i = 0; (size_t)i < strlen(string); i++) {
678
679 for (i = 0; (size_t)i < strlen(string); i++) {
680 if (string[i] == ',') { 640 if (string[i] == ',') {
681 string[i] = '&'; 641 string[i] = '&';
682 } 642 }