summaryrefslogtreecommitdiffstats
path: root/web/attachments/317729-check_snmp.patch
blob: 63ae35f1bcc2f9f888e8123b83ac7e40775e1318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
Index: plugins/check_snmp.c
===================================================================
--- plugins/check_snmp.c	(revision 2160)
+++ plugins/check_snmp.c	(working copy)
@@ -34,7 +34,7 @@
 
 #include "common.h"
 #include "utils.h"
-#include "popen.h"
+#include "utils_cmd.h"
 
 #define DEFAULT_COMMUNITY "public"
 #define DEFAULT_PORT "161"
@@ -91,14 +91,14 @@
 regmatch_t pmatch[10];
 char timestamp[10] = "";
 char errbuf[MAX_INPUT_BUFFER] = "";
-char perfstr[MAX_INPUT_BUFFER] = "";
+char perfstr[MAX_INPUT_BUFFER] = "| ";
 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
 int eflags = 0;
 int errcode, excode;
 
 char *server_address = NULL;
 char *community = NULL;
-char *authpriv = NULL;
+char **authpriv = NULL;
 char *proto = NULL;
 char *seclevel = NULL;
 char *secname = NULL;
@@ -106,10 +106,11 @@
 char *privproto = NULL;
 char *authpasswd = NULL;
 char *privpasswd = NULL;
-char *oid;
+char **oids = NULL; 
 char *label;
 char *units;
 char *port;
+char *snmpcmd;
 char string_value[MAX_INPUT_BUFFER] = "";
 char **labels = NULL;
 char **unitv = NULL;
@@ -117,6 +118,8 @@
 size_t labels_size = 8;
 size_t nunits = 0;
 size_t unitv_size = 8;
+int numoids = 0;
+int numauthpriv = 0;
 int verbose = FALSE;
 int usesnmpgetnext = FALSE;
 unsigned long long lower_warn_lim[MAX_OIDS];
@@ -139,18 +142,16 @@
 {
 	int i = 0;
 	int iresult = STATE_UNKNOWN;
-	int found = 0;
-	int result = STATE_DEPENDENT;
-	char input_buffer[MAX_INPUT_BUFFER];
-	char *command_line = NULL;
+	int result = STATE_UNKNOWN;
+	char **command_line = NULL;
 	char *cl_hidden_auth = NULL;
+	char *oidname = NULL;
 	char *response = NULL;
 	char *outbuff;
-	char *output;
 	char *ptr = NULL;
-	char *p2 = NULL;
 	char *show = NULL;
 	char type[8] = "";
+	output chld_out, chld_err;
 
 	setlocale (LC_ALL, "");
 	bindtextdomain (PACKAGE, LOCALEDIR);
@@ -162,12 +163,10 @@
 		eval_method[i] = CHECK_UNDEF;
 	i = 0;
 
-	oid = strdup ("");
 	label = strdup ("SNMP");
 	units = strdup ("");
 	port = strdup (DEFAULT_PORT);
 	outbuff = strdup ("");
-	output = strdup ("");
 	delimiter = strdup (" = ");
 	output_delim = strdup (DEFAULT_OUTPUT_DELIMITER);
 	/* miblist = strdup (DEFAULT_MIBLIST); */
@@ -180,92 +179,71 @@
 	if (process_arguments (argc, argv) == ERROR)
 		usage4 (_("Could not parse arguments"));
 
-	/* create the command line to execute */
-		if(usesnmpgetnext == TRUE) {
-		asprintf(&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
-			PATH_TO_SNMPGETNEXT, timeout_interval, retries, miblist, proto,
-			authpriv, server_address, port, oid);
-		asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
-			PATH_TO_SNMPGETNEXT, timeout_interval, retries, miblist, proto,
-			"[authpriv]", server_address, port, oid);
+	/* Create the command array to execute */
+	if(usesnmpgetnext == TRUE) {
+		snmpcmd = strdup (PATH_TO_SNMPGETNEXT);
 	}else{
+		snmpcmd = strdup (PATH_TO_SNMPGET);
+	} 
+	
+        /* 10 is the number of arguments to pass before authpriv options and OIDs. Add one for terminating NULL */
+	command_line = calloc (10 + numauthpriv + numoids + 1, sizeof (char *));
+	command_line[0] = snmpcmd;
+	command_line[1] = strdup ("-t");
+	asprintf (&command_line[2], "%d", timeout_interval);
+	command_line[3] = strdup ("-r");
+	asprintf (&command_line[4], "%d", retries);
+	command_line[5] = strdup ("-m");
+	command_line[6] = strdup (miblist);
+	command_line[7] = "-v";
+	command_line[8] = strdup (proto);
+	asprintf (&command_line[9], "%s:%s", server_address, port);
 
-		asprintf (&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
-			PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto,
-			authpriv, server_address, port, oid);
-		asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
-			PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto,
-			"[authpriv]", server_address, port, oid);
+	for (i = 0; i < numauthpriv; i++) {
+		command_line[10 + numoids + i] = authpriv[i];
 	}
 
