diff options
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | THANKS.in | 1 | ||||
-rw-r--r-- | plugins/Makefile.am | 4 | ||||
-rw-r--r-- | plugins/check_snmp.c | 295 | ||||
-rw-r--r-- | plugins/t/check_snmp.t | 49 |
5 files changed, 184 insertions, 166 deletions
@@ -22,6 +22,7 @@ This file documents the major additions and syntax changes between releases. | |||
22 | Fixed check_http behaviour: all check are now performed as long as a valid response is returned (sf.net #1460312) | 22 | Fixed check_http behaviour: all check are now performed as long as a valid response is returned (sf.net #1460312) |
23 | check_http --onredirect=sticky follows using the same IP address (sf.net #2550208) | 23 | check_http --onredirect=sticky follows using the same IP address (sf.net #2550208) |
24 | Fixed coredump from check_nt when invalid drive is specified (#2179754 - Olli Hauer) | 24 | Fixed coredump from check_nt when invalid drive is specified (#2179754 - Olli Hauer) |
25 | Fixed passing of quotes in OID for check_snmp (#1985230 - Jan Wagner, patch by John Barbuto) | ||
25 | 26 | ||
26 | 1.4.13 25th Sept 2008 | 27 | 1.4.13 25th Sept 2008 |
27 | Fix Debian bug #460097: check_http --max-age broken (Hilko Bengen) | 28 | Fix Debian bug #460097: check_http --max-age broken (Hilko Bengen) |
@@ -247,3 +247,4 @@ Erik Welch | |||
247 | Nik Soggia | 247 | Nik Soggia |
248 | Olli Hauer | 248 | Olli Hauer |
249 | Richard Edward Horner | 249 | Richard Edward Horner |
250 | John Barbuto | ||
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index f5272e1..4e1f210 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
@@ -93,7 +93,7 @@ check_ping_LDADD = $(NETLIBS) popen.o | |||
93 | check_procs_LDADD = $(BASEOBJS) | 93 | check_procs_LDADD = $(BASEOBJS) |
94 | check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS) | 94 | check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS) |
95 | check_real_LDADD = $(NETLIBS) | 95 | check_real_LDADD = $(NETLIBS) |
96 | check_snmp_LDADD = $(BASEOBJS) popen.o | 96 | check_snmp_LDADD = $(BASEOBJS) |
97 | check_smtp_LDADD = $(SSLOBJS) $(NETLIBS) $(SSLLIBS) | 97 | check_smtp_LDADD = $(SSLOBJS) $(NETLIBS) $(SSLLIBS) |
98 | check_ssh_LDADD = $(NETLIBS) | 98 | check_ssh_LDADD = $(NETLIBS) |
99 | check_swap_LDADD = $(MATHLIBS) $(BASEOBJS) popen.o | 99 | check_swap_LDADD = $(MATHLIBS) $(BASEOBJS) popen.o |
@@ -135,7 +135,7 @@ check_ping_DEPENDENCIES = check_ping.c $(NETOBJS) popen.o $(DEPLIBS) | |||
135 | check_procs_DEPENDENCIES = check_procs.c $(BASEOBJS) popen.o $(DEPLIBS) | 135 | check_procs_DEPENDENCIES = check_procs.c $(BASEOBJS) popen.o $(DEPLIBS) |
136 | check_radius_DEPENDENCIES = check_radius.c $(NETOBJS) $(DEPLIBS) | 136 | check_radius_DEPENDENCIES = check_radius.c $(NETOBJS) $(DEPLIBS) |
137 | check_real_DEPENDENCIES = check_real.c $(NETOBJS) $(DEPLIBS) | 137 | check_real_DEPENDENCIES = check_real.c $(NETOBJS) $(DEPLIBS) |
138 | check_snmp_DEPENDENCIES = check_snmp.c $(BASEOBJS) popen.o $(DEPLIBS) | 138 | check_snmp_DEPENDENCIES = check_snmp.c $(BASEOBJS) $(DEPLIBS) |
139 | check_smtp_DEPENDENCIES = check_smtp.c $(SSLOBJS) $(NETOBJS) $(DEPLIBS) | 139 | check_smtp_DEPENDENCIES = check_smtp.c $(SSLOBJS) $(NETOBJS) $(DEPLIBS) |
140 | check_ssh_DEPENDENCIES = check_ssh.c $(NETOBJS) $(DEPLIBS) | 140 | check_ssh_DEPENDENCIES = check_ssh.c $(NETOBJS) $(DEPLIBS) |
141 | check_swap_DEPENDENCIES = check_swap.c $(BASEOBJS) popen.o $(DEPLIBS) | 141 | check_swap_DEPENDENCIES = check_swap.c $(BASEOBJS) popen.o $(DEPLIBS) |
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c index a8e08fa..1b1eb2e 100644 --- a/plugins/check_snmp.c +++ b/plugins/check_snmp.c | |||
@@ -34,7 +34,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; | |||
34 | 34 | ||
35 | #include "common.h" | 35 | #include "common.h" |
36 | #include "utils.h" | 36 | #include "utils.h" |
37 | #include "popen.h" | 37 | #include "utils_cmd.h" |
38 | 38 | ||
39 | #define DEFAULT_COMMUNITY "public" | 39 | #define DEFAULT_COMMUNITY "public" |
40 | #define DEFAULT_PORT "161" | 40 | #define DEFAULT_PORT "161" |
@@ -91,14 +91,14 @@ regex_t preg; | |||
91 | regmatch_t pmatch[10]; | 91 | regmatch_t pmatch[10]; |
92 | char timestamp[10] = ""; | 92 | char timestamp[10] = ""; |
93 | char errbuf[MAX_INPUT_BUFFER] = ""; | 93 | char errbuf[MAX_INPUT_BUFFER] = ""; |
94 | char perfstr[MAX_INPUT_BUFFER] = ""; | 94 | char perfstr[MAX_INPUT_BUFFER] = "| "; |
95 | int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE; | 95 | int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE; |
96 | int eflags = 0; | 96 | int eflags = 0; |
97 | int errcode, excode; | 97 | int errcode, excode; |
98 | 98 | ||
99 | char *server_address = NULL; | 99 | char *server_address = NULL; |
100 | char *community = NULL; | 100 | char *community = NULL; |
101 | char *authpriv = NULL; | 101 | char **authpriv = NULL; |
102 | char *proto = NULL; | 102 | char *proto = NULL; |
103 | char *seclevel = NULL; | 103 | char *seclevel = NULL; |
104 | char *secname = NULL; | 104 | char *secname = NULL; |
@@ -106,10 +106,11 @@ char *authproto = NULL; | |||
106 | char *privproto = NULL; | 106 | char *privproto = NULL; |
107 | char *authpasswd = NULL; | 107 | char *authpasswd = NULL; |
108 | char *privpasswd = NULL; | 108 | char *privpasswd = NULL; |
109 | char *oid; | 109 | char **oids = NULL; |
110 | char *label; | 110 | char *label; |
111 | char *units; | 111 | char *units; |
112 | char *port; | 112 | char *port; |
113 | char *snmpcmd; | ||
113 | char string_value[MAX_INPUT_BUFFER] = ""; | 114 | char string_value[MAX_INPUT_BUFFER] = ""; |
114 | char **labels = NULL; | 115 | char **labels = NULL; |
115 | char **unitv = NULL; | 116 | char **unitv = NULL; |
@@ -117,6 +118,8 @@ size_t nlabels = 0; | |||
117 | size_t labels_size = 8; | 118 | size_t labels_size = 8; |
118 | size_t nunits = 0; | 119 | size_t nunits = 0; |
119 | size_t unitv_size = 8; | 120 | size_t unitv_size = 8; |
121 | int numoids = 0; | ||
122 | int numauthpriv = 0; | ||
120 | int verbose = FALSE; | 123 | int verbose = FALSE; |
121 | int usesnmpgetnext = FALSE; | 124 | int usesnmpgetnext = FALSE; |
122 | unsigned long long lower_warn_lim[MAX_OIDS]; | 125 | unsigned long long lower_warn_lim[MAX_OIDS]; |
@@ -139,18 +142,16 @@ main (int argc, char **argv) | |||
139 | { | 142 | { |
140 | int i = 0; | 143 | int i = 0; |
141 | int iresult = STATE_UNKNOWN; | 144 | int iresult = STATE_UNKNOWN; |
142 | int found = 0; | 145 | int result = STATE_UNKNOWN; |
143 | int result = STATE_DEPENDENT; | 146 | char **command_line = NULL; |
144 | char input_buffer[MAX_INPUT_BUFFER]; | ||
145 | char *command_line = NULL; | ||
146 | char *cl_hidden_auth = NULL; | 147 | char *cl_hidden_auth = NULL; |
148 | char *oidname = NULL; | ||
147 | char *response = NULL; | 149 | char *response = NULL; |
148 | char *outbuff; | 150 | char *outbuff; |
149 | char *output; | ||
150 | char *ptr = NULL; | 151 | char *ptr = NULL; |
151 | char *p2 = NULL; | ||
152 | char *show = NULL; | 152 | char *show = NULL; |
153 | char type[8] = ""; | 153 | char type[8] = ""; |
154 | output chld_out, chld_err; | ||
154 | 155 | ||
155 | setlocale (LC_ALL, ""); | 156 | setlocale (LC_ALL, ""); |
156 | bindtextdomain (PACKAGE, LOCALEDIR); | 157 | bindtextdomain (PACKAGE, LOCALEDIR); |
@@ -162,12 +163,10 @@ main (int argc, char **argv) | |||
162 | eval_method[i] = CHECK_UNDEF; | 163 | eval_method[i] = CHECK_UNDEF; |
163 | i = 0; | 164 | i = 0; |
164 | 165 | ||
165 | oid = strdup (""); | ||
166 | label = strdup ("SNMP"); | 166 | label = strdup ("SNMP"); |
167 | units = strdup (""); | 167 | units = strdup (""); |
168 | port = strdup (DEFAULT_PORT); | 168 | port = strdup (DEFAULT_PORT); |
169 | outbuff = strdup (""); | 169 | outbuff = strdup (""); |
170 | output = strdup (""); | ||
171 | delimiter = strdup (" = "); | 170 | delimiter = strdup (" = "); |
172 | output_delim = strdup (DEFAULT_OUTPUT_DELIMITER); | 171 | output_delim = strdup (DEFAULT_OUTPUT_DELIMITER); |
173 | /* miblist = strdup (DEFAULT_MIBLIST); */ | 172 | /* miblist = strdup (DEFAULT_MIBLIST); */ |
@@ -180,91 +179,73 @@ main (int argc, char **argv) | |||
180 | if (process_arguments (argc, argv) == ERROR) | 179 | if (process_arguments (argc, argv) == ERROR) |
181 | usage4 (_("Could not parse arguments")); | 180 | usage4 (_("Could not parse arguments")); |
182 | 181 | ||
183 | /* create the command line to execute */ | 182 | /* Create the command array to execute */ |
184 | if(usesnmpgetnext == TRUE) { | 183 | if(usesnmpgetnext == TRUE) { |
185 | asprintf(&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s", | 184 | snmpcmd = strdup (PATH_TO_SNMPGETNEXT); |
186 | PATH_TO_SNMPGETNEXT, timeout_interval, retries, miblist, proto, | ||
187 | authpriv, server_address, port, oid); | ||
188 | asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s %s", | ||
189 | PATH_TO_SNMPGETNEXT, timeout_interval, retries, miblist, proto, | ||
190 | "[authpriv]", server_address, port, oid); | ||
191 | }else{ | 185 | }else{ |
192 | 186 | snmpcmd = strdup (PATH_TO_SNMPGET); | |
193 | asprintf (&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s", | 187 | } |
194 | PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto, | 188 | |
195 | authpriv, server_address, port, oid); | 189 | /* 9 arguments to pass before authpriv options + 1 for host and numoids. Add one for terminating NULL */ |
196 | asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s %s", | 190 | command_line = calloc (9 + numauthpriv + 1 + numoids + 1, sizeof (char *)); |
197 | PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto, | 191 | command_line[0] = snmpcmd; |
198 | "[authpriv]", server_address, port, oid); | 192 | command_line[1] = strdup ("-t"); |
193 | asprintf (&command_line[2], "%d", timeout_interval); | ||
194 | command_line[3] = strdup ("-r"); | ||
195 | asprintf (&command_line[4], "%d", retries); | ||
196 | command_line[5] = strdup ("-m"); | ||
197 | command_line[6] = strdup (miblist); | ||
198 | command_line[7] = "-v"; | ||
199 | command_line[8] = strdup (proto); | ||
200 | |||
201 | for (i = 0; i < numauthpriv; i++) { | ||
202 | command_line[9 + i] = authpriv[i]; | ||
199 | } | 203 | } |
200 | 204 | ||
201 | if (verbose) | 205 | asprintf (&command_line[9 + numauthpriv], "%s:%s", server_address, port); |
202 | printf ("%s\n", command_line); | ||
203 | |||
204 | 206 | ||
205 | /* run the command */ | 207 | /* This is just for display purposes, so it can remain a string */ |
206 | child_process = spopen (command_line); | 208 | asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s", |
207 | if (child_process == NULL) { | 209 | snmpcmd, timeout_interval, retries, miblist, proto, "[authpriv]", |
208 | printf (_("Could not open pipe: %s\n"), cl_hidden_auth); | 210 | server_address, port); |
209 | exit (STATE_UNKNOWN); | ||
210 | } | ||
211 | 211 | ||
212 | #if 0 /* Removed May 29, 2007 */ | 212 | for (i = 0; i < numoids; i++) { |
213 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | 213 | command_line[9 + numauthpriv + 1 + i] = oids[i]; |
214 | if (child_stderr == NULL) { | 214 | asprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]); |
215 | printf (_("Could not open stderr for %s\n"), cl_hidden_auth); | ||
216 | } | 215 | } |
217 | #endif | ||
218 | 216 | ||
219 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) | 217 | command_line[9 + numauthpriv + 1 + numoids] = NULL; |
220 | asprintf (&output, "%s%s", output, input_buffer); | ||
221 | 218 | ||
222 | if (verbose) | 219 | if (verbose) |
223 | printf ("%s\n", output); | 220 | printf ("%s\n", cl_hidden_auth); |
224 | 221 | ||
225 | ptr = output; | 222 | /* Run the command */ |
223 | result = cmd_run_array (command_line, &chld_out, &chld_err, 0); | ||
226 | 224 | ||
227 | strncat(perfstr, "| ", sizeof(perfstr)-strlen(perfstr)-1); | 225 | if (chld_err.lines > 0) { |
228 | while (ptr) { | 226 | printf (_("External command error: %s\n"), chld_err.line[0]); |
229 | char *foo, *ptr2; | 227 | for (i = 1; i < chld_err.lines; i++) { |
230 | unsigned int copylen; | 228 | printf ("%s\n", chld_err.line[i]); |
231 | 229 | } | |
232 | foo = strstr (ptr, delimiter); | 230 | exit (STATE_UNKNOWN); |
233 | copylen = foo-ptr; | 231 | } |
234 | if (copylen > sizeof(perfstr)-strlen(perfstr)-1) | ||
235 | copylen = sizeof(perfstr)-strlen(perfstr)-1; | ||
236 | ptr2 = ptr; | ||
237 | ptr = foo; | ||
238 | |||
239 | if (ptr == NULL) | ||
240 | break; | ||
241 | |||
242 | ptr += strlen (delimiter); | ||
243 | ptr += strspn (ptr, " "); | ||
244 | 232 | ||
245 | found++; | 233 | /* Return UNKNOWN or worse if no output is returned */ |
234 | if (chld_out.lines == 0) | ||
235 | die (max_state_alt (result, STATE_UNKNOWN), _("%s problem - No data received from host\nCMD: %s\n"), | ||
236 | label, | ||
237 | cl_hidden_auth); | ||
246 | 238 | ||
247 | if (ptr[0] == '"') { | 239 | if (verbose) { |
248 | ptr++; | 240 | for (i = 0; i < chld_out.lines; i++) { |
249 | response = strpcpy (response, ptr, "\""); | 241 | printf ("%s\n", chld_out.line[i]); |
250 | ptr = strpbrk (ptr, "\""); | ||
251 | ptr += strspn (ptr, "\"\n"); | ||
252 | } | ||
253 | else { | ||
254 | response = strpcpy (response, ptr, "\n"); | ||
255 | ptr = strpbrk (ptr, "\n"); | ||
256 | ptr += strspn (ptr, "\n"); | ||
257 | while | ||
258 | (strstr (ptr, delimiter) && | ||
259 | strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) { | ||
260 | response = strpcat (response, ptr, "\n"); | ||
261 | ptr = strpbrk (ptr, "\n"); | ||
262 | } | ||
263 | if (ptr && strstr (ptr, delimiter) == NULL) { | ||
264 | asprintf (&response, "%s%s", response, ptr); | ||
265 | ptr = NULL; | ||
266 | } | ||
267 | } | 242 | } |
243 | } | ||
244 | |||
245 | for (i = 0; i < chld_out.lines; i++) { | ||
246 | ptr = chld_out.line[i]; | ||
247 | oidname = strpcpy (oidname, ptr, delimiter); | ||
248 | response = strstr (ptr, delimiter); | ||
268 | 249 | ||
269 | /* We strip out the datatype indicator for PHBs */ | 250 | /* We strip out the datatype indicator for PHBs */ |
270 | 251 | ||
@@ -289,7 +270,6 @@ main (int argc, char **argv) | |||
289 | show = strstr (response, "STRING: ") + 8; | 270 | show = strstr (response, "STRING: ") + 8; |
290 | else | 271 | else |
291 | show = response; | 272 | show = response; |
292 | p2 = show; | ||
293 | 273 | ||
294 | iresult = STATE_DEPENDENT; | 274 | iresult = STATE_DEPENDENT; |
295 | 275 | ||
@@ -306,10 +286,10 @@ main (int argc, char **argv) | |||
306 | eval_method[i] & WARN_LE || | 286 | eval_method[i] & WARN_LE || |
307 | eval_method[i] & WARN_EQ || | 287 | eval_method[i] & WARN_EQ || |
308 | eval_method[i] & WARN_NE) { | 288 | eval_method[i] & WARN_NE) { |
309 | p2 = strpbrk (p2, "0123456789"); | 289 | ptr = strpbrk (show, "0123456789"); |
310 | if (p2 == NULL) | 290 | if (ptr == NULL) |
311 | die (STATE_UNKNOWN,_("No valid data returned")); | 291 | die (STATE_UNKNOWN,_("No valid data returned")); |
312 | response_value[i] = strtoul (p2, NULL, 10); | 292 | response_value[i] = strtoul (ptr, NULL, 10); |
313 | iresult = check_num (i); | 293 | iresult = check_num (i); |
314 | asprintf (&show, "%llu", response_value[i]); | 294 | asprintf (&show, "%llu", response_value[i]); |
315 | } | 295 | } |
@@ -364,10 +344,8 @@ main (int argc, char **argv) | |||
364 | if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL) | 344 | if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL) |
365 | asprintf (&outbuff, "%s %s", outbuff, unitv[i]); | 345 | asprintf (&outbuff, "%s %s", outbuff, unitv[i]); |
366 | 346 | ||
367 | i++; | ||
368 | |||
369 | if (is_numeric(show)) { | 347 | if (is_numeric(show)) { |
370 | strncat(perfstr, ptr2, copylen); | 348 | strncat(perfstr, oidname, sizeof(perfstr)-strlen(perfstr)-1); |
371 | strncat(perfstr, "=", sizeof(perfstr)-strlen(perfstr)-1); | 349 | strncat(perfstr, "=", sizeof(perfstr)-strlen(perfstr)-1); |
372 | strncat(perfstr, show, sizeof(perfstr)-strlen(perfstr)-1); | 350 | strncat(perfstr, show, sizeof(perfstr)-strlen(perfstr)-1); |
373 | 351 | ||
@@ -375,29 +353,6 @@ main (int argc, char **argv) | |||
375 | strncat(perfstr, type, sizeof(perfstr)-strlen(perfstr)-1); | 353 | strncat(perfstr, type, sizeof(perfstr)-strlen(perfstr)-1); |
376 | strncat(perfstr, " ", sizeof(perfstr)-strlen(perfstr)-1); | 354 | strncat(perfstr, " ", sizeof(perfstr)-strlen(perfstr)-1); |
377 | } | 355 | } |
378 | |||
379 | } /* end while (ptr) */ | ||
380 | |||
381 | if (found == 0) | ||
382 | die (STATE_UNKNOWN, | ||
383 | _("%s problem - No data received from host\nCMD: %s\n"), | ||
384 | label, | ||
385 | cl_hidden_auth); | ||
386 | |||
387 | #if 0 /* Removed May 29, 2007 */ | ||
388 | /* WARNING if output found on stderr */ | ||
389 | if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
390 | result = max_state (result, STATE_WARNING); | ||
391 | |||
392 | /* close stderr */ | ||
393 | (void) fclose (child_stderr); | ||
394 | #endif | ||
395 | |||
396 | /* close the pipe */ | ||
397 | if (spclose (child_process)) { | ||
398 | if (result == STATE_OK) | ||
399 | result = STATE_UNKNOWN; | ||
400 | asprintf (&outbuff, "%s (%s)", outbuff, _("snmpget returned an error status")); | ||
401 | } | 356 | } |
402 | 357 | ||
403 | /* if (nunits == 1 || i == 1) */ | 358 | /* if (nunits == 1 || i == 1) */ |
@@ -563,12 +518,12 @@ process_arguments (int argc, char **argv) | |||
563 | */ | 518 | */ |
564 | needmibs = TRUE; | 519 | needmibs = TRUE; |
565 | } | 520 | } |
566 | 521 | oids = calloc(MAX_OIDS, sizeof (char *)); | |
567 | for (ptr = optarg; (ptr = index (ptr, ',')); ptr++) | 522 | for (ptr = strtok(optarg, ", "); ptr != NULL; ptr = strtok(NULL, ", ")) { |
568 | ptr[0] = ' '; /* relpace comma with space */ | 523 | oids[j] = strdup(ptr); |
569 | for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++) | 524 | j++; |
570 | j++; /* count OIDs */ | 525 | } |
571 | asprintf (&oid, "%s %s", (oid?oid:""), optarg); | 526 | numoids = j; |
572 | if (c == 'E' || c == 'e') { | 527 | if (c == 'E' || c == 'e') { |
573 | jj++; | 528 | jj++; |
574 | ii++; | 529 | ii++; |
@@ -675,8 +630,6 @@ process_arguments (int argc, char **argv) | |||
675 | if (community == NULL) | 630 | if (community == NULL) |
676 | community = strdup (DEFAULT_COMMUNITY); | 631 | community = strdup (DEFAULT_COMMUNITY); |
677 | 632 | ||
678 | |||
679 | |||
680 | return validate_arguments (); | 633 | return validate_arguments (); |
681 | } | 634 | } |
682 | 635 | ||
@@ -713,47 +666,79 @@ validate_arguments () | |||
713 | } | 666 | } |
714 | } | 667 | } |
715 | 668 | ||
669 | /* Check server_address is given */ | ||
670 | if (server_address == NULL) | ||
671 | die(STATE_UNKNOWN, _("No host specified\n")); | ||
716 | 672 | ||
717 | /* Need better checks to verify seclevel and authproto choices */ | 673 | /* Check oid is given */ |
718 | 674 | if (numoids == 0) | |
719 | if (seclevel == NULL) | 675 | die(STATE_UNKNOWN, _("No OIDs specified\n")); |
720 | asprintf (&seclevel, "noAuthNoPriv"); | ||
721 | |||
722 | |||
723 | if (authproto == NULL ) | ||
724 | asprintf(&authproto, DEFAULT_AUTH_PROTOCOL); | ||
725 | |||
726 | if (privproto == NULL ) | ||
727 | asprintf(&privproto, DEFAULT_PRIV_PROTOCOL); | ||
728 | 676 | ||
729 | if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) { /* default protocol version */ | 677 | if (proto == NULL) |
730 | asprintf(&proto, DEFAULT_PROTOCOL); | 678 | asprintf(&proto, DEFAULT_PROTOCOL); |
731 | asprintf(&authpriv, "%s%s", "-c ", community); | 679 | |
732 | } | 680 | if ((strcmp(proto,"1") == 0) || (strcmp(proto, "2c")==0)) { /* snmpv1 or snmpv2c */ |
733 | else if ( strcmp (proto, "2c") == 0 ) { /* snmpv2c args */ | 681 | numauthpriv = 2; |
734 | asprintf(&authpriv, "%s%s", "-c ", community); | 682 | authpriv = calloc (numauthpriv, sizeof (char *)); |
683 | authpriv[0] = strdup ("-c"); | ||
684 | authpriv[1] = strdup (community); | ||
735 | } | 685 | } |
736 | else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */ | 686 | else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */ |
737 | asprintf(&proto, "%s", "3"); | 687 | if (seclevel == NULL) |
738 | 688 | asprintf(&seclevel, "noAuthNoPriv"); | |
739 | if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) { | 689 | |
740 | asprintf(&authpriv, "%s", "-l noAuthNoPriv" ); | 690 | if (strcmp(seclevel, "noAuthNoPriv") == 0) { |
741 | } | 691 | numauthpriv = 2; |
742 | else if ( strcmp(seclevel, "authNoPriv") == 0 ) { | 692 | authpriv = calloc (numauthpriv, sizeof (char *)); |
743 | if ( secname == NULL || authpasswd == NULL) { | 693 | authpriv[0] = strdup ("-l"); |
744 | printf (_("Missing secname (%s) or authpassword (%s) ! \n"),secname, authpasswd ); | 694 | authpriv[1] = strdup ("noAuthNoPriv"); |
745 | print_usage (); | 695 | } else { |
746 | exit (STATE_UNKNOWN); | 696 | if (! ( (strcmp(seclevel, "authNoPriv")==0) || (strcmp(seclevel, "authPriv")==0) ) ) { |
697 | usage2 (_("Invalid seclevel"), seclevel); | ||
747 | } | 698 | } |
748 | asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd); | 699 | |
749 | } | 700 | if (authproto == NULL ) |
750 | else if ( strcmp(seclevel, "authPriv") == 0 ) { | 701 | asprintf(&authproto, DEFAULT_AUTH_PROTOCOL); |
751 | if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) { | 702 | |
752 | printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd ); | 703 | if (secname == NULL) |
753 | print_usage (); | 704 | die(STATE_UNKNOWN, _("Required parameter: %s\n"), "secname"); |
754 | exit (STATE_UNKNOWN); | 705 | |
706 | if (authpasswd == NULL) | ||
707 | die(STATE_UNKNOWN, _("Required parameter: %s\n"), "authpasswd"); | ||
708 | |||
709 | if ( strcmp(seclevel, "authNoPriv") == 0 ) { | ||
710 | numauthpriv = 8; | ||
711 | authpriv = calloc (numauthpriv, sizeof (char *)); | ||
712 | authpriv[0] = strdup ("-l"); | ||
713 | authpriv[1] = strdup ("authNoPriv"); | ||
714 | authpriv[2] = strdup ("-a"); | ||
715 | authpriv[3] = strdup (authproto); | ||
716 | authpriv[4] = strdup ("-u"); | ||
717 | authpriv[5] = strdup (secname); | ||
718 | authpriv[6] = strdup ("-A"); | ||
719 | authpriv[7] = strdup (authpasswd); | ||
720 | } else if ( strcmp(seclevel, "authPriv") == 0 ) { | ||
721 | if (privproto == NULL ) | ||
722 | asprintf(&privproto, DEFAULT_PRIV_PROTOCOL); | ||
723 | |||
724 | if (privpasswd == NULL) | ||
725 | die(STATE_UNKNOWN, _("Required parameter: %s\n"), "privpasswd"); | ||
726 | |||
727 | numauthpriv = 12; | ||
728 | authpriv = calloc (numauthpriv, sizeof (char *)); | ||
729 | authpriv[0] = strdup ("-l"); | ||
730 | authpriv[1] = strdup ("authPriv"); | ||
731 | authpriv[2] = strdup ("-a"); | ||
732 | authpriv[3] = strdup (authproto); | ||
733 | authpriv[4] = strdup ("-u"); | ||
734 | authpriv[5] = strdup (secname); | ||
735 | authpriv[6] = strdup ("-A"); | ||
736 | authpriv[7] = strdup (authpasswd); | ||
737 | authpriv[8] = strdup ("-x"); | ||
738 | authpriv[9] = strdup (privproto); | ||
739 | authpriv[10] = strdup ("-X"); | ||
740 | authpriv[11] = strdup (privpasswd); | ||
755 | } | 741 | } |
756 | asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x %s -X %s ", authproto, secname, authpasswd, privproto, privpasswd); | ||
757 | } | 742 | } |
758 | 743 | ||
759 | } | 744 | } |
diff --git a/plugins/t/check_snmp.t b/plugins/t/check_snmp.t index 369314e..4820aac 100644 --- a/plugins/t/check_snmp.t +++ b/plugins/t/check_snmp.t | |||
@@ -8,9 +8,8 @@ use strict; | |||
8 | use Test::More; | 8 | use Test::More; |
9 | use NPTest; | 9 | use NPTest; |
10 | 10 | ||
11 | use vars qw($tests); | 11 | my $tests = 32; |
12 | BEGIN {$tests = 14; plan tests => $tests} | 12 | plan tests => $tests; |
13 | |||
14 | my $res; | 13 | my $res; |
15 | 14 | ||
16 | SKIP: { | 15 | SKIP: { |
@@ -20,7 +19,7 @@ SKIP: { | |||
20 | "A host providing an SNMP Service"); | 19 | "A host providing an SNMP Service"); |
21 | 20 | ||
22 | my $snmp_community = getTestParameter( "snmp_community", "NP_SNMP_COMMUNITY", "public", | 21 | my $snmp_community = getTestParameter( "snmp_community", "NP_SNMP_COMMUNITY", "public", |
23 | "The SNMP Community string for SNMP Testing" ); | 22 | "The SNMP Community string for SNMP Testing (assumes snmp v1)" ); |
24 | 23 | ||
25 | my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", | 24 | my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRESPONSIVE", "10.0.0.1", |
26 | "The hostname of system not responsive to network requests" ); | 25 | "The hostname of system not responsive to network requests" ); |
@@ -28,12 +27,32 @@ SKIP: { | |||
28 | my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", | 27 | my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", |
29 | "An invalid (not known to DNS) hostname" ); | 28 | "An invalid (not known to DNS) hostname" ); |
30 | 29 | ||
30 | $res = NPTest->testCmd( "./check_snmp -t 1" ); | ||
31 | is( $res->return_code, 3, "No host name" ); | ||
32 | is( $res->output, "No host specified" ); | ||
33 | |||
34 | $res = NPTest->testCmd( "./check_snmp -H fakehostname" ); | ||
35 | is( $res->return_code, 3, "No OIDs specified" ); | ||
36 | is( $res->output, "No OIDs specified" ); | ||
37 | |||
38 | $res = NPTest->testCmd( "./check_snmp -H fakehost -o oids -P 3 --seclevel=rubbish" ); | ||
39 | is( $res->return_code, 3, "Invalid seclevel" ); | ||
40 | like( $res->output, "/check_snmp: Invalid seclevel - rubbish/" ); | ||
41 | |||
42 | $res = NPTest->testCmd( "./check_snmp -H fakehost -o oids -P 3c" ); | ||
43 | is( $res->return_code, 3, "Invalid protocol" ); | ||
44 | like( $res->output, "/check_snmp: Invalid SNMP version - 3c/" ); | ||
45 | |||
31 | SKIP: { | 46 | SKIP: { |
32 | skip "no snmp host defined", 10 if ( ! $host_snmp ); | 47 | skip "no snmp host defined", 20 if ( ! $host_snmp ); |
33 | 48 | ||
34 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:"); | 49 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:"); |
35 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying uptime" ); | 50 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying uptime" ); |
36 | like($res->output, '/^SNMP OK - \d+/', "String contains SNMP OK"); | 51 | like($res->output, '/^SNMP OK - (\d+)/', "String contains SNMP OK"); |
52 | $res->output =~ /^SNMP OK - (\d+)/; | ||
53 | my $value = $1; | ||
54 | cmp_ok( $value, ">", 0, "Got a time value" ); | ||
55 | like($res->perf_output, "/sysUpTime.*$1/", "Got perfdata with value '$1' in it"); | ||
37 | 56 | ||
38 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysDescr.0"); | 57 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o system.sysDescr.0"); |
39 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying sysDescr" ); | 58 | cmp_ok( $res->return_code, '==', 0, "Exit OK when querying sysDescr" ); |
@@ -49,21 +68,33 @@ SKIP: { | |||
49 | 68 | ||
50 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w :0 -c 0"); | 69 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunIndex.1 -w :0 -c 0"); |
51 | cmp_ok( $res->return_code, '==', 2, "Exit CRITICAL when querying hrSWRunIndex.1 and crit-th doesn't apply" ); | 70 | cmp_ok( $res->return_code, '==', 2, "Exit CRITICAL when querying hrSWRunIndex.1 and crit-th doesn't apply" ); |
52 | like($res->output, '/^SNMP CRITICAL - \*1\*\s.*$/', "String matches SNMP CRITICAL and ouput format"); | 71 | like($res->output, '/^SNMP CRITICAL - \*1\*\s.*$/', "String matches SNMP CRITICAL and output format"); |
72 | |||
73 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o ifIndex.2,ifIndex.1 -w 1:2 -c 1:2"); | ||
74 | cmp_ok( $res->return_code, '==', 0, "Checking two OIDs at once" ); | ||
75 | like($res->output, "/^SNMP OK - 2 1/", "Got two values back" ); | ||
76 | like( $res->perf_output, "/ifIndex.2=2/", "Got 1st perf data" ); | ||
77 | like( $res->perf_output, "/ifIndex.1=1/", "Got 2nd perf data" ); | ||
78 | |||
79 | $res = NPTest->testCmd( "./check_snmp -H $host_snmp -C $snmp_community -o ifIndex.2,ifIndex.1 -w 1:2,1:2 -c 2:2,2:2"); | ||
80 | cmp_ok( $res->return_code, '==', 2, "Checking critical threshold is passed if any one value crosses" ); | ||
81 | like($res->output, "/^SNMP CRITICAL - 2 *1*/", "Got two values back" ); | ||
82 | like( $res->perf_output, "/ifIndex.2=2/", "Got 1st perf data" ); | ||
83 | like( $res->perf_output, "/ifIndex.1=1/", "Got 2nd perf data" ); | ||
53 | } | 84 | } |
54 | 85 | ||
55 | SKIP: { | 86 | SKIP: { |
56 | skip "no non responsive host defined", 2 if ( ! $host_nonresponsive ); | 87 | skip "no non responsive host defined", 2 if ( ! $host_nonresponsive ); |
57 | $res = NPTest->testCmd( "./check_snmp -H $host_nonresponsive -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:"); | 88 | $res = NPTest->testCmd( "./check_snmp -H $host_nonresponsive -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:"); |
58 | cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with non responsive host" ); | 89 | cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with non responsive host" ); |
59 | like($res->output, '/SNMP problem - /', "String matches SNMP Problem"); | 90 | like($res->output, '/External command error: Timeout: No Response from /', "String matches timeout problem"); |
60 | } | 91 | } |
61 | 92 | ||
62 | SKIP: { | 93 | SKIP: { |
63 | skip "no non invalid host defined", 2 if ( ! $hostname_invalid ); | 94 | skip "no non invalid host defined", 2 if ( ! $hostname_invalid ); |
64 | $res = NPTest->testCmd( "./check_snmp -H $hostname_invalid -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:"); | 95 | $res = NPTest->testCmd( "./check_snmp -H $hostname_invalid -C $snmp_community -o system.sysUpTime.0 -w 1: -c 1:"); |
65 | cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with non responsive host" ); | 96 | cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with non responsive host" ); |
66 | like($res->output, '/SNMP problem - /', "String matches SNMP Problem"); | 97 | like($res->output, '/External command error: .*nosuchhost/', "String matches invalid host"); |
67 | } | 98 | } |
68 | 99 | ||
69 | } | 100 | } |