summaryrefslogtreecommitdiffstats
path: root/plugins/check_snmp.c
diff options
context:
space:
mode:
authorTon Voon <tonvoon@macbook.local>2009-03-14 01:17:50 (GMT)
committerTon Voon <tonvoon@macbook.local>2009-03-14 01:17:50 (GMT)
commit36e58ae0c3ad7a9d3660722b35d3ed9c97687dd2 (patch)
tree86bf7e19a500bc7d10c5bcaa6d976ffd476d0229 /plugins/check_snmp.c
parent674841e279cc1bdbcb5c84c9b26377b156aee76b (diff)
downloadmonitoring-plugins-36e58ae0c3ad7a9d3660722b35d3ed9c97687dd2.tar.gz
Fixed passing of quotes in OID for check_snmp (#1985230 - Jan Wagner, patch by John Barbuto)
Diffstat (limited to 'plugins/check_snmp.c')
-rw-r--r--plugins/check_snmp.c295
1 files changed, 140 insertions, 155 deletions
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;
91regmatch_t pmatch[10]; 91regmatch_t pmatch[10];
92char timestamp[10] = ""; 92char timestamp[10] = "";
93char errbuf[MAX_INPUT_BUFFER] = ""; 93char errbuf[MAX_INPUT_BUFFER] = "";
94char perfstr[MAX_INPUT_BUFFER] = ""; 94char perfstr[MAX_INPUT_BUFFER] = "| ";
95int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE; 95int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
96int eflags = 0; 96int eflags = 0;
97int errcode, excode; 97int errcode, excode;
98 98
99char *server_address = NULL; 99char *server_address = NULL;
100char *community = NULL; 100char *community = NULL;
101char *authpriv = NULL; 101char **authpriv = NULL;
102char *proto = NULL; 102char *proto = NULL;
103char *seclevel = NULL; 103char *seclevel = NULL;
104char *secname = NULL; 104char *secname = NULL;
@@ -106,10 +106,11 @@ char *authproto = NULL;
106char *privproto = NULL; 106char *privproto = NULL;
107char *authpasswd = NULL; 107char *authpasswd = NULL;
108char *privpasswd = NULL; 108char *privpasswd = NULL;
109char *oid; 109char **oids = NULL;
110char *label; 110char *label;
111char *units; 111char *units;
112char *port; 112char *port;
113char *snmpcmd;
113char string_value[MAX_INPUT_BUFFER] = ""; 114char string_value[MAX_INPUT_BUFFER] = "";
114char **labels = NULL; 115char **labels = NULL;
115char **unitv = NULL; 116char **unitv = NULL;
@@ -117,6 +118,8 @@ size_t nlabels = 0;
117size_t labels_size = 8; 118size_t labels_size = 8;
118size_t nunits = 0; 119size_t nunits = 0;
119size_t unitv_size = 8; 120size_t unitv_size = 8;
121int numoids = 0;
122int numauthpriv = 0;
120int verbose = FALSE; 123int verbose = FALSE;
121int usesnmpgetnext = FALSE; 124int usesnmpgetnext = FALSE;
122unsigned long long lower_warn_lim[MAX_OIDS]; 125unsigned 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 }