-	if (verbose)
-		printf ("%s\n", command_line);
+        /* This is just for display purposes, so it can remain a string */
+	asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s:%s %s",
+		snmpcmd, timeout_interval, retries, miblist, proto,
+		server_address, port, "[authpriv]");
 
-
-	/* run the command */
-	child_process = spopen (command_line);
-	if (child_process == NULL) {
-		printf (_("Could not open pipe: %s\n"), cl_hidden_auth);
-		exit (STATE_UNKNOWN);
+	for (i = 0; i < numoids; i++) {
+		command_line[10 + i] = oids[i];
+		asprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]);	
 	}
 
-#if 0		/* Removed May 29, 2007 */
-	child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
-	if (child_stderr == NULL) {
-		printf (_("Could not open stderr for %s\n"), cl_hidden_auth);
-	}
-#endif
-
-	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
-		asprintf (&output, "%s%s", output, input_buffer);
-
 	if (verbose)
-		printf ("%s\n", output);
+		printf ("%s\n", cl_hidden_auth);
 
-	ptr = output;
+	/* Run the command */
+	result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
 
-	strncat(perfstr, "| ", sizeof(perfstr)-strlen(perfstr)-1);
-	while (ptr) {
-		char *foo, *ptr2;
-		unsigned int copylen;
+	if (chld_err.lines > 0) {
+		printf ("%s problem - error output from command\nCMD: %s\n", label, cl_hidden_auth);
+		for (i = 0; i < chld_err.lines; i++) {
+			printf ("%s\n", chld_err.line[i]);
+		}
+		exit (STATE_WARNING);
+	}
 
-		foo = strstr (ptr, delimiter);
-		copylen = foo-ptr;
-		if (copylen > sizeof(perfstr)-strlen(perfstr)-1)
-			copylen = sizeof(perfstr)-strlen(perfstr)-1;
-		ptr2 = ptr;
-		ptr = foo;
+	/* Return UNKNOWN or worse if no output is returned */
+	if (chld_out.lines == 0)
+		die (max_state_alt (result, STATE_UNKNOWN), _("%s problem - No data received from host\nCMD: %s\n"),
+								label,
+								cl_hidden_auth);
 
-		if (ptr == NULL)
-			break;
-
-		ptr += strlen (delimiter);
-		ptr += strspn (ptr, " ");
-
-		found++;
-
-		if (ptr[0] == '"') {
-			ptr++;
-			response = strpcpy (response, ptr, "\"");
-			ptr = strpbrk (ptr, "\"");
-			ptr += strspn (ptr, "\"\n");
+	if (verbose) {
+		for (i = 0; i < chld_out.lines; i++) {
+			printf ("%s\n", chld_out.line[i]);
 		}
-		else {
-			response = strpcpy (response, ptr, "\n");
-			ptr = strpbrk (ptr, "\n");
-			ptr += strspn (ptr, "\n");
-			while
-				(strstr (ptr, delimiter) &&
-				 strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
-				response = strpcat (response, ptr, "\n");
-				ptr = strpbrk (ptr, "\n");
-			}
-			if (ptr && strstr (ptr, delimiter) == NULL) {
-				asprintf (&response, "%s%s", response, ptr);
-				ptr = NULL;
-			}
-		}
+	}
 
+	for (i = 0; i < chld_out.lines; i++) {
+		ptr = chld_out.line[i];
+		oidname = strpcpy (oidname, ptr, delimiter); 
+		response = strstr (ptr, delimiter);
+
 		/* We strip out the datatype indicator for PHBs */
 
 		/* Clean up type array - Sol10 does not necessarily zero it out */
@@ -289,7 +267,6 @@
 			show = strstr (response, "STRING: ") + 8;
 		else
 			show = response;
-		p2 = show;
 
 		iresult = STATE_DEPENDENT;
 
@@ -306,10 +283,10 @@
 		    eval_method[i] & WARN_LE ||
 		    eval_method[i] & WARN_EQ ||
 		    eval_method[i] & WARN_NE) {
-			p2 = strpbrk (p2, "0123456789");
-			if (p2 == NULL)
+			ptr = strpbrk (show, "0123456789");
+			if (ptr == NULL)
 				die (STATE_UNKNOWN,_("No valid data returned"));
-			response_value[i] = strtoul (p2, NULL, 10);
+			response_value[i] = strtoul (ptr, NULL, 10);
 			iresult = check_num (i);
 			asprintf (&show, "%llu", response_value[i]);
 		}
@@ -364,10 +341,8 @@
 		if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL)
 			asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
 
-		i++;
-
 		if (is_numeric(show)) {
-			strncat(perfstr, ptr2, copylen);
+			strncat(perfstr, oidname, sizeof(perfstr)-strlen(perfstr)-1);
 			strncat(perfstr, "=", sizeof(perfstr)-strlen(perfstr)-1);
 			strncat(perfstr, show, sizeof(perfstr)-strlen(perfstr)-1);
 
@@ -375,29 +350,6 @@
 				strncat(perfstr, type, sizeof(perfstr)-strlen(perfstr)-1);
 			strncat(perfstr, " ", sizeof(perfstr)-strlen(perfstr)-1);
 		}
-
-	}	/* end while (ptr) */
-
-	if (found == 0)
-		die (STATE_UNKNOWN,
-			_("%s problem - No data received from host\nCMD: %s\n"),
-			label,
-			cl_hidden_auth);
-
-#if 0		/* Removed May 29, 2007 */
-	/* WARNING if output found on stderr */
-	if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
-		result = max_state (result, STATE_WARNING);
-
-	/* close stderr */
-	(void) fclose (child_stderr);
-#endif
-
-	/* close the pipe */
-	if (spclose (child_process)) {
-		if (result == STATE_OK)
-			result = STATE_UNKNOWN;
-		asprintf (&outbuff, "%s (%s)", outbuff, _("snmpget returned an error status"));
 	}
 
 /* 	if (nunits == 1 || i == 1) */
