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