Code

Option to verify check_nt version (799098 - Steve Hanselman)
[nagiosplug.git] / plugins / check_nt.c
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_warning_value=FALSE;
62 int check_critical_value=FALSE;
63 enum checkvars vars_to_check = CHECK_NONE;
64 int show_all=FALSE;
66 const char *progname = "check_nt";
68 char recv_buffer[MAX_INPUT_BUFFER];
70 void fetch_data (const char* address, int port, const char* sendb);
71 int process_arguments(int, char **);
72 void preparelist(char *string);
73 int strtoularray(unsigned long *array, char *string, const char *delim);
74 void print_help(void);
75 void print_usage(void);
77 int main(int argc, char **argv){
78         int return_code = STATE_UNKNOWN;
79         char *send_buffer=NULL;
80         char *output_message=NULL;
81         char *temp_string=NULL;
82         char *description=NULL;
84         double total_disk_space=0;
85         double free_disk_space=0;
86         double percent_used_space=0;
87         double mem_commitLimit=0;
88         double mem_commitByte=0;
89         unsigned long utilization;
90         unsigned long uptime;
91         unsigned long age_in_minutes;
92         double counter_value;
93         int offset=0;
94         int updays=0;
95         int uphours=0;
96         int upminutes=0;
98         setlocale (LC_ALL, "");
99         bindtextdomain (PACKAGE, LOCALEDIR);
100         textdomain (PACKAGE);
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         switch (vars_to_check) {
113         case CHECK_CLIENTVERSION:
115                 asprintf(&send_buffer, "%s&1", req_password);
116                 fetch_data (server_address, server_port, send_buffer);
117                 if (value_list != NULL && strcmp(recv_buffer, value_list) != 0) {
118                         asprintf (&output_message, _("Wrong client version - running: %s, required: %s"), recv_buffer, value_list);
119                         return_code = STATE_WARNING;
120                 } else {
121                         asprintf (&output_message, recv_buffer);
122                         return_code = STATE_OK;
123                 }
124                 break;
126         case CHECK_CPULOAD:
128                 if (value_list==NULL)
129                         output_message = strdup (_("missing -l parameters"));
130                 else if (strtoularray(lvalue_list,value_list,",")==FALSE)
131                         output_message = strdup (_("wrong -l parameter."));
132                 else {
133                         /* -l parameters is present with only integers */
134                         return_code=STATE_OK;
135                         temp_string = strdup (_("CPU Load"));
136                         /* loop until one of the parameters is wrong or not present */
137                         while (lvalue_list[0+offset]> (unsigned long)0 &&
138                                                  lvalue_list[0+offset]<=(unsigned long)17280 && 
139                                                  lvalue_list[1+offset]> (unsigned long)0 &&
140                                                  lvalue_list[1+offset]<=(unsigned long)100 && 
141                                                  lvalue_list[2+offset]> (unsigned long)0 &&
142                                                  lvalue_list[2+offset]<=(unsigned long)100) {
144                                 /* Send request and retrieve data */
145                                 asprintf(&send_buffer,"%s&2&%lu",req_password,lvalue_list[0+offset]);
146                                 fetch_data (server_address, server_port, send_buffer);
148                                 utilization=strtoul(recv_buffer,NULL,10);
149                                 
150                                 /* Check if any of the request is in a warning or critical state */
151                                 if(utilization >= lvalue_list[2+offset])
152                                         return_code=STATE_CRITICAL;
153                                 else if(utilization >= lvalue_list[1+offset] && return_code<STATE_WARNING)
154                                         return_code=STATE_WARNING;
156                                 asprintf(&output_message,_(" %lu%% (%lu min average)"), utilization, lvalue_list[0+offset]);
157                                 asprintf(&temp_string,"%s%s",temp_string,output_message);
158                                 offset+=3;      /* move across the array */
159                         }
160                         if (strlen(temp_string)>10)  /* we had at least one loop */
161                                 output_message = strdup (temp_string);
162                         else
163                                 output_message = strdup (_("not enough values for -l parameters"));
164                 }       
165                 break;
167         case CHECK_UPTIME:
169                 asprintf(&send_buffer, "%s&3", req_password);
170                 fetch_data (server_address, server_port, send_buffer);
171                 uptime=strtoul(recv_buffer,NULL,10);
172                 updays = uptime / 86400;                        
173                 uphours = (uptime % 86400) / 3600;
174                 upminutes = ((uptime % 86400) % 3600) / 60;
175                 asprintf(&output_message,_("System Uptime : %u day(s) %u hour(s) %u minute(s)"),updays,uphours, upminutes);
176                 return_code=STATE_OK;
177                 break;
179         case CHECK_USEDDISKSPACE:
181                 if (value_list==NULL)
182                         output_message = strdup (_("missing -l parameters"));
183                 else if (strlen(value_list)==1)
184                         output_message = strdup (_("wrong -l argument"));
185                 else {
186                         asprintf(&send_buffer,"%s&4&%s", req_password, value_list);
187                         fetch_data (server_address, server_port, send_buffer);
188                         free_disk_space=atof(strtok(recv_buffer,"&"));
189                         total_disk_space=atof(strtok(NULL,"&"));
190                         percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100;
192                         if (free_disk_space>=0) {
193                                 asprintf(&temp_string,_("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"),
194                                                                  value_list, total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space,
195                                                                  free_disk_space / 1073741824, (free_disk_space / total_disk_space)*100); 
198                                 if(check_critical_value==TRUE && percent_used_space >= critical_value)
199                                         return_code=STATE_CRITICAL;
200                                 else if (check_warning_value==TRUE && percent_used_space >= warning_value)
201                                         return_code=STATE_WARNING;      
202                                 else
203                                         return_code=STATE_OK;   
205                                 output_message = strdup (temp_string);
206                         }
207                         else {
208                                 output_message = strdup (_("Free disk space : Invalid drive "));
209                                 return_code=STATE_UNKNOWN;
210                         }
211                 }
212                 break;
214         case CHECK_SERVICESTATE:
215         case CHECK_PROCSTATE:
217                 if (value_list==NULL)
218                         output_message = strdup (_("No service/process specified"));
219                 else {
220                         preparelist(value_list);                /* replace , between services with & to send the request */
221                         asprintf(&send_buffer,"%s&%u&%s&%s", req_password,(vars_to_check==CHECK_SERVICESTATE)?5:6,
222                                                          (show_all==TRUE)?_("ShowAll"):_("ShowFail"),value_list);
223                         fetch_data (server_address, server_port, send_buffer);
224                         return_code=atoi(strtok(recv_buffer,"&"));
225                         temp_string=strtok(NULL,"&");
226                         output_message = strdup (temp_string);
227                 }
228                 break;
230         case CHECK_MEMUSE:
231                 
232                 asprintf(&send_buffer,"%s&7", req_password);
233                 fetch_data (server_address, server_port, send_buffer);
234                 mem_commitLimit=atof(strtok(recv_buffer,"&"));
235                 mem_commitByte=atof(strtok(NULL,"&"));
236                 percent_used_space = (mem_commitByte / mem_commitLimit) * 100;
237                 asprintf(&output_message,_("Memory usage: total:%.2f Mb - used: %.2f Mb (%.0f%%) - free: %.2f Mb (%.0f%%)"), 
238                         mem_commitLimit / 1048576, mem_commitByte / 1048567, percent_used_space,  
239                         (mem_commitLimit - mem_commitByte) / 1048576, (mem_commitLimit - mem_commitByte) / mem_commitLimit * 100);
240         
241                 if(check_critical_value==TRUE && percent_used_space >= critical_value)
242                         return_code=STATE_CRITICAL;
243                 else if (check_warning_value==TRUE && percent_used_space >= warning_value)
244                         return_code=STATE_WARNING;      
245                 else
246                         return_code=STATE_OK;   
248                 break;
250         case CHECK_COUNTER:
252                 if (value_list==NULL)
253                         output_message = strdup (_("No counter specified"));
254                 else {
255                         preparelist(value_list);                /* replace , between services with & to send the request */
256                         asprintf(&send_buffer,"%s&8&%s", req_password,value_list);
257                         fetch_data (server_address, server_port, send_buffer);
258                         strtok(value_list,"&");                 /* burn the first parameters */
259                         description = strtok(NULL,"&");
260                         counter_value = atof(recv_buffer);
262                         if (description == NULL) 
263                                 asprintf(&output_message, "%.f", counter_value);
264                         else
265                                 asprintf(&output_message,"%s = %.f",  description, counter_value);
266         
267                         if (critical_value > warning_value) {        /* Normal thresholds */
268                                 if(check_critical_value==TRUE && counter_value >= critical_value)
269                                         return_code=STATE_CRITICAL;
270                                 else if (check_warning_value==TRUE && counter_value >= warning_value)
271                                         return_code=STATE_WARNING;      
272                                 else
273                                         return_code=STATE_OK;   
274                         } 
275                         else {                                       /* inverse thresholds */
276                                 if(check_critical_value==TRUE && counter_value <= critical_value)
277                                         return_code=STATE_CRITICAL;
278                                 else if (check_warning_value==TRUE && counter_value <= warning_value)
279                                         return_code=STATE_WARNING;      
280                                 else
281                                         return_code=STATE_OK;   
282                         }       
283                 }
284                 break;
286         case CHECK_FILEAGE:
288                 if (value_list==NULL)
289                         output_message = strdup (_("No counter specified"));
290                 else {
291                         preparelist(value_list);                /* replace , between services with & to send the request */
292                         asprintf(&send_buffer,"%s&9&%s", req_password,value_list);
293                         fetch_data (server_address, server_port, send_buffer);
294                         age_in_minutes = atoi(strtok(recv_buffer,"&"));
295                         description = strtok(NULL,"&");
296                         output_message = strdup (description);
297         
298                         if (critical_value > warning_value) {        /* Normal thresholds */
299                                 if(check_critical_value==TRUE && age_in_minutes >= critical_value)
300                                         return_code=STATE_CRITICAL;
301                                 else if (check_warning_value==TRUE && age_in_minutes >= warning_value)
302                                         return_code=STATE_WARNING;      
303                                 else
304                                         return_code=STATE_OK;   
305                         } 
306                         else {                                       /* inverse thresholds */
307                                 if(check_critical_value==TRUE && age_in_minutes <= critical_value)
308                                         return_code=STATE_CRITICAL;
309                                 else if (check_warning_value==TRUE && age_in_minutes <= warning_value)
310                                         return_code=STATE_WARNING;      
311                                 else
312                                         return_code=STATE_OK;   
313                         }
314                 }
315                 break;
317         case CHECK_NONE:
318         default:
319                 usage (_(""));
320                 break;
322         }
324         /* reset timeout */
325         alarm(0);
327         printf("%s\n",output_message);
329         return return_code;
336 \f
337 /* process command-line arguments */
338 int process_arguments(int argc, char **argv){
339         int c;
341         int option = 0;
342         static struct option longopts[] =
343         { 
344                 {"port",     required_argument,0,'p'},
345                 {"timeout",  required_argument,0,'t'},
346                 {"critical", required_argument,0,'c'},
347                 {"warning",  required_argument,0,'w'},
348                 {"variable", required_argument,0,'v'},
349                 {"hostname", required_argument,0,'H'},
350                 {"version",  no_argument,      0,'V'},
351                 {"help",     no_argument,      0,'h'},
352                 {0,0,0,0}
353         };
355         /* no options were supplied */
356         if(argc<2) return ERROR;
358         /* backwards compatibility */
359         if (! is_option(argv[1])) {
360                 server_address = strdup(argv[1]);
361                 argv[1]=argv[0];
362                 argv=&argv[1];
363                 argc--;
364         }
366   for (c=1;c<argc;c++) {
367     if(strcmp("-to",argv[c])==0)
368       strcpy(argv[c],"-t");
369     else if (strcmp("-wv",argv[c])==0)
370       strcpy(argv[c],"-w");
371     else if (strcmp("-cv",argv[c])==0)
372       strcpy(argv[c],"-c");
373         }
375         while (1){
376                 c = getopt_long(argc,argv,"+hVH:t:c:w:p:v:l:s:d:",longopts,&option);
378                 if (c==-1||c==EOF||c==1)
379                         break;
381                 switch (c)
382                         {
383                         case '?': /* print short usage statement if args not parsable */
384                                 printf("%s: Unknown argument: %s\n\n",progname,optarg);
385                                 print_usage();
386                                 exit(STATE_UNKNOWN);
387                         case 'h': /* help */
388                                 print_help();
389                                 exit(STATE_OK);
390                         case 'V': /* version */
391                                 print_revision(progname,"$Revision$");
392                                 exit(STATE_OK);
393                         case 'H': /* hostname */
394                                 if (server_address)     free(server_address);
395                                 server_address = optarg;
396                                 break;
397                         case 's': /* password */
398                                 req_password = optarg;
399                                 break;
400                         case 'p': /* port */
401                                 if (is_intnonneg(optarg))
402                                         server_port=atoi(optarg);
403                                 else
404                                         die(STATE_UNKNOWN,_("Server port an integer (seconds)\nType '%s -h' for additional help\n"),progname);
405                                 break;
406                         case 'v':
407                                 if(strlen(optarg)<4)
408                                         return ERROR;
409                                 if(!strcmp(optarg,"CLIENTVERSION"))
410                                         vars_to_check=CHECK_CLIENTVERSION;
411                                 else if(!strcmp(optarg,"CPULOAD"))
412                                         vars_to_check=CHECK_CPULOAD;
413                                 else if(!strcmp(optarg,"UPTIME"))
414                                         vars_to_check=CHECK_UPTIME;
415                                 else if(!strcmp(optarg,"USEDDISKSPACE"))
416                                         vars_to_check=CHECK_USEDDISKSPACE;
417                                 else if(!strcmp(optarg,"SERVICESTATE"))
418                                         vars_to_check=CHECK_SERVICESTATE;
419                                 else if(!strcmp(optarg,"PROCSTATE"))
420                                         vars_to_check=CHECK_PROCSTATE;
421                                 else if(!strcmp(optarg,"MEMUSE"))
422                                         vars_to_check=CHECK_MEMUSE;
423                                 else if(!strcmp(optarg,"COUNTER"))
424                                         vars_to_check=CHECK_COUNTER;
425                                 else if(!strcmp(optarg,"FILEAGE"))
426                                         vars_to_check=CHECK_FILEAGE;
427                                 else
428                                         return ERROR;
429                                 break;
430                         case 'l': /* value list */
431                                 value_list = optarg;
432                                 break;
433                         case 'w': /* warning threshold */
434                                 warning_value=strtoul(optarg,NULL,10);
435                                 check_warning_value=TRUE;
436                                 break;
437                         case 'c': /* critical threshold */
438                                 critical_value=strtoul(optarg,NULL,10);
439                                 check_critical_value=TRUE;
440                                 break;
441                         case 'd': /* Display select for services */
442                                 if (!strcmp(optarg,"SHOWALL"))
443                                         show_all = TRUE;
444                                 break;
445                         case 't': /* timeout */
446                                 socket_timeout=atoi(optarg);
447                                 if(socket_timeout<=0)
448                                         return ERROR;
449                         }
451         }
453         if (vars_to_check==CHECK_NONE)
454                 return ERROR;
456         if (req_password == NULL)
457                 req_password = strdup (_("None"));
459         return OK;
466 \f
467 void fetch_data (const char *address, int port, const char *sendb) {
468         int result;
470         result=process_tcp_request(address, port, sendb, recv_buffer,sizeof(recv_buffer));
472         if(result!=STATE_OK)
473                 die (result, "could not fetch information from server\n");
474                 
475         if (!strncmp(recv_buffer,"ERROR",5))
476                 die (STATE_UNKNOWN, "NSClient - %s\n",recv_buffer);
479 int strtoularray(unsigned long *array, char *string, const char *delim) {
480         /* split a <delim> delimited string into a long array */
481         int idx=0;
482         char *t1;
484         for (idx=0;idx<MAX_VALUE_LIST;idx++)
485                 array[idx]=0;
486         
487         idx=0;
488         for(t1 = strtok(string,delim);t1 != NULL; t1 = strtok(NULL, delim)) {
489                 if (is_numeric(t1) && idx<MAX_VALUE_LIST) {
490                         array[idx]=strtoul(t1,NULL,10);
491                         idx++;
492                 } else  
493                         return FALSE;
494         }               
495         return TRUE;
498 void preparelist(char *string) {
499         /* Replace all , with & which is the delimiter for the request */
500         int i;
502         for (i = 0; (size_t)i < strlen(string); i++)
503                 if (string[i] == ',') {
504                         string[i]='&';
505                 }
512 \f
513 void print_help(void)
515         print_revision(progname,"$Revision$");
516         printf (_("\
517 Copyright (c) 2000 Yves Rubin (rubiyz@yahoo.com)\n\n\
518 This plugin collects data from the NSClient service running on a\n\
519 Windows NT/2000/XP server.\n\n"));
520         print_usage();
521   printf (_("\nOptions:\n\
522 -H, --hostname=HOST\n\
523   Name of the host to check\n\
524 -p, --port=INTEGER\n\
525   Optional port number (default: %d)\n\
526 -s <password>\n\
527   Password needed for the request\n\
528 -w, --warning=INTEGER\n\
529   Threshold which will result in a warning status\n\
530 -c, --critical=INTEGER\n\
531   Threshold which will result in a critical status\n\
532 -t, --timeout=INTEGER\n\
533   Seconds before connection attempt times out (default: %d)\n\
534 -h, --help\n\
535   Print this help screen\n\
536 -V, --version\n\
537   Print version information\n"),
538                 PORT, DEFAULT_SOCKET_TIMEOUT);
539   printf (_("\
540 -v, --variable=STRING\n\
541   Variable to check.  Valid variables are:\n"));
542   printf (_("\
543    CLIENTVERSION = Get the NSClient version\n\
544      If -l <version> is specified, will return warning if versions differ.\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"));
589 void print_usage(void)
591         printf(_("\
592 Usage: %s -H host -v variable [-p port] [-w warning] [-c critical]\n\
593   [-l params] [-d SHOWALL] [-t timeout]\n"), progname);
594         printf (_(UT_HLP_VRS), progname, progname);