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 "stdlib.h"
32 #include "config.h"
33 #include "common.h"
34 #include "netutils.h"
35 #include "utils.h"
37 #define CHECK_NONE 0
38 #define CHECK_CLIENTVERSION 1
39 #define CHECK_CPULOAD 2
40 #define CHECK_UPTIME 3
41 #define CHECK_USEDDISKSPACE 4
42 #define CHECK_SERVICESTATE 5
43 #define CHECK_PROCSTATE 6
44 #define CHECK_MEMUSE 7
45 #define CHECK_COUNTER 8
46 #define CHECK_FILEAGE 9
47 #define MAX_VALUE_LIST 30
49 #define PORT 1248
51 char *server_address=NULL;
52 char *volume_name=NULL;
53 int server_port=PORT;
54 char *value_list=NULL;
55 char *req_password=NULL;
56 unsigned long lvalue_list[MAX_VALUE_LIST];
57 unsigned long warning_value=0L;
58 unsigned long critical_value=0L;
59 int check_value_list=FALSE;
60 int check_warning_value=FALSE;
61 int check_critical_value=FALSE;
62 int vars_to_check=CHECK_NONE;
63 int show_all=FALSE;
65 const char *progname = "check_nt";
67 int process_arguments(int, char **);
68 void print_usage(void);
69 void print_help(void);
70 void preparelist(char *string);
72 int main(int argc, char **argv){
73 int result;
74 int return_code;
75 char *send_buffer=NULL;
76 char recv_buffer[MAX_INPUT_BUFFER];
77 char *output_message=NULL;
78 char *temp_buffer=NULL;
79 char *temp_string=NULL;
80 char *sep_string=NULL;
81 char *description=NULL;
83 double total_disk_space=0;
84 double free_disk_space=0;
85 double percent_used_space=0;
86 double mem_commitLimit=0;
87 double mem_commitByte=0;
88 unsigned long current_connections=0;
89 unsigned long utilization;
90 unsigned long uptime;
91 unsigned long cache_hits;
92 unsigned long cache_buffers;
93 unsigned long lru_time;
94 unsigned long age_in_minutes;
95 double counter_value;
96 int offset=0;
97 int updays=0;
98 int uphours=0;
99 int upminutes=0;
100 asprintf(&req_password,"None");
102 if(process_arguments(argc,argv)==ERROR)
103 usage("Could not parse arguments\n");
105 /* initialize alarm signal handling */
106 signal(SIGALRM,socket_timeout_alarm_handler);
108 /* set socket timeout */
109 alarm(socket_timeout);
111 if (vars_to_check==CHECK_CLIENTVERSION) {
113 asprintf(&send_buffer,strcat(req_password,"&1"));
114 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
115 if(result!=STATE_OK)
116 return result;
117 asprintf(&output_message,recv_buffer);
118 return_code=STATE_OK;
119 }
120 else if(vars_to_check==CHECK_CPULOAD){
122 if (check_value_list==TRUE) {
123 if (strtolarray(&lvalue_list,value_list,",")==TRUE) {
124 // -l parameters is present with only integers
125 return_code=STATE_OK;
126 asprintf(&temp_string,"CPU Load");
127 while (lvalue_list[0+offset]>0 && lvalue_list[0+offset]<=17280 &&
128 lvalue_list[1+offset]>=0 && lvalue_list[1+offset]<=100 &&
129 lvalue_list[2+offset]>=0 && lvalue_list[2+offset]<=100) {
130 // loop until one of the parameters is wrong or not present
132 // Send request and retrieve data
133 asprintf(&send_buffer,"%s&2&%lu",req_password,lvalue_list[0+offset]);
134 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
135 if(result!=STATE_OK)
136 return result;
138 if (!strncmp(recv_buffer,"ERROR",5)) {
139 printf("NSClient - %s\n",recv_buffer);
140 exit(STATE_UNKNOWN);
141 }
143 utilization=strtoul(recv_buffer,NULL,10);
145 // Check if any of the request is in a warning or critical state
146 if(utilization >= lvalue_list[2+offset])
147 return_code=STATE_CRITICAL;
148 else if(utilization >= lvalue_list[1+offset] && return_code<STATE_WARNING)
149 return_code=STATE_WARNING;
151 asprintf(&output_message," %lu%% (%lu min average)", utilization, lvalue_list[0+offset]);
152 asprintf(&temp_string,"%s%s",temp_string,output_message);
153 offset+=3; //move across the array
154 }
155 if (strlen(temp_string)>10) {
156 // we had at least on loop
157 asprintf(&output_message,"%s",temp_string);
158 }
159 else
160 asprintf(&output_message,"%s","not enough values for -l parameters");
162 } else
163 asprintf(&output_message,"wrong -l parameter.");
165 } else
166 asprintf(&output_message,"missing -l parameters");
167 }
169 else if(vars_to_check==CHECK_UPTIME){
171 asprintf(&send_buffer,strcat(req_password,"&3"));
172 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
173 if(result!=STATE_OK)
174 return result;
176 if (!strncmp(recv_buffer,"ERROR",5)) {
177 printf("NSClient - %s\n",recv_buffer);
178 exit(STATE_UNKNOWN);
179 }
181 uptime=strtoul(recv_buffer,NULL,10);
182 updays = uptime / 86400;
183 uphours = (uptime % 86400) / 3600;
184 upminutes = ((uptime % 86400) % 3600) / 60;
185 asprintf(&output_message,"System Uptime : %u day(s) %u hour(s) %u minute(s)",updays,uphours, upminutes);
186 return_code=STATE_OK;
187 }
189 else if(vars_to_check==CHECK_USEDDISKSPACE){
191 return_code=STATE_UNKNOWN;
192 if (check_value_list==TRUE) {
193 if (strlen(value_list)==1) {
194 asprintf(&send_buffer,"%s&4&%s", req_password, value_list);
195 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
196 if(result!=STATE_OK)
197 return result;
199 if (!strncmp(recv_buffer,"ERROR",5)) {
200 printf("NSClient - %s\n",recv_buffer);
201 exit(STATE_UNKNOWN);
202 }
204 free_disk_space=atof(strtok(recv_buffer,"&"));
205 total_disk_space=atof(strtok(NULL,"&"));
206 percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100;
208 if (free_disk_space>=0) {
209 asprintf(&temp_string,"%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)",
210 value_list, total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space,
211 free_disk_space / 1073741824, (free_disk_space / total_disk_space)*100);
214 if(check_critical_value==TRUE && percent_used_space >= critical_value)
215 return_code=STATE_CRITICAL;
216 else if (check_warning_value==TRUE && percent_used_space >= warning_value)
217 return_code=STATE_WARNING;
218 else
219 return_code=STATE_OK;
221 asprintf(&output_message,"%s",temp_string);
223 }
224 else {
225 asprintf(&output_message,"Free disk space : Invalid drive ");
226 return_code=STATE_UNKNOWN;
227 }
228 }
229 else
230 asprintf(&output_message,"wrong -l argument");
231 } else
232 asprintf(&output_message,"missing -l parameters");
234 }
236 else if(vars_to_check==CHECK_SERVICESTATE || vars_to_check==CHECK_PROCSTATE){
238 if (check_value_list==TRUE) {
239 preparelist(value_list); // replace , between services with & to send the request
240 asprintf(&send_buffer,"%s&%u&%s&%s", req_password,(vars_to_check==CHECK_SERVICESTATE)?5:6,
241 (show_all==TRUE)?"ShowAll":"ShowFail",value_list);
242 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
243 if(result!=STATE_OK)
244 return result;
246 if (!strncmp(recv_buffer,"ERROR",5)) {
247 printf("NSClient - %s\n",recv_buffer);
248 exit(STATE_UNKNOWN);
249 }
250 return_code=atoi(strtok(recv_buffer,"&"));
251 temp_string=strtok(NULL,"&");
252 asprintf(&output_message, "%s",temp_string);
253 }
254 else
255 asprintf(&output_message,"No service/process specified");
256 }
258 else if(vars_to_check==CHECK_MEMUSE) {
260 asprintf(&send_buffer,"%s&7", req_password);
261 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
262 if (result!=STATE_OK)
263 return result;
265 if (!strncmp(recv_buffer,"ERROR",5)) {
266 printf("NSClient - %s\n",recv_buffer);
267 exit(STATE_UNKNOWN);
268 }
270 mem_commitLimit=atof(strtok(recv_buffer,"&"));
271 mem_commitByte=atof(strtok(NULL,"&"));
272 percent_used_space = (mem_commitByte / mem_commitLimit) * 100;
273 asprintf(&output_message,"Memory usage: total:%.2f Mb - used: %.2f Mb (%.0f%%) - free: %.2f Mb (%.0f%%)",
274 mem_commitLimit / 1048576, mem_commitByte / 1048567, percent_used_space,
275 (mem_commitLimit - mem_commitByte) / 1048576, (mem_commitLimit - mem_commitByte) / mem_commitLimit * 100);
277 if(check_critical_value==TRUE && percent_used_space >= critical_value)
278 return_code=STATE_CRITICAL;
279 else if (check_warning_value==TRUE && percent_used_space >= warning_value)
280 return_code=STATE_WARNING;
281 else
282 return_code=STATE_OK;
284 }
286 else if(vars_to_check==CHECK_COUNTER) {
288 if (check_value_list==TRUE) {
289 preparelist(value_list); // replace , between services with & to send the request
290 asprintf(&send_buffer,"%s&8&%s", req_password,value_list);
291 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
292 if (result!=STATE_OK)
293 return result;
295 if (!strncmp(recv_buffer,"ERROR",5)) {
296 printf("NSClient - %s\n",recv_buffer);
297 exit(STATE_UNKNOWN);
298 }
300 strtok(value_list,"&"); // burn the first parameters
301 description = strtok(NULL,"&");
302 counter_value = atof(recv_buffer);
303 if (description == NULL)
304 asprintf(&output_message, "%.f", counter_value);
305 else
306 asprintf(&output_message, description, counter_value);
308 if (critical_value > warning_value) {
309 // Normal thresholds
310 if(check_critical_value==TRUE && counter_value >= critical_value)
311 return_code=STATE_CRITICAL;
312 else if (check_warning_value==TRUE && counter_value >= warning_value)
313 return_code=STATE_WARNING;
314 else
315 return_code=STATE_OK;
316 }
317 else {
318 // inverse thresholds
319 if(check_critical_value==TRUE && counter_value <= critical_value)
320 return_code=STATE_CRITICAL;
321 else if (check_warning_value==TRUE && counter_value <= warning_value)
322 return_code=STATE_WARNING;
323 else
324 return_code=STATE_OK;
325 }
327 }
328 else {
329 asprintf(&output_message,"No counter specified");
330 result=STATE_UNKNOWN;
331 }
332 }
333 else if(vars_to_check==CHECK_FILEAGE) {
335 if (check_value_list==TRUE) {
336 preparelist(value_list); // replace , between services with & to send the request
337 asprintf(&send_buffer,"%s&9&%s", req_password,value_list);
338 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
339 if (result!=STATE_OK)
340 return result;
342 if (!strncmp(recv_buffer,"ERROR",5)) {
343 printf("NSClient - %s\n",recv_buffer);
344 exit(STATE_UNKNOWN);
345 }
347 age_in_minutes = atoi(strtok(recv_buffer,"&"));
348 description = strtok(NULL,"&");
349 asprintf(&output_message, description);
351 if (critical_value > warning_value) {
352 // Normal thresholds
353 if(check_critical_value==TRUE && age_in_minutes >= critical_value)
354 return_code=STATE_CRITICAL;
355 else if (check_warning_value==TRUE && age_in_minutes >= warning_value)
356 return_code=STATE_WARNING;
357 else
358 return_code=STATE_OK;
359 }
360 else {
361 // inverse thresholds
362 if(check_critical_value==TRUE && age_in_minutes <= critical_value)
363 return_code=STATE_CRITICAL;
364 else if (check_warning_value==TRUE && age_in_minutes <= warning_value)
365 return_code=STATE_WARNING;
366 else
367 return_code=STATE_OK;
368 }
370 }
371 else {
372 asprintf(&output_message,"No file specified");
373 result=STATE_UNKNOWN;
374 }
375 }
377 /* reset timeout */
378 alarm(0);
380 printf("%s\n",output_message);
382 return return_code;
383 }
386 /* process command-line arguments */
387 int process_arguments(int argc, char **argv){
388 int c;
390 #ifdef HAVE_GETOPT_H
391 int option_index = 0;
392 static struct option long_options[] =
393 {
394 {"port", required_argument,0,'p'},
395 {"timeout", required_argument,0,'t'},
396 {"critical", required_argument,0,'c'},
397 {"warning", required_argument,0,'w'},
398 {"variable", required_argument,0,'v'},
399 {"hostname", required_argument,0,'H'},
400 {"version", no_argument, 0,'V'},
401 {"help", no_argument, 0,'h'},
402 {0,0,0,0}
403 };
404 #endif
406 /* no options were supplied */
407 if(argc<2) return ERROR;
409 /* backwards compatibility */
410 if (! is_option(argv[1])) {
411 server_address=argv[1];
412 argv[1]=argv[0];
413 argv=&argv[1];
414 argc--;
415 }
417 for (c=1;c<argc;c++) {
418 if(strcmp("-to",argv[c])==0)
419 strcpy(argv[c],"-t");
420 else if (strcmp("-wv",argv[c])==0)
421 strcpy(argv[c],"-w");
422 else if (strcmp("-cv",argv[c])==0)
423 strcpy(argv[c],"-c");
424 }
426 while (1){
427 #ifdef HAVE_GETOPT_H
428 c = getopt_long(argc,argv,"+hVH:t:c:w:p:v:l:s:d:",long_options,&option_index);
429 #else
430 c = getopt(argc,argv,"+hVH:t:c:w:p:v:l:s:d:");
431 #endif
433 if (c==-1||c==EOF||c==1)
434 break;
436 switch (c)
437 {
438 case '?': /* print short usage statement if args not parsable */
439 printf("%s: Unknown argument: %s\n\n",progname,optarg);
440 print_usage();
441 exit(STATE_UNKNOWN);
442 case 'h': /* help */
443 print_help();
444 exit(STATE_OK);
445 case 'V': /* version */
446 print_revision(progname,"$Revision$");
447 exit(STATE_OK);
448 case 'H': /* hostname */
449 server_address=optarg;
450 break;
451 case 's': /* password */
452 asprintf(&req_password,optarg);
453 break;
454 case 'p': /* port */
455 if (is_intnonneg(optarg))
456 server_port=atoi(optarg);
457 else
458 terminate(STATE_UNKNOWN,"Server port an integer (seconds)\nType '%s -h' for additional help\n",progname);
459 break;
460 case 'v':
461 if(strlen(optarg)<4)
462 return ERROR;
463 if(!strcmp(optarg,"CLIENTVERSION"))
464 vars_to_check=CHECK_CLIENTVERSION;
465 else if(!strcmp(optarg,"CPULOAD"))
466 vars_to_check=CHECK_CPULOAD;
467 else if(!strcmp(optarg,"UPTIME"))
468 vars_to_check=CHECK_UPTIME;
469 else if(!strcmp(optarg,"USEDDISKSPACE"))
470 vars_to_check=CHECK_USEDDISKSPACE;
471 else if(!strcmp(optarg,"SERVICESTATE"))
472 vars_to_check=CHECK_SERVICESTATE;
473 else if(!strcmp(optarg,"PROCSTATE"))
474 vars_to_check=CHECK_PROCSTATE;
475 else if(!strcmp(optarg,"MEMUSE"))
476 vars_to_check=CHECK_MEMUSE;
477 else if(!strcmp(optarg,"COUNTER"))
478 vars_to_check=CHECK_COUNTER;
479 else if(!strcmp(optarg,"FILEAGE"))
480 vars_to_check=CHECK_FILEAGE;
481 else
482 return ERROR;
483 break;
484 case 'l': /* value list */
485 asprintf(&value_list,"%s",optarg);
486 check_value_list=TRUE;
487 break;
488 case 'w': /* warning threshold */
489 warning_value=strtoul(optarg,NULL,10);
490 check_warning_value=TRUE;
491 break;
492 case 'c': /* critical threshold */
493 critical_value=strtoul(optarg,NULL,10);
494 check_critical_value=TRUE;
495 break;
496 case 'd': /* Display select for services */
497 if (!strcmp(optarg,"SHOWALL"))
498 show_all = TRUE;
499 break;
500 case 't': /* timeout */
501 socket_timeout=atoi(optarg);
502 if(socket_timeout<=0)
503 return ERROR;
504 }
506 }
508 if (vars_to_check==CHECK_NONE)
509 return ERROR;
511 return OK;
512 }
515 void print_usage(void)
516 {
517 printf("Usage: %s -H host -v variable [-p port] [-w warning] [-c critical] [-l params] [-d SHOWALL] [-t timeout]\n",progname);
518 }
521 void print_help(void)
522 {
523 print_revision(progname,"$Revision$");
524 printf
525 ("Copyright (c) 2000 Yves Rubin (rubiyz@yahoo.com)\n\n"
526 "This plugin attempts to contact the NSClient service running on a Windows NT/2000/XP server to\n"
527 "gather the requested system information.\n\n");
528 print_usage();
529 printf
530 ("\nOptions:\n"
531 "-H, --hostname=HOST\n"
532 " Name of the host to check\n"
533 "-p, --port=INTEGER\n"
534 " Optional port number (default: %d)\n"
535 "-s <password>\n"
536 " Password needed for the request\n"
537 "-v, --variable=STRING\n"
538 " Variable to check. Valid variables are:\n"
539 " CLIENTVERSION = Get the NSClient version\n"
540 " CPULOAD = Average CPU load on last x minutes. Request a -l parameter with the following syntax:\n"
541 " -l <minutes range>,<warning threshold>,<critical threshold>. <minute range> should be less than 24*60.\n"
542 " Thresholds are percentage and up to 10 requests can be done in one shot. ie: -l 60,90,95,120,90,95\n"
543 " UPTIME = Get the uptime of the machine. No specific parameters. No warning or critical threshold\n"
544 " USEDDISKSPACE = Size and percentage of disk use. Request a -l parameter containing the drive letter only.\n"
545 " Warning and critical thresholds can be specified with -w and -c.\n"
546 " MEMUSE = Memory use. Warning and critical thresholds can be specified with -w and -c.\n"
547 " SERVICESTATE = Check the state of one or several services. Request a -l parameters with the following syntax:\n"
548 " -l <service1>,<service2>,<service3>,... You can specify -d SHOWALL in case you want to see working services\n"
549 " in the returned string.\n"
550 " PROCSTATE = Check if one or several process are running. Same syntax as SERVICESTATE.\n"
551 " COUNTER = Check any performance counter of Windows NT/2000. Request a -l parameters with the following syntax:\n"
552 " -l \"\\\\<performance object>\\\\counter\",\"<description>\" The <description> parameter is optional and \n"
553 " is given to a printf output command which require a float parameters. Some examples:\n"
554 " \"Paging file usage is %%.2f %%%%\" or \"%%.f %%%% paging file used.\"\n"
555 " -w, --warning=INTEGER\n"
556 " Threshold which will result in a warning status\n"
557 " -c, --critical=INTEGER\n"
558 " Threshold which will result in a critical status\n"
559 " -t, --timeout=INTEGER\n"
560 " Seconds before connection attempt times out (default: %d)\n"
561 "-h, --help\n"
562 " Print this help screen\n"
563 "-V, --version\n"
564 " Print version information\n\n"
565 "Notes:\n"
566 " - The NSClient service should be running on the server to get any information.\n"
567 " - Critical thresholds should be lower than warning thresholds\n", PORT, DEFAULT_SOCKET_TIMEOUT);
568 }
570 int strtolarray(unsigned long *array, char *string, char *delim) {
571 // split a <delim> delimited string into a long array
572 int idx=0;
573 char *t1;
575 for (idx=0;idx<MAX_VALUE_LIST;idx++)
576 array[idx]=-1;
578 idx=0;
579 for(t1 = strtok(string,delim);t1 != NULL; t1 = strtok(NULL, delim)) {
580 if (is_numeric(t1) && idx<MAX_VALUE_LIST) {
581 array[idx]=strtoul(t1,NULL,10);
582 idx++;
583 } else
584 return FALSE;
585 }
586 return TRUE;
587 }
589 void preparelist(char *string) {
590 // Replace all , with & which is the delimiter for the request
591 int i;
593 for (i = 0; i < strlen(string); i++)
594 if (string[i] == ',') {
595 string[i]='&';
596 }
597 }