summaryrefslogtreecommitdiffstats
path: root/plugins/check_nt.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_nt.c')
-rw-r--r--plugins/check_nt.c548
1 files changed, 548 insertions, 0 deletions
diff --git a/plugins/check_nt.c b/plugins/check_nt.c
new file mode 100644
index 0000000..ed0dc98
--- /dev/null
+++ b/plugins/check_nt.c
@@ -0,0 +1,548 @@
1/******************************************************************************
2 *
3 * This file is part of the Nagios Plugins.
4 *
5 * Copyright (c) 2000 Yves Rubin <rubiyz@yahoo.com>
6 *
7 * The Nagios Plugins are free software; you can redistribute them
8 * and/or modify them under the terms of the GNU General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * $Id$
22 *
23 *****************************************************************************/
24
25#define PROGRAM check_nt
26#define DESCRIPTION "Windows NT plugin for Nagios"
27#define AUTHOR "Yves Rubin"
28#define EMAIL "rubiyz@yahoo.com"
29#define COPYRIGHTDATE "2000"
30
31//#include "stdlib.h"
32#include "config.h"
33#include "common.h"
34#include "netutils.h"
35#include "utils.h"
36
37#define CHECK_NONE 0
38#define CHECK_CLIENTVERSION 1
39#define CHECK_CPULOAD 2
40#define CHECK_UPTIME 3
41#define CHECK_USEDDISKSPACE 4
42#define CHECK_SERVICESTATE 5
43#define CHECK_PROCSTATE 6
44#define CHECK_MEMUSE 7
45#define CHECK_COUNTER 8
46#define MAX_VALUE_LIST 30
47
48#define PORT 1248
49
50char *server_address=NULL;
51char *volume_name=NULL;
52int server_port=PORT;
53char *value_list=NULL;
54char *req_password=NULL;
55unsigned long lvalue_list[MAX_VALUE_LIST];
56unsigned long warning_value=0L;
57unsigned long critical_value=0L;
58int check_value_list=FALSE;
59int check_warning_value=FALSE;
60int check_critical_value=FALSE;
61int vars_to_check=CHECK_NONE;
62int show_all=FALSE;
63
64#define PROGNAME "check_nt"
65
66int process_arguments(int, char **);
67void print_usage(void);
68void print_help(void);
69void preparelist(char *string);
70
71int 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;
81
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 double counter_value;
94 int offset=0;
95 int updays=0;
96 int uphours=0;
97 int upminutes=0;
98 req_password=strscpy(req_password,"None");
99
100 if(process_arguments(argc,argv)==ERROR)
101 usage("Could not parse arguments\n");
102
103 /* initialize alarm signal handling */
104 signal(SIGALRM,socket_timeout_alarm_handler);
105
106 /* set socket timeout */
107 alarm(socket_timeout);
108
109 if (vars_to_check==CHECK_CLIENTVERSION) {
110
111 send_buffer = strscpy(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 output_message = strscpy(output_message,recv_buffer);
116 return_code=STATE_OK;
117 }
118 else if(vars_to_check==CHECK_CPULOAD){
119
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 temp_string = strscpy(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
129
130 // Send request and retrieve data
131 send_buffer = ssprintf(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;
135
136 if (!strncmp(recv_buffer,"ERROR",5)) {
137 printf("NSClient - %s\n",recv_buffer);
138 exit(STATE_UNKNOWN);
139 }
140
141 utilization=strtoul(recv_buffer,NULL,10);
142
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;
148
149 output_message = ssprintf(output_message," (%lu min. %lu%)",lvalue_list[0+offset], utilization);
150 temp_string = strscat(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 output_message = ssprintf(output_message,"%s",temp_string);
156 }
157 else
158 output_message = strscpy(output_message,"not enough values for -l parameters");
159
160 } else
161 output_message = strscpy(output_message,"wrong -l parameter.");
162
163 } else
164 output_message = strscpy(output_message,"missing -l parameters");
165 }
166
167 else if(vars_to_check==CHECK_UPTIME){
168
169 send_buffer = strscpy(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;
173
174 if (!strncmp(recv_buffer,"ERROR",5)) {
175 printf("NSClient - %s\n",recv_buffer);
176 exit(STATE_UNKNOWN);
177 }
178
179 uptime=strtoul(recv_buffer,NULL,10);
180 updays = uptime / 86400;
181 uphours = (uptime % 86400) / 3600;
182 upminutes = ((uptime % 86400) % 3600) / 60;
183 output_message = ssprintf(output_message,"System Uptime : %u day(s) %u hour(s) %u minute(s)",updays,uphours, upminutes);
184 return_code=STATE_OK;
185 }
186
187 else if(vars_to_check==CHECK_USEDDISKSPACE){
188
189 return_code=STATE_UNKNOWN;
190 if (check_value_list==TRUE) {
191 if (strlen(value_list)==1) {
192 send_buffer = ssprintf(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 }
201
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;
205
206 if (free_disk_space>=0) {
207 temp_string = ssprintf(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);
210
211
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;
218
219 output_message = ssprintf(output_message,"%s",temp_string);
220
221 }
222 else {
223 output_message = ssprintf(output_message,"Free disk space : Invalid drive ");
224 return_code=STATE_UNKNOWN;
225 }
226 }
227 else
228 output_message = strscpy(output_message,"wrong -l argument");
229 } else
230 output_message = strscpy(output_message,"missing -l parameters");
231
232 }
233
234 else if(vars_to_check==CHECK_SERVICESTATE || vars_to_check==CHECK_PROCSTATE){
235
236 if (check_value_list==TRUE) {
237 preparelist(value_list); // replace , between services with & to send the request
238 send_buffer = ssprintf(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 output_message = ssprintf(output_message, "%s",temp_string);
251 }
252 else
253 output_message = strscpy(output_message,"No service/process specified");
254 }
255
256 else if(vars_to_check==CHECK_MEMUSE) {
257
258 send_buffer = ssprintf(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;
262
263 if (!strncmp(recv_buffer,"ERROR",5)) {
264 printf("NSClient - %s\n",recv_buffer);
265 exit(STATE_UNKNOWN);
266 }
267
268 mem_commitLimit=atof(strtok(recv_buffer,"&"));
269 mem_commitByte=atof(strtok(NULL,"&"));
270 percent_used_space = (mem_commitByte / mem_commitLimit) * 100;
271 output_message = ssprintf(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 }
283
284 else if(vars_to_check==CHECK_COUNTER) {
285
286 if (check_value_list==TRUE) {
287 preparelist(value_list); // replace , between services with & to send the request
288 send_buffer = ssprintf(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 }
297
298 strtok(value_list,"&"); // burn the first parameters
299 description = strtok(NULL,"&");
300 counter_value = atof(recv_buffer);
301 if (description == NULL)
302 output_message = ssprintf(output_message, "%.f", counter_value);
303 else
304 output_message = ssprintf(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 output_message = strscpy(output_message,"No counter specified");
328 result=STATE_UNKNOWN;
329 }
330 }
331
332 /* reset timeout */
333 alarm(0);
334
335 printf("%s\n",output_message);
336
337 return return_code;
338}
339
340
341/* process command-line arguments */
342int process_arguments(int argc, char **argv){
343 int c;
344
345#ifdef HAVE_GETOPT_H
346 int option_index = 0;
347 static struct option long_options[] =
348 {
349 {"port", required_argument,0,'p'},
350 {"timeout", required_argument,0,'t'},
351 {"critical", required_argument,0,'c'},
352 {"warning", required_argument,0,'w'},
353 {"variable", required_argument,0,'v'},
354 {"hostname", required_argument,0,'H'},
355 {"version", no_argument, 0,'V'},
356 {"help", no_argument, 0,'h'},
357 {0,0,0,0}
358 };
359#endif
360
361 /* no options were supplied */
362 if(argc<2) return ERROR;
363
364 /* backwards compatibility */
365 if (! is_option(argv[1])) {
366 server_address=argv[1];
367 argv[1]=argv[0];
368 argv=&argv[1];
369 argc--;
370 }
371
372 for (c=1;c<argc;c++) {
373 if(strcmp("-to",argv[c])==0)
374 strcpy(argv[c],"-t");
375 else if (strcmp("-wv",argv[c])==0)
376 strcpy(argv[c],"-w");
377 else if (strcmp("-cv",argv[c])==0)
378 strcpy(argv[c],"-c");
379 }
380
381 while (1){
382#ifdef HAVE_GETOPT_H
383 c = getopt_long(argc,argv,"+hVH:t:c:w:p:v:l:s:d:",long_options,&option_index);
384#else
385 c = getopt(argc,argv,"+hVH:t:c:w:p:v:l:s:d:");
386#endif
387
388 if (c==-1||c==EOF||c==1)
389 break;
390
391 switch (c)
392 {
393 case '?': /* print short usage statement if args not parsable */
394 printf("%s: Unknown argument: %s\n\n",my_basename(argv[0]),optarg);
395 print_usage();
396 exit(STATE_UNKNOWN);
397 case 'h': /* help */
398 print_help();
399 exit(STATE_OK);
400 case 'V': /* version */
401 print_revision(my_basename(argv[0]),"$Revision$");
402 exit(STATE_OK);
403 case 'H': /* hostname */
404 server_address=optarg;
405 break;
406 case 's': /* password */
407 req_password=strscpy(req_password,optarg);
408 break;
409 case 'p': /* port */
410 if (is_intnonneg(optarg))
411 server_port=atoi(optarg);
412 else
413 terminate(STATE_UNKNOWN,"Server port an integer (seconds)\nType '%s -h' for additional help\n",PROGNAME);
414 break;
415 case 'v':
416 if(strlen(optarg)<4)
417 return ERROR;
418 if(!strcmp(optarg,"CLIENTVERSION"))
419 vars_to_check=CHECK_CLIENTVERSION;
420 else if(!strcmp(optarg,"CPULOAD"))
421 vars_to_check=CHECK_CPULOAD;
422 else if(!strcmp(optarg,"UPTIME"))
423 vars_to_check=CHECK_UPTIME;
424 else if(!strcmp(optarg,"USEDDISKSPACE"))
425 vars_to_check=CHECK_USEDDISKSPACE;
426 else if(!strcmp(optarg,"SERVICESTATE"))
427 vars_to_check=CHECK_SERVICESTATE;
428 else if(!strcmp(optarg,"PROCSTATE"))
429 vars_to_check=CHECK_PROCSTATE;
430 else if(!strcmp(optarg,"MEMUSE"))
431 vars_to_check=CHECK_MEMUSE;
432 else if(!strcmp(optarg,"COUNTER"))
433 vars_to_check=CHECK_COUNTER;
434 else
435 return ERROR;
436 break;
437 case 'l': /* value list */
438 value_list=strscpy(value_list,optarg);
439 check_value_list=TRUE;
440 break;
441 case 'w': /* warning threshold */
442 warning_value=strtoul(optarg,NULL,10);
443 check_warning_value=TRUE;
444 break;
445 case 'c': /* critical threshold */
446 critical_value=strtoul(optarg,NULL,10);
447 check_critical_value=TRUE;
448 break;
449 case 'd': /* Display select for services */
450 if (!strcmp(optarg,"SHOWALL"))
451 show_all = TRUE;
452 break;
453 case 't': /* timeout */
454 socket_timeout=atoi(optarg);
455 if(socket_timeout<=0)
456 return ERROR;
457 }
458
459 }
460
461 return OK;
462}
463
464
465void print_usage(void)
466{
467 printf("Usage: %s -H host [-p port] [-v variable] [-w warning] [-c critical] [-l params] [-d SHOWALL] [-t timeout]\n",PROGNAME);
468}
469
470
471void print_help(void)
472{
473 print_revision(PROGNAME,"$Revision$");
474 printf
475 ("Copyright (c) 2000 Yves Rubin (rubiyz@yahoo.com)\n\n"
476 "This plugin attempts to contact the NSClient service running on a Windows NT or Windows 2000 server to\n"
477 "gather the requested system information.\n\n");
478 print_usage();
479 printf
480 ("\nOptions:\n"
481 "-H, --hostname=HOST\n"
482 " Name of the host to check\n"
483 "-p, --port=INTEGER\n"
484 " Optional port number (default: %d)\n"
485 "-s <password>\n"
486 " Password needed for the request\n"
487 "-v, --variable=STRING\n"
488 " Variable to check. Valid variables are:\n"
489 " CLIENTVERSION = Get the NSClient version\n"
490 " CPULOAD = Average CPU load on last x minutes. Request a -l parameter with the following syntax:\n"
491 " -l <minutes range>,<warning threshold>,<critical threshold>. <minute range> should be less than 24*60.\n"
492 " Thresholds are percentage and up to 10 requests can be done in one shot. ie: -l 60,90,95,120,90,95\n"
493 " UPTIME = Get the uptime of the machine. No specific parameters. No warning or critical threshold\n"
494 " USEDDISKSPACE = Size and percentage of disk use. Request a -l parameter containing the drive letter only.\n"
495 " Warning and critical thresholds can be specified with -w and -c.\n"
496 " MEMUSE = Memory use. Warning and critical thresholds can be specified with -w and -c.\n"
497 " SERVICESTATE = Check the state of one or several services. Request a -l parameters with the following syntax:\n"
498 " -l <service1>,<service2>,<service3>,... You can specify -d SHOWALL in case you want to see working services\n"
499 " in the returned string.\n"
500 " PROCSTATE = Check if one or several process are running. Same syntax as SERVICESTATE.\n"
501 " COUNTER = Check any performance counter of Windows NT/2000. Request a -l parameters with the following syntax:\n"
502 " -l \"\\\\<performance object>\\\\counter\",\"<description>\" The <description> parameter is optional and \n"
503 " is given to a printf output command which require a float parameters. Some examples:\n"
504 " \"Paging file usage is %%.2f %%%%\" or \"%%.f %%%% paging file used.\"\n"
505 " -w, --warning=INTEGER\n"
506 " Threshold which will result in a warning status\n"
507 " -c, --critical=INTEGER\n"
508 " Threshold which will result in a critical status\n"
509 " -t, --timeout=INTEGER\n"
510 " Seconds before connection attempt times out (default: %d)\n"
511 "-h, --help\n"
512 " Print this help screen\n"
513 "-V, --version\n"
514 " Print version information\n\n"
515 "Notes:\n"
516 " - The NSClient service should be running on the server to get any information.\n"
517 " - Critical thresholds should be lower than warning thresholds\n", PORT, DEFAULT_SOCKET_TIMEOUT);
518}
519
520int strtolarray(unsigned long *array, char *string, char *delim) {
521 // split a <delim> delimited string into a long array
522 int idx=0;
523 char *t1;
524
525 for (idx=0;idx<MAX_VALUE_LIST;idx++)
526 array[idx]=-1;
527
528 idx=0;
529 for(t1 = strtok(string,delim);t1 != NULL; t1 = strtok(NULL, delim)) {
530 if (is_numeric(t1) && idx<MAX_VALUE_LIST) {
531 array[idx]=strtoul(t1,NULL,10);
532 idx++;
533 } else
534 return FALSE;
535 }
536 return TRUE;
537}
538
539void preparelist(char *string) {
540 // Replace all , with & which is the delimiter for the request
541 int i;
542
543 for (i = 0; i < strlen(string); i++)
544 if (string[i] == ',') {
545 string[i]='&';
546 }
547}
548