1 /******************************************************************************
2 *
3 * CHECK_NT.C
4 *
5 * Program: Windows NT plugin for NetSaint
6 * License: GPL
7 * Copyright (c) 2000-2002 Yves Rubin (rubiyz@yahoo.com)
8 *
9 * Description:
10 *
11 * This requires NSClient software to run on NT (http://nsclient.ready2run.nl/)
12 *
13 * License Information:
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 *****************************************************************************/
31 #include "config.h"
32 #include "common.h"
33 #include "netutils.h"
34 #include "utils.h"
36 #define CHECK_NONE 0
37 #define CHECK_CLIENTVERSION 1
38 #define CHECK_CPULOAD 2
39 #define CHECK_UPTIME 3
40 #define CHECK_USEDDISKSPACE 4
41 #define CHECK_SERVICESTATE 5
42 #define CHECK_PROCSTATE 6
43 #define CHECK_MEMUSE 7
44 #define CHECK_COUNTER 8
45 #define CHECK_FILEAGE 9
46 #define MAX_VALUE_LIST 30
48 #define PORT 1248
50 char *server_address=NULL;
51 char *volume_name=NULL;
52 int server_port=PORT;
53 char *value_list=NULL;
54 char *req_password=NULL;
55 unsigned long lvalue_list[MAX_VALUE_LIST];
56 unsigned long warning_value=0L;
57 unsigned long critical_value=0L;
58 int check_value_list=FALSE;
59 int check_warning_value=FALSE;
60 int check_critical_value=FALSE;
61 int vars_to_check=CHECK_NONE;
62 int show_all=FALSE;
64 const char *progname = "check_nt";
66 int process_arguments(int, char **);
67 void print_usage(void);
68 void print_help(void);
69 void preparelist(char *string);
71 int main(int argc, char **argv){
72 int result;
73 int return_code = STATE_UNKNOWN;
74 char *send_buffer=NULL;
75 char recv_buffer[MAX_INPUT_BUFFER];
76 char *output_message=NULL;
77 char *temp_string=NULL;
78 char *description=NULL;
80 double total_disk_space=0;
81 double free_disk_space=0;
82 double percent_used_space=0;
83 double mem_commitLimit=0;
84 double mem_commitByte=0;
85 unsigned long utilization;
86 unsigned long uptime;
87 unsigned long age_in_minutes;
88 double counter_value;
89 int offset=0;
90 int updays=0;
91 int uphours=0;
92 int upminutes=0;
94 asprintf(&req_password,"None");
96 if(process_arguments(argc,argv)==ERROR)
97 usage("Could not parse arguments\n");
99 /* initialize alarm signal handling */
100 signal(SIGALRM,socket_timeout_alarm_handler);
102 /* set socket timeout */
103 alarm(socket_timeout);
105 if (vars_to_check==CHECK_CLIENTVERSION) {
107 asprintf(&send_buffer,strcat(req_password,"&1"));
108 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
109 if(result!=STATE_OK)
110 return result;
111 asprintf(&output_message,recv_buffer);
112 return_code=STATE_OK;
114 }
116 else if(vars_to_check==CHECK_CPULOAD){
118 if (check_value_list==TRUE) {
119 if (strtolarray(&lvalue_list,value_list,",")==TRUE) {
120 /* -l parameters is present with only integers */
121 return_code=STATE_OK;
122 asprintf(&temp_string,"CPU Load");
123 while (lvalue_list[0+offset]>(unsigned long)0 &&
124 lvalue_list[0+offset]<=(unsigned long)17280 &&
125 lvalue_list[1+offset]>=(unsigned long)0 &&
126 lvalue_list[1+offset]<=(unsigned long)100 &&
127 lvalue_list[2+offset]>=(unsigned long)0 &&
128 lvalue_list[2+offset]<=(unsigned long)100) {
129 /* loop until one of the parameters is wrong or not present */
131 /* Send request and retrieve data */
132 asprintf(&send_buffer,"%s&2&%lu",req_password,lvalue_list[0+offset]);
133 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
134 if(result!=STATE_OK)
135 return result;
137 if (!strncmp(recv_buffer,"ERROR",5)) {
138 printf("NSClient - %s\n",recv_buffer);
139 exit(STATE_UNKNOWN);
140 }
142 utilization=strtoul(recv_buffer,NULL,10);
144 /* Check if any of the request is in a warning or critical state */
145 if(utilization >= lvalue_list[2+offset])
146 return_code=STATE_CRITICAL;
147 else if(utilization >= lvalue_list[1+offset] && return_code<STATE_WARNING)
148 return_code=STATE_WARNING;
150 asprintf(&output_message," %lu%% (%lu min average)", utilization, lvalue_list[0+offset]);
151 asprintf(&temp_string,"%s%s",temp_string,output_message);
152 offset+=3; /* move across the array */
153 }
154 if (strlen(temp_string)>10) {
155 /* we had at least on loop */
156 asprintf(&output_message,"%s",temp_string);
157 }
158 else
159 asprintf(&output_message,"%s","not enough values for -l parameters");
161 } else
162 asprintf(&output_message,"wrong -l parameter.");
164 } else
165 asprintf(&output_message,"missing -l parameters");
166 }
168 else if(vars_to_check==CHECK_UPTIME){
170 asprintf(&send_buffer,strcat(req_password,"&3"));
171 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
172 if(result!=STATE_OK)
173 return result;
175 if (!strncmp(recv_buffer,"ERROR",5)) {
176 printf("NSClient - %s\n",recv_buffer);
177 exit(STATE_UNKNOWN);
178 }
180 uptime=strtoul(recv_buffer,NULL,10);
181 updays = uptime / 86400;
182 uphours = (uptime % 86400) / 3600;
183 upminutes = ((uptime % 86400) % 3600) / 60;
184 asprintf(&output_message,"System Uptime : %u day(s) %u hour(s) %u minute(s)",updays,uphours, upminutes);
185 return_code=STATE_OK;
186 }
188 else if(vars_to_check==CHECK_USEDDISKSPACE){
190 return_code=STATE_UNKNOWN;
191 if (check_value_list==TRUE) {
192 if (strlen(value_list)==1) {
193 asprintf(&send_buffer,"%s&4&%s", req_password, value_list);
194 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
195 if(result!=STATE_OK)
196 return result;
198 if (!strncmp(recv_buffer,"ERROR",5)) {
199 printf("NSClient - %s\n",recv_buffer);
200 exit(STATE_UNKNOWN);
201 }
203 free_disk_space=atof(strtok(recv_buffer,"&"));
204 total_disk_space=atof(strtok(NULL,"&"));
205 percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100;
207 if (free_disk_space>=0) {
208 asprintf(&temp_string,"%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)",
209 value_list, total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space,
210 free_disk_space / 1073741824, (free_disk_space / total_disk_space)*100);
213 if(check_critical_value==TRUE && percent_used_space >= critical_value)
214 return_code=STATE_CRITICAL;
215 else if (check_warning_value==TRUE && percent_used_space >= warning_value)
216 return_code=STATE_WARNING;
217 else
218 return_code=STATE_OK;
220 asprintf(&output_message,"%s",temp_string);
222 }
223 else {
224 asprintf(&output_message,"Free disk space : Invalid drive ");
225 return_code=STATE_UNKNOWN;
226 }
227 }
228 else
229 asprintf(&output_message,"wrong -l argument");
230 } else
231 asprintf(&output_message,"missing -l parameters");
233 }
235 else if(vars_to_check==CHECK_SERVICESTATE || vars_to_check==CHECK_PROCSTATE){
237 if (check_value_list==TRUE) {
238 preparelist(value_list); /* replace , between services with & to send the request */
239 asprintf(&send_buffer,"%s&%u&%s&%s", req_password,(vars_to_check==CHECK_SERVICESTATE)?5:6,
240 (show_all==TRUE)?"ShowAll":"ShowFail",value_list);
241 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
242 if(result!=STATE_OK)
243 return result;
245 if (!strncmp(recv_buffer,"ERROR",5)) {
246 printf("NSClient - %s\n",recv_buffer);
247 exit(STATE_UNKNOWN);
248 }
249 return_code=atoi(strtok(recv_buffer,"&"));
250 temp_string=strtok(NULL,"&");
251 asprintf(&output_message, "%s",temp_string);
252 }
253 else
254 asprintf(&output_message,"No service/process specified");
255 }
257 else if(vars_to_check==CHECK_MEMUSE) {
259 asprintf(&send_buffer,"%s&7", req_password);
260 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
261 if (result!=STATE_OK)
262 return result;
264 if (!strncmp(recv_buffer,"ERROR",5)) {
265 printf("NSClient - %s\n",recv_buffer);
266 exit(STATE_UNKNOWN);
267 }
269 mem_commitLimit=atof(strtok(recv_buffer,"&"));
270 mem_commitByte=atof(strtok(NULL,"&"));
271 percent_used_space = (mem_commitByte / mem_commitLimit) * 100;
272 asprintf(&output_message,"Memory usage: total:%.2f Mb - used: %.2f Mb (%.0f%%) - free: %.2f Mb (%.0f%%)",
273 mem_commitLimit / 1048576, mem_commitByte / 1048567, percent_used_space,
274 (mem_commitLimit - mem_commitByte) / 1048576, (mem_commitLimit - mem_commitByte) / mem_commitLimit * 100);
276 if(check_critical_value==TRUE && percent_used_space >= critical_value)
277 return_code=STATE_CRITICAL;
278 else if (check_warning_value==TRUE && percent_used_space >= warning_value)
279 return_code=STATE_WARNING;
280 else
281 return_code=STATE_OK;
283 }
285 else if(vars_to_check==CHECK_COUNTER) {
287 if (check_value_list==TRUE) {
288 preparelist(value_list); /* replace , between services with & to send the request */
289 asprintf(&send_buffer,"%s&8&%s", req_password,value_list);
290 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
291 if (result!=STATE_OK)
292 return result;
294 if (!strncmp(recv_buffer,"ERROR",5)) {
295 printf("NSClient - %s\n",recv_buffer);
296 exit(STATE_UNKNOWN);
297 }
299 strtok(value_list,"&"); /* burn the first parameters */
300 description = strtok(NULL,"&");
301 counter_value = atof(recv_buffer);
302 if (description == NULL)
303 asprintf(&output_message, "%.f", counter_value);
304 else
305 asprintf(&output_message, description, counter_value);
307 if (critical_value > warning_value) {
308 /* Normal thresholds */
309 if(check_critical_value==TRUE && counter_value >= critical_value)
310 return_code=STATE_CRITICAL;
311 else if (check_warning_value==TRUE && counter_value >= warning_value)
312 return_code=STATE_WARNING;
313 else
314 return_code=STATE_OK;
315 }
316 else {
317 /* inverse thresholds */
318 if(check_critical_value==TRUE && counter_value <= critical_value)
319 return_code=STATE_CRITICAL;
320 else if (check_warning_value==TRUE && counter_value <= warning_value)
321 return_code=STATE_WARNING;
322 else
323 return_code=STATE_OK;
324 }
326 }
327 else {
328 asprintf(&output_message,"No counter specified");
329 result=STATE_UNKNOWN;
330 }
331 }
332 else if(vars_to_check==CHECK_FILEAGE) {
334 if (check_value_list==TRUE) {
335 preparelist(value_list); /* replace , between services with & to send the request */
336 asprintf(&send_buffer,"%s&9&%s", req_password,value_list);
337 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
338 if (result!=STATE_OK)
339 return result;
341 if (!strncmp(recv_buffer,"ERROR",5)) {
342 printf("NSClient - %s\n",recv_buffer);
343 exit(STATE_UNKNOWN);
344 }
346 age_in_minutes = atoi(strtok(recv_buffer,"&"));
347 description = strtok(NULL,"&");
348 asprintf(&output_message, description);
350 if (critical_value > warning_value) {
351 /* Normal thresholds */
352 if(check_critical_value==TRUE && age_in_minutes >= critical_value)
353 return_code=STATE_CRITICAL;
354 else if (check_warning_value==TRUE && age_in_minutes >= warning_value)
355 return_code=STATE_WARNING;
356 else
357 return_code=STATE_OK;
358 }
359 else {
360 /* inverse thresholds */
361 if(check_critical_value==TRUE && age_in_minutes <= critical_value)
362 return_code=STATE_CRITICAL;
363 else if (check_warning_value==TRUE && age_in_minutes <= warning_value)
364 return_code=STATE_WARNING;
365 else
366 return_code=STATE_OK;
367 }
369 }
370 else {
371 asprintf(&output_message,"No file specified");
372 result=STATE_UNKNOWN;
373 }
374 }
376 /* reset timeout */
377 alarm(0);
379 printf("%s\n",output_message);
381 return return_code;
382 }
385 /* process command-line arguments */
386 int process_arguments(int argc, char **argv){
387 int c;
389 int option_index = 0;
390 static struct option long_options[] =
391 {
392 {"port", required_argument,0,'p'},
393 {"timeout", required_argument,0,'t'},
394 {"critical", required_argument,0,'c'},
395 {"warning", required_argument,0,'w'},
396 {"variable", required_argument,0,'v'},
397 {"hostname", required_argument,0,'H'},
398 {"version", no_argument, 0,'V'},
399 {"help", no_argument, 0,'h'},
400 {0,0,0,0}
401 };
403 /* no options were supplied */
404 if(argc<2) return ERROR;
406 /* backwards compatibility */
407 if (! is_option(argv[1])) {
408 server_address=argv[1];
409 argv[1]=argv[0];
410 argv=&argv[1];
411 argc--;
412 }
414 for (c=1;c<argc;c++) {
415 if(strcmp("-to",argv[c])==0)
416 strcpy(argv[c],"-t");
417 else if (strcmp("-wv",argv[c])==0)
418 strcpy(argv[c],"-w");
419 else if (strcmp("-cv",argv[c])==0)
420 strcpy(argv[c],"-c");
421 }
423 while (1){
424 c = getopt_long(argc,argv,"+hVH:t:c:w:p:v:l:s:d:",long_options,&option_index);
426 if (c==-1||c==EOF||c==1)
427 break;
429 switch (c)
430 {
431 case '?': /* print short usage statement if args not parsable */
432 printf("%s: Unknown argument: %s\n\n",progname,optarg);
433 print_usage();
434 exit(STATE_UNKNOWN);
435 case 'h': /* help */
436 print_help();
437 exit(STATE_OK);
438 case 'V': /* version */
439 print_revision(progname,"$Revision$");
440 exit(STATE_OK);
441 case 'H': /* hostname */
442 server_address=optarg;
443 break;
444 case 's': /* password */
445 asprintf(&req_password,optarg);
446 break;
447 case 'p': /* port */
448 if (is_intnonneg(optarg))
449 server_port=atoi(optarg);
450 else
451 terminate(STATE_UNKNOWN,"Server port an integer (seconds)\nType '%s -h' for additional help\n",progname);
452 break;
453 case 'v':
454 if(strlen(optarg)<4)
455 return ERROR;
456 if(!strcmp(optarg,"CLIENTVERSION"))
457 vars_to_check=CHECK_CLIENTVERSION;
458 else if(!strcmp(optarg,"CPULOAD"))
459 vars_to_check=CHECK_CPULOAD;
460 else if(!strcmp(optarg,"UPTIME"))
461 vars_to_check=CHECK_UPTIME;
462 else if(!strcmp(optarg,"USEDDISKSPACE"))
463 vars_to_check=CHECK_USEDDISKSPACE;
464 else if(!strcmp(optarg,"SERVICESTATE"))
465 vars_to_check=CHECK_SERVICESTATE;
466 else if(!strcmp(optarg,"PROCSTATE"))
467 vars_to_check=CHECK_PROCSTATE;
468 else if(!strcmp(optarg,"MEMUSE"))
469 vars_to_check=CHECK_MEMUSE;
470 else if(!strcmp(optarg,"COUNTER"))
471 vars_to_check=CHECK_COUNTER;
472 else if(!strcmp(optarg,"FILEAGE"))
473 vars_to_check=CHECK_FILEAGE;
474 else
475 return ERROR;
476 break;
477 case 'l': /* value list */
478 asprintf(&value_list,"%s",optarg);
479 check_value_list=TRUE;
480 break;
481 case 'w': /* warning threshold */
482 warning_value=strtoul(optarg,NULL,10);
483 check_warning_value=TRUE;
484 break;
485 case 'c': /* critical threshold */
486 critical_value=strtoul(optarg,NULL,10);
487 check_critical_value=TRUE;
488 break;
489 case 'd': /* Display select for services */
490 if (!strcmp(optarg,"SHOWALL"))
491 show_all = TRUE;
492 break;
493 case 't': /* timeout */
494 socket_timeout=atoi(optarg);
495 if(socket_timeout<=0)
496 return ERROR;
497 }
499 }
501 if (vars_to_check==CHECK_NONE)
502 return ERROR;
504 return OK;
505 }
508 void print_usage(void)
509 {
510 printf("Usage: %s -H host -v variable [-p port] [-w warning] [-c critical] [-l params] [-d SHOWALL] [-t timeout]\n",progname);
511 }
514 void print_help(void)
515 {
516 print_revision(progname,"$Revision$");
517 printf ("\
518 Copyright (c) 2000 Yves Rubin (rubiyz@yahoo.com)\n\n\
519 This plugin collects data from the NSClient service running on a\n\
520 Windows NT/2000/XP server.\n\n");
521 print_usage();
522 printf ("\nOptions:\n\
523 -H, --hostname=HOST\n\
524 Name of the host to check\n\
525 -p, --port=INTEGER\n\
526 Optional port number (default: %d)\n\
527 -s <password>\n\
528 Password needed for the request\n\
529 -w, --warning=INTEGER\n\
530 Threshold which will result in a warning status\n\
531 -c, --critical=INTEGER\n\
532 Threshold which will result in a critical status\n\
533 -t, --timeout=INTEGER\n\
534 Seconds before connection attempt times out (default: %d)\n\
535 -h, --help\n\
536 Print this help screen\n\
537 -V, --version\n\
538 Print version information\n",
539 PORT, DEFAULT_SOCKET_TIMEOUT);
540 printf ("\
541 -v, --variable=STRING\n\
542 Variable to check. Valid variables are:\n");
543 printf ("\
544 CLIENTVERSION = Get the NSClient version\n");
545 printf ("\
546 CPULOAD = Average CPU load on last x minutes.\n\
547 Request a -l parameter with the following syntax:\n\
548 -l <minutes range>,<warning threshold>,<critical threshold>.\n\
549 <minute range> should be less than 24*60.\n\
550 Thresholds are percentage and up to 10 requests can be done in one shot.\n\
551 ie: -l 60,90,95,120,90,95\n");
552 printf ("\
553 UPTIME = Get the uptime of the machine.\n\
554 No specific parameters. No warning or critical threshold\n");
555 printf ("\
556 USEDDISKSPACE = Size and percentage of disk use.\n\
557 Request a -l parameter containing the drive letter only.\n\
558 Warning and critical thresholds can be specified with -w and -c.\n");
559 printf ("\
560 MEMUSE = Memory use.\n\
561 Warning and critical thresholds can be specified with -w and -c.\n");
562 printf ("\
563 SERVICESTATE = Check the state of one or several services.\n\
564 Request a -l parameters with the following syntax:\n\
565 -l <service1>,<service2>,<service3>,...\n\
566 You can specify -d SHOWALL in case you want to see working services\n\
567 in the returned string.\n");
568 printf ("\
569 PROCSTATE = Check if one or several process are running.\n\
570 Same syntax as SERVICESTATE.\n");
571 printf ("\
572 COUNTER = Check any performance counter of Windows NT/2000.\n\
573 Request a -l parameters with the following syntax:\n\
574 -l \"\\\\<performance object>\\\\counter\",\"<description>\n\
575 The <description> parameter is optional and \n\
576 is given to a printf output command which require a float parameters.\n\
577 Some examples:\n\
578 \"Paging file usage is %%.2f %%%%\"\n\
579 \"%%.f %%%% paging file used.\"\n");
580 printf ("Notes:\n\
581 - The NSClient service should be running on the server to get any information\n\
582 (http://nsclient.ready2run.nl).\n\
583 - Critical thresholds should be lower than warning thresholds\n");
584 }
586 int strtolarray(unsigned long *array, char *string, char *delim) {
587 /* split a <delim> delimited string into a long array */
588 int idx=0;
589 char *t1;
591 for (idx=0;idx<MAX_VALUE_LIST;idx++)
592 array[idx]=-1;
594 idx=0;
595 for(t1 = strtok(string,delim);t1 != NULL; t1 = strtok(NULL, delim)) {
596 if (is_numeric(t1) && idx<MAX_VALUE_LIST) {
597 array[idx]=strtoul(t1,NULL,10);
598 idx++;
599 } else
600 return FALSE;
601 }
602 return TRUE;
603 }
605 void preparelist(char *string) {
606 /* Replace all , with & which is the delimiter for the request */
607 int i;
609 for (i = 0; (size_t)i < strlen(string); i++)
610 if (string[i] == ',') {
611 string[i]='&';
612 }
613 }