Code

Changed // to /* */ comments as some compilers do not like them
[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 "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;
74         char *send_buffer=NULL;
75         char recv_buffer[MAX_INPUT_BUFFER];
76         char *output_message=NULL;
77         char *temp_buffer=NULL;
78         char *temp_string=NULL;
79         char *sep_string=NULL;
80         char *description=NULL;
82         double total_disk_space=0;
83         double free_disk_space=0;
84         double percent_used_space=0;
85         double mem_commitLimit=0;
86         double mem_commitByte=0;
87         unsigned long current_connections=0;
88         unsigned long utilization;
89         unsigned long uptime;
90         unsigned long cache_hits;
91         unsigned long cache_buffers;
92         unsigned long lru_time;
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;
99         asprintf(&req_password,"None");
101         if(process_arguments(argc,argv)==ERROR)
102                 usage("Could not parse arguments\n");
104         /* initialize alarm signal handling */
105         signal(SIGALRM,socket_timeout_alarm_handler);
107         /* set socket timeout */
108         alarm(socket_timeout);
110         if (vars_to_check==CHECK_CLIENTVERSION) {
111                         
112                 asprintf(&send_buffer,strcat(req_password,"&1"));
113                 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
114                 if(result!=STATE_OK)
115                         return result;
116                 asprintf(&output_message,recv_buffer);
117                 return_code=STATE_OK;
118         }
119         else if(vars_to_check==CHECK_CPULOAD){
121                 if (check_value_list==TRUE) {                                                                                                                                                   
122                         if (strtolarray(&lvalue_list,value_list,",")==TRUE) {
123                                 /* -l parameters is present with only integers */
124                                 return_code=STATE_OK;
125                                 asprintf(&temp_string,"CPU Load");
126                                 while (lvalue_list[0+offset]>0 && lvalue_list[0+offset]<=17280 && 
127                                                         lvalue_list[1+offset]>=0 && lvalue_list[1+offset]<=100 && 
128                                                         lvalue_list[2+offset]>=0 && lvalue_list[2+offset]<=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");
160                                         
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;
197                 
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");
232                         
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;
244         
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) {
258                 
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);
275         
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;   
282                 
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;
293         
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);
306         
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                         }       
325                 
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;
340         
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);
349         
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                         }       
368                 
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;
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;
508 void print_usage(void)
510         printf("Usage: %s -H host -v variable [-p port] [-w warning] [-c critical] [-l params] [-d SHOWALL] [-t timeout]\n",progname);
514 void print_help(void)
516         print_revision(progname,"$Revision$");
517         printf
518                 ("Copyright (c) 2000 Yves Rubin (rubiyz@yahoo.com)\n\n"
519                  "This plugin attempts to contact the NSClient service running on a Windows NT/2000/XP server to\n"
520                  "gather the requested system information.\n\n");
521         print_usage();
522   printf
523                 ("\nOptions:\n"
524                  "-H, --hostname=HOST\n"
525                  "   Name of the host to check\n"
526                  "-p, --port=INTEGER\n"
527                  "   Optional port number (default: %d)\n"
528                  "-s <password>\n"
529                  "   Password needed for the request\n"
530                  "-v, --variable=STRING\n"
531                  "   Variable to check.  Valid variables are:\n"
532                  "      CLIENTVERSION = Get the NSClient version\n"
533                  "      CPULOAD = Average CPU load on last x minutes. Request a -l parameter with the following syntax:\n"
534                  "        -l <minutes range>,<warning threshold>,<critical threshold>. <minute range> should be less than 24*60.\n"
535                  "        Thresholds are percentage and up to 10 requests can be done in one shot. ie: -l 60,90,95,120,90,95\n"
536                  "      UPTIME = Get the uptime of the machine. No specific parameters. No warning or critical threshold\n"
537                  "      USEDDISKSPACE = Size and percentage of disk use. Request a -l parameter containing the drive letter only.\n"
538                  "                      Warning and critical thresholds can be specified with -w and -c.\n"
539                  "      MEMUSE = Memory use. Warning and critical thresholds can be specified with -w and -c.\n" 
540                  "      SERVICESTATE = Check the state of one or several services. Request a -l parameters with the following syntax:\n"
541                  "        -l <service1>,<service2>,<service3>,... You can specify -d SHOWALL in case you want to see working services\n"
542                  "        in the returned string.\n"
543                  "      PROCSTATE = Check if one or several process are running. Same syntax as SERVICESTATE.\n"
544                  "      COUNTER = Check any performance counter of Windows NT/2000. Request a -l parameters with the following syntax:\n"
545                  "        -l \"\\\\<performance object>\\\\counter\",\"<description>\"  The <description> parameter is optional and \n"
546                  "        is given to a printf output command which require a float parameters. Some examples:\n"
547                  "          \"Paging file usage is %%.2f %%%%\" or \"%%.f %%%% paging file used.\"\n"
548                  " -w, --warning=INTEGER\n"
549                  "   Threshold which will result in a warning status\n"
550                  " -c, --critical=INTEGER\n"
551                  "   Threshold which will result in a critical status\n"
552                  " -t, --timeout=INTEGER\n"
553                  "   Seconds before connection attempt times out (default: %d)\n"
554                  "-h, --help\n"
555                  "   Print this help screen\n"
556                  "-V, --version\n"
557                  "   Print version information\n\n"
558                  "Notes:\n"
559                  " - The NSClient service should be running on the server to get any information\n"
560                  "   (http://nsclient.ready2run.nl).\n"
561                  " - Critical thresholds should be lower than warning thresholds\n", PORT, DEFAULT_SOCKET_TIMEOUT);
564 int strtolarray(unsigned long *array, char *string, char *delim) {
565         /* split a <delim> delimited string into a long array */
566         int idx=0;
567         char *t1;
569         for (idx=0;idx<MAX_VALUE_LIST;idx++)
570                 array[idx]=-1;
571         
572         idx=0;
573         for(t1 = strtok(string,delim);t1 != NULL; t1 = strtok(NULL, delim)) {
574                 if (is_numeric(t1) && idx<MAX_VALUE_LIST) {
575                         array[idx]=strtoul(t1,NULL,10);
576                         idx++;
577                 } else  
578                         return FALSE;
579         }               
580         return TRUE;
583 void preparelist(char *string) {
584         /* Replace all , with & which is the delimiter for the request */
585         int i;
587         for (i = 0; i < strlen(string); i++)
588                 if (string[i] == ',') {
589                         string[i]='&';
590                 }