diff options
author | Ethan Galstad <egalstad@users.sourceforge.net> | 2002-02-28 06:42:51 +0000 |
---|---|---|
committer | Ethan Galstad <egalstad@users.sourceforge.net> | 2002-02-28 06:42:51 +0000 |
commit | 44a321cb8a42d6c0ea2d96a1086a17f2134c89cc (patch) | |
tree | a1a4d9f7b92412a17ab08f34f04eec45433048b7 /plugins/check_snmp.c | |
parent | 54fd5d7022ff2d6a59bc52b8869182f3fc77a058 (diff) | |
download | monitoring-plugins-44a321cb8a42d6c0ea2d96a1086a17f2134c89cc.tar.gz |
Initial revision
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@2 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/check_snmp.c')
-rw-r--r-- | plugins/check_snmp.c | 805 |
1 files changed, 805 insertions, 0 deletions
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c new file mode 100644 index 00000000..2f970b3d --- /dev/null +++ b/plugins/check_snmp.c | |||
@@ -0,0 +1,805 @@ | |||
1 | /****************************************************************************** | ||
2 | * | ||
3 | * CHECK_SNMP.C | ||
4 | * | ||
5 | * Program: SNMP plugin for Nagios | ||
6 | * License: GPL | ||
7 | * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org) | ||
8 | * | ||
9 | * Last Modified: $Date$ | ||
10 | * | ||
11 | * Description: | ||
12 | * | ||
13 | * This plugin uses the 'snmpget' command included with the UCD-SNMP | ||
14 | * package. If you don't have the package installed you will need to | ||
15 | * download it from http://ucd-snmp.ucdavis.edu before you can use | ||
16 | * this plugin. | ||
17 | * | ||
18 | * License Information: | ||
19 | * | ||
20 | * This program is free software; you can redistribute it and/or modify | ||
21 | * it under the terms of the GNU General Public License as published by | ||
22 | * the Free Software Foundation; either version 2 of the License, or | ||
23 | * (at your option) any later version. | ||
24 | * | ||
25 | * This program is distributed in the hope that it will be useful, | ||
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
28 | * GNU General Public License for more details. | ||
29 | * | ||
30 | * You should have received a copy of the GNU General Public License | ||
31 | * along with this program; if not, write to the Free Software | ||
32 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
33 | *./plugins/check_snmp 127.0.0.1 -c public -o .1.3.6.1.4.1.2021.9.1.2.1 | ||
34 | *****************************************************************************/ | ||
35 | |||
36 | #include "common.h" | ||
37 | #include "utils.h" | ||
38 | #include "popen.h" | ||
39 | |||
40 | #define PROGNAME check_snmp | ||
41 | |||
42 | #define mark(a) ((a)!=0?"*":"") | ||
43 | |||
44 | #define CHECK_UNDEF 0 | ||
45 | #define CRIT_PRESENT 1 | ||
46 | #define CRIT_STRING 2 | ||
47 | #define CRIT_REGEX 4 | ||
48 | #define CRIT_GT 8 | ||
49 | #define CRIT_LT 16 | ||
50 | #define CRIT_GE 32 | ||
51 | #define CRIT_LE 64 | ||
52 | #define CRIT_EQ 128 | ||
53 | #define CRIT_NE 256 | ||
54 | #define CRIT_RANGE 512 | ||
55 | #define WARN_PRESENT 1024 | ||
56 | #define WARN_STRING 2048 | ||
57 | #define WARN_REGEX 4096 | ||
58 | #define WARN_GT 8192 | ||
59 | #define WARN_LT 16384 | ||
60 | #define WARN_GE 32768 | ||
61 | #define WARN_LE 65536 | ||
62 | #define WARN_EQ 131072 | ||
63 | #define WARN_NE 262144 | ||
64 | #define WARN_RANGE 524288 | ||
65 | |||
66 | #define MAX_OIDS 8 | ||
67 | #define MAX_DELIM_LENGTH 8 | ||
68 | #define DEFAULT_DELIMITER "=" | ||
69 | #define DEFAULT_OUTPUT_DELIMITER " " | ||
70 | |||
71 | void print_usage (void); | ||
72 | void print_help (char *); | ||
73 | int process_arguments (int, char **); | ||
74 | int call_getopt (int, char **); | ||
75 | int check_num (int); | ||
76 | char *clarify_message (char *); | ||
77 | int lu_getll (unsigned long *, char *); | ||
78 | int lu_getul (unsigned long *, char *); | ||
79 | char *thisarg (char *str); | ||
80 | char *nextarg (char *str); | ||
81 | |||
82 | #ifdef HAVE_REGEX_H | ||
83 | #include <regex.h> | ||
84 | char regex_expect[MAX_INPUT_BUFFER] = ""; | ||
85 | regex_t preg; | ||
86 | regmatch_t pmatch[10]; | ||
87 | char timestamp[10] = ""; | ||
88 | char regex[MAX_INPUT_BUFFER]; | ||
89 | char errbuf[MAX_INPUT_BUFFER]; | ||
90 | int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE; | ||
91 | int eflags = 0; | ||
92 | int errcode, excode; | ||
93 | #endif | ||
94 | |||
95 | char *server_address = NULL; | ||
96 | char *community = NULL; | ||
97 | char oid[MAX_INPUT_BUFFER] = ""; | ||
98 | char *label = NULL; | ||
99 | char *units = NULL; | ||
100 | char string_value[MAX_INPUT_BUFFER] = ""; | ||
101 | char **labels = NULL; | ||
102 | char **unitv = NULL; | ||
103 | int nlabels = 0; | ||
104 | int labels_size = 8; | ||
105 | int nunits = 0; | ||
106 | int unitv_size = 8; | ||
107 | unsigned long lower_warn_lim[MAX_OIDS]; | ||
108 | unsigned long upper_warn_lim[MAX_OIDS]; | ||
109 | unsigned long lower_crit_lim[MAX_OIDS]; | ||
110 | unsigned long upper_crit_lim[MAX_OIDS]; | ||
111 | unsigned long response_value[MAX_OIDS]; | ||
112 | int check_warning_value = FALSE; | ||
113 | int check_critical_value = FALSE; | ||
114 | int eval_method[MAX_OIDS]; | ||
115 | char *delimiter = NULL; | ||
116 | char *output_delim = NULL; | ||
117 | |||
118 | |||
119 | int | ||
120 | main (int argc, char **argv) | ||
121 | { | ||
122 | int i = 0; | ||
123 | int iresult = STATE_UNKNOWN; | ||
124 | int found = 0; | ||
125 | int result = STATE_DEPENDENT; | ||
126 | char input_buffer[MAX_INPUT_BUFFER]; | ||
127 | char *command_line = NULL; | ||
128 | char *response = NULL; | ||
129 | char *outbuff = NULL; | ||
130 | char *output = NULL; | ||
131 | char *ptr = NULL; | ||
132 | char *p2 = NULL; | ||
133 | char *show = NULL; | ||
134 | |||
135 | labels = malloc (labels_size); | ||
136 | unitv = malloc (unitv_size); | ||
137 | outbuff = strscpy (outbuff, ""); | ||
138 | for (i = 0; i < MAX_OIDS; i++) | ||
139 | eval_method[i] = CHECK_UNDEF; | ||
140 | i = 0; | ||
141 | |||
142 | if (process_arguments (argc, argv) == ERROR) | ||
143 | usage ("Incorrect arguments supplied\n"); | ||
144 | |||
145 | /* create the command line to execute */ | ||
146 | command_line = ssprintf | ||
147 | (command_line, | ||
148 | "%s -m ALL -v 1 %s %s %s", | ||
149 | PATH_TO_SNMPGET, server_address, community, oid); | ||
150 | |||
151 | /* run the command */ | ||
152 | child_process = spopen (command_line); | ||
153 | if (child_process == NULL) { | ||
154 | printf ("Could not open pipe: %s\n", command_line); | ||
155 | exit (STATE_UNKNOWN); | ||
156 | } | ||
157 | |||
158 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
159 | if (child_stderr == NULL) { | ||
160 | printf ("Could not open stderr for %s\n", command_line); | ||
161 | } | ||
162 | |||
163 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) | ||
164 | output = strscat (output, input_buffer); | ||
165 | |||
166 | ptr = output; | ||
167 | |||
168 | while (ptr) { | ||
169 | |||
170 | ptr = strstr (ptr, delimiter); | ||
171 | if (ptr == NULL) | ||
172 | break; | ||
173 | |||
174 | ptr += strlen (delimiter); | ||
175 | ptr += strspn (ptr, " "); | ||
176 | |||
177 | found++; | ||
178 | |||
179 | if (ptr[0] == '"') { | ||
180 | ptr++; | ||
181 | response = strpcpy (response, ptr, "\""); | ||
182 | ptr = strpbrk (ptr, "\""); | ||
183 | ptr += strspn (ptr, "\"\n"); | ||
184 | } | ||
185 | else { | ||
186 | response = strpcpy (response, ptr, "\n"); | ||
187 | ptr = strpbrk (ptr, "\n"); | ||
188 | ptr += strspn (ptr, "\n"); | ||
189 | while | ||
190 | (strstr (ptr, delimiter) && | ||
191 | strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) { | ||
192 | response = strpcat (response, ptr, "\n"); | ||
193 | ptr = strpbrk (ptr, "\n"); | ||
194 | } | ||
195 | if (ptr && strstr (ptr, delimiter) == NULL) { | ||
196 | response = strscat (response, ptr); | ||
197 | ptr = NULL; | ||
198 | } | ||
199 | } | ||
200 | |||
201 | if (strstr (response, "Gauge: ")) | ||
202 | show = strstr (response, "Gauge: ") + 7; | ||
203 | else if (strstr (response, "Gauge32: ")) | ||
204 | show = strstr (response, "Gauge32: ") + 9; | ||
205 | else | ||
206 | show = response; | ||
207 | p2 = show; | ||
208 | |||
209 | if (eval_method[i] & CRIT_GT || | ||
210 | eval_method[i] & CRIT_LT || | ||
211 | eval_method[i] & CRIT_GE || | ||
212 | eval_method[i] & CRIT_LE || | ||
213 | eval_method[i] & CRIT_EQ || | ||
214 | eval_method[i] & CRIT_NE || | ||
215 | eval_method[i] & WARN_GT || | ||
216 | eval_method[i] & WARN_LT || | ||
217 | eval_method[i] & WARN_GE || | ||
218 | eval_method[i] & WARN_LE || | ||
219 | eval_method[i] & WARN_EQ || eval_method[i] & WARN_NE) { | ||
220 | p2 = strpbrk (p2, "0123456789"); | ||
221 | response_value[i] = strtoul (p2, NULL, 10); | ||
222 | iresult = check_num (i); | ||
223 | show = ssprintf (show, "%d", response_value[i]); | ||
224 | } | ||
225 | |||
226 | else if (eval_method[i] & CRIT_STRING) { | ||
227 | if (strcmp (response, string_value)) | ||
228 | iresult = STATE_CRITICAL; | ||
229 | else | ||
230 | iresult = STATE_OK; | ||
231 | } | ||
232 | |||
233 | else if (eval_method[i] & CRIT_REGEX) { | ||
234 | #ifdef HAVE_REGEX_H | ||
235 | excode = regexec (&preg, response, 10, pmatch, eflags); | ||
236 | if (excode == 0) { | ||
237 | iresult = STATE_OK; | ||
238 | } | ||
239 | else if (excode != REG_NOMATCH) { | ||
240 | regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER); | ||
241 | printf ("Execute Error: %s\n", errbuf); | ||
242 | exit (STATE_CRITICAL); | ||
243 | } | ||
244 | else { | ||
245 | iresult = STATE_CRITICAL; | ||
246 | } | ||
247 | #else | ||
248 | printf ("SNMP UNKNOWN: call for regex which was not a compiled option"); | ||
249 | exit (STATE_UNKNOWN); | ||
250 | #endif | ||
251 | } | ||
252 | |||
253 | else { | ||
254 | if (response) | ||
255 | iresult = STATE_OK; | ||
256 | else if (eval_method[i] & CRIT_PRESENT) | ||
257 | iresult = STATE_CRITICAL; | ||
258 | else | ||
259 | iresult = STATE_WARNING; | ||
260 | } | ||
261 | |||
262 | result = max (result, iresult); | ||
263 | |||
264 | if (nlabels > 1 && i < nlabels && labels[i] != NULL) | ||
265 | outbuff = ssprintf | ||
266 | (outbuff, | ||
267 | "%s%s%s %s%s%s", | ||
268 | outbuff, | ||
269 | (i == 0) ? " " : output_delim, | ||
270 | labels[i], mark (iresult), show, mark (iresult)); | ||
271 | else | ||
272 | outbuff = ssprintf | ||
273 | (outbuff, | ||
274 | "%s%s%s%s%s", | ||
275 | outbuff, | ||
276 | (i == 0) ? " " : output_delim, mark (iresult), show, mark (iresult)); | ||
277 | |||
278 | if (nunits > 0 && i < nunits) | ||
279 | outbuff = ssprintf (outbuff, "%s %s", outbuff, unitv[i]); | ||
280 | |||
281 | i++; | ||
282 | |||
283 | } /* end while */ | ||
284 | |||
285 | if (found == 0) | ||
286 | terminate | ||
287 | (STATE_UNKNOWN, | ||
288 | "%s problem - No data recieved from host\nCMD: %s\n", | ||
289 | label, command_line); | ||
290 | |||
291 | /* WARNING if output found on stderr */ | ||
292 | if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
293 | result = max (result, STATE_WARNING); | ||
294 | |||
295 | /* close stderr */ | ||
296 | (void) fclose (child_stderr); | ||
297 | |||
298 | /* close the pipe */ | ||
299 | if (spclose (child_process)) | ||
300 | result = max (result, STATE_WARNING); | ||
301 | |||
302 | if (nunits > 0) | ||
303 | printf ("%s %s -%s\n", label, state_text (result), outbuff); | ||
304 | else | ||
305 | printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); | ||
306 | |||
307 | return result; | ||
308 | } | ||
309 | |||
310 | /* process command-line arguments */ | ||
311 | int | ||
312 | process_arguments (int argc, char **argv) | ||
313 | { | ||
314 | int c; | ||
315 | |||
316 | if (argc < 2) | ||
317 | return ERROR; | ||
318 | |||
319 | for (c = 1; c < argc; c++) { | ||
320 | if (strcmp ("-to", argv[c]) == 0) | ||
321 | strcpy (argv[c], "-t"); | ||
322 | if (strcmp ("-wv", argv[c]) == 0) | ||
323 | strcpy (argv[c], "-w"); | ||
324 | if (strcmp ("-cv", argv[c]) == 0) | ||
325 | strcpy (argv[c], "-c"); | ||
326 | } | ||
327 | |||
328 | c = 0; | ||
329 | while (c += (call_getopt (argc - c, &argv[c]))) { | ||
330 | if (argc <= c) | ||
331 | break; | ||
332 | if (server_address == NULL) | ||
333 | server_address = strscpy (NULL, argv[c]); | ||
334 | } | ||
335 | |||
336 | if (community == NULL) | ||
337 | community = strscpy (NULL, "public"); | ||
338 | |||
339 | if (delimiter == NULL) | ||
340 | delimiter = strscpy (NULL, DEFAULT_DELIMITER); | ||
341 | |||
342 | if (output_delim == NULL) | ||
343 | output_delim = strscpy (NULL, DEFAULT_OUTPUT_DELIMITER); | ||
344 | |||
345 | if (label == NULL) | ||
346 | label = strscpy (NULL, "SNMP"); | ||
347 | |||
348 | if (units == NULL) | ||
349 | units = strscpy (NULL, ""); | ||
350 | |||
351 | return c; | ||
352 | } | ||
353 | |||
354 | int | ||
355 | call_getopt (int argc, char **argv) | ||
356 | { | ||
357 | char *ptr; | ||
358 | int c, i = 1; | ||
359 | int j = 0, jj = 0; | ||
360 | |||
361 | #ifdef HAVE_GETOPT_H | ||
362 | int option_index = 0; | ||
363 | static struct option long_options[] = { | ||
364 | {"help", no_argument, 0, 'h'}, | ||
365 | {"version", no_argument, 0, 'V'}, | ||
366 | {"timeout", required_argument, 0, 't'}, | ||
367 | {"critical", required_argument, 0, 'c'}, | ||
368 | {"warning", required_argument, 0, 'w'}, | ||
369 | {"hostname", required_argument, 0, 'H'}, | ||
370 | {"community", required_argument, 0, 'C'}, | ||
371 | {"oid", required_argument, 0, 'o'}, | ||
372 | {"object", required_argument, 0, 'o'}, | ||
373 | {"delimiter", required_argument, 0, 'd'}, | ||
374 | {"output-delimiter", required_argument, 0, 'D'}, | ||
375 | {"string", required_argument, 0, 's'}, | ||
376 | {"regex", required_argument, 0, 'r'}, | ||
377 | {"ereg", required_argument, 0, 'r'}, | ||
378 | {"eregi", required_argument, 0, 'R'}, | ||
379 | {"label", required_argument, 0, 'l'}, | ||
380 | {"units", required_argument, 0, 'u'}, | ||
381 | {0, 0, 0, 0} | ||
382 | }; | ||
383 | #endif | ||
384 | |||
385 | while (1) { | ||
386 | #ifdef HAVE_GETOPT_H | ||
387 | c = | ||
388 | getopt_long (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:", | ||
389 | long_options, &option_index); | ||
390 | #else | ||
391 | c = getopt (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:"); | ||
392 | #endif | ||
393 | |||
394 | if (c == -1 || c == EOF) | ||
395 | break; | ||
396 | |||
397 | i++; | ||
398 | switch (c) { | ||
399 | case 't': | ||
400 | case 'c': | ||
401 | case 'w': | ||
402 | case 'H': | ||
403 | case 'C': | ||
404 | case 'o': | ||
405 | case 'd': | ||
406 | case 'D': | ||
407 | case 's': | ||
408 | case 'R': | ||
409 | case 'r': | ||
410 | case 'l': | ||
411 | case 'u': | ||
412 | i++; | ||
413 | } | ||
414 | |||
415 | switch (c) { | ||
416 | case '?': /* help */ | ||
417 | printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg); | ||
418 | print_usage (); | ||
419 | exit (STATE_UNKNOWN); | ||
420 | case 'h': /* help */ | ||
421 | print_help (my_basename (argv[0])); | ||
422 | exit (STATE_OK); | ||
423 | case 'V': /* version */ | ||
424 | print_revision (my_basename (argv[0]), "$Revision$"); | ||
425 | exit (STATE_OK); | ||
426 | case 't': /* timeout period */ | ||
427 | if (!is_integer (optarg)) { | ||
428 | printf ("%s: Timeout Interval must be an integer!\n\n", | ||
429 | my_basename (argv[0])); | ||
430 | print_usage (); | ||
431 | exit (STATE_UNKNOWN); | ||
432 | } | ||
433 | timeout_interval = atoi (optarg); | ||
434 | break; | ||
435 | case 'c': /* critical time threshold */ | ||
436 | if (strspn (optarg, "0123456789:,") < strlen (optarg)) { | ||
437 | printf ("Invalid critical threshold: %s\n", optarg); | ||
438 | print_usage (); | ||
439 | exit (STATE_UNKNOWN); | ||
440 | } | ||
441 | for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) { | ||
442 | if (lu_getll (&lower_crit_lim[jj], ptr) == 1) | ||
443 | eval_method[jj] |= CRIT_LT; | ||
444 | if (lu_getul (&upper_crit_lim[jj], ptr) == 1) | ||
445 | eval_method[jj] |= CRIT_GT; | ||
446 | (ptr = index (ptr, ',')) ? ptr++ : ptr; | ||
447 | } | ||
448 | break; | ||
449 | case 'w': /* warning time threshold */ | ||
450 | if (strspn (optarg, "0123456789:,") < strlen (optarg)) { | ||
451 | printf ("Invalid warning threshold: %s\n", optarg); | ||
452 | print_usage (); | ||
453 | exit (STATE_UNKNOWN); | ||
454 | } | ||
455 | for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) { | ||
456 | if (lu_getll (&lower_warn_lim[jj], ptr) == 1) | ||
457 | eval_method[jj] |= WARN_LT; | ||
458 | if (lu_getul (&upper_warn_lim[jj], ptr) == 1) | ||
459 | eval_method[jj] |= WARN_GT; | ||
460 | (ptr = index (ptr, ',')) ? ptr++ : ptr; | ||
461 | } | ||
462 | break; | ||
463 | case 'H': /* Host or server */ | ||
464 | server_address = strscpy (server_address, optarg); | ||
465 | break; | ||
466 | case 'C': /* group or community */ | ||
467 | community = strscpy (community, optarg); | ||
468 | break; | ||
469 | case 'o': /* object identifier */ | ||
470 | for (ptr = optarg; (ptr = index (ptr, ',')); ptr++) | ||
471 | ptr[0] = ' '; | ||
472 | strncpy (oid, optarg, sizeof (oid) - 1); | ||
473 | oid[sizeof (oid) - 1] = 0; | ||
474 | for (ptr = optarg, j = 1; (ptr = index (ptr, ' ')); ptr++) | ||
475 | j++; | ||
476 | break; | ||
477 | case 'd': /* delimiter */ | ||
478 | delimiter = strscpy (delimiter, optarg); | ||
479 | break; | ||
480 | case 'D': /* output-delimiter */ | ||
481 | output_delim = strscpy (output_delim, optarg); | ||
482 | break; | ||
483 | case 's': /* string or substring */ | ||
484 | strncpy (string_value, optarg, sizeof (string_value) - 1); | ||
485 | string_value[sizeof (string_value) - 1] = 0; | ||
486 | eval_method[jj++] = CRIT_STRING; | ||
487 | break; | ||
488 | case 'R': /* regex */ | ||
489 | #ifdef HAVE_REGEX_H | ||
490 | cflags = REG_ICASE; | ||
491 | #endif | ||
492 | case 'r': /* regex */ | ||
493 | #ifdef HAVE_REGEX_H | ||
494 | cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE; | ||
495 | strncpy (regex_expect, optarg, sizeof (regex_expect) - 1); | ||
496 | regex_expect[sizeof (regex_expect) - 1] = 0; | ||
497 | errcode = regcomp (&preg, regex_expect, cflags); | ||
498 | if (errcode != 0) { | ||
499 | regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); | ||
500 | printf ("Could Not Compile Regular Expression"); | ||
501 | return ERROR; | ||
502 | } | ||
503 | eval_method[jj++] = CRIT_REGEX; | ||
504 | #else | ||
505 | printf ("SNMP UNKNOWN: call for regex which was not a compiled option"); | ||
506 | exit (STATE_UNKNOWN); | ||
507 | #endif | ||
508 | break; | ||
509 | case 'l': /* label */ | ||
510 | label = optarg; | ||
511 | nlabels++; | ||
512 | if (nlabels >= labels_size) { | ||
513 | labels_size += 8; | ||
514 | labels = realloc (labels, labels_size); | ||
515 | if (labels == NULL) | ||
516 | terminate (STATE_UNKNOWN, | ||
517 | "Could not realloc() labels[%d]", nlabels); | ||
518 | } | ||
519 | labels[nlabels - 1] = optarg; | ||
520 | ptr = thisarg (optarg); | ||
521 | if (strstr (ptr, "'") == ptr) | ||
522 | labels[nlabels - 1] = ptr + 1; | ||
523 | else | ||
524 | labels[nlabels - 1] = ptr; | ||
525 | while (ptr && (ptr = nextarg (ptr))) { | ||
526 | if (nlabels >= labels_size) { | ||
527 | labels_size += 8; | ||
528 | labels = realloc (labels, labels_size); | ||
529 | if (labels == NULL) | ||
530 | terminate (STATE_UNKNOWN, "Could not realloc() labels\n"); | ||
531 | } | ||
532 | labels++; | ||
533 | ptr = thisarg (ptr); | ||
534 | if (strstr (ptr, "'") == ptr) | ||
535 | labels[nlabels - 1] = ptr + 1; | ||
536 | else | ||
537 | labels[nlabels - 1] = ptr; | ||
538 | } | ||
539 | break; | ||
540 | case 'u': /* units */ | ||
541 | units = optarg; | ||
542 | nunits++; | ||
543 | if (nunits >= unitv_size) { | ||
544 | unitv_size += 8; | ||
545 | unitv = realloc (unitv, unitv_size); | ||
546 | if (unitv == NULL) | ||
547 | terminate (STATE_UNKNOWN, | ||
548 | "Could not realloc() units [%d]\n", nunits); | ||
549 | } | ||
550 | unitv[nunits - 1] = optarg; | ||
551 | ptr = thisarg (optarg); | ||
552 | if (strstr (ptr, "'") == ptr) | ||
553 | unitv[nunits - 1] = ptr + 1; | ||
554 | else | ||
555 | unitv[nunits - 1] = ptr; | ||
556 | while (ptr && (ptr = nextarg (ptr))) { | ||
557 | if (nunits >= unitv_size) { | ||
558 | unitv_size += 8; | ||
559 | unitv = realloc (unitv, unitv_size); | ||
560 | if (units == NULL) | ||
561 | terminate (STATE_UNKNOWN, "Could not realloc() units\n"); | ||
562 | } | ||
563 | nunits++; | ||
564 | ptr = thisarg (ptr); | ||
565 | if (strstr (ptr, "'") == ptr) | ||
566 | unitv[nunits - 1] = ptr + 1; | ||
567 | else | ||
568 | unitv[nunits - 1] = ptr; | ||
569 | } | ||
570 | break; | ||
571 | } | ||
572 | } | ||
573 | return i; | ||
574 | } | ||
575 | |||
576 | void | ||
577 | print_usage (void) | ||
578 | { | ||
579 | printf | ||
580 | ("Usage: check_snmp -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n" | ||
581 | " [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n" | ||
582 | " [-l label] [-u units] [-d delimiter] [-D output-delimiter]\n" | ||
583 | " check_snmp --help\n" " check_snmp --version\n"); | ||
584 | } | ||
585 | |||
586 | void | ||
587 | print_help (char *cmd) | ||
588 | { | ||
589 | printf ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n" | ||
590 | "License: GPL\n\n"); | ||
591 | print_usage (); | ||
592 | printf | ||
593 | ("\nOptions:\n" | ||
594 | " -h, --help\n" | ||
595 | " Print detailed help screen\n" | ||
596 | " -V, --version\n" | ||
597 | " Print version information\n" | ||
598 | " -H, --hostname=HOST\n" | ||
599 | " Name or IP address of the device you wish to query\n" | ||
600 | " -o, --oid=OID(s)\n" | ||
601 | " Object identifier(s) whose value you wish to query\n" | ||
602 | " -w, --warning=INTEGER_RANGE(s)\n" | ||
603 | " Range(s) which will not result in a WARNING status\n" | ||
604 | " -c, --critical=INTEGER_RANGE(s)\n" | ||
605 | " Range(s) which will not result in a CRITICAL status\n" | ||
606 | " -C, --community=STRING\n" | ||
607 | " Optional community string for SNMP communication\n" | ||
608 | " (default is \"public\")\n" | ||
609 | " -u, --units=STRING\n" | ||
610 | " Units label(s) for output data (e.g., 'sec.').\n" | ||
611 | " -d, --delimiter=STRING\n" | ||
612 | " Delimiter to use when parsing returned data. Default is \"%s\"\n" | ||
613 | " Any data on the right hand side of the delimiter is considered\n" | ||
614 | " to be the data that should be used in the evaluation.\n" | ||
615 | " -t, --timeout=INTEGER\n" | ||
616 | " Seconds to wait before plugin times out (see also nagios server timeout)\n" | ||
617 | " -D, --output-delimiter=STRING\n" | ||
618 | " Separates output on multiple OID requests\n" | ||
619 | " -s, --string=STRING\n" | ||
620 | " Return OK state (for that OID) if STRING is an exact match\n" | ||
621 | " -r, --ereg=REGEX\n" | ||
622 | " Return OK state (for that OID) if extended regular expression REGEX matches\n" | ||
623 | " -R, --eregi=REGEX\n" | ||
624 | " Return OK state (for that OID) if case-insensitive extended REGEX matches\n" | ||
625 | " -l, --label=STRING\n" | ||
626 | " Prefix label for output from plugin (default -s 'SNMP')\n\n" | ||
627 | "- This plugin uses the 'snmpget' command included with the UCD-SNMP package.\n" | ||
628 | " If you don't have the package installed, you will need to download it from\n" | ||
629 | " http://ucd-snmp.ucdavis.edu before you can use this plugin.\n" | ||
630 | "- Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n" | ||
631 | " internal spaces must be quoted)\n" | ||
632 | "- Ranges are inclusive and are indicated with colons. When specified as\n" | ||
633 | " 'min:max' a STATE_OK will be returned if the result is within the indicated\n" | ||
634 | " range or is equal to the upper or lower bound. A non-OK state will be\n" | ||
635 | " returned if the result is outside the specified range.\n" | ||
636 | "- If spcified in the order 'max:min' a non-OK state will be returned if the\n" | ||
637 | " result is within the (inclusive) range.\n" | ||
638 | "- Upper or lower bounds may be omitted to skip checking the respective limit.\n" | ||
639 | "- Bare integers are interpreted as upper limits.\n" | ||
640 | "- When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n" | ||
641 | "- Note that only one string and one regex may be checked at present\n" | ||
642 | "- All evaluation methods other than PR, STR, and SUBSTR expect that the value\n" | ||
643 | " returned from the SNMP query is an unsigned integer.\n\n", | ||
644 | DEFAULT_DELIMITER); | ||
645 | } | ||
646 | |||
647 | char * | ||
648 | clarify_message (char *msg) | ||
649 | { | ||
650 | int i = 0; | ||
651 | int foo; | ||
652 | char tmpmsg_c[MAX_INPUT_BUFFER]; | ||
653 | char *tmpmsg = (char *) &tmpmsg_c; | ||
654 | tmpmsg = strcpy (tmpmsg, msg); | ||
655 | if (!strncmp (tmpmsg, " Hex:", 5)) { | ||
656 | tmpmsg = strtok (tmpmsg, ":"); | ||
657 | while ((tmpmsg = strtok (NULL, " "))) { | ||
658 | foo = strtol (tmpmsg, NULL, 16); | ||
659 | /* Translate chars that are not the same value in the printers | ||
660 | * character set. | ||
661 | */ | ||
662 | switch (foo) { | ||
663 | case 208: | ||
664 | { | ||
665 | foo = 197; | ||
666 | break; | ||
667 | } | ||
668 | case 216: | ||
669 | { | ||
670 | foo = 196; | ||
671 | break; | ||
672 | } | ||
673 | } | ||
674 | msg[i] = foo; | ||
675 | i++; | ||
676 | } | ||
677 | msg[i] = 0; | ||
678 | } | ||
679 | return (msg); | ||
680 | } | ||
681 | |||
682 | |||
683 | int | ||
684 | check_num (int i) | ||
685 | { | ||
686 | int result; | ||
687 | result = STATE_OK; | ||
688 | if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT && | ||
689 | lower_warn_lim[i] > upper_warn_lim[i]) { | ||
690 | if (response_value[i] <= lower_warn_lim[i] && | ||
691 | response_value[i] >= upper_warn_lim[i]) { | ||
692 | result = STATE_WARNING; | ||
693 | } | ||
694 | } | ||
695 | else if | ||
696 | ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) || | ||
697 | (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) || | ||
698 | (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) || | ||
699 | (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) || | ||
700 | (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) || | ||
701 | (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) { | ||
702 | result = STATE_WARNING; | ||
703 | } | ||
704 | |||
705 | if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT && | ||
706 | lower_warn_lim[i] > upper_warn_lim[i]) { | ||
707 | if (response_value[i] <= lower_crit_lim[i] && | ||
708 | response_value[i] >= upper_crit_lim[i]) { | ||
709 | result = STATE_CRITICAL; | ||
710 | } | ||
711 | } | ||
712 | else if | ||
713 | ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) || | ||
714 | (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) || | ||
715 | (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) || | ||
716 | (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) || | ||
717 | (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) || | ||
718 | (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) { | ||
719 | result = STATE_CRITICAL; | ||
720 | } | ||
721 | |||
722 | return result; | ||
723 | } | ||
724 | |||
725 | |||
726 | int | ||
727 | lu_getll (unsigned long *ll, char *str) | ||
728 | { | ||
729 | char tmp[100]; | ||
730 | if (strchr (str, ':') == NULL) | ||
731 | return 0; | ||
732 | if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':'))) | ||
733 | return 0; | ||
734 | if (sscanf (str, "%lu%[:]", ll, tmp) == 2) | ||
735 | return 1; | ||
736 | return 0; | ||
737 | } | ||
738 | |||
739 | int | ||
740 | lu_getul (unsigned long *ul, char *str) | ||
741 | { | ||
742 | char tmp[100]; | ||
743 | if (sscanf (str, "%lu%[^,]", ul, tmp) == 1) | ||
744 | return 1; | ||
745 | if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1) | ||
746 | return 1; | ||
747 | if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1) | ||
748 | return 1; | ||
749 | return 0; | ||
750 | } | ||
751 | |||
752 | |||
753 | |||
754 | |||
755 | |||
756 | |||
757 | /* trim leading whitespace | ||
758 | if there is a leading quote, make sure it balances */ | ||
759 | |||
760 | char * | ||
761 | thisarg (char *str) | ||
762 | { | ||
763 | str += strspn (str, " \t\r\n"); /* trim any leading whitespace */ | ||
764 | if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */ | ||
765 | if (strlen (str) == 1 || !strstr (str + 1, "'")) | ||
766 | terminate (STATE_UNKNOWN, "Unbalanced quotes\n"); | ||
767 | } | ||
768 | return str; | ||
769 | } | ||
770 | |||
771 | |||
772 | /* if there's a leading quote, advance to the trailing quote | ||
773 | set the trailing quote to '\x0' | ||
774 | if the string continues, advance beyond the comma */ | ||
775 | |||
776 | char * | ||
777 | nextarg (char *str) | ||
778 | { | ||
779 | if (strstr (str, "'") == str) { | ||
780 | if (strlen (str) > 1) { | ||
781 | str = strstr (str + 1, "'"); | ||
782 | str[0] = 0; | ||
783 | return (++str); | ||
784 | } | ||
785 | else { | ||
786 | str[0] = 0; | ||
787 | return NULL; | ||
788 | } | ||
789 | } | ||
790 | if (strstr (str, ",") == str) { | ||
791 | if (strlen (str) > 1) { | ||
792 | str[0] = 0; | ||
793 | return (++str); | ||
794 | } | ||
795 | else { | ||
796 | str[0] = 0; | ||
797 | return NULL; | ||
798 | } | ||
799 | } | ||
800 | if ((str = strstr (str, ",")) && strlen (str) > 1) { | ||
801 | str[0] = 0; | ||
802 | return (++str); | ||
803 | } | ||
804 | return NULL; | ||
805 | } | ||