diff options
Diffstat (limited to 'web/attachments/371559-check_nt.c')
-rw-r--r-- | web/attachments/371559-check_nt.c | 772 |
1 files changed, 772 insertions, 0 deletions
diff --git a/web/attachments/371559-check_nt.c b/web/attachments/371559-check_nt.c new file mode 100644 index 0000000..f21a17a --- /dev/null +++ b/web/attachments/371559-check_nt.c | |||
@@ -0,0 +1,772 @@ | |||
1 | /***************************************************************************** | ||
2 | * | ||
3 | * Nagios check_nt plugin | ||
4 | * | ||
5 | * License: GPL | ||
6 | * Copyright (c) 2000-2002 Yves Rubin (rubiyz@yahoo.com) | ||
7 | * Copyright (c) 2003-2007 Nagios Plugins Development Team | ||
8 | * | ||
9 | * Description: | ||
10 | * | ||
11 | * This file contains the check_nt plugin | ||
12 | * | ||
13 | * This plugin collects data from the NSClient service running on a | ||
14 | * Windows NT/2000/XP/2003 server. | ||
15 | * This plugin requires NSClient software to run on NT | ||
16 | * (http://nsclient.ready2run.nl/) | ||
17 | * | ||
18 | * | ||
19 | * This program is free software: you can redistribute it and/or modify | ||
20 | * it under the terms of the GNU General Public License as published by | ||
21 | * the Free Software Foundation, either version 3 of the License, or | ||
22 | * (at your option) any later version. | ||
23 | * | ||
24 | * This program is distributed in the hope that it will be useful, | ||
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
27 | * GNU General Public License for more details. | ||
28 | * | ||
29 | * You should have received a copy of the GNU General Public License | ||
30 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
31 | * | ||
32 | * | ||
33 | *****************************************************************************/ | ||
34 | |||
35 | const char *progname = "check_nt"; | ||
36 | const char *copyright = "2000-2007"; | ||
37 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
38 | |||
39 | #include "common.h" | ||
40 | #include "netutils.h" | ||
41 | #include "utils.h" | ||
42 | |||
43 | enum checkvars { | ||
44 | CHECK_NONE, | ||
45 | CHECK_CLIENTVERSION, | ||
46 | CHECK_CPULOAD, | ||
47 | CHECK_UPTIME, | ||
48 | CHECK_USEDDISKSPACE, | ||
49 | CHECK_SERVICESTATE, | ||
50 | CHECK_PROCSTATE, | ||
51 | CHECK_MEMUSE, | ||
52 | CHECK_COUNTER, | ||
53 | CHECK_FILEAGE, | ||
54 | CHECK_INSTANCES | ||
55 | }; | ||
56 | |||
57 | enum { | ||
58 | MAX_VALUE_LIST = 30, | ||
59 | PORT = 1248 | ||
60 | }; | ||
61 | |||
62 | char *server_address=NULL; | ||
63 | char *volume_name=NULL; | ||
64 | int server_port=PORT; | ||
65 | char *value_list=NULL; | ||
66 | char *req_password=NULL; | ||
67 | unsigned long lvalue_list[MAX_VALUE_LIST]; | ||
68 | unsigned long warning_value=0L; | ||
69 | unsigned long critical_value=0L; | ||
70 | int check_warning_value=FALSE; | ||
71 | int check_critical_value=FALSE; | ||
72 | enum checkvars vars_to_check = CHECK_NONE; | ||
73 | int show_all=FALSE; | ||
74 | |||
75 | char recv_buffer[MAX_INPUT_BUFFER]; | ||
76 | |||
77 | void fetch_data (const char* address, int port, const char* sendb); | ||
78 | int process_arguments(int, char **); | ||
79 | void preparelist(char *string); | ||
80 | int strtoularray(unsigned long *array, char *string, const char *delim); | ||
81 | void print_help(void); | ||
82 | void print_usage(void); | ||
83 | |||
84 | int main(int argc, char **argv){ | ||
85 | |||
86 | /* should be int result = STATE_UNKNOWN; */ | ||
87 | |||
88 | int return_code = STATE_UNKNOWN; | ||
89 | char *send_buffer=NULL; | ||
90 | char *output_message=NULL; | ||
91 | char *perfdata=NULL; | ||
92 | char *temp_string=NULL; | ||
93 | char *temp_string_perf=NULL; | ||
94 | char *description=NULL,*counter_unit = NULL; | ||
95 | char *minval = NULL, *maxval = NULL, *errcvt = NULL; | ||
96 | char *fds=NULL, *tds=NULL; | ||
97 | |||
98 | double total_disk_space=0; | ||
99 | double free_disk_space=0; | ||
100 | double percent_used_space=0; | ||
101 | double warning_used_space=0; | ||
102 | double critical_used_space=0; | ||
103 | double mem_commitLimit=0; | ||
104 | double mem_commitByte=0; | ||
105 | double fminval = 0, fmaxval = 0; | ||
106 | unsigned long utilization; | ||
107 | unsigned long uptime; | ||
108 | unsigned long age_in_minutes; | ||
109 | double counter_value = 0.0; | ||
110 | int offset=0; | ||
111 | int updays=0; | ||
112 | int uphours=0; | ||
113 | int upminutes=0; | ||
114 | |||
115 | int isPercent = FALSE; | ||
116 | int allRight = FALSE; | ||
117 | |||
118 | setlocale (LC_ALL, ""); | ||
119 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
120 | textdomain (PACKAGE); | ||
121 | |||
122 | /* Parse extra opts if any */ | ||
123 | argv=np_extra_opts (&argc, argv, progname); | ||
124 | |||
125 | if(process_arguments(argc,argv) == ERROR) | ||
126 | usage4 (_("Could not parse arguments")); | ||
127 | |||
128 | /* initialize alarm signal handling */ | ||
129 | signal(SIGALRM,socket_timeout_alarm_handler); | ||
130 | |||
131 | /* set socket timeout */ | ||
132 | alarm(socket_timeout); | ||
133 | |||
134 | switch (vars_to_check) { | ||
135 | |||
136 | case CHECK_CLIENTVERSION: | ||
137 | |||
138 | asprintf(&send_buffer, "%s&1", req_password); | ||
139 | fetch_data (server_address, server_port, send_buffer); | ||
140 | if (value_list != NULL && strcmp(recv_buffer, value_list) != 0) { | ||
141 | asprintf (&output_message, _("Wrong client version - running: %s, required: %s"), recv_buffer, value_list); | ||
142 | return_code = STATE_WARNING; | ||
143 | } else { | ||
144 | asprintf (&output_message, "%s", recv_buffer); | ||
145 | return_code = STATE_OK; | ||
146 | } | ||
147 | break; | ||
148 | |||
149 | case CHECK_CPULOAD: | ||
150 | |||
151 | if (value_list==NULL) | ||
152 | output_message = strdup (_("missing -l parameters")); | ||
153 | else if (strtoularray(lvalue_list,value_list,",")==FALSE) | ||
154 | output_message = strdup (_("wrong -l parameter.")); | ||
155 | else { | ||
156 | /* -l parameters is present with only integers */ | ||
157 | return_code=STATE_OK; | ||
158 | temp_string = strdup (_("CPU Load")); | ||
159 | temp_string_perf = strdup (" "); | ||
160 | |||
161 | /* loop until one of the parameters is wrong or not present */ | ||
162 | while (lvalue_list[0+offset]> (unsigned long)0 && | ||
163 | lvalue_list[0+offset]<=(unsigned long)17280 && | ||
164 | lvalue_list[1+offset]> (unsigned long)0 && | ||
165 | lvalue_list[1+offset]<=(unsigned long)100 && | ||
166 | lvalue_list[2+offset]> (unsigned long)0 && | ||
167 | lvalue_list[2+offset]<=(unsigned long)100) { | ||
168 | |||
169 | /* Send request and retrieve data */ | ||
170 | asprintf(&send_buffer,"%s&2&%lu",req_password,lvalue_list[0+offset]); | ||
171 | fetch_data (server_address, server_port, send_buffer); | ||
172 | |||
173 | utilization=strtoul(recv_buffer,NULL,10); | ||
174 | |||
175 | /* Check if any of the request is in a warning or critical state */ | ||
176 | if(utilization >= lvalue_list[2+offset]) | ||
177 | return_code=STATE_CRITICAL; | ||
178 | else if(utilization >= lvalue_list[1+offset] && return_code<STATE_WARNING) | ||
179 | return_code=STATE_WARNING; | ||
180 | |||
181 | asprintf(&output_message,_(" %lu%% (%lu min average)"), utilization, lvalue_list[0+offset]); | ||
182 | asprintf(&temp_string,"%s%s",temp_string,output_message); | ||
183 | asprintf(&perfdata,_(" '%lu min avg Load'=%lu%%;%lu;%lu;0;100"), lvalue_list[0+offset], utilization, | ||
184 | lvalue_list[1+offset], lvalue_list[2+offset]); | ||
185 | asprintf(&temp_string_perf,"%s%s",temp_string_perf,perfdata); | ||
186 | offset+=3; /* move across the array */ | ||
187 | } | ||
188 | |||
189 | if (strlen(temp_string)>10) { /* we had at least one loop */ | ||
190 | output_message = strdup (temp_string); | ||
191 | perfdata = temp_string_perf; | ||
192 | } else | ||
193 | output_message = strdup (_("not enough values for -l parameters")); | ||
194 | } | ||
195 | break; | ||
196 | |||
197 | case CHECK_UPTIME: | ||
198 | |||
199 | asprintf(&send_buffer, "%s&3", req_password); | ||
200 | fetch_data (server_address, server_port, send_buffer); | ||
201 | uptime=strtoul(recv_buffer,NULL,10); | ||
202 | updays = uptime / 86400; | ||
203 | uphours = (uptime % 86400) / 3600; | ||
204 | upminutes = ((uptime % 86400) % 3600) / 60; | ||
205 | asprintf(&output_message,_("System Uptime - %u day(s) %u hour(s) %u minute(s)"),updays,uphours, upminutes); | ||
206 | return_code=STATE_OK; | ||
207 | break; | ||
208 | |||
209 | case CHECK_USEDDISKSPACE: | ||
210 | |||
211 | if (value_list==NULL) | ||
212 | output_message = strdup (_("missing -l parameters")); | ||
213 | else if (strlen(value_list)!=1) | ||
214 | output_message = strdup (_("wrong -l argument")); | ||
215 | else { | ||
216 | asprintf(&send_buffer,"%s&4&%s", req_password, value_list); | ||
217 | fetch_data (server_address, server_port, send_buffer); | ||
218 | fds=strtok(recv_buffer,"&"); | ||
219 | tds=strtok(NULL,"&"); | ||
220 | if(fds!=NULL) | ||
221 | free_disk_space=atof(fds); | ||
222 | if(tds!=NULL) | ||
223 | total_disk_space=atof(tds); | ||
224 | |||
225 | if (total_disk_space>0 && free_disk_space>=0) { | ||
226 | percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100; | ||
227 | warning_used_space = ((float)warning_value / 100) * total_disk_space; | ||
228 | critical_used_space = ((float)critical_value / 100) * total_disk_space; | ||
229 | |||
230 | asprintf(&temp_string,_("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"), | ||
231 | value_list, total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, | ||
232 | percent_used_space, free_disk_space / 1073741824, (free_disk_space / total_disk_space)*100); | ||
233 | asprintf(&temp_string_perf,_("'%s:\\ Used Space'=%.2fGb;%.2f;%.2f;0.00;%.2f"), value_list, | ||
234 | (total_disk_space - free_disk_space) / 1073741824, warning_used_space / 1073741824, | ||
235 | critical_used_space / 1073741824, total_disk_space / 1073741824); | ||
236 | |||
237 | if(check_critical_value==TRUE && percent_used_space >= critical_value) | ||
238 | return_code=STATE_CRITICAL; | ||
239 | else if (check_warning_value==TRUE && percent_used_space >= warning_value) | ||
240 | return_code=STATE_WARNING; | ||
241 | else | ||
242 | return_code=STATE_OK; | ||
243 | |||
244 | output_message = strdup (temp_string); | ||
245 | perfdata = temp_string_perf; | ||
246 | } else { | ||
247 | output_message = strdup (_("Free disk space : Invalid drive")); | ||
248 | return_code=STATE_UNKNOWN; | ||
249 | } | ||
250 | } | ||
251 | break; | ||
252 | |||
253 | case CHECK_SERVICESTATE: | ||
254 | case CHECK_PROCSTATE: | ||
255 | |||
256 | if (value_list==NULL) | ||
257 | output_message = strdup (_("No service/process specified")); | ||
258 | else { | ||
259 | preparelist(value_list); /* replace , between services with & to send the request */ | ||
260 | asprintf(&send_buffer,"%s&%u&%s&%s", req_password,(vars_to_check==CHECK_SERVICESTATE)?5:6, | ||
261 | (show_all==TRUE) ? "ShowAll" : "ShowFail",value_list); | ||
262 | fetch_data (server_address, server_port, send_buffer); | ||
263 | if (strstr(recv_buffer, "&")) { | ||
264 | return_code=atoi(strtok(recv_buffer,"&")); | ||
265 | temp_string=strtok(NULL,"&"); | ||
266 | output_message = strdup (temp_string); | ||
267 | } else { | ||
268 | asprintf(&temp_string,"Remote server error:[%s]", recv_buffer); | ||
269 | output_message = temp_string; | ||
270 | return_code=STATE_UNKNOWN; | ||
271 | } | ||
272 | } | ||
273 | break; | ||
274 | |||
275 | case CHECK_MEMUSE: | ||
276 | |||
277 | asprintf(&send_buffer,"%s&7", req_password); | ||
278 | fetch_data (server_address, server_port, send_buffer); | ||
279 | mem_commitLimit=atof(strtok(recv_buffer,"&")); | ||
280 | mem_commitByte=atof(strtok(NULL,"&")); | ||
281 | percent_used_space = (mem_commitByte / mem_commitLimit) * 100; | ||
282 | warning_used_space = ((float)warning_value / 100) * mem_commitLimit; | ||
283 | critical_used_space = ((float)critical_value / 100) * mem_commitLimit; | ||
284 | |||
285 | /* Divisor should be 1048567, not 3044515, as we are measuring "Commit Charge" here, | ||
286 | which equals RAM + Pagefiles. */ | ||
287 | asprintf(&output_message,_("Memory usage: total:%.2f Mb - used: %.2f Mb (%.0f%%) - free: %.2f Mb (%.0f%%)"), | ||
288 | mem_commitLimit / 1048567, mem_commitByte / 1048567, percent_used_space, | ||
289 | (mem_commitLimit - mem_commitByte) / 1048567, (mem_commitLimit - mem_commitByte) / mem_commitLimit * 100); | ||
290 | asprintf(&perfdata,_("'Memory usage'=%.2fMb;%.2f;%.2f;0.00;%.2f"), mem_commitByte / 1048567, | ||
291 | warning_used_space / 1048567, critical_used_space / 1048567, mem_commitLimit / 1048567); | ||
292 | |||
293 | return_code=STATE_OK; | ||
294 | if(check_critical_value==TRUE && percent_used_space >= critical_value) | ||
295 | return_code=STATE_CRITICAL; | ||
296 | else if (check_warning_value==TRUE && percent_used_space >= warning_value) | ||
297 | return_code=STATE_WARNING; | ||
298 | |||
299 | break; | ||
300 | |||
301 | case CHECK_COUNTER: | ||
302 | |||
303 | |||
304 | /* | ||
305 | CHECK_COUNTER has been modified to provide extensive perfdata information. | ||
306 | In order to do this, some modifications have been done to the code | ||
307 | and some constraints have been introduced. | ||
308 | |||
309 | 1) For the sake of simplicity of the code, perfdata information will only be | ||
310 | provided when the "description" field is added. | ||
311 | |||
312 | 2) If the counter you're going to measure is percent-based, the code will detect | ||
313 | the percent sign in its name and will attribute minimum (0%) and maximum (100%) | ||
314 | values automagically, as well the ¨%" sign to graph units. | ||
315 | |||
316 | 3) OTOH, if the counter is "absolute", you'll have to provide the following | ||
317 | the counter unit - that is, the dimensions of the counter you're getting. Examples: | ||
318 | pages/s, packets transferred, etc. | ||
319 | |||
320 | 4) If you want, you may provide the minimum and maximum values to expect. They aren't mandatory, | ||
321 | but once specified they MUST have the same order of magnitude and units of -w and -c; otherwise. | ||
322 | strange things will happen when you make graphs of your data. | ||
323 | */ | ||
324 | |||
325 | if (value_list == NULL) | ||
326 | output_message = strdup (_("No counter specified")); | ||
327 | else | ||
328 | { | ||
329 | preparelist (value_list); /* replace , between services with & to send the request */ | ||
330 | isPercent = (strchr (value_list, '%') != NULL); | ||
331 | |||
332 | strtok (value_list, "&"); /* burn the first parameters */ | ||
333 | description = strtok (NULL, "&"); | ||
334 | counter_unit = strtok (NULL, "&"); | ||
335 | asprintf (&send_buffer, "%s&8&%s", req_password, value_list); | ||
336 | fetch_data (server_address, server_port, send_buffer); | ||
337 | counter_value = atof (recv_buffer); | ||
338 | |||
339 | if (description == NULL) | ||
340 | asprintf (&output_message, "%.f", counter_value); | ||
341 | else if (isPercent) | ||
342 | { | ||
343 | counter_unit = strdup ("%"); | ||
344 | allRight = TRUE; | ||
345 | } | ||
346 | |||
347 | if ((counter_unit != NULL) && (!allRight)) | ||
348 | { | ||
349 | minval = strtok (NULL, "&"); | ||
350 | maxval = strtok (NULL, "&"); | ||
351 | |||
352 | /* All parameters specified. Let's check the numbers */ | ||
353 | |||
354 | fminval = (minval != NULL) ? strtod (minval, &errcvt) : -1; | ||
355 | fmaxval = (minval != NULL) ? strtod (maxval, &errcvt) : -1; | ||
356 | |||
357 | if ((fminval == 0) && (minval == errcvt)) | ||
358 | output_message = strdup (_("Minimum value contains non-numbers")); | ||
359 | else | ||
360 | { | ||
361 | if ((fmaxval == 0) && (maxval == errcvt)) | ||
362 | output_message = strdup (_("Maximum value contains non-numbers")); | ||
363 | else | ||
364 | allRight = TRUE; /* Everything is OK. */ | ||
365 | |||
366 | } | ||
367 | } | ||
368 | else if ((counter_unit == NULL) && (description != NULL)) | ||
369 | output_message = strdup (_("No unit counter specified")); | ||
370 | |||
371 | if (allRight) | ||
372 | { | ||
373 | /* Let's format the output string, finally... */ | ||
374 | if (strstr(description, "%") == NULL) { | ||
375 | asprintf (&output_message, "%s = %.2f %s", description, counter_value, counter_unit); | ||
376 | } else { | ||
377 | /* has formatting, will segv if wrong */ | ||
378 | asprintf (&output_message, description, counter_value); | ||
379 | } | ||
380 | asprintf (&output_message, "%s |", output_message); | ||
381 | asprintf (&output_message,"%s %s", output_message, | ||
382 | fperfdata (description, counter_value, | ||
383 | counter_unit, 1, warning_value, 1, critical_value, | ||
384 | (!(isPercent) && (minval != NULL)), fminval, | ||
385 | (!(isPercent) && (minval != NULL)), fmaxval)); | ||
386 | } | ||
387 | } | ||
388 | |||
389 | if (critical_value > warning_value) | ||
390 | { /* Normal thresholds */ | ||
391 | if (check_critical_value == TRUE && counter_value >= critical_value) | ||
392 | return_code = STATE_CRITICAL; | ||
393 | else if (check_warning_value == TRUE && counter_value >= warning_value) | ||
394 | return_code = STATE_WARNING; | ||
395 | else | ||
396 | return_code = STATE_OK; | ||
397 | } | ||
398 | else | ||
399 | { /* inverse thresholds */ | ||
400 | return_code = STATE_OK; | ||
401 | if (check_critical_value == TRUE && counter_value <= critical_value) | ||
402 | return_code = STATE_CRITICAL; | ||
403 | else if (check_warning_value == TRUE && counter_value <= warning_value) | ||
404 | return_code = STATE_WARNING; | ||
405 | } | ||
406 | break; | ||
407 | |||
408 | case CHECK_FILEAGE: | ||
409 | |||
410 | if (value_list==NULL) | ||
411 | output_message = strdup (_("No counter specified")); | ||
412 | else { | ||
413 | preparelist(value_list); /* replace , between services with & to send the request */ | ||
414 | asprintf(&send_buffer,"%s&9&%s", req_password,value_list); | ||
415 | fetch_data (server_address, server_port, send_buffer); | ||
416 | age_in_minutes = atoi(strtok(recv_buffer,"&")); | ||
417 | description = strtok(NULL,"&"); | ||
418 | output_message = strdup (description); | ||
419 | |||
420 | if (critical_value > warning_value) { /* Normal thresholds */ | ||
421 | if(check_critical_value==TRUE && age_in_minutes >= critical_value) | ||
422 | return_code=STATE_CRITICAL; | ||
423 | else if (check_warning_value==TRUE && age_in_minutes >= warning_value) | ||
424 | return_code=STATE_WARNING; | ||
425 | else | ||
426 | return_code=STATE_OK; | ||
427 | } | ||
428 | else { /* inverse thresholds */ | ||
429 | if(check_critical_value==TRUE && age_in_minutes <= critical_value) | ||
430 | return_code=STATE_CRITICAL; | ||
431 | else if (check_warning_value==TRUE && age_in_minutes <= warning_value) | ||
432 | return_code=STATE_WARNING; | ||
433 | else | ||
434 | return_code=STATE_OK; | ||
435 | } | ||
436 | } | ||
437 | break; | ||
438 | |||
439 | case CHECK_INSTANCES: | ||
440 | if (value_list==NULL) | ||
441 | output_message = strdup (_("No counter specified")); | ||
442 | else { | ||
443 | asprintf(&send_buffer,"%s&10&%s", req_password,value_list); | ||
444 | fetch_data (server_address, server_port, send_buffer); | ||
445 | if (!strncmp(recv_buffer,"ERROR",5)) { | ||
446 | printf("NSClient - %s\n",recv_buffer); | ||
447 | exit(STATE_UNKNOWN); | ||
448 | } | ||
449 | asprintf(&output_message,"%s",recv_buffer); | ||
450 | return_code=STATE_OK; | ||
451 | } | ||
452 | break; | ||
453 | |||
454 | case CHECK_NONE: | ||
455 | default: | ||
456 | usage4 (_("Please specify a variable to check")); | ||
457 | break; | ||
458 | |||
459 | } | ||
460 | |||
461 | /* reset timeout */ | ||
462 | alarm(0); | ||
463 | |||
464 | if (perfdata==NULL) | ||
465 | printf("%s\n",output_message); | ||
466 | else | ||
467 | printf("%s | %s\n",output_message,perfdata); | ||
468 | return return_code; | ||
469 | } | ||
470 | |||
471 | |||
472 | |||
473 | /* process command-line arguments */ | ||
474 | int process_arguments(int argc, char **argv){ | ||
475 | int c; | ||
476 | |||
477 | int option = 0; | ||
478 | static struct option longopts[] = | ||
479 | { | ||
480 | {"port", required_argument,0,'p'}, | ||
481 | {"timeout", required_argument,0,'t'}, | ||
482 | {"critical", required_argument,0,'c'}, | ||
483 | {"warning", required_argument,0,'w'}, | ||
484 | {"variable", required_argument,0,'v'}, | ||
485 | {"hostname", required_argument,0,'H'}, | ||
486 | {"params", required_argument,0,'l'}, | ||
487 | {"secret", required_argument,0,'s'}, | ||
488 | {"display", required_argument,0,'d'}, | ||
489 | {"unknown-timeout", no_argument, 0, 'u'}, | ||
490 | {"version", no_argument, 0,'V'}, | ||
491 | {"help", no_argument, 0,'h'}, | ||
492 | {0,0,0,0} | ||
493 | }; | ||
494 | |||
495 | /* no options were supplied */ | ||
496 | if(argc<2) return ERROR; | ||
497 | |||
498 | /* backwards compatibility */ | ||
499 | if (! is_option(argv[1])) { | ||
500 | server_address = strdup(argv[1]); | ||
501 | argv[1]=argv[0]; | ||
502 | argv=&argv[1]; | ||
503 | argc--; | ||
504 | } | ||
505 | |||
506 | for (c=1;c<argc;c++) { | ||
507 | if(strcmp("-to",argv[c])==0) | ||
508 | strcpy(argv[c],"-t"); | ||
509 | else if (strcmp("-wv",argv[c])==0) | ||
510 | strcpy(argv[c],"-w"); | ||
511 | else if (strcmp("-cv",argv[c])==0) | ||
512 | strcpy(argv[c],"-c"); | ||
513 | } | ||
514 | |||
515 | while (1) { | ||
516 | c = getopt_long(argc,argv,"+hVH:t:c:w:p:v:l:s:d:u",longopts,&option); | ||
517 | |||
518 | if (c==-1||c==EOF||c==1) | ||
519 | break; | ||
520 | |||
521 | switch (c) { | ||
522 | case '?': /* print short usage statement if args not parsable */ | ||
523 | usage5 (); | ||
524 | case 'h': /* help */ | ||
525 | print_help(); | ||
526 | exit(STATE_OK); | ||
527 | case 'V': /* version */ | ||
528 | print_revision(progname, NP_VERSION); | ||
529 | exit(STATE_OK); | ||
530 | case 'H': /* hostname */ | ||
531 | server_address = optarg; | ||
532 | break; | ||
533 | case 's': /* password */ | ||
534 | req_password = optarg; | ||
535 | break; | ||
536 | case 'p': /* port */ | ||
537 | if (is_intnonneg(optarg)) | ||
538 | server_port=atoi(optarg); | ||
539 | else | ||
540 | die(STATE_UNKNOWN,_("Server port must be an integer\n")); | ||
541 | break; | ||
542 | case 'v': | ||
543 | if(strlen(optarg)<4) | ||
544 | return ERROR; | ||
545 | if(!strcmp(optarg,"CLIENTVERSION")) | ||
546 | vars_to_check=CHECK_CLIENTVERSION; | ||
547 | else if(!strcmp(optarg,"CPULOAD")) | ||
548 | vars_to_check=CHECK_CPULOAD; | ||
549 | else if(!strcmp(optarg,"UPTIME")) | ||
550 | vars_to_check=CHECK_UPTIME; | ||
551 | else if(!strcmp(optarg,"USEDDISKSPACE")) | ||
552 | vars_to_check=CHECK_USEDDISKSPACE; | ||
553 | else if(!strcmp(optarg,"SERVICESTATE")) | ||
554 | vars_to_check=CHECK_SERVICESTATE; | ||
555 | else if(!strcmp(optarg,"PROCSTATE")) | ||
556 | vars_to_check=CHECK_PROCSTATE; | ||
557 | else if(!strcmp(optarg,"MEMUSE")) | ||
558 | vars_to_check=CHECK_MEMUSE; | ||
559 | else if(!strcmp(optarg,"COUNTER")) | ||
560 | vars_to_check=CHECK_COUNTER; | ||
561 | else if(!strcmp(optarg,"FILEAGE")) | ||
562 | vars_to_check=CHECK_FILEAGE; | ||
563 | else if(!strcmp(optarg,"INSTANCES")) | ||
564 | vars_to_check=CHECK_INSTANCES; | ||
565 | else | ||
566 | return ERROR; | ||
567 | break; | ||
568 | case 'l': /* value list */ | ||
569 | value_list = optarg; | ||
570 | break; | ||
571 | case 'w': /* warning threshold */ | ||
572 | warning_value=strtoul(optarg,NULL,10); | ||
573 | check_warning_value=TRUE; | ||
574 | break; | ||
575 | case 'c': /* critical threshold */ | ||
576 | critical_value=strtoul(optarg,NULL,10); | ||
577 | check_critical_value=TRUE; | ||
578 | break; | ||
579 | case 'd': /* Display select for services */ | ||
580 | if (!strcmp(optarg,"SHOWALL")) | ||
581 | show_all = TRUE; | ||
582 | break; | ||
583 | case 'u': | ||
584 | socket_timeout_state=STATE_UNKNOWN; | ||
585 | break; | ||
586 | case 't': /* timeout */ | ||
587 | socket_timeout=atoi(optarg); | ||
588 | if(socket_timeout<=0) | ||
589 | return ERROR; | ||
590 | } | ||
591 | |||
592 | } | ||
593 | if (server_address == NULL) | ||
594 | usage4 (_("You must provide a server address or host name")); | ||
595 | |||
596 | if (vars_to_check==CHECK_NONE) | ||
597 | return ERROR; | ||
598 | |||
599 | if (req_password == NULL) | ||
600 | req_password = strdup (_("None")); | ||
601 | |||
602 | return OK; | ||
603 | } | ||
604 | |||
605 | |||
606 | |||
607 | void fetch_data (const char *address, int port, const char *sendb) { | ||
608 | int result; | ||
609 | |||
610 | result=process_tcp_request(address, port, sendb, recv_buffer,sizeof(recv_buffer)); | ||
611 | |||
612 | if(result!=STATE_OK) | ||
613 | die (result, _("could not fetch information from server\n")); | ||
614 | |||
615 | if (!strncmp(recv_buffer,"ERROR",5)) | ||
616 | die (STATE_UNKNOWN, "NSClient - %s\n",recv_buffer); | ||
617 | } | ||
618 | |||
619 | int strtoularray(unsigned long *array, char *string, const char *delim) { | ||
620 | /* split a <delim> delimited string into a long array */ | ||
621 | int idx=0; | ||
622 | char *t1; | ||
623 | |||
624 | for (idx=0;idx<MAX_VALUE_LIST;idx++) | ||
625 | array[idx]=0; | ||
626 | |||
627 | idx=0; | ||
628 | for(t1 = strtok(string,delim);t1 != NULL; t1 = strtok(NULL, delim)) { | ||
629 | if (is_numeric(t1) && idx<MAX_VALUE_LIST) { | ||
630 | array[idx]=strtoul(t1,NULL,10); | ||
631 | idx++; | ||
632 | } else | ||
633 | return FALSE; | ||
634 | } | ||
635 | return TRUE; | ||
636 | } | ||
637 | |||
638 | void preparelist(char *string) { | ||
639 | /* Replace all , with & which is the delimiter for the request */ | ||
640 | int i; | ||
641 | |||
642 | for (i = 0; (size_t)i < strlen(string); i++) | ||
643 | if (string[i] == ',') { | ||
644 | string[i]='&'; | ||
645 | } | ||
646 | } | ||
647 | |||
648 | |||
649 | |||
650 | void print_help(void) | ||
651 | { | ||
652 | print_revision(progname, NP_VERSION); | ||
653 | |||
654 | printf ("Copyright (c) 2000 Yves Rubin (rubiyz@yahoo.com)\n"); | ||
655 | printf (COPYRIGHT, copyright, email); | ||
656 | |||
657 | printf ("%s\n", _("This plugin collects data from the NSClient service running on a")); | ||
658 | printf ("%s\n", _("Windows NT/2000/XP/2003 server.")); | ||
659 | |||
660 | printf ("\n\n"); | ||
661 | |||
662 | print_usage(); | ||
663 | |||
664 | printf (_(UT_HELP_VRSN)); | ||
665 | printf (_(UT_EXTRA_OPTS)); | ||
666 | |||
667 | printf ("%s\n", _("Options:")); | ||
668 | printf (" %s\n", "-H, --hostname=HOST"); | ||
669 | printf (" %s\n", _("Name of the host to check")); | ||
670 | printf (" %s\n", "-p, --port=INTEGER"); | ||
671 | printf (" %s", _("Optional port number (default: ")); | ||
672 | printf ("%d)\n", PORT); | ||
673 | printf (" %s\n", "-s, --secret=<password>"); | ||
674 | printf (" %s\n", _("Password needed for the request")); | ||
675 | printf (" %s\n", "-w, --warning=INTEGER"); | ||
676 | printf (" %s\n", _("Threshold which will result in a warning status")); | ||
677 | printf (" %s\n", "-c, --critical=INTEGER"); | ||
678 | printf (" %s\n", _("Threshold which will result in a critical status")); | ||
679 | printf (" %s\n", "-t, --timeout=INTEGER"); | ||
680 | printf (" %s", _("Seconds before connection attempt times out (default: ")); | ||
681 | printf (" %s\n", "-l, --params=<parameters>"); | ||
682 | printf (" %s", _("Parameters passed to specified check (see below)")); | ||
683 | printf (" %s\n", "-d, --display={SHOWALL}"); | ||
684 | printf (" %s", _("Display options (currently only SHOWALL works)")); | ||
685 | printf (" %s\n", "-u, --unknown-timeout"); | ||
686 | printf (" %s", _("Return UNKNOWN on timeouts")); | ||
687 | printf ("%d)\n", DEFAULT_SOCKET_TIMEOUT); | ||
688 | printf (" %s\n", "-h, --help"); | ||
689 | printf (" %s\n", _("Print this help screen")); | ||
690 | printf (" %s\n", "-V, --version"); | ||
691 | printf (" %s\n", _("Print version information")); | ||
692 | printf (" %s\n", "-v, --variable=STRING"); | ||
693 | printf (" %s\n\n", _("Variable to check")); | ||
694 | printf ("%s\n", _("Valid variables are:")); | ||
695 | printf (" %s", "CLIENTVERSION ="); | ||
696 | printf (" %s\n", _("Get the NSClient version")); | ||
697 | printf (" %s\n", _("If -l <version> is specified, will return warning if versions differ.")); | ||
698 | printf (" %s\n", "CPULOAD ="); | ||
699 | printf (" %s\n", _("Average CPU load on last x minutes.")); | ||
700 | printf (" %s\n", _("Request a -l parameter with the following syntax:")); | ||
701 | printf (" %s\n", _("-l <minutes range>,<warning threshold>,<critical threshold>.")); | ||
702 | printf (" %s\n", _("<minute range> should be less than 24*60.")); | ||
703 | printf (" %s\n", _("Thresholds are percentage and up to 10 requests can be done in one shot.")); | ||
704 | printf (" %s\n", "ie: -l 60,90,95,120,90,95"); | ||
705 | printf (" %s\n", "UPTIME ="); | ||
706 | printf (" %s\n", _("Get the uptime of the machine.")); | ||
707 | printf (" %s\n", _("No specific parameters. No warning or critical threshold")); | ||
708 | printf (" %s\n", "USEDDISKSPACE ="); | ||
709 | printf (" %s\n", _("Size and percentage of disk use.")); | ||
710 | printf (" %s\n", _("Request a -l parameter containing the drive letter only.")); | ||
711 | printf (" %s\n", _("Warning and critical thresholds can be specified with -w and -c.")); | ||
712 | printf (" %s\n", "MEMUSE ="); | ||
713 | printf (" %s\n", _("Memory use.")); | ||
714 | printf (" %s\n", _("Warning and critical thresholds can be specified with -w and -c.")); | ||
715 | printf (" %s\n", "SERVICESTATE ="); | ||
716 | printf (" %s\n", _("Check the state of one or several services.")); | ||
717 | printf (" %s\n", _("Request a -l parameters with the following syntax:")); | ||
718 | printf (" %s\n", _("-l <service1>,<service2>,<service3>,...")); | ||
719 | printf (" %s\n", _("You can specify -d SHOWALL in case you want to see working services")); | ||
720 | printf (" %s\n", _("in the returned string.")); | ||
721 | printf (" %s\n", "PROCSTATE ="); | ||
722 | printf (" %s\n", _("Check if one or several process are running.")); | ||
723 | printf (" %s\n", _("Same syntax as SERVICESTATE.")); | ||
724 | printf (" %s\n", "COUNTER ="); | ||
725 | printf (" %s\n", _("Check any performance counter of Windows NT/2000.")); | ||
726 | printf (" %s\n", _("Request a -l parameters with the following syntax:")); | ||
727 | printf (" %s\n", _("-l \"\\\\<performance object>\\\\counter\",\"<description>")); | ||
728 | printf (" %s\n", _("The <description> parameter is optional and is given to a printf ")); | ||
729 | printf (" %s\n", _("output command which requires a float parameter.")); | ||
730 | printf (" %s\n", _("If <description> does not include \"%%\", it is used as a label.")); | ||
731 | printf (" %s\n", _("Some examples:")); | ||
732 | printf (" %s\n", "\"Paging file usage is %%.2f %%%%\""); | ||
733 | printf (" %s\n", "\"%%.f %%%% paging file used.\""); | ||
734 | printf (" %s\n", "INSTANCES ="); | ||
735 | printf (" %s\n", _("Check any performance counter object of Windows NT/2000.")); | ||
736 | printf (" %s\n", _("Syntax: check_nt -H <hostname> -p <port> -v INSTANCES -l <counter object>")); | ||
737 | printf (" %s\n", _("<counter object> is a Windows Perfmon Counter object (eg. Process),")); | ||
738 | printf (" %s\n", _("if it is two words, it should be enclosed in quotes")); | ||
739 | printf (" %s\n", _("The returned results will be a comma-separated list of instances on ")); | ||
740 | printf (" %s\n", _(" the selected computer for that object.")); | ||
741 | printf (" %s\n", _("The purpose of this is to be run from command line to determine what instances")); | ||
742 | printf (" %s\n", _(" are available for monitoring without having to log onto the Windows server")); | ||
743 | printf (" %s\n", _(" to run Perfmon directly.")); | ||
744 | printf (" %s\n", _("It can also be used in scripts that automatically create Nagios service")); | ||
745 | printf (" %s\n", _(" configuration files.")); | ||
746 | printf (" %s\n", _("Some examples:")); | ||
747 | printf (" %s\n\n", _("check_nt -H 192.168.1.1 -p 1248 -v INSTANCES -l Process")); | ||
748 | |||
749 | printf ("%s\n", _("Notes:")); | ||
750 | printf (" %s\n", _("- The NSClient service should be running on the server to get any information")); | ||
751 | printf (" %s\n", "(http://nsclient.ready2run.nl)."); | ||
752 | printf (" %s\n", _("- Critical thresholds should be lower than warning thresholds")); | ||
753 | printf (" %s\n", _("- Default port 1248 is sometimes in use by other services. The error")); | ||
754 | printf (" %s\n", _("output when this happens contains \"Cannot map xxxxx to protocol number\".")); | ||
755 | printf (" %s\n", _("One fix for this is to change the port to something else on check_nt ")); | ||
756 | printf (" %s\n", _("and on the client service it\'s connecting to.")); | ||
757 | #ifdef NP_EXTRA_OPTS | ||
758 | printf (" -%s", _(UT_EXTRA_OPTS_NOTES)); | ||
759 | #endif | ||
760 | |||
761 | printf (_(UT_SUPPORT)); | ||
762 | } | ||
763 | |||
764 | |||
765 | |||
766 | void print_usage(void) | ||
767 | { | ||
768 | printf (_("Usage:")); | ||
769 | printf ("%s -H host -v variable [-p port] [-w warning] [-c critical]\n",progname); | ||
770 | printf ("[-l params] [-d SHOWALL] [-u] [-t timeout]\n"); | ||
771 | } | ||
772 | |||