Code

Added in check_nt for bug 646516
[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  * License Information:
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *
27  *****************************************************************************/
29 //#include "stdlib.h"
30 #include "config.h"
31 #include "common.h"
32 #include "netutils.h"
33 #include "utils.h"
35 #define CHECK_NONE      0
36 #define CHECK_CLIENTVERSION     1
37 #define CHECK_CPULOAD   2
38 #define CHECK_UPTIME    3
39 #define CHECK_USEDDISKSPACE     4
40 #define CHECK_SERVICESTATE      5
41 #define CHECK_PROCSTATE 6
42 #define CHECK_MEMUSE    7
43 #define CHECK_COUNTER   8
44 #define CHECK_FILEAGE   9
45 #define MAX_VALUE_LIST 30
47 #define PORT    1248    
49 char *server_address=NULL;
50 char *volume_name=NULL;
51 int server_port=PORT;
52 char *value_list=NULL;
53 char *req_password=NULL;
54 unsigned long lvalue_list[MAX_VALUE_LIST];
55 unsigned long warning_value=0L;
56 unsigned long critical_value=0L;
57 int check_value_list=FALSE;
58 int check_warning_value=FALSE;
59 int check_critical_value=FALSE;
60 int vars_to_check=CHECK_NONE;
61 int show_all=FALSE;
63 const char *progname = "check_nt";
65 int process_arguments(int, char **);
66 void print_usage(void);
67 void print_help(void);
68 void preparelist(char *string);
70 int main(int argc, char **argv){
71         int result;
72         int return_code;
73         char *send_buffer=NULL;
74         char recv_buffer[MAX_INPUT_BUFFER];
75         char *output_message=NULL;
76         char *temp_buffer=NULL;
77         char *temp_string=NULL;
78         char *sep_string=NULL;
79         char *description=NULL;
81         double total_disk_space=0;
82         double free_disk_space=0;
83         double percent_used_space=0;
84         double mem_commitLimit=0;
85         double mem_commitByte=0;
86         unsigned long current_connections=0;
87         unsigned long utilization;
88         unsigned long uptime;
89         unsigned long cache_hits;
90         unsigned long cache_buffers;
91         unsigned long lru_time;
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;
98         asprintf(&req_password,"None");
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         if (vars_to_check==CHECK_CLIENTVERSION) {
110                         
111                 asprintf(&send_buffer,strcat(req_password,"&1"));
112                 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
113                 if(result!=STATE_OK)
114                         return result;
115                 asprintf(&output_message,recv_buffer);
116                 return_code=STATE_OK;
117         }
118         else if(vars_to_check==CHECK_CPULOAD){
120                 if (check_value_list==TRUE) {                                                                                                                                                   
121                         if (strtolarray(&lvalue_list,value_list,",")==TRUE) {
122                                 // -l parameters is present with only integers
123                                 return_code=STATE_OK;
124                                 asprintf(&temp_string,"CPU Load");
125                                 while (lvalue_list[0+offset]>0 && lvalue_list[0+offset]<=17280 && 
126                                                         lvalue_list[1+offset]>=0 && lvalue_list[1+offset]<=100 && 
127                                                         lvalue_list[2+offset]>=0 && lvalue_list[2+offset]<=100) {
128                                         // loop until one of the parameters is wrong or not present
130                                         // Send request and retrieve data
131                                         asprintf(&send_buffer,"%s&2&%lu",req_password,lvalue_list[0+offset]);
132                                         result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
133                                         if(result!=STATE_OK)
134                                                 return result;
136                                         if (!strncmp(recv_buffer,"ERROR",5)) {
137                                                 printf("NSClient - %s\n",recv_buffer);
138                                                 exit(STATE_UNKNOWN);
139                                         }
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 min. %lu%)",lvalue_list[0+offset], utilization);
150                                         asprintf(&temp_string,"%s%s",temp_string,output_message);
151                                         offset+=3;      //move accross the array 
152                                 }               
153                                 if (strlen(temp_string)>10) {
154                                         // we had at least on loop
155                                         asprintf(&output_message,"%s",temp_string);
156                                 }       
157                                 else
158                                         asprintf(&output_message,"%s","not enough values for -l parameters");
159                                         
160                         } else 
161                                 asprintf(&output_message,"wrong -l parameter.");
163                 } else
164                         asprintf(&output_message,"missing -l parameters");
165         }
167         else if(vars_to_check==CHECK_UPTIME){
169                 asprintf(&send_buffer,strcat(req_password,"&3"));
170                 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
171                 if(result!=STATE_OK)
172                         return result;
174                 if (!strncmp(recv_buffer,"ERROR",5)) {
175                         printf("NSClient - %s\n",recv_buffer);
176                         exit(STATE_UNKNOWN);
177                 }
179                 uptime=strtoul(recv_buffer,NULL,10);
180                 updays = uptime / 86400;                        
181                 uphours = (uptime % 86400) / 3600;
182                 upminutes = ((uptime % 86400) % 3600) / 60;
183                 asprintf(&output_message,"System Uptime : %u day(s) %u hour(s) %u minute(s)",updays,uphours, upminutes);
184                 return_code=STATE_OK;
185         }
187         else if(vars_to_check==CHECK_USEDDISKSPACE){
189                 return_code=STATE_UNKNOWN;      
190                 if (check_value_list==TRUE) {
191                         if (strlen(value_list)==1) {
192                                 asprintf(&send_buffer,"%s&4&%s", req_password, value_list);
193                                 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
194                                 if(result!=STATE_OK)
195                                         return result;
196                 
197                                 if (!strncmp(recv_buffer,"ERROR",5)) {
198                                         printf("NSClient - %s\n",recv_buffer);
199                                         exit(STATE_UNKNOWN);
200                                 }
202                                 free_disk_space=atof(strtok(recv_buffer,"&"));
203                                 total_disk_space=atof(strtok(NULL,"&"));
204                                 percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100;
206                                 if (free_disk_space>=0) {
207                                         asprintf(&temp_string,"%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)",
208                                                         value_list, total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space,
209                                                          free_disk_space / 1073741824, (free_disk_space / total_disk_space)*100); 
212                                         if(check_critical_value==TRUE && percent_used_space >= critical_value)
213                                                 return_code=STATE_CRITICAL;
214                                         else if (check_warning_value==TRUE && percent_used_space >= warning_value)
215                                                 return_code=STATE_WARNING;      
216                                         else
217                                                 return_code=STATE_OK;   
219                                         asprintf(&output_message,"%s",temp_string);
221                                 }
222                                 else {
223                                         asprintf(&output_message,"Free disk space : Invalid drive ");
224                                         return_code=STATE_UNKNOWN;
225                                 }               
226                         }
227                         else 
228                                 asprintf(&output_message,"wrong -l argument");
229                 } else 
230                         asprintf(&output_message,"missing -l parameters");
231                         
232         }
234         else if(vars_to_check==CHECK_SERVICESTATE || vars_to_check==CHECK_PROCSTATE){
236                 if (check_value_list==TRUE) {
237                         preparelist(value_list);                // replace , between services with & to send the request
238                         asprintf(&send_buffer,"%s&%u&%s&%s", req_password,(vars_to_check==CHECK_SERVICESTATE)?5:6,
239                                 (show_all==TRUE)?"ShowAll":"ShowFail",value_list);
240                         result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
241                         if(result!=STATE_OK)
242                                 return result;
243         
244                         if (!strncmp(recv_buffer,"ERROR",5)) {
245                                 printf("NSClient - %s\n",recv_buffer);
246                                 exit(STATE_UNKNOWN);
247                         }
248                         return_code=atoi(strtok(recv_buffer,"&"));
249                         temp_string=strtok(NULL,"&");
250                         asprintf(&output_message, "%s",temp_string);
251                 }
252                 else 
253                         asprintf(&output_message,"No service/process specified");
254         }
256         else if(vars_to_check==CHECK_MEMUSE) {
257                 
258                 asprintf(&send_buffer,"%s&7", req_password);
259                 result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
260                 if (result!=STATE_OK)
261                         return result;
263                 if (!strncmp(recv_buffer,"ERROR",5)) {
264                         printf("NSClient - %s\n",recv_buffer);
265                         exit(STATE_UNKNOWN);
266                 }
268                 mem_commitLimit=atof(strtok(recv_buffer,"&"));
269                 mem_commitByte=atof(strtok(NULL,"&"));
270                 percent_used_space = (mem_commitByte / mem_commitLimit) * 100;
271                 asprintf(&output_message,"Memory usage: total:%.2f Mb - used: %.2f Mb (%.0f%%) - free: %.2f Mb (%.0f%%)", 
272                         mem_commitLimit / 1048576, mem_commitByte / 1048567, percent_used_space,  
273                         (mem_commitLimit - mem_commitByte) / 1048576, (mem_commitLimit - mem_commitByte) / mem_commitLimit * 100);
274         
275                 if(check_critical_value==TRUE && percent_used_space >= critical_value)
276                         return_code=STATE_CRITICAL;
277                 else if (check_warning_value==TRUE && percent_used_space >= warning_value)
278                         return_code=STATE_WARNING;      
279                 else
280                         return_code=STATE_OK;   
281                 
282         }
284         else if(vars_to_check==CHECK_COUNTER) {
286                 if (check_value_list==TRUE) {                                                                                                                                                   
287                         preparelist(value_list);                // replace , between services with & to send the request
288                         asprintf(&send_buffer,"%s&8&%s", req_password,value_list);
289                         result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
290                         if (result!=STATE_OK)
291                                 return result;
292         
293                         if (!strncmp(recv_buffer,"ERROR",5)) {
294                                 printf("NSClient - %s\n",recv_buffer);
295                                 exit(STATE_UNKNOWN);
296                         }
298                         strtok(value_list,"&");                 // burn the first parameters
299                         description = strtok(NULL,"&");
300                         counter_value = atof(recv_buffer);
301                         if (description == NULL) 
302                                 asprintf(&output_message, "%.f", counter_value);
303                         else
304                                 asprintf(&output_message, description, counter_value);
305         
306                         if (critical_value > warning_value) {
307                                 // Normal thresholds            
308                                 if(check_critical_value==TRUE && counter_value >= critical_value)
309                                         return_code=STATE_CRITICAL;
310                                 else if (check_warning_value==TRUE && counter_value >= warning_value)
311                                         return_code=STATE_WARNING;      
312                                 else
313                                         return_code=STATE_OK;   
314                         } 
315                         else {
316                                 // inverse thresholds           
317                                 if(check_critical_value==TRUE && counter_value <= critical_value)
318                                         return_code=STATE_CRITICAL;
319                                 else if (check_warning_value==TRUE && counter_value <= warning_value)
320                                         return_code=STATE_WARNING;      
321                                 else
322                                         return_code=STATE_OK;   
323                         }       
324                 
325                 }
326                 else {
327                         asprintf(&output_message,"No counter specified");
328                         result=STATE_UNKNOWN;
329                 }
330         }
331         else if(vars_to_check==CHECK_FILEAGE) {
333                 if (check_value_list==TRUE) {                                                                                                                                                   
334                         preparelist(value_list);                // replace , between services with & to send the request
335                         asprintf(&send_buffer,"%s&9&%s", req_password,value_list);
336                         result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
337                         if (result!=STATE_OK)
338                                 return result;
339         
340                         if (!strncmp(recv_buffer,"ERROR",5)) {
341                                 printf("NSClient - %s\n",recv_buffer);
342                                 exit(STATE_UNKNOWN);
343                         }
345                         age_in_minutes = atoi(strtok(recv_buffer,"&"));
346                         description = strtok(NULL,"&");
347                         asprintf(&output_message, description);
348         
349                         if (critical_value > warning_value) {
350                                 // Normal thresholds            
351                                 if(check_critical_value==TRUE && age_in_minutes >= critical_value)
352                                         return_code=STATE_CRITICAL;
353                                 else if (check_warning_value==TRUE && age_in_minutes >= warning_value)
354                                         return_code=STATE_WARNING;      
355                                 else
356                                         return_code=STATE_OK;   
357                         } 
358                         else {
359                                 // inverse thresholds           
360                                 if(check_critical_value==TRUE && age_in_minutes <= critical_value)
361                                         return_code=STATE_CRITICAL;
362                                 else if (check_warning_value==TRUE && age_in_minutes <= warning_value)
363                                         return_code=STATE_WARNING;      
364                                 else
365                                         return_code=STATE_OK;   
366                         }       
367                 
368                 }
369                 else {
370                         asprintf(&output_message,"No file specified");
371                         result=STATE_UNKNOWN;
372                 }
373         }
375         /* reset timeout */
376         alarm(0);
378         printf("%s\n",output_message);
380         return return_code;
384 /* process command-line arguments */
385 int process_arguments(int argc, char **argv){
386         int c;
388 #ifdef HAVE_GETOPT_H
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         };
402 #endif
404         /* no options were supplied */
405         if(argc<2) return ERROR;
407         /* backwards compatibility */
408         if (! is_option(argv[1])) {
409                 server_address=argv[1];
410                 argv[1]=argv[0];
411                 argv=&argv[1];
412                 argc--;
413         }
415   for (c=1;c<argc;c++) {
416     if(strcmp("-to",argv[c])==0)
417       strcpy(argv[c],"-t");
418     else if (strcmp("-wv",argv[c])==0)
419       strcpy(argv[c],"-w");
420     else if (strcmp("-cv",argv[c])==0)
421       strcpy(argv[c],"-c");
422         }
424         while (1){
425 #ifdef HAVE_GETOPT_H
426                 c = getopt_long(argc,argv,"+hVH:t:c:w:p:v:l:s:d:",long_options,&option_index);
427 #else
428                 c = getopt(argc,argv,"+hVH:t:c:w:p:v:l:s:d:");
429 #endif
431                 if (c==-1||c==EOF||c==1)
432                         break;
434                 switch (c)
435                         {
436                         case '?': /* print short usage statement if args not parsable */
437                                 printf("%s: Unknown argument: %s\n\n",progname,optarg);
438                                 print_usage();
439                                 exit(STATE_UNKNOWN);
440                         case 'h': /* help */
441                                 print_help();
442                                 exit(STATE_OK);
443                         case 'V': /* version */
444                                 print_revision(progname,"$Revision$");
445                                 exit(STATE_OK);
446                         case 'H': /* hostname */
447                                 server_address=optarg;
448                                 break;
449                         case 's': /* password */
450                                 asprintf(&req_password,optarg);
451                                 break;
452                         case 'p': /* port */
453                                 if (is_intnonneg(optarg))
454                                         server_port=atoi(optarg);
455                                 else
456                                         terminate(STATE_UNKNOWN,"Server port an integer (seconds)\nType '%s -h' for additional help\n",progname);
457                                 break;
458                         case 'v':
459                                 if(strlen(optarg)<4)
460                                         return ERROR;
461                                 if(!strcmp(optarg,"CLIENTVERSION"))
462                                         vars_to_check=CHECK_CLIENTVERSION;
463                                 else if(!strcmp(optarg,"CPULOAD"))
464                                         vars_to_check=CHECK_CPULOAD;
465                                 else if(!strcmp(optarg,"UPTIME"))
466                                         vars_to_check=CHECK_UPTIME;
467                                 else if(!strcmp(optarg,"USEDDISKSPACE"))
468                                         vars_to_check=CHECK_USEDDISKSPACE;
469                                 else if(!strcmp(optarg,"SERVICESTATE"))
470                                         vars_to_check=CHECK_SERVICESTATE;
471                                 else if(!strcmp(optarg,"PROCSTATE"))
472                                         vars_to_check=CHECK_PROCSTATE;
473                                 else if(!strcmp(optarg,"MEMUSE"))
474                                         vars_to_check=CHECK_MEMUSE;
475                                 else if(!strcmp(optarg,"COUNTER"))
476                                         vars_to_check=CHECK_COUNTER;
477                                 else if(!strcmp(optarg,"FILEAGE"))
478                                         vars_to_check=CHECK_FILEAGE;
479                                 else
480                                         return ERROR;
481                                 break;
482                         case 'l': /* value list */
483                                 asprintf(&value_list,optarg);
484                                 check_value_list=TRUE;
485                                 break;
486                         case 'w': /* warning threshold */
487                                 warning_value=strtoul(optarg,NULL,10);
488                                 check_warning_value=TRUE;
489                                 break;
490                         case 'c': /* critical threshold */
491                                 critical_value=strtoul(optarg,NULL,10);
492                                 check_critical_value=TRUE;
493                                 break;
494                         case 'd': /* Display select for services */
495                                 if (!strcmp(optarg,"SHOWALL"))
496                                         show_all = TRUE;
497                                 break;
498                         case 't': /* timeout */
499                                 socket_timeout=atoi(optarg);
500                                 if(socket_timeout<=0)
501                                         return ERROR;
502                         }
504         }
506         return OK;
510 void print_usage(void)
512         printf("Usage: %s -H host [-p port] [-v variable] [-w warning] [-c critical] [-l params] [-d SHOWALL] [-t timeout]\n",progname);
516 void print_help(void)
518         print_revision(progname,"$Revision$");
519         printf
520                 ("Copyright (c) 2000 Yves Rubin (rubiyz@yahoo.com)\n\n"
521                  "This plugin attempts to contact the NSClient service running on a Windows NT/2000/XP server to\n"
522                  "gather the requested system information.\n\n");
523         print_usage();
524   printf
525                 ("\nOptions:\n"
526                  "-H, --hostname=HOST\n"
527                  "   Name of the host to check\n"
528                  "-p, --port=INTEGER\n"
529                  "   Optional port number (default: %d)\n"
530                  "-s <password>\n"
531                  "   Password needed for the request\n"
532                  "-v, --variable=STRING\n"
533                  "   Variable to check.  Valid variables are:\n"
534                  "      CLIENTVERSION = Get the NSClient version\n"
535                  "      CPULOAD = Average CPU load on last x minutes. Request a -l parameter with the following syntax:\n"
536                  "        -l <minutes range>,<warning threshold>,<critical threshold>. <minute range> should be less than 24*60.\n"
537                  "        Thresholds are percentage and up to 10 requests can be done in one shot. ie: -l 60,90,95,120,90,95\n"
538                  "      UPTIME = Get the uptime of the machine. No specific parameters. No warning or critical threshold\n"
539                  "      USEDDISKSPACE = Size and percentage of disk use. Request a -l parameter containing the drive letter only.\n"
540                  "                      Warning and critical thresholds can be specified with -w and -c.\n"
541                  "      MEMUSE = Memory use. Warning and critical thresholds can be specified with -w and -c.\n" 
542                  "      SERVICESTATE = Check the state of one or several services. Request a -l parameters with the following syntax:\n"
543                  "        -l <service1>,<service2>,<service3>,... You can specify -d SHOWALL in case you want to see working services\n"
544                  "        in the returned string.\n"
545                  "      PROCSTATE = Check if one or several process are running. Same syntax as SERVICESTATE.\n"
546                  "      COUNTER = Check any performance counter of Windows NT/2000. Request a -l parameters with the following syntax:\n"
547                  "        -l \"\\\\<performance object>\\\\counter\",\"<description>\"  The <description> parameter is optional and \n"
548                  "        is given to a printf output command which require a float parameters. Some examples:\n"
549                  "          \"Paging file usage is %%.2f %%%%\" or \"%%.f %%%% paging file used.\"\n"
550                  " -w, --warning=INTEGER\n"
551                  "   Threshold which will result in a warning status\n"
552                  " -c, --critical=INTEGER\n"
553                  "   Threshold which will result in a critical status\n"
554                  " -t, --timeout=INTEGER\n"
555                  "   Seconds before connection attempt times out (default: %d)\n"
556                  "-h, --help\n"
557                  "   Print this help screen\n"
558                  "-V, --version\n"
559                  "   Print version information\n\n"
560                  "Notes:\n"
561                  " - The NSClient service should be running on the server to get any information.\n"
562                  " - Critical thresholds should be lower than warning thresholds\n", PORT, DEFAULT_SOCKET_TIMEOUT);
565 int strtolarray(unsigned long *array, char *string, char *delim) {
566         // split a <delim> delimited string into a long array
567         int idx=0;
568         char *t1;
570         for (idx=0;idx<MAX_VALUE_LIST;idx++)
571                 array[idx]=-1;
572         
573         idx=0;
574         for(t1 = strtok(string,delim);t1 != NULL; t1 = strtok(NULL, delim)) {
575                 if (is_numeric(t1) && idx<MAX_VALUE_LIST) {
576                         array[idx]=strtoul(t1,NULL,10);
577                         idx++;
578                 } else  
579                         return FALSE;
580         }               
581         return TRUE;
584 void preparelist(char *string) {
585         // Replace all , with & which is the delimiter for the request
586         int i;
588         for (i = 0; i < strlen(string); i++)
589                 if (string[i] == ',') {
590                         string[i]='&';
591                 }