@@ -563,12 +515,12 @@
 					 */
 					needmibs = TRUE;
 			}
-
-			for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
-				ptr[0] = ' '; /* relpace comma with space */
-			for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
-				j++; /* count OIDs */
-			asprintf (&oid, "%s %s", (oid?oid:""), optarg);
+			oids = calloc(MAX_OIDS, sizeof (char *));
+			for (ptr = strtok(optarg, ", "); ptr != NULL; ptr = strtok(NULL, ", ")) {
+				oids[j] = strdup(ptr);
+				j++;
+			}
+			numoids = j;
 			if (c == 'E' || c == 'e') {
 				jj++;
 				ii++;
@@ -728,16 +680,25 @@
 
 	if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) {	/* default protocol version */
 		asprintf(&proto, DEFAULT_PROTOCOL);
-		asprintf(&authpriv, "%s%s", "-c ", community);
+		numauthpriv = 2;
+		authpriv = calloc (numauthpriv, sizeof (char *));
+		authpriv[0] = strdup ("-c");
+		authpriv[1] = strdup (community);
 	}
 	else if ( strcmp (proto, "2c") == 0 ) {		/* snmpv2c args */
-		asprintf(&authpriv, "%s%s", "-c ", community);
+		numauthpriv = 2;
+		authpriv = calloc (numauthpriv, sizeof (char *));
+		authpriv[0] = strdup ("-c");
+		authpriv[1] = strdup (community);
 	}
 	else if ( strcmp (proto, "3") == 0 ) {		/* snmpv3 args */
 		asprintf(&proto, "%s", "3");
 
 		if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
-			asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
+			numauthpriv = 2;
+			authpriv = calloc (numauthpriv, sizeof (char *));
+			authpriv[0] = strdup ("-l");
+			authpriv[1] = strdup ("noAuthNoPriv");
 		}
 		else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
 			if ( secname == NULL || authpasswd == NULL) {
@@ -745,7 +706,16 @@
 				print_usage ();
 				exit (STATE_UNKNOWN);
 			}
