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