-			asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
+			numauthpriv = 8;
+			authpriv = calloc (numauthpriv, sizeof (char *));
+			authpriv[0] = strdup ("-l");
+			authpriv[1] = strdup ("authNoPriv");
+			authpriv[3] = strdup ("-a");
+			authpriv[4] = strdup (authproto);
+			authpriv[5] = strdup ("-u");
+			authpriv[6] = strdup (secname);
+			authpriv[7] = strdup ("-A");
+			authpriv[8] = strdup (authpasswd);
 		}
 		else if ( strcmp(seclevel, "authPriv") == 0 ) {
 			if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
@@ -753,7 +723,20 @@
 				print_usage ();
 				exit (STATE_UNKNOWN);
 			}
-			asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x %s -X %s ", authproto, secname, authpasswd, privproto, privpasswd);
+			numauthpriv = 12;
+			authpriv = calloc (numauthpriv, sizeof (char *));
+			authpriv[0] = strdup ("-l");
+			authpriv[1] = strdup ("authPriv");
+			authpriv[3] = strdup ("-a");
+			authpriv[4] = strdup (authproto);
+			authpriv[5] = strdup ("-u");
+			authpriv[6] = strdup (secname);
+			authpriv[7] = strdup ("-A");
+			authpriv[8] = strdup (authpasswd);
+			authpriv[9] = strdup ("-x");
+			authpriv[10] = strdup (privproto);
+			authpriv[11] = strdup ("-X");
+			authpriv[12] = strdup (privpasswd);
 		}
 
 	}
Index: plugins/Makefile.am
===================================================================
--- plugins/Makefile.am	(revision 2160)
+++ plugins/Makefile.am	(working copy)
@@ -93,7 +93,7 @@
 check_procs_LDADD = $(BASEOBJS)
 check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS)
 check_real_LDADD = $(NETLIBS)
-check_snmp_LDADD = $(BASEOBJS) popen.o
+check_snmp_LDADD = $(BASEOBJS)
 check_smtp_LDADD = $(SSLOBJS) $(NETLIBS) $(SSLLIBS)
 check_ssh_LDADD = $(NETLIBS)
 check_swap_LDADD = $(MATHLIBS) $(BASEOBJS) popen.o
@@ -135,7 +135,7 @@
 check_procs_DEPENDENCIES = check_procs.c $(BASEOBJS) popen.o $(DEPLIBS)
 check_radius_DEPENDENCIES = check_radius.c $(NETOBJS)  $(DEPLIBS)
 check_real_DEPENDENCIES = check_real.c $(NETOBJS) $(DEPLIBS)
-check_snmp_DEPENDENCIES = check_snmp.c $(BASEOBJS) popen.o $(DEPLIBS)
+check_snmp_DEPENDENCIES = check_snmp.c $(BASEOBJS) $(DEPLIBS)
 check_smtp_DEPENDENCIES = check_smtp.c $(SSLOBJS) $(NETOBJS) $(DEPLIBS)
 check_ssh_DEPENDENCIES = check_ssh.c $(NETOBJS) $(DEPLIBS)
 check_swap_DEPENDENCIES = check_swap.c $(BASEOBJS) popen.o $(DEPLIBS)