diff options
Diffstat (limited to 'web/attachments/137902-nagiosplug-runcmd.diff')
| -rw-r--r-- | web/attachments/137902-nagiosplug-runcmd.diff | 3012 |
1 files changed, 3012 insertions, 0 deletions
diff --git a/web/attachments/137902-nagiosplug-runcmd.diff b/web/attachments/137902-nagiosplug-runcmd.diff new file mode 100644 index 0000000..abebe78 --- /dev/null +++ b/web/attachments/137902-nagiosplug-runcmd.diff | |||
| @@ -0,0 +1,3012 @@ | |||
| 1 | diff -urN ../nplg/plugins/check_by_ssh.c ./plugins/check_by_ssh.c | ||
| 2 | --- ../nplg/plugins/check_by_ssh.c 2005-01-05 21:53:11.000000000 +0100 | ||
| 3 | +++ ./plugins/check_by_ssh.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 4 | @@ -14,19 +14,19 @@ | ||
| 5 | along with this program; if not, write to the Free Software | ||
| 6 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 7 | |||
| 8 | - $Id: check_by_ssh.c,v 1.35 2004/12/25 12:09:19 opensides Exp $ | ||
| 9 | + $Id: check_by_ssh.c,v 1.3 2005/06/06 11:37:06 exon Exp $ | ||
| 10 | |||
| 11 | *****************************************************************************/ | ||
| 12 | |||
| 13 | const char *progname = "check_by_ssh"; | ||
| 14 | -const char *revision = "$Revision: 1.35 $"; | ||
| 15 | +const char *revision = "$Revision: 1.3 $"; | ||
| 16 | const char *copyright = "2000-2004"; | ||
| 17 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 18 | |||
| 19 | #include "common.h" | ||
| 20 | #include "netutils.h" | ||
| 21 | #include "utils.h" | ||
| 22 | -#include "popen.h" | ||
| 23 | +#include "runcmd.h" | ||
| 24 | |||
| 25 | int process_arguments (int, char **); | ||
| 26 | int validate_arguments (void); | ||
| 27 | @@ -49,15 +49,15 @@ | ||
| 28 | main (int argc, char **argv) | ||
| 29 | { | ||
| 30 | |||
| 31 | - char input_buffer[MAX_INPUT_BUFFER]; | ||
| 32 | char *result_text; | ||
| 33 | char *status_text; | ||
| 34 | - char *output; | ||
| 35 | + char *msg; | ||
| 36 | char *eol = NULL; | ||
| 37 | int cresult; | ||
| 38 | int result = STATE_UNKNOWN; | ||
| 39 | time_t local_time; | ||
| 40 | FILE *fp = NULL; | ||
| 41 | + struct output chld_out, chld_err; | ||
| 42 | |||
| 43 | remotecmd = strdup (""); | ||
| 44 | comm = strdup (SSH_COMMAND); | ||
| 45 | @@ -82,48 +82,22 @@ | ||
| 46 | if (verbose) | ||
| 47 | printf ("%s\n", comm); | ||
| 48 | |||
| 49 | - child_process = spopen (comm); | ||
| 50 | - | ||
| 51 | - if (child_process == NULL) { | ||
| 52 | - printf (_("Could not open pipe: %s\n"), comm); | ||
| 53 | - return STATE_UNKNOWN; | ||
| 54 | - } | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - /* open STDERR for spopen */ | ||
| 58 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 59 | - if (child_stderr == NULL) { | ||
| 60 | - printf (_("Could not open stderr for %s\n"), SSH_COMMAND); | ||
| 61 | - } | ||
| 62 | - | ||
| 63 | + result = np_runcmd(comm, &chld_out, &chld_err, 0); | ||
| 64 | |||
| 65 | /* build up results from remote command in result_text */ | ||
| 66 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) | ||
| 67 | - asprintf (&result_text, "%s%s", result_text, input_buffer); | ||
| 68 | + if(skip_lines && skip_lines < chld_out.lines) | ||
| 69 | + result_text = chld_out.line[skip_lines]; | ||
| 70 | + else | ||
| 71 | + result_text = chld_out.line[0]; | ||
| 72 | |||
| 73 | /* WARNING if output found on stderr */ | ||
| 74 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | ||
| 75 | - if (skip_lines > 0) { | ||
| 76 | - if (input_buffer[strlen(input_buffer)-1] == '\n') { | ||
| 77 | - skip_lines--; | ||
| 78 | - } | ||
| 79 | - } else { | ||
| 80 | - printf ("%s", input_buffer); | ||
| 81 | - result = STATE_WARNING; | ||
| 82 | - } | ||
| 83 | + if(chld_err.buflen) { | ||
| 84 | + printf("%s\n", chld_err.line[0]); | ||
| 85 | + return STATE_WARNING; | ||
| 86 | } | ||
| 87 | - (void) fclose (child_stderr); | ||
| 88 | - if (result == STATE_WARNING) | ||
| 89 | - return result; | ||
| 90 | - | ||
| 91 | - | ||
| 92 | - /* close the pipe */ | ||
| 93 | - result = spclose (child_process); | ||
| 94 | - | ||
| 95 | |||
| 96 | /* process output */ | ||
| 97 | if (passive) { | ||
| 98 | - | ||
| 99 | if (!(fp = fopen (outputfile, "a"))) { | ||
| 100 | printf (_("SSH WARNING: could not open %s\n"), outputfile); | ||
| 101 | exit (STATE_UNKNOWN); | ||
| 102 | @@ -137,19 +111,19 @@ | ||
| 103 | printf ("%s", result_text); | ||
| 104 | return result; | ||
| 105 | } | ||
| 106 | - asprintf (&output, "%s", result_text); | ||
| 107 | + asprintf (&msg, "%s", result_text); | ||
| 108 | result_text = strnl (status_text); | ||
| 109 | - eol = strpbrk (output, "\r\n"); | ||
| 110 | + eol = strpbrk (msg, "\r\n"); | ||
| 111 | if (eol != NULL) | ||
| 112 | eol[0] = 0; | ||
| 113 | if (service[commands] && status_text | ||
| 114 | - && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1) { | ||
| 115 | + && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1) | ||
| 116 | + { | ||
| 117 | fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", | ||
| 118 | - (int) local_time, host_shortname, service[commands++], cresult, | ||
| 119 | - output); | ||
| 120 | + (int) local_time, host_shortname, service[commands++], | ||
| 121 | + cresult, msg); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | - | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | @@ -206,7 +180,7 @@ | ||
| 129 | |||
| 130 | while (1) { | ||
| 131 | c = getopt_long (argc, argv, "Vvh1246ft:H:O:p:i:u:l:C:S:n:s:", longopts, | ||
| 132 | - &option); | ||
| 133 | + &option); | ||
| 134 | |||
| 135 | if (c == -1 || c == EOF) | ||
| 136 | break; | ||
| 137 | @@ -244,25 +218,27 @@ | ||
| 138 | passive = TRUE; | ||
| 139 | break; | ||
| 140 | case 's': /* description of service to check */ | ||
| 141 | - service = realloc (service, (++services) * sizeof(char *)); | ||
| 142 | p1 = optarg; | ||
| 143 | + service = realloc (service, (++services) * sizeof(char *)); | ||
| 144 | while ((p2 = index (p1, ':'))) { | ||
| 145 | *p2 = '\0'; | ||
| 146 | - asprintf (&service[services-1], "%s", p1); | ||
| 147 | + service[services - 1] = p1; | ||
| 148 | service = realloc (service, (++services) * sizeof(char *)); | ||
| 149 | p1 = p2 + 1; | ||
| 150 | } | ||
| 151 | - asprintf (&service[services-1], "%s", p1); | ||
| 152 | + service[services - 1] = p1; | ||
| 153 | break; | ||
| 154 | case 'n': /* short name of host in nagios configuration */ | ||
| 155 | host_shortname = optarg; | ||
| 156 | break; | ||
| 157 | + | ||
| 158 | case 'u': | ||
| 159 | c = 'l'; | ||
| 160 | case 'l': /* login name */ | ||
| 161 | case 'i': /* identity */ | ||
| 162 | asprintf (&comm, "%s -%c %s", comm, c, optarg); | ||
| 163 | break; | ||
| 164 | + | ||
| 165 | case '1': /* Pass these switches directly to ssh */ | ||
| 166 | case '2': /* 1 to force version 1, 2 to force version 2 */ | ||
| 167 | case '4': /* -4 for IPv4 */ | ||
| 168 | diff -urN ../nplg/plugins/check_dig.c ./plugins/check_dig.c | ||
| 169 | --- ../nplg/plugins/check_dig.c 2005-01-28 15:00:58.000000000 +0100 | ||
| 170 | +++ ./plugins/check_dig.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 171 | @@ -14,29 +14,33 @@ | ||
| 172 | along with this program; if not, write to the Free Software | ||
| 173 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 174 | |||
| 175 | - $Id: check_dig.c,v 1.40 2005/01/26 21:21:01 tonvoon Exp $ | ||
| 176 | + $Id: check_dig.c,v 1.3 2005/06/06 11:37:06 exon Exp $ | ||
| 177 | |||
| 178 | *****************************************************************************/ | ||
| 179 | |||
| 180 | +/* Hackers note: | ||
| 181 | + * There are typecasts to (char *) from _("foo bar") in this file. | ||
| 182 | + * They prevent compiler warnings. Never (ever), permute strings obtained | ||
| 183 | + * that are typecast from (const char *) (which happens when --disable-nls) | ||
| 184 | + * because on some architectures those strings are in non-writable memory */ | ||
| 185 | + | ||
| 186 | const char *progname = "check_dig"; | ||
| 187 | -const char *revision = "$Revision: 1.40 $"; | ||
| 188 | +const char *revision = "$Revision: 1.3 $"; | ||
| 189 | const char *copyright = "2002-2004"; | ||
| 190 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 191 | |||
| 192 | #include "common.h" | ||
| 193 | #include "netutils.h" | ||
| 194 | #include "utils.h" | ||
| 195 | -#include "popen.h" | ||
| 196 | +#include "runcmd.h" | ||
| 197 | |||
| 198 | int process_arguments (int, char **); | ||
| 199 | int validate_arguments (void); | ||
| 200 | void print_help (void); | ||
| 201 | void print_usage (void); | ||
| 202 | |||
| 203 | -enum { | ||
| 204 | - UNDEFINED = 0, | ||
| 205 | - DEFAULT_PORT = 53 | ||
| 206 | -}; | ||
| 207 | +#define UNDEFINED 0 | ||
| 208 | +#define DEFAULT_PORT 53 | ||
| 209 | |||
| 210 | char *query_address = NULL; | ||
| 211 | char *record_type = "A"; | ||
| 212 | @@ -51,16 +55,15 @@ | ||
| 213 | int | ||
| 214 | main (int argc, char **argv) | ||
| 215 | { | ||
| 216 | - char input_buffer[MAX_INPUT_BUFFER]; | ||
| 217 | char *command_line; | ||
| 218 | - char *output; | ||
| 219 | + output chld_out, chld_err; | ||
| 220 | + char *msg = NULL; | ||
| 221 | + size_t i; | ||
| 222 | char *t; | ||
| 223 | long microsec; | ||
| 224 | double elapsed_time; | ||
| 225 | int result = STATE_UNKNOWN; | ||
| 226 | |||
| 227 | - output = strdup (""); | ||
| 228 | - | ||
| 229 | setlocale (LC_ALL, ""); | ||
| 230 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 231 | textdomain (PACKAGE); | ||
| 232 | @@ -89,100 +92,75 @@ | ||
| 233 | } | ||
| 234 | |||
| 235 | /* run the command */ | ||
| 236 | - child_process = spopen (command_line); | ||
| 237 | - if (child_process == NULL) { | ||
| 238 | - printf (_("Could not open pipe: %s\n"), command_line); | ||
| 239 | - return STATE_UNKNOWN; | ||
| 240 | + if((result = np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0) { | ||
| 241 | + result = STATE_WARNING; | ||
| 242 | + msg = (char *)_("dig returned an error status"); | ||
| 243 | } | ||
| 244 | |||
| 245 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 246 | - if (child_stderr == NULL) | ||
| 247 | - printf (_("Could not open stderr for %s\n"), command_line); | ||
| 248 | - | ||
| 249 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
| 250 | - | ||
| 251 | + for(i = 0; i < chld_out.lines; i++) { | ||
| 252 | /* the server is responding, we just got the host name... */ | ||
| 253 | - if (strstr (input_buffer, ";; ANSWER SECTION:")) { | ||
| 254 | + if (strstr (chld_out.line[i], ";; ANSWER SECTION:")) { | ||
| 255 | |||
| 256 | /* loop through the whole 'ANSWER SECTION' */ | ||
| 257 | - do { | ||
| 258 | + for(; i < chld_out.lines; i++) { | ||
| 259 | /* get the host address */ | ||
| 260 | - if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) | ||
| 261 | - break; | ||
| 262 | - | ||
| 263 | - if (strpbrk (input_buffer, "\r\n")) | ||
| 264 | - input_buffer[strcspn (input_buffer, "\r\n")] = '\0'; | ||
| 265 | + if (verbose) | ||
| 266 | + printf ("%s\n", chld_out.line[i]); | ||
| 267 | |||
| 268 | - if (verbose && !strstr (input_buffer, ";; ")) | ||
| 269 | - printf ("%s\n", input_buffer); | ||
| 270 | - | ||
| 271 | - if (expected_address==NULL && strstr (input_buffer, query_address) != NULL) { | ||
| 272 | - output = strdup(input_buffer); | ||
| 273 | + if (strstr (chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) { | ||
| 274 | + msg = chld_out.line[i]; | ||
| 275 | result = STATE_OK; | ||
| 276 | - } | ||
| 277 | - else if (expected_address != NULL && strstr (input_buffer, expected_address) != NULL) { | ||
| 278 | - output = strdup(input_buffer); | ||
| 279 | - result = STATE_OK; | ||
| 280 | - } | ||
| 281 | |||
| 282 | - /* Translate output TAB -> SPACE */ | ||
| 283 | - t = output; | ||
| 284 | - while ((t = index(t, '\t')) != NULL) | ||
| 285 | - *t = ' '; | ||
| 286 | - | ||
| 287 | - } while (!strstr (input_buffer, ";; ")); | ||
| 288 | + /* Translate output TAB -> SPACE */ | ||
| 289 | + t = msg; | ||
| 290 | + while ((t = strchr(t, '\t')) != NULL) *t = ' '; | ||
| 291 | + break; | ||
| 292 | + } | ||
| 293 | + } | ||
| 294 | |||
| 295 | if (result == STATE_UNKNOWN) { | ||
| 296 | - asprintf (&output, _("Server not found in ANSWER SECTION")); | ||
| 297 | - result = STATE_WARNING; | ||
| 298 | - } | ||
| 299 | - } | ||
| 300 | - | ||
| 301 | - } | ||
| 302 | - | ||
| 303 | - if (result == STATE_UNKNOWN) { | ||
| 304 | - asprintf (&output, _("No ANSWER SECTION found")); | ||
| 305 | - } | ||
| 306 | + msg = (char *)_("Server not found in ANSWER SECTION"); | ||
| 307 | + result = STATE_WARNING; | ||
| 308 | + } | ||
| 309 | |||
| 310 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | ||
| 311 | - /* If we get anything on STDERR, at least set warning */ | ||
| 312 | - result = max_state (result, STATE_WARNING); | ||
| 313 | - printf ("%s", input_buffer); | ||
| 314 | - if (strlen (output) == 0) | ||
| 315 | - output = strdup (1 + index (input_buffer, ':')); | ||
| 316 | + /* we found the answer section, so break out of the loop */ | ||
| 317 | + break; | ||
| 318 | + } | ||
| 319 | } | ||
| 320 | |||
| 321 | - (void) fclose (child_stderr); | ||
| 322 | + if (result == STATE_UNKNOWN) | ||
| 323 | + msg = (char *)_("No ANSWER SECTION found"); | ||
| 324 | |||
| 325 | - /* close the pipe */ | ||
| 326 | - if (spclose (child_process)) { | ||
| 327 | - result = max_state (result, STATE_WARNING); | ||
| 328 | - if (strlen (output) == 0) | ||
| 329 | - asprintf (&output, _("dig returned an error status")); | ||
| 330 | + /* If we get anything on STDERR, at least set warning */ | ||
| 331 | + if(chld_err.buflen > 0) { | ||
| 332 | + result = max_state(result, STATE_WARNING); | ||
| 333 | + if(!msg) for(i = 0; i < chld_err.lines; i++) { | ||
| 334 | + msg = strchr(chld_err.line[0], ':'); | ||
| 335 | + if(msg) { | ||
| 336 | + msg++; | ||
| 337 | + break; | ||
| 338 | + } | ||
| 339 | + } | ||
| 340 | } | ||
| 341 | |||
| 342 | microsec = deltime (tv); | ||
| 343 | elapsed_time = (double)microsec / 1.0e6; | ||
| 344 | |||
| 345 | - if (output == NULL || strlen (output) == 0) | ||
| 346 | - asprintf (&output, _(" Probably a non-existent host/domain")); | ||
| 347 | - | ||
| 348 | if (critical_interval > UNDEFINED && elapsed_time > critical_interval) | ||
| 349 | result = STATE_CRITICAL; | ||
| 350 | |||
| 351 | else if (warning_interval > UNDEFINED && elapsed_time > warning_interval) | ||
| 352 | result = STATE_WARNING; | ||
| 353 | |||
| 354 | - asprintf (&output, _("%.3f seconds response time (%s)"), elapsed_time, output); | ||
| 355 | - | ||
| 356 | - printf ("DNS %s - %s|%s\n", | ||
| 357 | - state_text (result), output, | ||
| 358 | + printf ("DNS %s - %.3f seconds response time (%s)|%s\n", | ||
| 359 | + state_text (result), elapsed_time, | ||
| 360 | + msg ? msg : _("Probably a non-existent host/domain"), | ||
| 361 | fperfdata("time", elapsed_time, "s", | ||
| 362 | - (warning_interval>UNDEFINED?TRUE:FALSE), | ||
| 363 | - warning_interval, | ||
| 364 | - (critical_interval>UNDEFINED?TRUE:FALSE), | ||
| 365 | - critical_interval, | ||
| 366 | - TRUE, 0, FALSE, 0)); | ||
| 367 | + (warning_interval>UNDEFINED?TRUE:FALSE), | ||
| 368 | + warning_interval, | ||
| 369 | + (critical_interval>UNDEFINED?TRUE:FALSE), | ||
| 370 | + critical_interval, | ||
| 371 | + TRUE, 0, FALSE, 0)); | ||
| 372 | return result; | ||
| 373 | } | ||
| 374 | |||
| 375 | @@ -359,6 +337,6 @@ | ||
| 376 | { | ||
| 377 | printf ("\ | ||
| 378 | Usage: %s -H host -l lookup [-p <server port>] [-T <query type>]\n\ | ||
| 379 | - [-w <warning interval>] [-c <critical interval>] [-t <timeout>]\n\ | ||
| 380 | - [-a <expected answer address>] [-v]\n", progname); | ||
| 381 | + [-w <warning interval>] [-c <critical interval>] [-t <timeout>]\n\ | ||
| 382 | + [-a <expected answer address>] [-v]\n", progname); | ||
| 383 | } | ||
| 384 | diff -urN ../nplg/plugins/check_dns.c ./plugins/check_dns.c | ||
| 385 | --- ../nplg/plugins/check_dns.c 2005-01-05 21:53:12.000000000 +0100 | ||
| 386 | +++ ./plugins/check_dns.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 387 | @@ -17,19 +17,19 @@ | ||
| 388 | LIMITATION: nslookup on Solaris 7 can return output over 2 lines, which will not | ||
| 389 | be picked up by this plugin | ||
| 390 | |||
| 391 | - $Id: check_dns.c,v 1.47 2004/12/30 00:41:39 opensides Exp $ | ||
| 392 | + $Id: check_dns.c,v 1.3 2005/06/06 11:37:06 exon Exp $ | ||
| 393 | |||
| 394 | ******************************************************************************/ | ||
| 395 | |||
| 396 | const char *progname = "check_dns"; | ||
| 397 | -const char *revision = "$Revision: 1.47 $"; | ||
| 398 | +const char *revision = "$Revision: 1.3 $"; | ||
| 399 | const char *copyright = "2000-2004"; | ||
| 400 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 401 | |||
| 402 | #include "common.h" | ||
| 403 | -#include "popen.h" | ||
| 404 | #include "utils.h" | ||
| 405 | #include "netutils.h" | ||
| 406 | +#include "runcmd.h" | ||
| 407 | |||
| 408 | int process_arguments (int, char **); | ||
| 409 | int validate_arguments (void); | ||
| 410 | @@ -51,8 +51,8 @@ | ||
| 411 | { | ||
| 412 | char *command_line = NULL; | ||
| 413 | char input_buffer[MAX_INPUT_BUFFER]; | ||
| 414 | - char *output = NULL; | ||
| 415 | char *address = NULL; | ||
| 416 | + char *msg = NULL; | ||
| 417 | char *temp_buffer = NULL; | ||
| 418 | int non_authoritative = FALSE; | ||
| 419 | int result = STATE_UNKNOWN; | ||
| 420 | @@ -61,6 +61,8 @@ | ||
| 421 | struct timeval tv; | ||
| 422 | int multi_address; | ||
| 423 | int parse_address = FALSE; /* This flag scans for Address: but only after Name: */ | ||
| 424 | + output chld_out, chld_err; | ||
| 425 | + size_t i; | ||
| 426 | |||
| 427 | setlocale (LC_ALL, ""); | ||
| 428 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 429 | @@ -85,37 +87,31 @@ | ||
| 430 | printf ("%s\n", command_line); | ||
| 431 | |||
| 432 | /* run the command */ | ||
| 433 | - child_process = spopen (command_line); | ||
| 434 | - if (child_process == NULL) { | ||
| 435 | - printf (_("Could not open pipe: %s\n"), command_line); | ||
| 436 | - return STATE_UNKNOWN; | ||
| 437 | + if((np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0) { | ||
| 438 | + msg = (char *)_("nslookup returned error status"); | ||
| 439 | + result = STATE_WARNING; | ||
| 440 | } | ||
| 441 | |||
| 442 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 443 | - if (child_stderr == NULL) | ||
| 444 | - printf (_("Could not open stderr for %s\n"), command_line); | ||
| 445 | - | ||
| 446 | /* scan stdout */ | ||
| 447 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
| 448 | - | ||
| 449 | + for(i = 0; i < chld_out.lines; i++) { | ||
| 450 | if (verbose) | ||
| 451 | - printf ("%s", input_buffer); | ||
| 452 | + printf ("%s", chld_out.line[i]); | ||
| 453 | |||
| 454 | - if (strstr (input_buffer, ".in-addr.arpa")) { | ||
| 455 | - if ((temp_buffer = strstr (input_buffer, "name = "))) | ||
| 456 | + if (strstr (chld_out.line[i], ".in-addr.arpa")) { | ||
| 457 | + if ((temp_buffer = strstr (chld_out.line[i], "name = "))) | ||
| 458 | address = strdup (temp_buffer + 7); | ||
| 459 | else { | ||
| 460 | - output = strdup (_("Warning plugin error")); | ||
| 461 | + msg = (char *)_("Warning plugin error"); | ||
| 462 | result = STATE_WARNING; | ||
| 463 | } | ||
| 464 | } | ||
| 465 | |||
| 466 | /* the server is responding, we just got the host name... */ | ||
| 467 | - if (strstr (input_buffer, "Name:")) | ||
| 468 | + if (strstr (chld_out.line[i], "Name:")) | ||
| 469 | parse_address = TRUE; | ||
| 470 | - else if (parse_address == TRUE && (strstr (input_buffer, "Address:") || | ||
| 471 | - strstr (input_buffer, "Addresses:"))) { | ||
| 472 | - temp_buffer = index (input_buffer, ':'); | ||
| 473 | + else if (parse_address == TRUE && (strstr (chld_out.line[i], "Address:") || | ||
| 474 | + strstr (chld_out.line[i], "Addresses:"))) { | ||
| 475 | + temp_buffer = index (chld_out.line[i], ':'); | ||
| 476 | temp_buffer++; | ||
| 477 | |||
| 478 | /* Strip leading spaces */ | ||
| 479 | @@ -135,59 +131,47 @@ | ||
| 480 | asprintf(&address, "%s,%s", address, temp_buffer); | ||
| 481 | } | ||
| 482 | |||
| 483 | - else if (strstr (input_buffer, _("Non-authoritative answer:"))) { | ||
| 484 | + else if (strstr (chld_out.line[i], _("Non-authoritative answer:"))) { | ||
| 485 | non_authoritative = TRUE; | ||
| 486 | } | ||
| 487 | |||
| 488 | - result = error_scan (input_buffer); | ||
| 489 | + result = error_scan (chld_out.line[i]); | ||
| 490 | if (result != STATE_OK) { | ||
| 491 | - output = strdup (1 + index (input_buffer, ':')); | ||
| 492 | - strip (output); | ||
| 493 | + msg = strchr (chld_out.line[i], ':'); | ||
| 494 | + if(msg) msg++; | ||
| 495 | break; | ||
| 496 | } | ||
| 497 | - | ||
| 498 | } | ||
| 499 | |||
| 500 | /* scan stderr */ | ||
| 501 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | ||
| 502 | - | ||
| 503 | + for(i = 0; i < chld_err.lines; i++) { | ||
| 504 | if (verbose) | ||
| 505 | - printf ("%s", input_buffer); | ||
| 506 | + printf ("%s", chld_err.line[i]); | ||
| 507 | |||
| 508 | - if (error_scan (input_buffer) != STATE_OK) { | ||
| 509 | - result = max_state (result, error_scan (input_buffer)); | ||
| 510 | - output = strdup (1 + index (input_buffer, ':')); | ||
| 511 | - strip (output); | ||
| 512 | + if (error_scan (chld_err.line[i]) != STATE_OK) { | ||
| 513 | + result = max_state (result, error_scan (chld_err.line[i])); | ||
| 514 | + msg = strchr(input_buffer, ':'); | ||
| 515 | + if(msg) msg++; | ||
| 516 | } | ||
| 517 | } | ||
| 518 | |||
| 519 | - /* close stderr */ | ||
| 520 | - (void) fclose (child_stderr); | ||
| 521 | - | ||
| 522 | - /* close stdout */ | ||
| 523 | - if (spclose (child_process)) { | ||
| 524 | - result = max_state (result, STATE_WARNING); | ||
| 525 | - if (output == NULL || !strcmp (output, "")) | ||
| 526 | - output = strdup (_("nslookup returned error status")); | ||
| 527 | - } | ||
| 528 | - | ||
| 529 | - /* If we got here, we should have an address string, | ||
| 530 | - and we can segfault if we do not */ | ||
| 531 | + /* If we got here, we should have an address string, | ||
| 532 | + * and we can segfault if we do not */ | ||
| 533 | if (address==NULL || strlen(address)==0) | ||
| 534 | die (STATE_CRITICAL, | ||
| 535 | - _("DNS CRITICAL - '%s' output parsing exited with no address\n"), | ||
| 536 | + _("DNS CRITICAL - '%s' msg parsing exited with no address\n"), | ||
| 537 | NSLOOKUP_COMMAND); | ||
| 538 | |||
| 539 | /* compare to expected address */ | ||
| 540 | if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) { | ||
| 541 | result = STATE_CRITICAL; | ||
| 542 | - asprintf(&output, _("expected %s but got %s"), expected_address, address); | ||
| 543 | + asprintf(&msg, _("expected %s but got %s"), expected_address, address); | ||
| 544 | } | ||
| 545 | |||
| 546 | /* check if authoritative */ | ||
| 547 | if (result == STATE_OK && expect_authority && non_authoritative) { | ||
| 548 | result = STATE_CRITICAL; | ||
| 549 | - asprintf(&output, _("server %s is not authoritative for %s"), dns_server, query_address); | ||
| 550 | + asprintf(&msg, _("server %s is not authoritative for %s"), dns_server, query_address); | ||
| 551 | } | ||
| 552 | |||
| 553 | microsec = deltime (tv); | ||
| 554 | @@ -206,13 +190,13 @@ | ||
| 555 | } | ||
| 556 | else if (result == STATE_WARNING) | ||
| 557 | printf (_("DNS WARNING - %s\n"), | ||
| 558 | - !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output); | ||
| 559 | + !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg); | ||
| 560 | else if (result == STATE_CRITICAL) | ||
| 561 | printf (_("DNS CRITICAL - %s\n"), | ||
| 562 | - !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output); | ||
| 563 | + !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg); | ||
| 564 | else | ||
| 565 | printf (_("DNS UNKNOW - %s\n"), | ||
| 566 | - !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output); | ||
| 567 | + !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg); | ||
| 568 | |||
| 569 | return result; | ||
| 570 | } | ||
| 571 | diff -urN ../nplg/plugins/check_fping.c ./plugins/check_fping.c | ||
| 572 | --- ../nplg/plugins/check_fping.c 2005-01-05 21:53:13.000000000 +0100 | ||
| 573 | +++ ./plugins/check_fping.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 574 | @@ -14,26 +14,24 @@ | ||
| 575 | along with this program; if not, write to the Free Software | ||
| 576 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 577 | |||
| 578 | - $Id: check_fping.c,v 1.24 2004/12/25 23:17:44 opensides Exp $ | ||
| 579 | + $Id: check_fping.c,v 1.2 2005/06/05 21:55:26 exon Exp $ | ||
| 580 | |||
| 581 | ******************************************************************************/ | ||
| 582 | |||
| 583 | const char *progname = "check_fping"; | ||
| 584 | -const char *revision = "$Revision: 1.24 $"; | ||
| 585 | +const char *revision = "$Revision: 1.2 $"; | ||
| 586 | const char *copyright = "2000-2004"; | ||
| 587 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 588 | |||
| 589 | #include "common.h" | ||
| 590 | -#include "popen.h" | ||
| 591 | #include "netutils.h" | ||
| 592 | #include "utils.h" | ||
| 593 | +#include "runcmd.h" | ||
| 594 | |||
| 595 | -enum { | ||
| 596 | - PACKET_COUNT = 1, | ||
| 597 | - PACKET_SIZE = 56, | ||
| 598 | - PL = 0, | ||
| 599 | - RTA = 1 | ||
| 600 | -}; | ||
| 601 | +#define PACKET_COUNT 1 | ||
| 602 | +#define PACKET_SIZE 56 | ||
| 603 | +#define PL 0 | ||
| 604 | +#define RTA 1 | ||
| 605 | |||
| 606 | int textscan (char *buf); | ||
| 607 | int process_arguments (int, char **); | ||
| 608 | @@ -59,11 +57,11 @@ | ||
| 609 | { | ||
| 610 | /* normaly should be int result = STATE_UNKNOWN; */ | ||
| 611 | |||
| 612 | - int status = STATE_UNKNOWN; | ||
| 613 | + int result = STATE_UNKNOWN; | ||
| 614 | char *server = NULL; | ||
| 615 | char *command_line = NULL; | ||
| 616 | - char *input_buffer = NULL; | ||
| 617 | - input_buffer = malloc (MAX_INPUT_BUFFER); | ||
| 618 | + output chld_out, chld_err; | ||
| 619 | + size_t i; | ||
| 620 | |||
| 621 | setlocale (LC_ALL, ""); | ||
| 622 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 623 | @@ -79,43 +77,21 @@ | ||
| 624 | packet_size, packet_count, server); | ||
| 625 | |||
| 626 | if (verbose) | ||
| 627 | - printf ("%s\n", command_line); | ||
| 628 | + printf ("command_line: %s\n", command_line); | ||
| 629 | |||
| 630 | /* run the command */ | ||
| 631 | - child_process = spopen (command_line); | ||
| 632 | - if (child_process == NULL) { | ||
| 633 | - printf (_("Could not open pipe: %s\n"), command_line); | ||
| 634 | - return STATE_UNKNOWN; | ||
| 635 | - } | ||
| 636 | - | ||
| 637 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 638 | - if (child_stderr == NULL) { | ||
| 639 | - printf (_("Could not open stderr for %s\n"), command_line); | ||
| 640 | - } | ||
| 641 | + if((np_runcmd(command_line, &chld_out, &chld_err, 0)) || chld_err.buflen) | ||
| 642 | + result = STATE_WARNING; | ||
| 643 | |||
| 644 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
| 645 | - if (verbose) | ||
| 646 | - printf ("%s", input_buffer); | ||
| 647 | - status = max_state (status, textscan (input_buffer)); | ||
| 648 | - } | ||
| 649 | - | ||
| 650 | - /* If we get anything on STDERR, at least set warning */ | ||
| 651 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | ||
| 652 | - status = max_state (status, STATE_WARNING); | ||
| 653 | - if (verbose) | ||
| 654 | - printf ("%s", input_buffer); | ||
| 655 | - status = max_state (status, textscan (input_buffer)); | ||
| 656 | - } | ||
| 657 | - (void) fclose (child_stderr); | ||
| 658 | + for(i = 0; i < chld_out.lines; i++) | ||
| 659 | + result = max_state (result, textscan (chld_out.line[i])); | ||
| 660 | |||
| 661 | - /* close the pipe */ | ||
| 662 | - if (spclose (child_process)) | ||
| 663 | - /* need to use max_state not max */ | ||
| 664 | - status = max_state (status, STATE_WARNING); | ||
| 665 | + printf ("FPING %s - %s\n", state_text (result), server_name); | ||
| 666 | |||
| 667 | - printf ("FPING %s - %s\n", state_text (status), server_name); | ||
| 668 | + if (verbose) for(i = 0; i < chld_out.lines; i++) | ||
| 669 | + printf ("%s", chld_out.line[i]); | ||
| 670 | |||
| 671 | - return status; | ||
| 672 | + return result; | ||
| 673 | } | ||
| 674 | |||
| 675 | |||
| 676 | @@ -127,7 +103,7 @@ | ||
| 677 | char *losstr = NULL; | ||
| 678 | double loss; | ||
| 679 | double rta; | ||
| 680 | - int status = STATE_UNKNOWN; | ||
| 681 | + int result = STATE_UNKNOWN; | ||
| 682 | |||
| 683 | if (strstr (buf, "not found")) { | ||
| 684 | die (STATE_CRITICAL, _("FPING UNKNOW - %s not found\n"), server_name); | ||
| 685 | @@ -143,7 +119,7 @@ | ||
| 686 | |||
| 687 | } | ||
| 688 | else if (strstr (buf, "is alive")) { | ||
| 689 | - status = STATE_OK; | ||
| 690 | + result = STATE_OK; | ||
| 691 | |||
| 692 | } | ||
| 693 | else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) { | ||
| 694 | @@ -156,18 +132,18 @@ | ||
| 695 | loss = strtod (losstr, NULL); | ||
| 696 | rta = strtod (rtastr, NULL); | ||
| 697 | if (cpl_p == TRUE && loss > cpl) | ||
| 698 | - status = STATE_CRITICAL; | ||
| 699 | + result = STATE_CRITICAL; | ||
| 700 | else if (crta_p == TRUE && rta > crta) | ||
| 701 | - status = STATE_CRITICAL; | ||
| 702 | + result = STATE_CRITICAL; | ||
| 703 | else if (wpl_p == TRUE && loss > wpl) | ||
| 704 | - status = STATE_WARNING; | ||
| 705 | + result = STATE_WARNING; | ||
| 706 | else if (wrta_p == TRUE && rta > wrta) | ||
| 707 | - status = STATE_WARNING; | ||
| 708 | + result = STATE_WARNING; | ||
| 709 | else | ||
| 710 | - status = STATE_OK; | ||
| 711 | - die (status, | ||
| 712 | + result = STATE_OK; | ||
| 713 | + die (result, | ||
| 714 | _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), | ||
| 715 | - state_text (status), server_name, loss, rta, | ||
| 716 | + state_text (result), server_name, loss, rta, | ||
| 717 | perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100), | ||
| 718 | fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0)); | ||
| 719 | |||
| 720 | @@ -179,24 +155,24 @@ | ||
| 721 | losstr = 1 + strstr (losstr, "/"); | ||
| 722 | loss = strtod (losstr, NULL); | ||
| 723 | if (atoi(losstr) == 100) | ||
| 724 | - status = STATE_CRITICAL; | ||
| 725 | + result = STATE_CRITICAL; | ||
| 726 | else if (cpl_p == TRUE && loss > cpl) | ||
| 727 | - status = STATE_CRITICAL; | ||
| 728 | + result = STATE_CRITICAL; | ||
| 729 | else if (wpl_p == TRUE && loss > wpl) | ||
| 730 | - status = STATE_WARNING; | ||
| 731 | + result = STATE_WARNING; | ||
| 732 | else | ||
| 733 | - status = STATE_OK; | ||
| 734 | + result = STATE_OK; | ||
| 735 | /* loss=%.0f%%;%d;%d;0;100 */ | ||
| 736 | - die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"), | ||
| 737 | - state_text (status), server_name, loss , | ||
| 738 | + die (result, _("FPING %s - %s (loss=%.0f%% )|%s\n"), | ||
| 739 | + state_text (result), server_name, loss , | ||
| 740 | perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100)); | ||
| 741 | |||
| 742 | } | ||
| 743 | else { | ||
| 744 | - status = max_state (status, STATE_WARNING); | ||
| 745 | + result = max_state (result, STATE_WARNING); | ||
| 746 | } | ||
| 747 | |||
| 748 | - return status; | ||
| 749 | + return result; | ||
| 750 | } | ||
| 751 | |||
| 752 | |||
| 753 | diff -urN ../nplg/plugins/check_game.c ./plugins/check_game.c | ||
| 754 | --- ../nplg/plugins/check_game.c 2005-01-05 21:53:13.000000000 +0100 | ||
| 755 | +++ ./plugins/check_game.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 756 | @@ -14,17 +14,17 @@ | ||
| 757 | * along with this program; if not, write to the Free Software | ||
| 758 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 759 | * | ||
| 760 | -* $Id: check_game.c,v 1.22 2004/12/25 23:17:44 opensides Exp $ | ||
| 761 | +* $Id: check_game.c,v 1.2 2005/06/05 21:55:26 exon Exp $ | ||
| 762 | *****************************************************************************/ | ||
| 763 | |||
| 764 | const char *progname = "check_game"; | ||
| 765 | -const char *revision = "$Revision: 1.22 $"; | ||
| 766 | +const char *revision = "$Revision: 1.2 $"; | ||
| 767 | const char *copyright = "2002-2004"; | ||
| 768 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 769 | |||
| 770 | #include "common.h" | ||
| 771 | -#include "popen.h" | ||
| 772 | #include "utils.h" | ||
| 773 | +#include "runcmd.h" | ||
| 774 | |||
| 775 | int process_arguments (int, char **); | ||
| 776 | int validate_arguments (void); | ||
| 777 | @@ -57,9 +57,9 @@ | ||
| 778 | char *command_line; | ||
| 779 | int result = STATE_UNKNOWN; | ||
| 780 | FILE *fp; | ||
| 781 | - char input_buffer[MAX_INPUT_BUFFER]; | ||
| 782 | char *p, *ret[QSTAT_MAX_RETURN_ARGS]; | ||
| 783 | - int i; | ||
| 784 | + size_t i = 0; | ||
| 785 | + output chld_out; | ||
| 786 | |||
| 787 | setlocale (LC_ALL, ""); | ||
| 788 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 789 | @@ -80,17 +80,9 @@ | ||
| 790 | if (verbose > 0) | ||
| 791 | printf ("%s\n", command_line); | ||
| 792 | |||
| 793 | - /* run the command */ | ||
| 794 | - fp = spopen (command_line); | ||
| 795 | - if (fp == NULL) { | ||
| 796 | - printf (_("Could not open pipe: %s\n"), command_line); | ||
| 797 | - return STATE_UNKNOWN; | ||
| 798 | - } | ||
| 799 | - | ||
| 800 | - fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp); /* Only interested in the first line */ | ||
| 801 | - | ||
| 802 | - /* strip the newline character from the end of the input */ | ||
| 803 | - input_buffer[strlen (input_buffer) - 1] = 0; | ||
| 804 | + /* run the command. historically, this plugin ignores output on stderr, | ||
| 805 | + * as well as return status of the qstat program */ | ||
| 806 | + (void)np_runcmd(command_line, &chld_out, NULL, 0); | ||
| 807 | |||
| 808 | /* sanity check */ | ||
| 809 | /* was thinking about running qstat without any options, capturing the | ||
| 810 | @@ -102,18 +94,13 @@ | ||
| 811 | In the end, I figured I'd simply let an error occur & then trap it | ||
| 812 | */ | ||
| 813 | |||
| 814 | - if (!strncmp (input_buffer, "unknown option", 14)) { | ||
| 815 | + if (!strncmp (chld_out.line[0], "unknown option", 14)) { | ||
| 816 | printf (_("CRITICAL - Host type parameter incorrect!\n")); | ||
| 817 | result = STATE_CRITICAL; | ||
| 818 | return result; | ||
| 819 | } | ||
| 820 | |||
| 821 | - /* initialize the returned data buffer */ | ||
| 822 | - for (i = 0; i < QSTAT_MAX_RETURN_ARGS; i++) | ||
| 823 | - ret[i] = strdup(""); | ||
| 824 | - | ||
| 825 | - i = 0; | ||
| 826 | - p = (char *) strtok (input_buffer, QSTAT_DATA_DELIMITER); | ||
| 827 | + p = (char *) strtok (chld_out.line[0], QSTAT_DATA_DELIMITER); | ||
| 828 | while (p != NULL) { | ||
| 829 | ret[i] = p; | ||
| 830 | p = (char *) strtok (NULL, QSTAT_DATA_DELIMITER); | ||
| 831 | @@ -141,17 +128,14 @@ | ||
| 832 | ret[qstat_game_field], | ||
| 833 | ret[qstat_map_field], | ||
| 834 | ret[qstat_ping_field], | ||
| 835 | - perfdata ("players", atol(ret[qstat_game_players]), "", | ||
| 836 | + perfdata ("players", atol(ret[qstat_game_players]), "", | ||
| 837 | FALSE, 0, FALSE, 0, | ||
| 838 | TRUE, 0, TRUE, atol(ret[qstat_game_players_max])), | ||
| 839 | - fperfdata ("ping", strtod(ret[qstat_ping_field], NULL), "", | ||
| 840 | + fperfdata ("ping", strtod(ret[qstat_ping_field], NULL), "", | ||
| 841 | FALSE, 0, FALSE, 0, | ||
| 842 | TRUE, 0, FALSE, 0)); | ||
| 843 | } | ||
| 844 | |||
| 845 | - /* close the pipe */ | ||
| 846 | - spclose (fp); | ||
| 847 | - | ||
| 848 | return result; | ||
| 849 | } | ||
| 850 | |||
| 851 | diff -urN ../nplg/plugins/check_hpjd.c ./plugins/check_hpjd.c | ||
| 852 | --- ../nplg/plugins/check_hpjd.c 2005-01-05 21:53:13.000000000 +0100 | ||
| 853 | +++ ./plugins/check_hpjd.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 854 | @@ -14,24 +14,22 @@ | ||
| 855 | * along with this program; if not, write to the Free Software | ||
| 856 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 857 | * | ||
| 858 | -* $Id: check_hpjd.c,v 1.31 2004/12/25 23:17:44 opensides Exp $ | ||
| 859 | +* $Id: check_hpjd.c,v 1.4 2005/06/06 11:37:06 exon Exp $ | ||
| 860 | *****************************************************************************/ | ||
| 861 | |||
| 862 | const char *progname = "check_hpjd"; | ||
| 863 | -const char *revision = "$Revision: 1.31 $"; | ||
| 864 | +const char *revision = "$Revision: 1.4 $"; | ||
| 865 | const char *copyright = "2000-2004"; | ||
| 866 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 867 | |||
| 868 | #include "common.h" | ||
| 869 | -#include "popen.h" | ||
| 870 | #include "utils.h" | ||
| 871 | #include "netutils.h" | ||
| 872 | +#include "runcmd.h" | ||
| 873 | |||
| 874 | #define DEFAULT_COMMUNITY "public" | ||
| 875 | |||
| 876 | - | ||
| 877 | -const char *option_summary = "-H host [-C community]\n"; | ||
| 878 | - | ||
| 879 | +/* this macro is only avavilable from main() */ | ||
| 880 | #define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1" | ||
| 881 | #define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2" | ||
| 882 | #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3" | ||
| 883 | @@ -44,42 +42,50 @@ | ||
| 884 | #define HPJD_GD_DOOR_OPEN ".1.3.6.1.4.1.11.2.3.9.1.1.2.17" | ||
| 885 | #define HPJD_GD_PAPER_OUTPUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.19" | ||
| 886 | #define HPJD_GD_STATUS_DISPLAY ".1.3.6.1.4.1.11.2.3.9.1.1.3" | ||
| 887 | +#define SNMP_VARS 12 | ||
| 888 | |||
| 889 | #define ONLINE 0 | ||
| 890 | #define OFFLINE 1 | ||
| 891 | |||
| 892 | -int process_arguments (int, char **); | ||
| 893 | -int validate_arguments (void); | ||
| 894 | -void print_help (void); | ||
| 895 | +/* make it compile without warnings when internationalization is disabled */ | ||
| 896 | +#ifndef ENABLE_NLS | ||
| 897 | +# ifdef _ | ||
| 898 | +# undef _ | ||
| 899 | +# endif | ||
| 900 | +# define _(x) x | ||
| 901 | +#endif | ||
| 902 | + | ||
| 903 | +static int process_arguments (int, char **); | ||
| 904 | +static int validate_arguments (void); | ||
| 905 | +static void print_help (void); | ||
| 906 | void print_usage (void); | ||
| 907 | |||
| 908 | -char *community = NULL; | ||
| 909 | -char *address = NULL; | ||
| 910 | +static char *community = NULL; | ||
| 911 | +static char *address = NULL; | ||
| 912 | + | ||
| 913 | |||
| 914 | int | ||
| 915 | main (int argc, char **argv) | ||
| 916 | { | ||
| 917 | char command_line[1024]; | ||
| 918 | - int result = STATE_UNKNOWN; | ||
| 919 | - int line; | ||
| 920 | - char input_buffer[MAX_INPUT_BUFFER]; | ||
| 921 | - char query_string[512]; | ||
| 922 | - char *errmsg; | ||
| 923 | - char *temp_buffer; | ||
| 924 | - int line_status = ONLINE; | ||
| 925 | - int paper_status = 0; | ||
| 926 | - int intervention_required = 0; | ||
| 927 | - int peripheral_error = 0; | ||
| 928 | - int paper_jam = 0; | ||
| 929 | - int paper_out = 0; | ||
| 930 | - int toner_low = 0; | ||
| 931 | - int page_punt = 0; | ||
| 932 | - int memory_out = 0; | ||
| 933 | - int door_open = 0; | ||
| 934 | - int paper_output = 0; | ||
| 935 | - char display_message[MAX_INPUT_BUFFER]; | ||
| 936 | - | ||
| 937 | - errmsg = malloc(MAX_INPUT_BUFFER); | ||
| 938 | + int result = STATE_OK; | ||
| 939 | + size_t i, val = 0; | ||
| 940 | + int errorflag = 0; /* bitflag error tracker */ | ||
| 941 | + output chld_out, chld_err; | ||
| 942 | + char *errmsg_strings[11]; | ||
| 943 | + | ||
| 944 | + /* populate the the error message array */ | ||
| 945 | + errmsg_strings[0] = _("Printer Offline"); | ||
| 946 | + errmsg_strings[1] = _("Unknown Paper Error"); | ||
| 947 | + errmsg_strings[2] = _("Intervention Required"); | ||
| 948 | + errmsg_strings[3] = _("Peripheral Error"); | ||
| 949 | + errmsg_strings[4] = _("Paper Jam"); | ||
| 950 | + errmsg_strings[5] = _("Out of Paper"); | ||
| 951 | + errmsg_strings[6] = _("Toner Low"); | ||
| 952 | + errmsg_strings[7] = _("Data too Slow for Engine"); | ||
| 953 | + errmsg_strings[8] = _("Insufficient Memory"); | ||
| 954 | + errmsg_strings[9] = _("A Door is Open"); | ||
| 955 | + errmsg_strings[10] = _("Output Tray is Full"); | ||
| 956 | |||
| 957 | setlocale (LC_ALL, ""); | ||
| 958 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 959 | @@ -88,206 +94,99 @@ | ||
| 960 | if (process_arguments (argc, argv) == ERROR) | ||
| 961 | usage4 (_("Could not parse arguments")); | ||
| 962 | |||
| 963 | - /* removed ' 2>1' at end of command 10/27/1999 - EG */ | ||
| 964 | - /* create the query string */ | ||
| 965 | - sprintf | ||
| 966 | - (query_string, | ||
| 967 | - "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0", | ||
| 968 | - HPJD_LINE_STATUS, | ||
| 969 | - HPJD_PAPER_STATUS, | ||
| 970 | - HPJD_INTERVENTION_REQUIRED, | ||
| 971 | - HPJD_GD_PERIPHERAL_ERROR, | ||
| 972 | - HPJD_GD_PAPER_JAM, | ||
| 973 | - HPJD_GD_PAPER_OUT, | ||
| 974 | - HPJD_GD_TONER_LOW, | ||
| 975 | - HPJD_GD_PAGE_PUNT, | ||
| 976 | - HPJD_GD_MEMORY_OUT, | ||
| 977 | - HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY); | ||
| 978 | - | ||
| 979 | - /* get the command to run */ | ||
| 980 | - sprintf (command_line, "%s -OQa -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community, | ||
| 981 | - address, query_string); | ||
| 982 | + /* create the command-line. Get status display line first, so we can | ||
| 983 | + * match line status against powersave as we parse it */ | ||
| 984 | + sprintf (command_line, "%s -Oqv -m : -v 1 -c %s %s " | ||
| 985 | + "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0", | ||
| 986 | + PATH_TO_SNMPGET, community, address, | ||
| 987 | + HPJD_GD_STATUS_DISPLAY, | ||
| 988 | + HPJD_LINE_STATUS, | ||
| 989 | + HPJD_PAPER_STATUS, | ||
| 990 | + HPJD_INTERVENTION_REQUIRED, | ||
| 991 | + HPJD_GD_PERIPHERAL_ERROR, | ||
| 992 | + HPJD_GD_PAPER_JAM, | ||
| 993 | + HPJD_GD_PAPER_OUT, | ||
| 994 | + HPJD_GD_TONER_LOW, | ||
| 995 | + HPJD_GD_PAGE_PUNT, | ||
| 996 | + HPJD_GD_MEMORY_OUT, | ||
| 997 | + HPJD_GD_DOOR_OPEN, | ||
| 998 | + HPJD_GD_PAPER_OUTPUT); | ||
| 999 | |||
| 1000 | /* run the command */ | ||
| 1001 | - child_process = spopen (command_line); | ||
| 1002 | - if (child_process == NULL) { | ||
| 1003 | - printf (_("Could not open pipe: %s\n"), command_line); | ||
| 1004 | - return STATE_UNKNOWN; | ||
| 1005 | - } | ||
| 1006 | - | ||
| 1007 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 1008 | - if (child_stderr == NULL) { | ||
| 1009 | - printf (_("Could not open stderr for %s\n"), command_line); | ||
| 1010 | - } | ||
| 1011 | - | ||
| 1012 | - result = STATE_OK; | ||
| 1013 | - | ||
| 1014 | - line = 0; | ||
| 1015 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
| 1016 | + if((np_runcmd(command_line, &chld_out, &chld_err, 0)) || chld_err.buflen) | ||
| 1017 | + result = STATE_WARNING; | ||
| 1018 | |||
| 1019 | - /* strip the newline character from the end of the input */ | ||
| 1020 | - if (input_buffer[strlen (input_buffer) - 1] == '\n') | ||
| 1021 | - input_buffer[strlen (input_buffer) - 1] = 0; | ||
| 1022 | - | ||
| 1023 | - line++; | ||
| 1024 | - | ||
| 1025 | - temp_buffer = strtok (input_buffer, "="); | ||
| 1026 | - temp_buffer = strtok (NULL, "="); | ||
| 1027 | - | ||
| 1028 | - if (temp_buffer == NULL) { | ||
| 1029 | - | ||
| 1030 | - result = STATE_UNKNOWN; | ||
| 1031 | - strcpy (errmsg, input_buffer); | ||
| 1032 | - | ||
| 1033 | - } else { | ||
| 1034 | - | ||
| 1035 | - switch (line) { | ||
| 1036 | - | ||
| 1037 | - case 1: /* 1st line should contain the line status */ | ||
| 1038 | - line_status = atoi (temp_buffer); | ||
| 1039 | - break; | ||
| 1040 | - case 2: /* 2nd line should contain the paper status */ | ||
| 1041 | - paper_status = atoi (temp_buffer); | ||
| 1042 | - break; | ||
| 1043 | - case 3: /* 3rd line should be intervention required */ | ||
| 1044 | - intervention_required = atoi (temp_buffer); | ||
| 1045 | - break; | ||
| 1046 | - case 4: /* 4th line should be peripheral error */ | ||
| 1047 | - peripheral_error = atoi (temp_buffer); | ||
| 1048 | - break; | ||
| 1049 | - case 5: /* 5th line should contain the paper jam status */ | ||
| 1050 | - paper_jam = atoi (temp_buffer); | ||
| 1051 | - break; | ||
| 1052 | - case 6: /* 6th line should contain the paper out status */ | ||
| 1053 | - paper_out = atoi (temp_buffer); | ||
| 1054 | - break; | ||
| 1055 | - case 7: /* 7th line should contain the toner low status */ | ||
| 1056 | - toner_low = atoi (temp_buffer); | ||
| 1057 | - break; | ||
| 1058 | - case 8: /* did data come too slow for engine */ | ||
| 1059 | - page_punt = atoi (temp_buffer); | ||
| 1060 | - break; | ||
| 1061 | - case 9: /* did we run out of memory */ | ||
| 1062 | - memory_out = atoi (temp_buffer); | ||
| 1063 | - break; | ||
| 1064 | - case 10: /* is there a door open */ | ||
| 1065 | - door_open = atoi (temp_buffer); | ||
| 1066 | - break; | ||
| 1067 | - case 11: /* is output tray full */ | ||
| 1068 | - paper_output = atoi (temp_buffer); | ||
| 1069 | - break; | ||
| 1070 | - case 12: /* display panel message */ | ||
| 1071 | - strcpy (display_message, temp_buffer + 1); | ||
| 1072 | - break; | ||
| 1073 | - default: | ||
| 1074 | - break; | ||
| 1075 | - } | ||
| 1076 | - | ||
| 1077 | - } | ||
| 1078 | - | ||
| 1079 | - /* break out of the read loop if we encounter an error */ | ||
| 1080 | - if (result != STATE_OK) | ||
| 1081 | - break; | ||
| 1082 | - } | ||
| 1083 | + /* if there was none or not enough output, display an error and exit */ | ||
| 1084 | + if (chld_out.buflen == 0 || chld_out.lines != SNMP_VARS - 1) { | ||
| 1085 | + if(chld_err.buflen) printf("%s : ", chld_err.line[0]); | ||
| 1086 | |||
| 1087 | - /* WARNING if output found on stderr */ | ||
| 1088 | - if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | ||
| 1089 | - result = max_state (result, STATE_WARNING); | ||
| 1090 | - /* remove CRLF */ | ||
| 1091 | - if (input_buffer[strlen (input_buffer) - 1] == '\n') | ||
| 1092 | - input_buffer[strlen (input_buffer) - 1] = 0; | ||
| 1093 | - sprintf (errmsg, "%s", input_buffer ); | ||
| 1094 | + if(chld_out.buflen == 0) | ||
| 1095 | + printf (_("No data returned from %s\n"), address ); | ||
| 1096 | + else | ||
| 1097 | + printf(_("Not enough data returned from %s\n"), address); | ||
| 1098 | |||
| 1099 | - } | ||
| 1100 | - | ||
| 1101 | - /* close stderr */ | ||
| 1102 | - (void) fclose (child_stderr); | ||
| 1103 | - | ||
| 1104 | - /* close the pipe */ | ||
| 1105 | - if (spclose (child_process)) | ||
| 1106 | - result = max_state (result, STATE_WARNING); | ||
| 1107 | - | ||
| 1108 | - /* if there wasn't any output, display an error */ | ||
| 1109 | - if (line == 0) { | ||
| 1110 | - | ||
| 1111 | - /* might not be the problem, but most likely is. */ | ||
| 1112 | - result = STATE_UNKNOWN ; | ||
| 1113 | - asprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address ); | ||
| 1114 | - | ||
| 1115 | + return STATE_UNKNOWN; | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | - /* if we had no read errors, check the printer status results... */ | ||
| 1119 | - if (result == STATE_OK) { | ||
| 1120 | + for(i = 1; i < chld_out.lines; i++) { | ||
| 1121 | + /* move to next if there's no error tag */ | ||
| 1122 | + if(!(val = (size_t)strtol(chld_out.line[i], NULL, 0))) | ||
| 1123 | + continue; | ||
| 1124 | + | ||
| 1125 | + /* tag the error */ | ||
| 1126 | + errorflag |= 1 << i; | ||
| 1127 | + | ||
| 1128 | + /* this switch statement is only for additional exception handling */ | ||
| 1129 | + switch (i) { | ||
| 1130 | + case 1: /* line status */ | ||
| 1131 | + /* clear this flag if it's powersaving (we know it's set if | ||
| 1132 | + * we get here, so use xor and save 2 instructions) */ | ||
| 1133 | + if(!strstr(chld_out.line[0], "POWERSAVE ON")) | ||
| 1134 | + errorflag ^= 1 << i; | ||
| 1135 | + break; | ||
| 1136 | |||
| 1137 | - if (paper_jam) { | ||
| 1138 | - result = STATE_WARNING; | ||
| 1139 | - strcpy (errmsg, _("Paper Jam")); | ||
| 1140 | - } | ||
| 1141 | - else if (paper_out) { | ||
| 1142 | - result = STATE_WARNING; | ||
| 1143 | - strcpy (errmsg, _("Out of Paper")); | ||
| 1144 | - } | ||
| 1145 | - else if (line_status == OFFLINE) { | ||
| 1146 | - if (strcmp (errmsg, "POWERSAVE ON") != 0) { | ||
| 1147 | - result = STATE_WARNING; | ||
| 1148 | - strcpy (errmsg, _("Printer Offline")); | ||
| 1149 | - } | ||
| 1150 | - } | ||
| 1151 | - else if (peripheral_error) { | ||
| 1152 | - result = STATE_WARNING; | ||
| 1153 | - strcpy (errmsg, _("Peripheral Error")); | ||
| 1154 | - } | ||
| 1155 | - else if (intervention_required) { | ||
| 1156 | - result = STATE_WARNING; | ||
| 1157 | - strcpy (errmsg, _("Intervention Required")); | ||
| 1158 | - } | ||
| 1159 | - else if (toner_low) { | ||
| 1160 | - result = STATE_WARNING; | ||
| 1161 | - strcpy (errmsg, _("Toner Low")); | ||
| 1162 | - } | ||
| 1163 | - else if (memory_out) { | ||
| 1164 | - result = STATE_WARNING; | ||
| 1165 | - strcpy (errmsg, _("Insufficient Memory")); | ||
| 1166 | - } | ||
| 1167 | - else if (door_open) { | ||
| 1168 | - result = STATE_WARNING; | ||
| 1169 | - strcpy (errmsg, _("A Door is Open")); | ||
| 1170 | - } | ||
| 1171 | - else if (paper_output) { | ||
| 1172 | - result = STATE_WARNING; | ||
| 1173 | - strcpy (errmsg, _("Output Tray is Full")); | ||
| 1174 | - } | ||
| 1175 | - else if (page_punt) { | ||
| 1176 | - result = STATE_WARNING; | ||
| 1177 | - strcpy (errmsg, _("Data too Slow for Engine")); | ||
| 1178 | - } | ||
| 1179 | - else if (paper_status) { | ||
| 1180 | - result = STATE_WARNING; | ||
| 1181 | - strcpy (errmsg, _("Unknown Paper Error")); | ||
| 1182 | + case 2: /* paper status */ | ||
| 1183 | + case 3: /* intervention required */ | ||
| 1184 | + case 4: /* peripheral error */ | ||
| 1185 | + case 5: /* paper jam */ | ||
| 1186 | + case 6: /* paper out */ | ||
| 1187 | + case 7: /* toner low */ | ||
| 1188 | + case 8: /* data came too slow for engine */ | ||
| 1189 | + case 9: /* out of memory */ | ||
| 1190 | + case 10: /* door open */ | ||
| 1191 | + case 11: /* output tray full */ | ||
| 1192 | + break; | ||
| 1193 | } | ||
| 1194 | } | ||
| 1195 | |||
| 1196 | - if (result == STATE_OK) | ||
| 1197 | - printf (_("Printer ok - (%s)\n"), display_message); | ||
| 1198 | - | ||
| 1199 | - else if (result == STATE_UNKNOWN) { | ||
| 1200 | + /* if some error occurred, print a starting line and all the errors */ | ||
| 1201 | + if(errorflag) { | ||
| 1202 | + printf ("%s (%s)", chld_err.buflen != 0 ? chld_err.buf : "", | ||
| 1203 | + chld_out.line[0]); | ||
| 1204 | |||
| 1205 | - printf ("%s\n", errmsg); | ||
| 1206 | + for(i = 0; i < SNMP_VARS; i++) { /* loop the error flags */ | ||
| 1207 | + if((errorflag >> i) & 1) /* only print if flag is set */ | ||
| 1208 | + printf(" :: %s", errmsg_strings[i++]); | ||
| 1209 | + } | ||
| 1210 | |||
| 1211 | - /* if printer could not be reached, escalate to critical */ | ||
| 1212 | - if (strstr (errmsg, "Timeout")) | ||
| 1213 | - result = STATE_CRITICAL; | ||
| 1214 | + return STATE_CRITICAL; | ||
| 1215 | } | ||
| 1216 | |||
| 1217 | - else if (result == STATE_WARNING) | ||
| 1218 | - printf ("%s (%s)\n", errmsg, display_message); | ||
| 1219 | + /* set WARNING if output on stderr */ | ||
| 1220 | + if (chld_err.buflen) { | ||
| 1221 | + printf("WARNING - Printer seems ok, but %s printed to stderr (%s)", | ||
| 1222 | + PATH_TO_SNMPGET, chld_err.line[0]); | ||
| 1223 | + return STATE_WARNING; | ||
| 1224 | + } | ||
| 1225 | |||
| 1226 | - return result; | ||
| 1227 | + /* all is well if we end up here */ | ||
| 1228 | + printf (_("Printer ok - (%s)\n"), chld_out.line[0]); | ||
| 1229 | + return STATE_OK; | ||
| 1230 | } | ||
| 1231 | |||
| 1232 | |||
| 1233 | /* process command-line arguments */ | ||
| 1234 | -int | ||
| 1235 | +static int | ||
| 1236 | process_arguments (int argc, char **argv) | ||
| 1237 | { | ||
| 1238 | int c; | ||
| 1239 | @@ -296,9 +195,6 @@ | ||
| 1240 | static struct option longopts[] = { | ||
| 1241 | {"hostname", required_argument, 0, 'H'}, | ||
| 1242 | {"community", required_argument, 0, 'C'}, | ||
| 1243 | -/* {"critical", required_argument,0,'c'}, */ | ||
| 1244 | -/* {"warning", required_argument,0,'w'}, */ | ||
| 1245 | -/* {"port", required_argument,0,'P'}, */ | ||
| 1246 | {"version", no_argument, 0, 'V'}, | ||
| 1247 | {"help", no_argument, 0, 'h'}, | ||
| 1248 | {0, 0, 0, 0} | ||
| 1249 | @@ -307,7 +203,6 @@ | ||
| 1250 | if (argc < 2) | ||
| 1251 | return ERROR; | ||
| 1252 | |||
| 1253 | - | ||
| 1254 | while (1) { | ||
| 1255 | c = getopt_long (argc, argv, "+hVH:C:", longopts, &option); | ||
| 1256 | |||
| 1257 | @@ -317,7 +212,7 @@ | ||
| 1258 | switch (c) { | ||
| 1259 | case 'H': /* hostname */ | ||
| 1260 | if (is_host (optarg)) { | ||
| 1261 | - address = strscpy(address, optarg) ; | ||
| 1262 | + address = strdup(optarg) ; | ||
| 1263 | } | ||
| 1264 | else { | ||
| 1265 | usage2 (_("Invalid hostname/address"), optarg); | ||
| 1266 | @@ -346,26 +241,26 @@ | ||
| 1267 | usage2 (_("Invalid hostname/address"), argv[c]); | ||
| 1268 | } | ||
| 1269 | } | ||
| 1270 | - | ||
| 1271 | + | ||
| 1272 | if (community == NULL) { | ||
| 1273 | if (argv[c] != NULL ) | ||
| 1274 | community = argv[c]; | ||
| 1275 | else | ||
| 1276 | - community = strdup (DEFAULT_COMMUNITY); | ||
| 1277 | + community = DEFAULT_COMMUNITY; | ||
| 1278 | } | ||
| 1279 | |||
| 1280 | return validate_arguments (); | ||
| 1281 | } | ||
| 1282 | |||
| 1283 | |||
| 1284 | -int | ||
| 1285 | +static int | ||
| 1286 | validate_arguments (void) | ||
| 1287 | { | ||
| 1288 | return OK; | ||
| 1289 | } | ||
| 1290 | |||
| 1291 | |||
| 1292 | -void | ||
| 1293 | +static void | ||
| 1294 | print_help (void) | ||
| 1295 | { | ||
| 1296 | print_revision (progname, revision); | ||
| 1297 | @@ -389,7 +284,6 @@ | ||
| 1298 | } | ||
| 1299 | |||
| 1300 | |||
| 1301 | - | ||
| 1302 | void | ||
| 1303 | print_usage (void) | ||
| 1304 | { | ||
| 1305 | diff -urN ../nplg/plugins/check_load.c ./plugins/check_load.c | ||
| 1306 | --- ../nplg/plugins/check_load.c 2005-05-30 17:44:13.000000000 +0200 | ||
| 1307 | +++ ./plugins/check_load.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 1308 | @@ -14,18 +14,18 @@ | ||
| 1309 | along with this program; if not, write to the Free Software | ||
| 1310 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 1311 | |||
| 1312 | - $Id: check_load.c,v 1.28 2005/05/28 01:21:54 seanius Exp $ | ||
| 1313 | + $Id: check_load.c,v 1.3 2005/06/05 21:55:26 exon Exp $ | ||
| 1314 | |||
| 1315 | ******************************************************************************/ | ||
| 1316 | |||
| 1317 | const char *progname = "check_load"; | ||
| 1318 | -const char *revision = "$Revision: 1.28 $"; | ||
| 1319 | +const char *revision = "$Revision: 1.3 $"; | ||
| 1320 | const char *copyright = "1999-2004"; | ||
| 1321 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 1322 | |||
| 1323 | #include "common.h" | ||
| 1324 | #include "utils.h" | ||
| 1325 | -#include "popen.h" | ||
| 1326 | +#include "runcmd.h" | ||
| 1327 | |||
| 1328 | #ifdef HAVE_SYS_LOADAVG_H | ||
| 1329 | #include <sys/loadavg.h> | ||
| 1330 | @@ -47,12 +47,8 @@ | ||
| 1331 | /* strictly for pretty-print usage in loops */ | ||
| 1332 | static const int nums[3] = { 1, 5, 15 }; | ||
| 1333 | |||
| 1334 | -/* provide some fairly sane defaults */ | ||
| 1335 | double wload[3] = { 0.0, 0.0, 0.0 }; | ||
| 1336 | double cload[3] = { 0.0, 0.0, 0.0 }; | ||
| 1337 | -#define la1 la[0] | ||
| 1338 | -#define la5 la[1] | ||
| 1339 | -#define la15 la[2] | ||
| 1340 | |||
| 1341 | char *status_line; | ||
| 1342 | |||
| 1343 | @@ -86,14 +82,16 @@ | ||
| 1344 | main (int argc, char **argv) | ||
| 1345 | { | ||
| 1346 | int result; | ||
| 1347 | - int i; | ||
| 1348 | + size_t i; /* size_t so we can match against chld_out.lines if need be */ | ||
| 1349 | |||
| 1350 | double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about unitialized arrays */ | ||
| 1351 | #ifndef HAVE_GETLOADAVG | ||
| 1352 | - char input_buffer[MAX_INPUT_BUFFER]; | ||
| 1353 | # ifdef HAVE_PROC_LOADAVG | ||
| 1354 | + char input_buffer[MAX_INPUT_BUFFER]; | ||
| 1355 | FILE *fp; | ||
| 1356 | char *str, *next; | ||
| 1357 | +# else | ||
| 1358 | + output chld_out, chld_err; | ||
| 1359 | # endif | ||
| 1360 | #endif | ||
| 1361 | |||
| 1362 | @@ -126,23 +124,11 @@ | ||
| 1363 | |||
| 1364 | fclose (fp); | ||
| 1365 | # else | ||
| 1366 | - child_process = spopen (PATH_TO_UPTIME); | ||
| 1367 | - if (child_process == NULL) { | ||
| 1368 | - printf (_("Error opening %s\n"), PATH_TO_UPTIME); | ||
| 1369 | - return STATE_UNKNOWN; | ||
| 1370 | - } | ||
| 1371 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 1372 | - if (child_stderr == NULL) { | ||
| 1373 | - printf (_("Could not open stderr for %s\n"), PATH_TO_UPTIME); | ||
| 1374 | - } | ||
| 1375 | - fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); | ||
| 1376 | - sscanf (input_buffer, "%*[^l]load average: %f, %f, %f", &la1, &la5, &la15); | ||
| 1377 | + if((result = np_runcmd(PATH_TO_UPTIME, &chld_out, &chld_err, 0)) || chld_err.buflen) | ||
| 1378 | + die(STATE_UNKNOWN, _("Error code %d returned in %s\n"), | ||
| 1379 | + result, PATH_TO_UPTIME); | ||
| 1380 | |||
| 1381 | - result = spclose (child_process); | ||
| 1382 | - if (result) { | ||
| 1383 | - printf (_("Error code %d returned in %s\n"), result, PATH_TO_UPTIME); | ||
| 1384 | - return STATE_UNKNOWN; | ||
| 1385 | - } | ||
| 1386 | + sscanf (chld_out.buf, "%*[^l]load average: %f, %f, %f", &la[0], &la[1], &la[2]); | ||
| 1387 | # endif | ||
| 1388 | #endif | ||
| 1389 | |||
| 1390 | @@ -162,17 +148,17 @@ | ||
| 1391 | /* we got this far, so assume OK until we've measured */ | ||
| 1392 | result = STATE_OK; | ||
| 1393 | |||
| 1394 | - asprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15); | ||
| 1395 | - | ||
| 1396 | for(i = 0; i < 3; i++) { | ||
| 1397 | if(la[i] > cload[i]) { | ||
| 1398 | result = STATE_CRITICAL; | ||
| 1399 | break; | ||
| 1400 | } | ||
| 1401 | - else if(la[i] > wload[i]) result = STATE_WARNING; | ||
| 1402 | + | ||
| 1403 | + if(la[i] > wload[i]) result = STATE_WARNING; | ||
| 1404 | } | ||
| 1405 | |||
| 1406 | - printf("%s - %s|", state_text(result), status_line); | ||
| 1407 | + printf(_("%s - load average: %.2f, %.2f, %.2f|"), | ||
| 1408 | + state_text(result), la[0], la[1], la[2]); | ||
| 1409 | for(i = 0; i < 3; i++) | ||
| 1410 | printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]); | ||
| 1411 | |||
| 1412 | @@ -248,14 +234,13 @@ | ||
| 1413 | int i = 0; | ||
| 1414 | |||
| 1415 | /* match cload first, as it will give the most friendly error message | ||
| 1416 | - * if user hasn't given the -c switch properly */ | ||
| 1417 | + * if user hasn't given the -c switch properly. Don't make sure wload[i] | ||
| 1418 | + * < cload[i] as it prevents users from forcing a critical state */ | ||
| 1419 | for(i = 0; i < 3; i++) { | ||
| 1420 | - if(cload[i] < 0) | ||
| 1421 | + if(cload[i] == 0.0) | ||
| 1422 | die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]); | ||
| 1423 | - if(wload[i] < 0) | ||
| 1424 | + if(wload[i] == 0.0) | ||
| 1425 | die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]); | ||
| 1426 | - if(wload[i] > cload[i]) | ||
| 1427 | - die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]); | ||
| 1428 | } | ||
| 1429 | |||
| 1430 | return OK; | ||
| 1431 | diff -urN ../nplg/plugins/check_nagios.c ./plugins/check_nagios.c | ||
| 1432 | --- ../nplg/plugins/check_nagios.c 2005-05-26 14:12:21.000000000 +0200 | ||
| 1433 | +++ ./plugins/check_nagios.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 1434 | @@ -14,17 +14,17 @@ | ||
| 1435 | along with this program; if not, write to the Free Software | ||
| 1436 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 1437 | |||
| 1438 | - $Id: check_nagios.c,v 1.26 2005/05/25 00:43:20 seanius Exp $ | ||
| 1439 | + $Id: check_nagios.c,v 1.3 2005/06/06 11:37:06 exon Exp $ | ||
| 1440 | |||
| 1441 | ******************************************************************************/ | ||
| 1442 | |||
| 1443 | const char *progname = "check_nagios"; | ||
| 1444 | -const char *revision = "$Revision: 1.26 $"; | ||
| 1445 | +const char *revision = "$Revision: 1.3 $"; | ||
| 1446 | const char *copyright = "1999-2004"; | ||
| 1447 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 1448 | |||
| 1449 | #include "common.h" | ||
| 1450 | -#include "popen.h" | ||
| 1451 | +#include "runcmd.h" | ||
| 1452 | #include "utils.h" | ||
| 1453 | |||
| 1454 | int process_arguments (int, char **); | ||
| 1455 | @@ -55,6 +55,8 @@ | ||
| 1456 | int procrss = 0; | ||
| 1457 | float procpcpu = 0; | ||
| 1458 | char procstat[8]; | ||
| 1459 | + /* procetime is unused in most configurations, but may be in PS_VAR_LIST | ||
| 1460 | + * so it must be here in spite of it producing compiler warnings */ | ||
| 1461 | char procetime[MAX_INPUT_BUFFER]; | ||
| 1462 | char procprog[MAX_INPUT_BUFFER]; | ||
| 1463 | char *procargs; | ||
| 1464 | @@ -62,6 +64,8 @@ | ||
| 1465 | int expected_cols = PS_COLS - 1; | ||
| 1466 | const char *zombie = "Z"; | ||
| 1467 | char *temp_string; | ||
| 1468 | + output chld_out, chld_err; | ||
| 1469 | + size_t i; | ||
| 1470 | |||
| 1471 | setlocale (LC_ALL, ""); | ||
| 1472 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 1473 | @@ -99,40 +103,30 @@ | ||
| 1474 | printf(_("command: %s\n"), PS_COMMAND); | ||
| 1475 | |||
| 1476 | /* run the command to check for the Nagios process.. */ | ||
| 1477 | - child_process = spopen (PS_COMMAND); | ||
| 1478 | - if (child_process == NULL) { | ||
| 1479 | - printf (_("Could not open pipe: %s\n"), PS_COMMAND); | ||
| 1480 | - return STATE_UNKNOWN; | ||
| 1481 | - } | ||
| 1482 | - | ||
| 1483 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 1484 | - if (child_stderr == NULL) { | ||
| 1485 | - printf (_("Could not open stderr for %s\n"), PS_COMMAND); | ||
| 1486 | - } | ||
| 1487 | - | ||
| 1488 | - fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); | ||
| 1489 | + if((result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0)) != 0) | ||
| 1490 | + result = STATE_WARNING; | ||
| 1491 | |||
| 1492 | /* count the number of matching Nagios processes... */ | ||
| 1493 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
| 1494 | - cols = sscanf (input_buffer, PS_FORMAT, PS_VARLIST); | ||
| 1495 | - /* Zombie processes do not give a procprog command */ | ||
| 1496 | - if ( cols == (expected_cols - 1) && strstr(procstat, zombie) ) { | ||
| 1497 | - cols = expected_cols; | ||
| 1498 | - /* Set some value for procargs for the strip command further below | ||
| 1499 | - Seen to be a problem on some Solaris 7 and 8 systems */ | ||
| 1500 | - input_buffer[pos] = '\n'; | ||
| 1501 | - input_buffer[pos+1] = 0x0; | ||
| 1502 | - } | ||
| 1503 | + for(i = 0; i < chld_out.lines; i++) { | ||
| 1504 | + cols = sscanf (chld_out.line[i], PS_FORMAT, PS_VARLIST); | ||
| 1505 | + /* Zombie processes do not give a procprog command */ | ||
| 1506 | + if ( cols == (expected_cols - 1) && strstr(procstat, zombie) ) { | ||
| 1507 | + cols = expected_cols; | ||
| 1508 | + /* Set some value for procargs for the strip command further below | ||
| 1509 | + * Seen to be a problem on some Solaris 7 and 8 systems */ | ||
| 1510 | + chld_out.line[i][pos] = '\n'; | ||
| 1511 | + chld_out.line[i][pos+1] = 0x0; | ||
| 1512 | + } | ||
| 1513 | if ( cols >= expected_cols ) { | ||
| 1514 | - asprintf (&procargs, "%s", input_buffer + pos); | ||
| 1515 | + asprintf (&procargs, "%s", chld_out.line[i] + pos); | ||
| 1516 | strip (procargs); | ||
| 1517 | - | ||
| 1518 | + | ||
| 1519 | /* Some ps return full pathname for command. This removes path */ | ||
| 1520 | - temp_string = strtok ((char *)procprog, "/"); | ||
| 1521 | - while (temp_string) { | ||
| 1522 | - strcpy(procprog, temp_string); | ||
| 1523 | - temp_string = strtok (NULL, "/"); | ||
| 1524 | - } | ||
| 1525 | + temp_string = strtok ((char *)procprog, "/"); | ||
| 1526 | + while (temp_string) { | ||
| 1527 | + strcpy(procprog, temp_string); | ||
| 1528 | + temp_string = strtok (NULL, "/"); | ||
| 1529 | + } | ||
| 1530 | |||
| 1531 | /* May get empty procargs */ | ||
| 1532 | if (!strstr(procargs, argv[0]) && strstr(procargs, process_string) && strcmp(procargs,"")) { | ||
| 1533 | @@ -145,14 +139,7 @@ | ||
| 1534 | } | ||
| 1535 | |||
| 1536 | /* If we get anything on stderr, at least set warning */ | ||
| 1537 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
| 1538 | - result = max_state (result, STATE_WARNING); | ||
| 1539 | - | ||
| 1540 | - /* close stderr */ | ||
| 1541 | - (void) fclose (child_stderr); | ||
| 1542 | - | ||
| 1543 | - /* close the pipe */ | ||
| 1544 | - if (spclose (child_process)) | ||
| 1545 | + if(chld_err.buflen) | ||
| 1546 | result = max_state (result, STATE_WARNING); | ||
| 1547 | |||
| 1548 | /* reset the alarm handler */ | ||
| 1549 | diff -urN ../nplg/plugins/check_ping.c ./plugins/check_ping.c | ||
| 1550 | --- ../nplg/plugins/check_ping.c 2005-05-26 14:12:21.000000000 +0200 | ||
| 1551 | +++ ./plugins/check_ping.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 1552 | @@ -14,18 +14,18 @@ | ||
| 1553 | along with this program; if not, write to the Free Software | ||
| 1554 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 1555 | |||
| 1556 | - $Id: check_ping.c,v 1.44 2005/05/25 14:25:55 sghosh Exp $ | ||
| 1557 | + $Id: check_ping.c,v 1.2 2005/06/05 21:55:26 exon Exp $ | ||
| 1558 | |||
| 1559 | ******************************************************************************/ | ||
| 1560 | |||
| 1561 | const char *progname = "check_ping"; | ||
| 1562 | -const char *revision = "$Revision: 1.44 $"; | ||
| 1563 | +const char *revision = "$Revision: 1.2 $"; | ||
| 1564 | const char *copyright = "2000-2004"; | ||
| 1565 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 1566 | |||
| 1567 | #include "common.h" | ||
| 1568 | #include "netutils.h" | ||
| 1569 | -#include "popen.h" | ||
| 1570 | +#include "runcmd.h" | ||
| 1571 | #include "utils.h" | ||
| 1572 | |||
| 1573 | #define WARN_DUPLICATES "DUPLICATES FOUND! " | ||
| 1574 | @@ -40,7 +40,7 @@ | ||
| 1575 | int get_threshold (char *, float *, int *); | ||
| 1576 | int validate_arguments (void); | ||
| 1577 | int run_ping (const char *cmd, const char *addr); | ||
| 1578 | -int error_scan (char buf[MAX_INPUT_BUFFER], const char *addr); | ||
| 1579 | +int error_scan (const char *buf, const char *addr); | ||
| 1580 | void print_usage (void); | ||
| 1581 | void print_help (void); | ||
| 1582 | |||
| 1583 | @@ -58,7 +58,7 @@ | ||
| 1584 | float rta = UNKNOWN_TRIP_TIME; | ||
| 1585 | int pl = UNKNOWN_PACKET_LOSS; | ||
| 1586 | |||
| 1587 | -char *warn_text; | ||
| 1588 | +char *warn_text = NULL; | ||
| 1589 | |||
| 1590 | |||
| 1591 | |||
| 1592 | @@ -95,10 +95,8 @@ | ||
| 1593 | if (is_inet6_addr(addresses[i]) && address_family != AF_INET) | ||
| 1594 | rawcmd = strdup(PING6_COMMAND); | ||
| 1595 | else | ||
| 1596 | - rawcmd = strdup(PING_COMMAND); | ||
| 1597 | -#else | ||
| 1598 | - rawcmd = strdup(PING_COMMAND); | ||
| 1599 | #endif | ||
| 1600 | + rawcmd = strdup(PING_COMMAND); | ||
| 1601 | |||
| 1602 | /* does the host address of number of packets argument come first? */ | ||
| 1603 | #ifdef PING_PACKETS_FIRST | ||
| 1604 | @@ -119,8 +117,7 @@ | ||
| 1605 | |||
| 1606 | if (pl == UNKNOWN_PACKET_LOSS || rta < 0.0) { | ||
| 1607 | printf ("%s\n", cmd); | ||
| 1608 | - die (STATE_UNKNOWN, | ||
| 1609 | - _("CRITICAL - Could not interpret output from ping command\n")); | ||
| 1610 | + die (STATE_UNKNOWN, _("CRITICAL - Could not interpret output from ping command\n")); | ||
| 1611 | } | ||
| 1612 | |||
| 1613 | if (pl >= cpl || rta >= crta || rta < 0) | ||
| 1614 | @@ -136,11 +133,11 @@ | ||
| 1615 | if (display_html == TRUE) | ||
| 1616 | printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]); | ||
| 1617 | if (pl == 100) | ||
| 1618 | - printf (_("PING %s - %sPacket loss = %d%%"), state_text (this_result), warn_text, | ||
| 1619 | - pl); | ||
| 1620 | + printf (_("PING %s - %sPacket loss = %d%%"), | ||
| 1621 | + state_text(this_result), warn_text ? warn_text : " ", pl); | ||
| 1622 | else | ||
| 1623 | printf (_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"), | ||
| 1624 | - state_text (this_result), warn_text, pl, rta); | ||
| 1625 | + state_text (this_result), warn_text ? warn_text : " ", pl, rta); | ||
| 1626 | if (display_html == TRUE) | ||
| 1627 | printf ("</A>"); | ||
| 1628 | printf ("\n"); | ||
| 1629 | @@ -399,42 +396,42 @@ | ||
| 1630 | int | ||
| 1631 | run_ping (const char *cmd, const char *addr) | ||
| 1632 | { | ||
| 1633 | - char buf[MAX_INPUT_BUFFER]; | ||
| 1634 | int result = STATE_UNKNOWN; | ||
| 1635 | - | ||
| 1636 | - if ((child_process = spopen (cmd)) == NULL) | ||
| 1637 | - die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd); | ||
| 1638 | - | ||
| 1639 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 1640 | - if (child_stderr == NULL) | ||
| 1641 | - printf (_("Cannot open stderr for %s\n"), cmd); | ||
| 1642 | - | ||
| 1643 | - while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) { | ||
| 1644 | - | ||
| 1645 | - result = max_state (result, error_scan (buf, addr)); | ||
| 1646 | - | ||
| 1647 | - /* get the percent loss statistics */ | ||
| 1648 | - if(sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl)==1 || | ||
| 1649 | - sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl)==1 || | ||
| 1650 | - sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl)==1 || | ||
| 1651 | - sscanf(buf,"%*d packets transmitted, %*d received, %d%% loss, time", &pl)==1 || | ||
| 1652 | - sscanf(buf,"%*d packets transmitted, %*d received, %d%% packet loss, time", &pl)==1 || | ||
| 1653 | - sscanf(buf,"%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss", &pl) == 1 || | ||
| 1654 | - sscanf(buf,"%*d packets transmitted %*d received, +%*d errors, %d%% packet loss", &pl) == 1 | ||
| 1655 | - ) | ||
| 1656 | + output chld_out, chld_err; | ||
| 1657 | + char *p; | ||
| 1658 | + size_t mult = 1, i; | ||
| 1659 | + | ||
| 1660 | + if((result = np_runcmd(cmd, &chld_out, &chld_err, 0)) != 0) | ||
| 1661 | + result = STATE_WARNING; | ||
| 1662 | + | ||
| 1663 | + for(i = 0; i < chld_out.lines; i++) { | ||
| 1664 | + /* get the packet loss. Find the % sign and count backwards */ | ||
| 1665 | + if((p = memchr(chld_out.line[i], '%', chld_out.lens[i]))) { | ||
| 1666 | + p--; | ||
| 1667 | + pl = 0; | ||
| 1668 | + while(p != chld_out.line[i] && isdigit(*p)) { | ||
| 1669 | + pl += *p - '0' * mult; | ||
| 1670 | + mult *= 10; | ||
| 1671 | + p--; | ||
| 1672 | + } | ||
| 1673 | continue; | ||
| 1674 | - | ||
| 1675 | + } | ||
| 1676 | /* get the round trip average */ | ||
| 1677 | - else | ||
| 1678 | - if(sscanf(buf,"round-trip min/avg/max = %*f/%f/%*f",&rta)==1 || | ||
| 1679 | - sscanf(buf,"round-trip min/avg/max/mdev = %*f/%f/%*f/%*f",&rta)==1 || | ||
| 1680 | - sscanf(buf,"round-trip min/avg/max/sdev = %*f/%f/%*f/%*f",&rta)==1 || | ||
| 1681 | - sscanf(buf,"round-trip min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 || | ||
| 1682 | - sscanf(buf,"round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f",&rta)==1 || | ||
| 1683 | - sscanf(buf,"round-trip (ms) min/avg/max = %*f/%f/%*f",&rta)==1 || | ||
| 1684 | - sscanf(buf,"round-trip (ms) min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 || | ||
| 1685 | - sscanf(buf,"rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms",&rta)==1) | ||
| 1686 | - continue; | ||
| 1687 | + else { | ||
| 1688 | + if(sscanf(chld_out.line[i], "round-trip min/avg/max = %*f/%f/%*f", &rta) == 1 || | ||
| 1689 | + sscanf(chld_out.line[i], "round-trip min/avg/max/mdev = %*f/%f/%*f/%*f", &rta) == 1 || | ||
| 1690 | + sscanf(chld_out.line[i], "round-trip min/avg/max/sdev = %*f/%f/%*f/%*f", &rta) == 1 || | ||
| 1691 | + sscanf(chld_out.line[i], "round-trip min/avg/max/stddev = %*f/%f/%*f/%*f", &rta) == 1 || | ||
| 1692 | + sscanf(chld_out.line[i], "round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f", &rta) == 1 || | ||
| 1693 | + sscanf(chld_out.line[i], "round-trip (ms) min/avg/max = %*f/%f/%*f", &rta) == 1 || | ||
| 1694 | + sscanf(chld_out.line[i], "round-trip (ms) min/avg/max/stddev = %*f/%f/%*f/%*f", &rta) == 1 || | ||
| 1695 | + sscanf(chld_out.line[i], "rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms", &rta) == 1) | ||
| 1696 | + { | ||
| 1697 | + continue; | ||
| 1698 | + } | ||
| 1699 | + | ||
| 1700 | + result = max_state (result, error_scan (chld_out.line[i], addr)); | ||
| 1701 | + } | ||
| 1702 | } | ||
| 1703 | |||
| 1704 | /* this is needed because there is no rta if all packets are lost */ | ||
| 1705 | @@ -442,19 +439,10 @@ | ||
| 1706 | rta = crta; | ||
| 1707 | |||
| 1708 | /* check stderr, setting at least WARNING if there is output here */ | ||
| 1709 | - while (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
| 1710 | - if (! strstr(buf,"WARNING - no SO_TIMESTAMP support, falling back to SIOCGSTAMP")) | ||
| 1711 | - result = max_state (STATE_WARNING, error_scan (buf, addr)); | ||
| 1712 | - | ||
| 1713 | - (void) fclose (child_stderr); | ||
| 1714 | - | ||
| 1715 | - | ||
| 1716 | - /* close the pipe - WARNING if status is set */ | ||
| 1717 | - if (spclose (child_process)) | ||
| 1718 | - result = max_state (result, STATE_WARNING); | ||
| 1719 | - | ||
| 1720 | - if (warn_text == NULL) | ||
| 1721 | - warn_text = strdup(""); | ||
| 1722 | + if(chld_err.buflen > 0) { | ||
| 1723 | + if (! strstr(chld_err.line[0], "WARNING - no SO_TIMESTAMP support, falling back to SIOCGSTAMP")) | ||
| 1724 | + result = max_state (STATE_WARNING, error_scan (chld_err.line[0], addr)); | ||
| 1725 | + } | ||
| 1726 | |||
| 1727 | return result; | ||
| 1728 | } | ||
| 1729 | @@ -462,7 +450,7 @@ | ||
| 1730 | |||
| 1731 | |||
| 1732 | int | ||
| 1733 | -error_scan (char buf[MAX_INPUT_BUFFER], const char *addr) | ||
| 1734 | +error_scan (const char *buf, const char *addr) | ||
| 1735 | { | ||
| 1736 | if (strstr (buf, "Network is unreachable")) | ||
| 1737 | die (STATE_CRITICAL, _("CRITICAL - Network unreachable (%s)"), addr); | ||
| 1738 | diff -urN ../nplg/plugins/check_procs.c ./plugins/check_procs.c | ||
| 1739 | --- ../nplg/plugins/check_procs.c 2005-06-03 19:35:45.000000000 +0200 | ||
| 1740 | +++ ./plugins/check_procs.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 1741 | @@ -14,17 +14,17 @@ | ||
| 1742 | along with this program; if not, write to the Free Software | ||
| 1743 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 1744 | |||
| 1745 | - $Id: check_procs.c,v 1.45 2005/06/03 13:53:43 seanius Exp $ | ||
| 1746 | + $Id: check_procs.c,v 1.3 2005/06/06 11:37:06 exon Exp $ | ||
| 1747 | |||
| 1748 | ******************************************************************************/ | ||
| 1749 | |||
| 1750 | const char *progname = "check_procs"; | ||
| 1751 | -const char *revision = "$Revision: 1.45 $"; | ||
| 1752 | +const char *revision = "$Revision: 1.3 $"; | ||
| 1753 | const char *copyright = "2000-2004"; | ||
| 1754 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 1755 | |||
| 1756 | #include "common.h" | ||
| 1757 | -#include "popen.h" | ||
| 1758 | +#include "runcmd.h" | ||
| 1759 | #include "utils.h" | ||
| 1760 | |||
| 1761 | #include <pwd.h> | ||
| 1762 | @@ -96,7 +96,7 @@ | ||
| 1763 | char procstat[8]; | ||
| 1764 | char procetime[MAX_INPUT_BUFFER] = { '\0' }; | ||
| 1765 | char *procargs; | ||
| 1766 | - char *temp_string; | ||
| 1767 | + output chld_out, chld_err; | ||
| 1768 | |||
| 1769 | const char *zombie = "Z"; | ||
| 1770 | |||
| 1771 | @@ -118,7 +118,7 @@ | ||
| 1772 | input_buffer = malloc (MAX_INPUT_BUFFER); | ||
| 1773 | procprog = malloc (MAX_INPUT_BUFFER); | ||
| 1774 | |||
| 1775 | - asprintf (&metric_name, "PROCS"); | ||
| 1776 | + metric_name = "PROCS"; | ||
| 1777 | metric = METRIC_PROCS; | ||
| 1778 | |||
| 1779 | if (process_arguments (argc, argv) == ERROR) | ||
| 1780 | @@ -136,27 +136,15 @@ | ||
| 1781 | if (verbose >= 2) | ||
| 1782 | printf (_("CMD: %s\n"), PS_COMMAND); | ||
| 1783 | |||
| 1784 | - child_process = spopen (PS_COMMAND); | ||
| 1785 | - if (child_process == NULL) { | ||
| 1786 | - printf (_("Could not open pipe: %s\n"), PS_COMMAND); | ||
| 1787 | - return STATE_UNKNOWN; | ||
| 1788 | - } | ||
| 1789 | - | ||
| 1790 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 1791 | - if (child_stderr == NULL) | ||
| 1792 | - printf (_("Could not open stderr for %s\n"), PS_COMMAND); | ||
| 1793 | - | ||
| 1794 | - /* flush first line */ | ||
| 1795 | - fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); | ||
| 1796 | - while ( input_buffer[strlen(input_buffer)-1] != '\n' ) | ||
| 1797 | - fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); | ||
| 1798 | - | ||
| 1799 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
| 1800 | - asprintf (&input_line, "%s", input_buffer); | ||
| 1801 | - while ( input_buffer[strlen(input_buffer)-1] != '\n' ) { | ||
| 1802 | - fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); | ||
| 1803 | - asprintf (&input_line, "%s%s", input_line, input_buffer); | ||
| 1804 | - } | ||
| 1805 | + result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0); | ||
| 1806 | + if(result) { | ||
| 1807 | + printf (_("System call returned nonzero status\n")); | ||
| 1808 | + result = max_state (result, STATE_WARNING); | ||
| 1809 | + } | ||
| 1810 | + | ||
| 1811 | + /* start parsing on second line */ | ||
| 1812 | + for(i = 1; i < chld_out.lines; i++) { | ||
| 1813 | + input_line = chld_out.line[i]; | ||
| 1814 | |||
| 1815 | if (verbose >= 3) | ||
| 1816 | printf ("%s", input_line); | ||
| 1817 | @@ -177,8 +165,7 @@ | ||
| 1818 | |||
| 1819 | /* Some ps return full pathname for command. This removes path */ | ||
| 1820 | #ifdef HAVE_BASENAME | ||
| 1821 | - temp_string = strdup(procprog); | ||
| 1822 | - procprog = basename(temp_string); | ||
| 1823 | + procprog = strdup(basename(procprog)); | ||
| 1824 | #endif /* HAVE_BASENAME */ | ||
| 1825 | |||
| 1826 | /* we need to convert the elapsed time to seconds */ | ||
| 1827 | @@ -248,27 +235,17 @@ | ||
| 1828 | } | ||
| 1829 | |||
| 1830 | /* If we get anything on STDERR, at least set warning */ | ||
| 1831 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | ||
| 1832 | - if (verbose) | ||
| 1833 | - printf ("STDERR: %s", input_buffer); | ||
| 1834 | + if(chld_err.buflen > 0) { | ||
| 1835 | result = max_state (result, STATE_WARNING); | ||
| 1836 | printf (_("System call sent warnings to stderr\n")); | ||
| 1837 | } | ||
| 1838 | - | ||
| 1839 | - (void) fclose (child_stderr); | ||
| 1840 | - | ||
| 1841 | - /* close the pipe */ | ||
| 1842 | - if (spclose (child_process)) { | ||
| 1843 | - printf (_("System call returned nonzero status\n")); | ||
| 1844 | - result = max_state (result, STATE_WARNING); | ||
| 1845 | - } | ||
| 1846 | |||
| 1847 | if (found == 0) { /* no process lines parsed so return STATE_UNKNOWN */ | ||
| 1848 | printf (_("Unable to read output\n")); | ||
| 1849 | return result; | ||
| 1850 | } | ||
| 1851 | |||
| 1852 | - if ( result == STATE_UNKNOWN ) | ||
| 1853 | + if ( result == STATE_UNKNOWN ) | ||
| 1854 | result = STATE_OK; | ||
| 1855 | |||
| 1856 | /* Needed if procs found, but none match filter */ | ||
| 1857 | @@ -459,7 +436,7 @@ | ||
| 1858 | } | ||
| 1859 | usage4 (_("PCPU must be a float!")); | ||
| 1860 | case 'm': | ||
| 1861 | - asprintf (&metric_name, "%s", optarg); | ||
| 1862 | + metric_name = optarg; | ||
| 1863 | if ( strcmp(optarg, "PROCS") == 0) { | ||
| 1864 | metric = METRIC_PROCS; | ||
| 1865 | break; | ||
| 1866 | @@ -494,7 +471,7 @@ | ||
| 1867 | if (cmax == -1 && argv[c]) | ||
| 1868 | cmax = atoi (argv[c++]); | ||
| 1869 | if (statopts == NULL && argv[c]) { | ||
| 1870 | - asprintf (&statopts, "%s", argv[c++]); | ||
| 1871 | + statopts = argv[c++]; | ||
| 1872 | asprintf (&fmt, _("%s%sSTATE = %s"), (fmt ? fmt : ""), (options ? ", " : ""), statopts); | ||
| 1873 | options |= STAT; | ||
| 1874 | } | ||
| 1875 | diff -urN ../nplg/plugins/check_snmp.c ./plugins/check_snmp.c | ||
| 1876 | --- ../nplg/plugins/check_snmp.c 2005-06-03 12:07:28.000000000 +0200 | ||
| 1877 | +++ ./plugins/check_snmp.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 1878 | @@ -14,18 +14,18 @@ | ||
| 1879 | along with this program; if not, write to the Free Software | ||
| 1880 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 1881 | |||
| 1882 | - $Id: check_snmp.c,v 1.57 2005/06/01 19:41:01 sghosh Exp $ | ||
| 1883 | + $Id: check_snmp.c,v 1.2 2005/06/05 21:55:26 exon Exp $ | ||
| 1884 | |||
| 1885 | ******************************************************************************/ | ||
| 1886 | |||
| 1887 | const char *progname = "check_snmp"; | ||
| 1888 | -const char *revision = "$Revision: 1.57 $"; | ||
| 1889 | +const char *revision = "$Revision: 1.2 $"; | ||
| 1890 | const char *copyright = "1999-2004"; | ||
| 1891 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 1892 | |||
| 1893 | #include "common.h" | ||
| 1894 | #include "utils.h" | ||
| 1895 | -#include "popen.h" | ||
| 1896 | +#include "runcmd.h" | ||
| 1897 | |||
| 1898 | #define DEFAULT_COMMUNITY "public" | ||
| 1899 | #define DEFAULT_PORT "161" | ||
| 1900 | @@ -136,11 +136,12 @@ | ||
| 1901 | char *command_line = NULL; | ||
| 1902 | char *response = NULL; | ||
| 1903 | char *outbuff; | ||
| 1904 | - char *output; | ||
| 1905 | + char *msg; | ||
| 1906 | char *ptr = NULL; | ||
| 1907 | char *p2 = NULL; | ||
| 1908 | char *show = NULL; | ||
| 1909 | char type[8]; | ||
| 1910 | + output chld_out, chld_err; | ||
| 1911 | |||
| 1912 | setlocale (LC_ALL, ""); | ||
| 1913 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 1914 | @@ -157,7 +158,7 @@ | ||
| 1915 | units = strdup (""); | ||
| 1916 | port = strdup (DEFAULT_PORT); | ||
| 1917 | outbuff = strdup (""); | ||
| 1918 | - output = strdup (""); | ||
| 1919 | + msg = strdup (""); | ||
| 1920 | delimiter = strdup (" = "); | ||
| 1921 | output_delim = strdup (DEFAULT_OUTPUT_DELIMITER); | ||
| 1922 | /* miblist = strdup (DEFAULT_MIBLIST); */ | ||
| 1923 | @@ -168,41 +169,25 @@ | ||
| 1924 | usage4 (_("Could not parse arguments")); | ||
| 1925 | |||
| 1926 | /* create the command line to execute */ | ||
| 1927 | - if(usesnmpgetnext == TRUE) { | ||
| 1928 | - asprintf(&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s", | ||
| 1929 | - PATH_TO_SNMPGETNEXT, timeout_interval, retries, miblist, proto, | ||
| 1930 | - authpriv, server_address, port, oid); | ||
| 1931 | - }else{ | ||
| 1932 | - | ||
| 1933 | - asprintf (&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s", | ||
| 1934 | - PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto, | ||
| 1935 | - authpriv, server_address, port, oid); | ||
| 1936 | - } | ||
| 1937 | - | ||
| 1938 | + asprintf(&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s", | ||
| 1939 | + usesnmpgetnext == TRUE ? PATH_TO_SNMPGETNEXT : PATH_TO_SNMPGET, | ||
| 1940 | + timeout_interval, retries, miblist, proto, | ||
| 1941 | + authpriv, server_address, port, oid); | ||
| 1942 | + | ||
| 1943 | if (verbose) | ||
| 1944 | printf ("%s\n", command_line); | ||
| 1945 | - | ||
| 1946 | |||
| 1947 | /* run the command */ | ||
| 1948 | - child_process = spopen (command_line); | ||
| 1949 | - if (child_process == NULL) { | ||
| 1950 | - printf (_("Could not open pipe: %s\n"), command_line); | ||
| 1951 | - exit (STATE_UNKNOWN); | ||
| 1952 | - } | ||
| 1953 | - | ||
| 1954 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 1955 | - if (child_stderr == NULL) { | ||
| 1956 | - printf (_("Could not open stderr for %s\n"), command_line); | ||
| 1957 | - } | ||
| 1958 | + if((np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0 || chld_err.buflen) | ||
| 1959 | + result = STATE_WARNING; | ||
| 1960 | |||
| 1961 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) | ||
| 1962 | - asprintf (&output, "%s%s", output, input_buffer); | ||
| 1963 | + for(i = 0; (size_t)i < chld_out.lines; i++) | ||
| 1964 | + asprintf (&msg, "%s%s", msg, input_buffer); | ||
| 1965 | |||
| 1966 | if (verbose) | ||
| 1967 | - printf ("%s\n", output); | ||
| 1968 | - | ||
| 1969 | - ptr = output; | ||
| 1970 | + printf ("%s\n", msg); | ||
| 1971 | |||
| 1972 | + ptr = msg; | ||
| 1973 | strcat(perfstr, "| "); | ||
| 1974 | while (ptr) { | ||
| 1975 | char *foo; | ||
| 1976 | @@ -261,18 +246,11 @@ | ||
| 1977 | iresult = STATE_DEPENDENT; | ||
| 1978 | |||
| 1979 | /* Process this block for integer comparisons */ | ||
| 1980 | - if (eval_method[i] & CRIT_GT || | ||
| 1981 | - eval_method[i] & CRIT_LT || | ||
| 1982 | - eval_method[i] & CRIT_GE || | ||
| 1983 | - eval_method[i] & CRIT_LE || | ||
| 1984 | - eval_method[i] & CRIT_EQ || | ||
| 1985 | - eval_method[i] & CRIT_NE || | ||
| 1986 | - eval_method[i] & WARN_GT || | ||
| 1987 | - eval_method[i] & WARN_LT || | ||
| 1988 | - eval_method[i] & WARN_GE || | ||
| 1989 | - eval_method[i] & WARN_LE || | ||
| 1990 | - eval_method[i] & WARN_EQ || | ||
| 1991 | - eval_method[i] & WARN_NE) { | ||
| 1992 | + if (eval_method[i] & (CRIT_GT | CRIT_LT | CRIT_GE | | ||
| 1993 | + CRIT_LE | CRIT_EQ | CRIT_NE | | ||
| 1994 | + WARN_GT | WARN_LT | WARN_GE | | ||
| 1995 | + WARN_LE | WARN_EQ | WARN_NE)) | ||
| 1996 | + { | ||
| 1997 | p2 = strpbrk (p2, "0123456789"); | ||
| 1998 | if (p2 == NULL) | ||
| 1999 | die (STATE_UNKNOWN,_("No valid data returned")); | ||
| 2000 | @@ -350,20 +328,6 @@ | ||
| 2001 | label, | ||
| 2002 | command_line); | ||
| 2003 | |||
| 2004 | - /* WARNING if output found on stderr */ | ||
| 2005 | - if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
| 2006 | - result = max_state (result, STATE_WARNING); | ||
| 2007 | - | ||
| 2008 | - /* close stderr */ | ||
| 2009 | - (void) fclose (child_stderr); | ||
| 2010 | - | ||
| 2011 | - /* close the pipe */ | ||
| 2012 | - if (spclose (child_process)) | ||
| 2013 | - result = max_state (result, STATE_WARNING); | ||
| 2014 | - | ||
| 2015 | -/* if (nunits == 1 || i == 1) */ | ||
| 2016 | -/* printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */ | ||
| 2017 | -/* else */ | ||
| 2018 | printf ("%s %s -%s %s \n", label, state_text (result), outbuff, perfstr); | ||
| 2019 | |||
| 2020 | return result; | ||
| 2021 | diff -urN ../nplg/plugins/check_swap.c ./plugins/check_swap.c | ||
| 2022 | --- ../nplg/plugins/check_swap.c 2005-01-24 08:29:54.000000000 +0100 | ||
| 2023 | +++ ./plugins/check_swap.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 2024 | @@ -21,18 +21,18 @@ | ||
| 2025 | * | ||
| 2026 | * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) | ||
| 2027 | * | ||
| 2028 | - * $Id: check_swap.c,v 1.47 2005/01/19 21:14:47 tonvoon Exp $ | ||
| 2029 | + * $Id: check_swap.c,v 1.4 2005/06/06 11:37:06 exon Exp $ | ||
| 2030 | * | ||
| 2031 | *****************************************************************************/ | ||
| 2032 | |||
| 2033 | const char *progname = "check_swap"; | ||
| 2034 | -const char *revision = "$Revision: 1.47 $"; | ||
| 2035 | +const char *revision = "$Revision: 1.4 $"; | ||
| 2036 | const char *copyright = "2000-2004"; | ||
| 2037 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 2038 | |||
| 2039 | #include "common.h" | ||
| 2040 | -#include "popen.h" | ||
| 2041 | #include "utils.h" | ||
| 2042 | +#include "runcmd.h" | ||
| 2043 | |||
| 2044 | int check_swap (int usp, float free_swap); | ||
| 2045 | int process_arguments (int argc, char **argv); | ||
| 2046 | @@ -64,6 +64,8 @@ | ||
| 2047 | char *temp_buffer; | ||
| 2048 | char *swap_command; | ||
| 2049 | char *swap_format; | ||
| 2050 | + output chld_out, chld_err; | ||
| 2051 | + size_t i = 0; | ||
| 2052 | # else | ||
| 2053 | # ifdef HAVE_DECL_SWAPCTL | ||
| 2054 | int i=0, nswaps=0, swapctl_res=0; | ||
| 2055 | @@ -79,14 +81,13 @@ | ||
| 2056 | # endif | ||
| 2057 | #endif | ||
| 2058 | char str[32]; | ||
| 2059 | - char *status, *tmp_status; | ||
| 2060 | + char *status; | ||
| 2061 | |||
| 2062 | setlocale (LC_ALL, ""); | ||
| 2063 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 2064 | textdomain (PACKAGE); | ||
| 2065 | |||
| 2066 | status = strdup (""); | ||
| 2067 | - tmp_status = strdup (""); | ||
| 2068 | perf = strdup (""); | ||
| 2069 | |||
| 2070 | if (process_arguments (argc, argv) == ERROR) | ||
| 2071 | @@ -146,15 +147,9 @@ | ||
| 2072 | if (verbose >= 3) | ||
| 2073 | printf (_("Format: %s\n"), swap_format); | ||
| 2074 | |||
| 2075 | - child_process = spopen (swap_command); | ||
| 2076 | - if (child_process == NULL) { | ||
| 2077 | - printf (_("Could not open pipe: %s\n"), swap_command); | ||
| 2078 | - return STATE_UNKNOWN; | ||
| 2079 | - } | ||
| 2080 | - | ||
| 2081 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 2082 | - if (child_stderr == NULL) | ||
| 2083 | - printf (_("Could not open stderr for %s\n"), swap_command); | ||
| 2084 | + /* run the command. set WARNING if non-zero return or output on stderr */ | ||
| 2085 | + if((np_runcmd(swap_command, &chld_out, &chld_err, 0)) || chld_err.buflen) | ||
| 2086 | + result = STATE_WARNING; | ||
| 2087 | |||
| 2088 | sprintf (str, "%s", ""); | ||
| 2089 | /* read 1st line */ | ||
| 2090 | @@ -210,17 +205,6 @@ | ||
| 2091 | # ifdef _AIX | ||
| 2092 | } | ||
| 2093 | # endif | ||
| 2094 | - | ||
| 2095 | - /* If we get anything on STDERR, at least set warning */ | ||
| 2096 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
| 2097 | - result = max_state (result, STATE_WARNING); | ||
| 2098 | - | ||
| 2099 | - /* close stderr */ | ||
| 2100 | - (void) fclose (child_stderr); | ||
| 2101 | - | ||
| 2102 | - /* close the pipe */ | ||
| 2103 | - if (spclose (child_process)) | ||
| 2104 | - result = max_state (result, STATE_WARNING); | ||
| 2105 | # else | ||
| 2106 | # ifdef CHECK_SWAP_SWAPCTL_SVR4 | ||
| 2107 | |||
| 2108 | @@ -314,16 +298,19 @@ | ||
| 2109 | percent_used = 100 * ((double) used_swap) / ((double) total_swap); | ||
| 2110 | result = max_state (result, check_swap (percent_used, free_swap)); | ||
| 2111 | /* broken into two steps because of funkiness with builtin asprintf */ | ||
| 2112 | - asprintf (&tmp_status, _(" %d%% free (%.0f MB out of %.0f MB)"), | ||
| 2113 | - (100 - percent_used), free_swap, total_swap); | ||
| 2114 | - asprintf (&status, "%s%s", tmp_status, status); | ||
| 2115 | - | ||
| 2116 | - asprintf (&perf, "%s", perfdata ("swap", (long) free_swap, "MB", | ||
| 2117 | - TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap), | ||
| 2118 | - TRUE, (long) max (crit_size/1024, crit_percent/100.0*total_swap), | ||
| 2119 | - TRUE, 0, | ||
| 2120 | - TRUE, (long) total_swap)); | ||
| 2121 | - printf ("SWAP %s:%s |%s\n", state_text (result), status, perf); | ||
| 2122 | + /* the breaking was removed because it was stupid. There's no reason | ||
| 2123 | + * to concatenate a string using asprintf() just to print it to stdout | ||
| 2124 | + * in the next statement. */ | ||
| 2125 | + printf (_("SWAP %s - %d%% free (%.0f MB out of %.0f MB) %s|"), | ||
| 2126 | + state_text (result), | ||
| 2127 | + (100 - percent_used), free_swap, total_swap, status); | ||
| 2128 | + | ||
| 2129 | + puts (perfdata ("swap", (long) free_swap, "MB", | ||
| 2130 | + TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap), | ||
| 2131 | + TRUE, (long) max (crit_size/1024, crit_percent/100.0*total_swap), | ||
| 2132 | + TRUE, 0, | ||
| 2133 | + TRUE, (long) total_swap)); | ||
| 2134 | + | ||
| 2135 | return result; | ||
| 2136 | } | ||
| 2137 | |||
| 2138 | @@ -383,7 +370,7 @@ | ||
| 2139 | } | ||
| 2140 | else if (strstr (optarg, ",") && | ||
| 2141 | strstr (optarg, "%") && | ||
| 2142 | - sscanf (optarg, "%.0f,%d%%", &warn_size, &warn_percent) == 2) { | ||
| 2143 | + sscanf (optarg, "%f,%d%%", &warn_size, &warn_percent) == 2) { | ||
| 2144 | break; | ||
| 2145 | } | ||
| 2146 | else if (strstr (optarg, "%") && | ||
| 2147 | @@ -400,7 +387,7 @@ | ||
| 2148 | } | ||
| 2149 | else if (strstr (optarg, ",") && | ||
| 2150 | strstr (optarg, "%") && | ||
| 2151 | - sscanf (optarg, "%.0f,%d%%", &crit_size, &crit_percent) == 2) { | ||
| 2152 | + sscanf (optarg, "%f,%d%%", &crit_size, &crit_percent) == 2) { | ||
| 2153 | break; | ||
| 2154 | } | ||
| 2155 | else if (strstr (optarg, "%") && | ||
| 2156 | diff -urN ../nplg/plugins/check_users.c ./plugins/check_users.c | ||
| 2157 | --- ../nplg/plugins/check_users.c 2005-01-05 21:53:22.000000000 +0100 | ||
| 2158 | +++ ./plugins/check_users.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 2159 | @@ -14,17 +14,17 @@ | ||
| 2160 | along with this program; if not, write to the Free Software | ||
| 2161 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 2162 | |||
| 2163 | - $Id: check_users.c,v 1.19 2004/12/25 23:17:44 opensides Exp $ | ||
| 2164 | + $Id: check_users.c,v 1.2 2005/06/05 21:55:26 exon Exp $ | ||
| 2165 | |||
| 2166 | *****************************************************************************/ | ||
| 2167 | |||
| 2168 | const char *progname = "check_users"; | ||
| 2169 | -const char *revision = "$Revision: 1.19 $"; | ||
| 2170 | +const char *revision = "$Revision: 1.2 $"; | ||
| 2171 | const char *copyright = "2000-2004"; | ||
| 2172 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 2173 | |||
| 2174 | #include "common.h" | ||
| 2175 | -#include "popen.h" | ||
| 2176 | +#include "runcmd.h" | ||
| 2177 | #include "utils.h" | ||
| 2178 | |||
| 2179 | #define possibly_set(a,b) ((a) == 0 ? (b) : 0) | ||
| 2180 | @@ -41,52 +41,28 @@ | ||
| 2181 | { | ||
| 2182 | int users = -1; | ||
| 2183 | int result = STATE_UNKNOWN; | ||
| 2184 | - char input_buffer[MAX_INPUT_BUFFER]; | ||
| 2185 | - char *perf; | ||
| 2186 | + output chld_out, chld_err; | ||
| 2187 | + size_t i; | ||
| 2188 | |||
| 2189 | setlocale (LC_ALL, ""); | ||
| 2190 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 2191 | textdomain (PACKAGE); | ||
| 2192 | |||
| 2193 | - perf = strdup(""); | ||
| 2194 | - | ||
| 2195 | if (process_arguments (argc, argv) == ERROR) | ||
| 2196 | usage4 (_("Could not parse arguments")); | ||
| 2197 | |||
| 2198 | /* run the command */ | ||
| 2199 | - child_process = spopen (WHO_COMMAND); | ||
| 2200 | - if (child_process == NULL) { | ||
| 2201 | - printf (_("Could not open pipe: %s\n"), WHO_COMMAND); | ||
| 2202 | - return STATE_UNKNOWN; | ||
| 2203 | - } | ||
| 2204 | - | ||
| 2205 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 2206 | - if (child_stderr == NULL) | ||
| 2207 | - printf (_("Could not open stderr for %s\n"), WHO_COMMAND); | ||
| 2208 | - | ||
| 2209 | - users = 0; | ||
| 2210 | - | ||
| 2211 | - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
| 2212 | - | ||
| 2213 | - /* increment 'users' on all lines except total user count */ | ||
| 2214 | - if (input_buffer[0] != '#') { | ||
| 2215 | - users++; | ||
| 2216 | - continue; | ||
| 2217 | - } | ||
| 2218 | - | ||
| 2219 | - /* get total logged in users */ | ||
| 2220 | - if (sscanf (input_buffer, _("# users=%d"), &users) == 1) | ||
| 2221 | - break; | ||
| 2222 | - | ||
| 2223 | - } | ||
| 2224 | + result = np_runcmd(WHO_COMMAND, &chld_out, &chld_err, 0); | ||
| 2225 | + if(result != 0) | ||
| 2226 | + result = STATE_UNKNOWN; | ||
| 2227 | + | ||
| 2228 | + /* one user is listed per line, except when it begins with '#' */ | ||
| 2229 | + users = chld_out.lines; | ||
| 2230 | + for(i = 0; i < chld_out.lines; i++) | ||
| 2231 | + if (chld_out.line[0][0] == '#') users--; | ||
| 2232 | |||
| 2233 | /* check STDERR */ | ||
| 2234 | - if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
| 2235 | - result = possibly_set (result, STATE_UNKNOWN); | ||
| 2236 | - (void) fclose (child_stderr); | ||
| 2237 | - | ||
| 2238 | - /* close the pipe */ | ||
| 2239 | - if (spclose (child_process)) | ||
| 2240 | + if(chld_err.buflen > 0) | ||
| 2241 | result = possibly_set (result, STATE_UNKNOWN); | ||
| 2242 | |||
| 2243 | /* else check the user count against warning and critical thresholds */ | ||
| 2244 | @@ -98,16 +74,12 @@ | ||
| 2245 | result = STATE_OK; | ||
| 2246 | |||
| 2247 | if (result == STATE_UNKNOWN) | ||
| 2248 | - printf (_("Unable to read output\n")); | ||
| 2249 | - else { | ||
| 2250 | - asprintf(&perf, "%s", perfdata ("users", users, "", | ||
| 2251 | - TRUE, wusers, | ||
| 2252 | - TRUE, cusers, | ||
| 2253 | - TRUE, 0, | ||
| 2254 | - FALSE, 0)); | ||
| 2255 | - printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result), | ||
| 2256 | - users, perf); | ||
| 2257 | - } | ||
| 2258 | + die (STATE_UNKNOWN, _("Unable to read output\n")); | ||
| 2259 | + | ||
| 2260 | + printf (_("USERS %s - %d users currently logged in |%s\n"), | ||
| 2261 | + state_text (result), users, | ||
| 2262 | + perfdata("users", users, "", | ||
| 2263 | + TRUE, wusers, TRUE, cusers, TRUE, 0, FALSE, 0)); | ||
| 2264 | |||
| 2265 | return result; | ||
| 2266 | } | ||
| 2267 | @@ -129,8 +101,10 @@ | ||
| 2268 | {0, 0, 0, 0} | ||
| 2269 | }; | ||
| 2270 | |||
| 2271 | - if (argc < 2) | ||
| 2272 | - usage ("\n"); | ||
| 2273 | + if (argc < 2) { | ||
| 2274 | + print_usage (); | ||
| 2275 | + exit(STATE_UNKNOWN); | ||
| 2276 | + } | ||
| 2277 | |||
| 2278 | while (1) { | ||
| 2279 | c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option); | ||
| 2280 | diff -urN ../nplg/plugins/foo ./plugins/foo | ||
| 2281 | --- ../nplg/plugins/foo 1970-01-01 01:00:00.000000000 +0100 | ||
| 2282 | +++ ./plugins/foo 2005-06-10 15:05:25.000000000 +0200 | ||
| 2283 | @@ -0,0 +1,12 @@ | ||
| 2284 | +$Id: check_by_ssh.c,v 1.35 2004/12/25 12:09:19 opensides Exp $ | ||
| 2285 | +$Id: check_dig.c,v 1.40 2005/01/26 21:21:01 tonvoon Exp $ | ||
| 2286 | +$Id: check_dns.c,v 1.47 2004/12/30 00:41:39 opensides Exp $ | ||
| 2287 | +$Id: check_fping.c,v 1.24 2004/12/25 23:17:44 opensides Exp $ | ||
| 2288 | +$Id: check_load.c,v 1.28 2005/05/28 01:21:54 seanius Exp $ | ||
| 2289 | +$Id: check_nagios.c,v 1.26 2005/05/25 00:43:20 seanius Exp $ | ||
| 2290 | +$Id: check_ping.c,v 1.44 2005/05/25 14:25:55 sghosh Exp $ | ||
| 2291 | +$Id: check_procs.c,v 1.45 2005/06/03 13:53:43 seanius Exp $ | ||
| 2292 | +$Id: check_snmp.c,v 1.57 2005/06/01 19:41:01 sghosh Exp $ | ||
| 2293 | +$Id: check_users.c,v 1.19 2004/12/25 23:17:44 opensides Exp $ | ||
| 2294 | +$Id: negate.c,v 1.24 2004/12/25 23:17:44 opensides Exp $ | ||
| 2295 | +$Id: urlize.c,v 1.17 2004/12/25 23:17:44 opensides Exp $ | ||
| 2296 | diff -urN ../nplg/plugins/Makefile.am ./plugins/Makefile.am | ||
| 2297 | --- ../nplg/plugins/Makefile.am 2005-02-15 09:28:36.000000000 +0100 | ||
| 2298 | +++ ./plugins/Makefile.am 2005-06-10 15:01:26.000000000 +0200 | ||
| 2299 | @@ -24,7 +24,8 @@ | ||
| 2300 | check_nagios check_by_ssh check_dns check_nt check_ide_smart \ | ||
| 2301 | check_procs | ||
| 2302 | |||
| 2303 | -EXTRA_DIST = t utils.c netutils.c popen.c utils.h netutils.h popen.h common.h \ | ||
| 2304 | +EXTRA_DIST = t common.h utils.c utils.h netutils.c netutils.h \ | ||
| 2305 | + runcmd.c runcmd.h \ | ||
| 2306 | getaddrinfo.c getaddrinfo.h gethostbyname.c gethostbyname.h | ||
| 2307 | |||
| 2308 | PLUGINHDRS = common.h | ||
| 2309 | @@ -46,85 +47,85 @@ | ||
| 2310 | # the actual targets | ||
| 2311 | |||
| 2312 | check_dhcp_LDADD = $(NETLIBS) | ||
| 2313 | -check_dig_LDADD = $(NETLIBS) popen.o | ||
| 2314 | -check_disk_LDADD = $(BASEOBJS) popen.o | ||
| 2315 | -check_dns_LDADD = $(NETLIBS) popen.o | ||
| 2316 | +check_dig_LDADD = $(NETLIBS) runcmd.o | ||
| 2317 | +check_disk_LDADD = $(BASEOBJS) runcmd.o | ||
| 2318 | +check_dns_LDADD = $(NETLIBS) runcmd.o | ||
| 2319 | check_dummy_LDADD = $(BASEOBJS) | ||
| 2320 | -check_fping_LDADD = $(NETLIBS) popen.o | ||
| 2321 | -check_game_LDADD = $(BASEOBJS) popen.o | ||
| 2322 | +check_fping_LDADD = $(NETLIBS) runcmd.o | ||
| 2323 | +check_game_LDADD = $(BASEOBJS) runcmd.o | ||
| 2324 | check_http_LDADD = $(NETLIBS) $(SSLLIBS) | ||
| 2325 | -check_hpjd_LDADD = $(NETLIBS) popen.o | ||
| 2326 | +check_hpjd_LDADD = $(NETLIBS) runcmd.o | ||
| 2327 | check_icmp_LDADD = $(SOCKETLIBS) | ||
| 2328 | check_ldap_LDADD = $(NETLIBS) $(LDAPLIBS) | ||
| 2329 | -check_load_LDADD = $(BASEOBJS) popen.o | ||
| 2330 | +check_load_LDADD = $(BASEOBJS) runcmd.o | ||
| 2331 | check_mrtg_LDADD = $(BASEOBJS) | ||
| 2332 | check_mrtgtraf_LDADD = $(BASEOBJS) | ||
| 2333 | check_mysql_LDADD = $(NETLIBS) $(MYSQLLIBS) | ||
| 2334 | -check_nagios_LDADD = $(BASEOBJS) popen.o | ||
| 2335 | +check_nagios_LDADD = $(BASEOBJS) runcmd.o | ||
| 2336 | check_nt_LDADD = $(NETLIBS) | ||
| 2337 | check_nwstat_LDADD = $(NETLIBS) | ||
| 2338 | check_overcr_LDADD = $(NETLIBS) | ||
| 2339 | check_pgsql_LDADD = $(NETLIBS) $(PGLIBS) | ||
| 2340 | -check_ping_LDADD = $(NETLIBS) popen.o | ||
| 2341 | -check_procs_LDADD = $(BASEOBJS) popen.o | ||
| 2342 | +check_ping_LDADD = $(NETLIBS) runcmd.o | ||
| 2343 | +check_procs_LDADD = $(BASEOBJS) runcmd.o | ||
| 2344 | check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS) | ||
| 2345 | check_real_LDADD = $(NETLIBS) | ||
| 2346 | -check_snmp_LDADD = $(BASEOBJS) popen.o | ||
| 2347 | +check_snmp_LDADD = $(BASEOBJS) runcmd.o | ||
| 2348 | check_smtp_LDADD = $(NETLIBS) $(SSLLIBS) | ||
| 2349 | check_ssh_LDADD = $(NETLIBS) | ||
| 2350 | -check_swap_LDADD = $(BASEOBJS) popen.o | ||
| 2351 | +check_swap_LDADD = $(BASEOBJS) runcmd.o | ||
| 2352 | check_tcp_LDADD = $(NETLIBS) $(SSLLIBS) | ||
| 2353 | check_time_LDADD = $(NETLIBS) | ||
| 2354 | check_udp_LDADD = $(NETLIBS) | ||
| 2355 | check_ups_LDADD = $(NETLIBS) | ||
| 2356 | -check_users_LDADD = $(BASEOBJS) popen.o | ||
| 2357 | -check_by_ssh_LDADD = $(NETLIBS) popen.o | ||
| 2358 | +check_users_LDADD = $(BASEOBJS) runcmd.o | ||
| 2359 | +check_by_ssh_LDADD = $(NETLIBS) runcmd.o | ||
| 2360 | check_ide_smart_LDADD = $(BASEOBJS) | ||
| 2361 | -negate_LDADD = $(BASEOBJS) popen.o | ||
| 2362 | -urlize_LDADD = $(BASEOBJS) popen.o | ||
| 2363 | +negate_LDADD = $(BASEOBJS) runcmd.o | ||
| 2364 | +urlize_LDADD = $(BASEOBJS) runcmd.o | ||
| 2365 | |||
| 2366 | check_dhcp_DEPENDENCIES = check_dhcp.c $(NETOBJS) $(DEPLIBS) | ||
| 2367 | -check_dig_DEPENDENCIES = check_dig.c $(NETOBJS) popen.o $(DEPLIBS) | ||
| 2368 | -check_disk_DEPENDENCIES = check_disk.c $(BASEOBJS) popen.o $(DEPLIBS) | ||
| 2369 | -check_dns_DEPENDENCIES = check_dns.c $(NETOBJS) popen.o $(DEPLIBS) | ||
| 2370 | +check_dig_DEPENDENCIES = check_dig.c $(NETOBJS) runcmd.o $(DEPLIBS) | ||
| 2371 | +check_disk_DEPENDENCIES = check_disk.c $(BASEOBJS) runcmd.o $(DEPLIBS) | ||
| 2372 | +check_dns_DEPENDENCIES = check_dns.c $(NETOBJS) runcmd.o $(DEPLIBS) | ||
| 2373 | check_dummy_DEPENDENCIES = check_dummy.c $(DEPLIBS) | ||
| 2374 | -check_fping_DEPENDENCIES = check_fping.c $(NETOBJS) popen.o $(DEPLIBS) | ||
| 2375 | +check_fping_DEPENDENCIES = check_fping.c $(NETOBJS) runcmd.o $(DEPLIBS) | ||
| 2376 | check_game_DEPENDENCIES = check_game.c $(DEPLIBS) | ||
| 2377 | check_http_DEPENDENCIES = check_http.c $(NETOBJS) $(DEPLIBS) | ||
| 2378 | -check_hpjd_DEPENDENCIES = check_hpjd.c $(NETOBJS) popen.o $(DEPLIBS) | ||
| 2379 | +check_hpjd_DEPENDENCIES = check_hpjd.c $(NETOBJS) runcmd.o $(DEPLIBS) | ||
| 2380 | check_icmp_DEPENDENCIES = check_icmp.c | ||
| 2381 | check_ide_smart_DEPENDENCIES = check_ide_smart.c $(BASEOBJS) $(DEPLIBS) | ||
| 2382 | check_ldap_DEPENDENCIES = check_ldap.c $(NETOBJS) $(DEPLIBS) | ||
| 2383 | -check_load_DEPENDENCIES = check_load.c $(BASEOBJS) popen.o $(DEPLIBS) | ||
| 2384 | +check_load_DEPENDENCIES = check_load.c $(BASEOBJS) runcmd.o $(DEPLIBS) | ||
| 2385 | check_mrtg_DEPENDENCIES = check_mrtg.c $(DEPLIBS) | ||
| 2386 | check_mrtgtraf_DEPENDENCIES = check_mrtgtraf.c $(DEPLIBS) | ||
| 2387 | check_mysql_DEPENDENCIES = check_mysql.c $(NETOBJS) $(DEPLIBS) | ||
| 2388 | -check_nagios_DEPENDENCIES = check_nagios.c $(BASEOBJS) popen.o $(DEPLIBS) | ||
| 2389 | +check_nagios_DEPENDENCIES = check_nagios.c $(BASEOBJS) runcmd.o $(DEPLIBS) | ||
| 2390 | check_nt_DEPENDENCIES = check_nt.c $(NETOBJS) $(DEPLIBS) | ||
| 2391 | check_nwstat_DEPENDENCIES = check_nwstat.c $(NETOBJS) $(DEPLIBS) | ||
| 2392 | check_overcr_DEPENDENCIES = check_overcr.c $(NETOBJS) $(DEPLIBS) | ||
| 2393 | check_pgsql_DEPENDENCIES = check_pgsql.c $(NETOBJS) $(DEPLIBS) | ||
| 2394 | -check_ping_DEPENDENCIES = check_ping.c $(NETOBJS) popen.o $(DEPLIBS) | ||
| 2395 | -check_procs_DEPENDENCIES = check_procs.c $(BASEOBJS) popen.o $(DEPLIBS) | ||
| 2396 | +check_ping_DEPENDENCIES = check_ping.c $(NETOBJS) runcmd.o $(DEPLIBS) | ||
| 2397 | +check_procs_DEPENDENCIES = check_procs.c $(BASEOBJS) runcmd.o $(DEPLIBS) | ||
| 2398 | check_radius_DEPENDENCIES = check_radius.c $(NETOBJS) $(DEPLIBS) | ||
| 2399 | check_real_DEPENDENCIES = check_real.c $(NETOBJS) $(DEPLIBS) | ||
| 2400 | -check_snmp_DEPENDENCIES = check_snmp.c $(BASEOBJS) popen.o $(DEPLIBS) | ||
| 2401 | +check_snmp_DEPENDENCIES = check_snmp.c $(BASEOBJS) runcmd.o $(DEPLIBS) | ||
| 2402 | check_smtp_DEPENDENCIES = check_smtp.c $(NETOBJS) $(DEPLIBS) | ||
| 2403 | check_ssh_DEPENDENCIES = check_ssh.c $(NETOBJS) $(DEPLIBS) | ||
| 2404 | -check_swap_DEPENDENCIES = check_swap.c $(BASEOBJS) popen.o $(DEPLIBS) | ||
| 2405 | +check_swap_DEPENDENCIES = check_swap.c $(BASEOBJS) runcmd.o $(DEPLIBS) | ||
| 2406 | check_tcp_DEPENDENCIES = check_tcp.c $(NETOBJS) $(DEPLIBS) | ||
| 2407 | check_time_DEPENDENCIES = check_time.c $(NETOBJS) $(DEPLIBS) | ||
| 2408 | check_udp_DEPENDENCIES = check_udp.c $(NETOBJS) $(DEPLIBS) | ||
| 2409 | check_ups_DEPENDENCIES = check_ups.c $(NETOBJS) $(DEPLIBS) | ||
| 2410 | -check_users_DEPENDENCIES = check_users.c $(BASEOBJS) popen.o $(DEPLIBS) | ||
| 2411 | -check_by_ssh_DEPENDENCIES = check_by_ssh.c $(NETOBJS) popen.o $(DEPLIBS) | ||
| 2412 | -negate_DEPENDENCIES = negate.c $(BASEOBJS) popen.o $(DEPLIBS) | ||
| 2413 | -urlize_DEPENDENCIES = urlize.c $(BASEOBJS) popen.o $(DEPLIBS) | ||
| 2414 | +check_users_DEPENDENCIES = check_users.c $(BASEOBJS) runcmd.o $(DEPLIBS) | ||
| 2415 | +check_by_ssh_DEPENDENCIES = check_by_ssh.c $(NETOBJS) runcmd.o $(DEPLIBS) | ||
| 2416 | +negate_DEPENDENCIES = negate.c $(BASEOBJS) runcmd.o $(DEPLIBS) | ||
| 2417 | +urlize_DEPENDENCIES = urlize.c $(BASEOBJS) runcmd.o $(DEPLIBS) | ||
| 2418 | |||
| 2419 | ############################################################################## | ||
| 2420 | # secondary dependencies | ||
| 2421 | |||
| 2422 | -popen.o: popen.c popen.h $(PLUGINHDRS) | ||
| 2423 | +runcmd.o: runcmd.c runcmd.h $(PLUGINHDRS) | ||
| 2424 | |||
| 2425 | utils.o: utils.c utils.h $(PLUGINHDRS) | ||
| 2426 | |||
| 2427 | diff -urN ../nplg/plugins/negate.c ./plugins/negate.c | ||
| 2428 | --- ../nplg/plugins/negate.c 2005-01-05 21:53:22.000000000 +0100 | ||
| 2429 | +++ ./plugins/negate.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 2430 | @@ -14,7 +14,7 @@ | ||
| 2431 | along with this program; if not, write to the Free Software | ||
| 2432 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 2433 | |||
| 2434 | - $Id: negate.c,v 1.24 2004/12/25 23:17:44 opensides Exp $ | ||
| 2435 | + $Id: negate.c,v 1.2 2005/06/05 21:55:26 exon Exp $ | ||
| 2436 | |||
| 2437 | @@-<article> | ||
| 2438 | |||
| 2439 | @@ -54,7 +54,7 @@ | ||
| 2440 | ******************************************************************************/ | ||
| 2441 | |||
| 2442 | const char *progname = "negate"; | ||
| 2443 | -const char *revision = "$Revision: 1.24 $"; | ||
| 2444 | +const char *revision = "$Revision: 1.2 $"; | ||
| 2445 | const char *copyright = "2002-2004"; | ||
| 2446 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 2447 | |||
| 2448 | @@ -62,7 +62,7 @@ | ||
| 2449 | |||
| 2450 | #include "common.h" | ||
| 2451 | #include "utils.h" | ||
| 2452 | -#include "popen.h" | ||
| 2453 | +#include "runcmd.h" | ||
| 2454 | |||
| 2455 | char *command_line; | ||
| 2456 | |||
| 2457 | @@ -76,8 +76,9 @@ | ||
| 2458 | int | ||
| 2459 | main (int argc, char **argv) | ||
| 2460 | { | ||
| 2461 | - int found = 0, result = STATE_UNKNOWN; | ||
| 2462 | - char *buf; | ||
| 2463 | + int result = STATE_UNKNOWN; | ||
| 2464 | + output chld_out, chld_err; | ||
| 2465 | + size_t i; | ||
| 2466 | |||
| 2467 | setlocale (LC_ALL, ""); | ||
| 2468 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 2469 | @@ -92,43 +93,26 @@ | ||
| 2470 | |||
| 2471 | (void) alarm ((unsigned) timeout_interval); | ||
| 2472 | |||
| 2473 | - child_process = spopen (command_line); | ||
| 2474 | - if (child_process == NULL) | ||
| 2475 | - die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), command_line); | ||
| 2476 | - | ||
| 2477 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 2478 | - | ||
| 2479 | - if (child_stderr == NULL) { | ||
| 2480 | - printf (_("Could not open stderr for %s\n"), command_line); | ||
| 2481 | - } | ||
| 2482 | - | ||
| 2483 | - buf = malloc(MAX_INPUT_BUFFER); | ||
| 2484 | - while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) { | ||
| 2485 | - found++; | ||
| 2486 | - printf ("%s", buf); | ||
| 2487 | - } | ||
| 2488 | - | ||
| 2489 | - if (!found) | ||
| 2490 | + result = np_runcmd(command_line, &chld_out, &chld_err, 0); | ||
| 2491 | + if (!chld_out.buflen) | ||
| 2492 | die (STATE_UNKNOWN, | ||
| 2493 | _("%s problem - No data received from host\nCMD: %s\n"),\ | ||
| 2494 | argv[0], command_line); | ||
| 2495 | |||
| 2496 | - /* close the pipe */ | ||
| 2497 | - result = spclose (child_process); | ||
| 2498 | + for(i = 0; i < chld_out.lines; i++) | ||
| 2499 | + printf("%s", chld_out.line[i]); | ||
| 2500 | |||
| 2501 | /* WARNING if output found on stderr */ | ||
| 2502 | - if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
| 2503 | + if (chld_err.buflen != 0) | ||
| 2504 | result = max_state (result, STATE_WARNING); | ||
| 2505 | |||
| 2506 | - /* close stderr */ | ||
| 2507 | - (void) fclose (child_stderr); | ||
| 2508 | - | ||
| 2509 | if (result == STATE_OK) | ||
| 2510 | exit (STATE_CRITICAL); | ||
| 2511 | - else if (result == STATE_CRITICAL) | ||
| 2512 | + | ||
| 2513 | + if (result == STATE_CRITICAL) | ||
| 2514 | exit (EXIT_SUCCESS); | ||
| 2515 | - else | ||
| 2516 | - exit (result); | ||
| 2517 | + | ||
| 2518 | + exit (result); | ||
| 2519 | } | ||
| 2520 | |||
| 2521 | /****************************************************************************** | ||
| 2522 | diff -urN ../nplg/plugins/runcmd.c ./plugins/runcmd.c | ||
| 2523 | --- ../nplg/plugins/runcmd.c 1970-01-01 01:00:00.000000000 +0100 | ||
| 2524 | +++ ./plugins/runcmd.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 2525 | @@ -0,0 +1,339 @@ | ||
| 2526 | +/* | ||
| 2527 | + * $Id: runcmd.c,v 1.1 2005/06/05 21:55:26 exon Exp $ | ||
| 2528 | + * | ||
| 2529 | + * A simple interface to executing programs from other programs, using an | ||
| 2530 | + * optimized and safe popen()-like implementation. It is considered safe | ||
| 2531 | + * in that no shell needs to be spawned and the environment passed to the | ||
| 2532 | + * execve()'d program is essentially empty. | ||
| 2533 | + * | ||
| 2534 | + * | ||
| 2535 | + * The code in this file is a derivative of popen.c which in turn was taken | ||
| 2536 | + * from "Advanced Programming for the Unix Environment" by W. Richard Stevens. | ||
| 2537 | + * | ||
| 2538 | + * Care has been taken to make sure the functions are async-safe. The one | ||
| 2539 | + * function which isn't is np_runcmd_init() which it doesn't make sense to | ||
| 2540 | + * call twice anyway, so the api as a whole should be considered async-safe. | ||
| 2541 | + * | ||
| 2542 | + */ | ||
| 2543 | + | ||
| 2544 | +/** includes **/ | ||
| 2545 | +#include "runcmd.h" | ||
| 2546 | +#ifdef HAVE_SYS_WAIT_H | ||
| 2547 | +# include <sys/wait.h> | ||
| 2548 | +#endif | ||
| 2549 | + | ||
| 2550 | +/** macros **/ | ||
| 2551 | +#ifndef WEXITSTATUS | ||
| 2552 | +# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) | ||
| 2553 | +#endif | ||
| 2554 | + | ||
| 2555 | +#ifndef WIFEXITED | ||
| 2556 | +# define WIFEXITED(stat_val) (((stat_val) & 255) == 0) | ||
| 2557 | +#endif | ||
| 2558 | + | ||
| 2559 | +/* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */ | ||
| 2560 | +#if defined(SIG_IGN) && !defined(SIG_ERR) | ||
| 2561 | +# define SIG_ERR ((Sigfunc *)-1) | ||
| 2562 | +#endif | ||
| 2563 | + | ||
| 2564 | +/* This variable must be global, since there's no way the caller | ||
| 2565 | + * can forcibly slay a dead or ungainly running program otherwise. | ||
| 2566 | + * Multithreading apps and plugins can initialize it (via NP_RUNCMD_INIT) | ||
| 2567 | + * in an async safe manner PRIOR to calling np_runcmd() for the first time. | ||
| 2568 | + * | ||
| 2569 | + * The check for initialized values is atomic and can | ||
| 2570 | + * occur in any number of threads simultaneously. */ | ||
| 2571 | +static pid_t *np_pids = NULL; | ||
| 2572 | + | ||
| 2573 | +/* If OPEN_MAX isn't defined, we try the sysconf syscall first. | ||
| 2574 | + * If that fails, we fall back to an educated guess which is accurate | ||
| 2575 | + * on Linux and some other systems. There's no guarantee that our guess is | ||
| 2576 | + * adequate and the program will die with SIGSEGV if it isn't and the | ||
| 2577 | + * upper boundary is breached. */ | ||
| 2578 | +#ifdef OPEN_MAX | ||
| 2579 | +# define maxfd OPEN_MAX | ||
| 2580 | +#else | ||
| 2581 | +# ifndef _SC_OPEN_MAX /* sysconf macro unavailable, so guess */ | ||
| 2582 | +# define maxfd 256 | ||
| 2583 | +# else | ||
| 2584 | +static int maxfd = 0; | ||
| 2585 | +# endif /* _SC_OPEN_MAX */ | ||
| 2586 | +#endif /* OPEN_MAX */ | ||
| 2587 | + | ||
| 2588 | + | ||
| 2589 | +/** prototypes **/ | ||
| 2590 | +static int np_runcmd_open(const char *, int *, int *) | ||
| 2591 | + __attribute__((__nonnull__(1, 2, 3))); | ||
| 2592 | + | ||
| 2593 | +static int np_fetch_output(int, output *, int) | ||
| 2594 | + __attribute__((__nonnull__(2))); | ||
| 2595 | + | ||
| 2596 | +static int np_runcmd_close(int); | ||
| 2597 | + | ||
| 2598 | +/* imported from utils.h */ | ||
| 2599 | +extern void die (int, const char *, ...) | ||
| 2600 | + __attribute__((__noreturn__,__format__(__printf__, 2, 3))); | ||
| 2601 | + | ||
| 2602 | + | ||
| 2603 | +/* this function is NOT async-safe. It is exported so multithreaded | ||
| 2604 | + * plugins (or other apps) can call it prior to running any commands | ||
| 2605 | + * through this api and thus achieve async-safeness throughout the api */ | ||
| 2606 | +void np_runcmd_init(void) | ||
| 2607 | +{ | ||
| 2608 | +#if !defined(OPEN_MAX) && defined(_SC_OPEN_MAX) | ||
| 2609 | + if(!maxfd) { | ||
| 2610 | + if((maxfd = sysconf(_SC_OPEN_MAX)) < 0) { | ||
| 2611 | + /* possibly log or emit a warning here, since there's no | ||
| 2612 | + * guarantee that our guess at maxfd will be adequate */ | ||
| 2613 | + maxfd = 256; | ||
| 2614 | + } | ||
| 2615 | + } | ||
| 2616 | +#endif | ||
| 2617 | + | ||
| 2618 | + if(!np_pids) np_pids = calloc(maxfd, sizeof(pid_t)); | ||
| 2619 | +} | ||
| 2620 | + | ||
| 2621 | + | ||
| 2622 | +/* Start running a command */ | ||
| 2623 | +static int | ||
| 2624 | +np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) | ||
| 2625 | +{ | ||
| 2626 | + char *env[2]; | ||
| 2627 | + char *cmd = NULL; | ||
| 2628 | + char **argv = NULL; | ||
| 2629 | + char *str; | ||
| 2630 | + int argc; | ||
| 2631 | + size_t cmdlen; | ||
| 2632 | + pid_t pid; | ||
| 2633 | +#ifdef RLIMIT_CORE | ||
| 2634 | + struct rlimit limit; | ||
| 2635 | +#endif | ||
| 2636 | + | ||
| 2637 | + int i = 0; | ||
| 2638 | + | ||
| 2639 | + if(!np_pids) NP_RUNCMD_INIT; | ||
| 2640 | + | ||
| 2641 | + env[0] = strdup("LC_ALL=C"); | ||
| 2642 | + env[1] = '\0'; | ||
| 2643 | + | ||
| 2644 | + /* if no command was passed, return with no error */ | ||
| 2645 | + if (cmdstring == NULL) | ||
| 2646 | + return -1; | ||
| 2647 | + | ||
| 2648 | + /* make copy of command string so strtok() doesn't silently modify it */ | ||
| 2649 | + /* (the calling program may want to access it later) */ | ||
| 2650 | + cmdlen = strlen(cmdstring); | ||
| 2651 | + cmd = malloc(cmdlen + 1); | ||
| 2652 | + if (cmd == NULL) return -1; | ||
| 2653 | + memcpy(cmd, cmdstring, cmdlen); | ||
| 2654 | + | ||
| 2655 | + /* This is not a shell, so we don't handle "???" */ | ||
| 2656 | + if (strstr (cmdstring, "\"")) return -1; | ||
| 2657 | + | ||
| 2658 | + /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ | ||
| 2659 | + if (strstr (cmdstring, " ' ") || strstr (cmdstring, "'''")) | ||
| 2660 | + return -1; | ||
| 2661 | + | ||
| 2662 | + /* each arg must be whitespace-separated, so args can be a maximum | ||
| 2663 | + * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */ | ||
| 2664 | + argc = (cmdlen >> 1) + 2; | ||
| 2665 | + argv = calloc(sizeof(char *), argc); | ||
| 2666 | + | ||
| 2667 | + if (argv == NULL) { | ||
| 2668 | + printf (_("Could not malloc argv array in popen()\n")); | ||
| 2669 | + return -1; | ||
| 2670 | + } | ||
| 2671 | + | ||
| 2672 | + /* get command arguments (stupidly, but fairly quickly) */ | ||
| 2673 | + while (cmd) { | ||
| 2674 | + str = cmd; | ||
| 2675 | + str += strspn (str, " \t\r\n"); /* trim any leading whitespace */ | ||
| 2676 | + | ||
| 2677 | + if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */ | ||
| 2678 | + str++; | ||
| 2679 | + if (!strstr (str, "'")) return -1; /* balanced? */ | ||
| 2680 | + cmd = 1 + strstr (str, "'"); | ||
| 2681 | + str[strcspn (str, "'")] = 0; | ||
| 2682 | + } | ||
| 2683 | + else { | ||
| 2684 | + if (strpbrk (str, " \t\r\n")) { | ||
| 2685 | + cmd = 1 + strpbrk (str, " \t\r\n"); | ||
| 2686 | + str[strcspn (str, " \t\r\n")] = 0; | ||
| 2687 | + } | ||
| 2688 | + else { | ||
| 2689 | + cmd = NULL; | ||
| 2690 | + } | ||
| 2691 | + } | ||
| 2692 | + | ||
| 2693 | + if (cmd && strlen (cmd) == strspn (cmd, " \t\r\n")) | ||
| 2694 | + cmd = NULL; | ||
| 2695 | + | ||
| 2696 | + argv[i++] = str; | ||
| 2697 | + } | ||
| 2698 | + | ||
| 2699 | + if (pipe(pfd) < 0 || pipe(pfderr) < 0 || (pid = fork()) < 0) | ||
| 2700 | + return -1; /* errno set by the failing function */ | ||
| 2701 | + | ||
| 2702 | + /* child runs exceve() and _exit. */ | ||
| 2703 | + if (pid == 0) { | ||
| 2704 | +#ifdef RLIMIT_CORE | ||
| 2705 | + /* the program we execve shouldn't leave core files */ | ||
| 2706 | + getrlimit (RLIMIT_CORE, &limit); | ||
| 2707 | + limit.rlim_cur = 0; | ||
| 2708 | + setrlimit (RLIMIT_CORE, &limit); | ||
| 2709 | +#endif | ||
| 2710 | + close (pfd[0]); | ||
| 2711 | + if (pfd[1] != STDOUT_FILENO) { | ||
| 2712 | + dup2 (pfd[1], STDOUT_FILENO); | ||
| 2713 | + close (pfd[1]); | ||
| 2714 | + } | ||
| 2715 | + close (pfderr[0]); | ||
| 2716 | + if (pfderr[1] != STDERR_FILENO) { | ||
| 2717 | + dup2 (pfderr[1], STDERR_FILENO); | ||
| 2718 | + close (pfderr[1]); | ||
| 2719 | + } | ||
| 2720 | + | ||
| 2721 | + /* close all descriptors in np_pids[] | ||
| 2722 | + * This is executed in a separate address space (pure child), | ||
| 2723 | + * so we don't have to worry about async safety */ | ||
| 2724 | + for (i = 0; i < maxfd; i++) | ||
| 2725 | + if(np_pids[i] > 0) | ||
| 2726 | + close (i); | ||
| 2727 | + | ||
| 2728 | + execve (argv[0], argv, env); | ||
| 2729 | + _exit (0); | ||
| 2730 | + } | ||
| 2731 | + | ||
| 2732 | + /* parent picks up execution here */ | ||
| 2733 | + /* close childs descriptors in our address space */ | ||
| 2734 | + close(pfd[1]); | ||
| 2735 | + close(pfderr[1]); | ||
| 2736 | + | ||
| 2737 | + /* tag our file's entry in the pid-list and return it */ | ||
| 2738 | + np_pids[pfd[0]] = pid; | ||
| 2739 | + | ||
| 2740 | + return pfd[0]; | ||
| 2741 | +} | ||
| 2742 | + | ||
| 2743 | + | ||
| 2744 | +static int | ||
| 2745 | +np_runcmd_close(int fd) | ||
| 2746 | +{ | ||
| 2747 | + int status; | ||
| 2748 | + pid_t pid; | ||
| 2749 | + | ||
| 2750 | + /* make sure this fd was opened by popen() */ | ||
| 2751 | + if(fd < 0 || fd > maxfd || !np_pids || (pid = np_pids[fd]) == 0) | ||
| 2752 | + return -1; | ||
| 2753 | + | ||
| 2754 | + np_pids[fd] = 0; | ||
| 2755 | + if (close (fd) == -1) return -1; | ||
| 2756 | + | ||
| 2757 | + /* EINTR is ok (sort of), everything else is bad */ | ||
| 2758 | + while (waitpid (pid, &status, 0) < 0) | ||
| 2759 | + if (errno != EINTR) return -1; | ||
| 2760 | + | ||
| 2761 | + /* return child's termination status */ | ||
| 2762 | + return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1; | ||
| 2763 | +} | ||
| 2764 | + | ||
| 2765 | + | ||
| 2766 | +void | ||
| 2767 | +popen_timeout_alarm_handler (int signo) | ||
| 2768 | +{ | ||
| 2769 | + size_t i; | ||
| 2770 | + | ||
| 2771 | + if (signo == SIGALRM) | ||
| 2772 | + puts(_("CRITICAL - Plugin timed out while executing system call\n")); | ||
| 2773 | + | ||
| 2774 | + if(np_pids) for(i = 0; i < maxfd; i++) { | ||
| 2775 | + if(np_pids[i] != 0) kill(np_pids[i], SIGKILL); | ||
| 2776 | + } | ||
| 2777 | + | ||
| 2778 | + exit (STATE_CRITICAL); | ||
| 2779 | +} | ||
| 2780 | + | ||
| 2781 | + | ||
| 2782 | +static int | ||
| 2783 | +np_fetch_output(int fd, output *op, int flags) | ||
| 2784 | +{ | ||
| 2785 | + size_t len = 0, i = 0; | ||
| 2786 | + size_t rsf = 6, ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */ | ||
| 2787 | + char *buf = NULL; | ||
| 2788 | + int ret; | ||
| 2789 | + char tmpbuf[4096]; | ||
| 2790 | + | ||
| 2791 | + op->buf = NULL; | ||
| 2792 | + op->buflen = 0; | ||
| 2793 | + while((ret = read(fd, tmpbuf, sizeof(tmpbuf))) > 0) { | ||
| 2794 | + len = (size_t)ret; | ||
| 2795 | + op->buf = realloc(op->buf, op->buflen + len + 1); | ||
| 2796 | + memcpy(op->buf + op->buflen, tmpbuf, len); | ||
| 2797 | + op->buflen += len; | ||
| 2798 | + i++; | ||
| 2799 | + } | ||
| 2800 | + | ||
| 2801 | + if(ret < 0) { | ||
| 2802 | + printf("read() returned %d: %s\n", ret, strerror(errno)); | ||
| 2803 | + return ret; | ||
| 2804 | + } | ||
| 2805 | + | ||
| 2806 | + if(!op->buf || !op->buflen) return 0; | ||
| 2807 | + | ||
| 2808 | + /* some plugins may want to keep output unbroken */ | ||
| 2809 | + if(flags & RUNCMD_NO_ARRAYS) | ||
| 2810 | + return op->buflen; | ||
| 2811 | + | ||
| 2812 | + /* and some may want both (*sigh*) */ | ||
| 2813 | + if(flags & RUNCMD_NO_ASSOC) { | ||
| 2814 | + buf = malloc(op->buflen); | ||
| 2815 | + memcpy(buf, op->buf, op->buflen); | ||
| 2816 | + } | ||
| 2817 | + else buf = op->buf; | ||
| 2818 | + | ||
| 2819 | + op->line = NULL; | ||
| 2820 | + op->lens = NULL; | ||
| 2821 | + len = i = 0; | ||
| 2822 | + while(i < op->buflen) { | ||
| 2823 | + /* make sure we have enough memory */ | ||
| 2824 | + if(len >= ary_size) { | ||
| 2825 | + ary_size = op->buflen >> --rsf; | ||
| 2826 | + op->line = realloc(op->line, ary_size * sizeof(char *)); | ||
| 2827 | + op->lens = realloc(op->lens, ary_size * sizeof(size_t)); | ||
| 2828 | + } | ||
| 2829 | + | ||
| 2830 | + /* set the pointer to the string */ | ||
| 2831 | + op->line[len] = &buf[i]; | ||
| 2832 | + | ||
| 2833 | + /* hop to next newline or end of buffer */ | ||
| 2834 | + while(buf[i] != '\n' && i < op->buflen) i++; | ||
| 2835 | + buf[i] = '\0'; | ||
| 2836 | + | ||
| 2837 | + /* calculate the string length using pointer difference */ | ||
| 2838 | + op->lens[len] = (size_t)&buf[i] - (size_t)op->line[len]; | ||
| 2839 | + | ||
| 2840 | + len++; | ||
| 2841 | + i++; | ||
| 2842 | + } | ||
| 2843 | + | ||
| 2844 | + return len; | ||
| 2845 | +} | ||
| 2846 | + | ||
| 2847 | + | ||
| 2848 | +int | ||
| 2849 | +np_runcmd(const char *cmd, output *out, output *err, int flags) | ||
| 2850 | +{ | ||
| 2851 | + int fd, pfd_out[2], pfd_err[2]; | ||
| 2852 | + | ||
| 2853 | + /* initialize the structs */ | ||
| 2854 | + if(out) memset(out, 0, sizeof(output)); | ||
| 2855 | + if(err) memset(err, 0, sizeof(output)); | ||
| 2856 | + | ||
| 2857 | + if((fd = np_runcmd_open(cmd, pfd_out, pfd_err)) == -1) | ||
| 2858 | + die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd); | ||
| 2859 | + | ||
| 2860 | + if(out) out->lines = np_fetch_output(pfd_out[0], out, flags); | ||
| 2861 | + if(err) err->lines = np_fetch_output(pfd_err[0], err, flags); | ||
| 2862 | + | ||
| 2863 | + return np_runcmd_close(fd); | ||
| 2864 | +} | ||
| 2865 | diff -urN ../nplg/plugins/runcmd.h ./plugins/runcmd.h | ||
| 2866 | --- ../nplg/plugins/runcmd.h 1970-01-01 01:00:00.000000000 +0100 | ||
| 2867 | +++ ./plugins/runcmd.h 2005-06-10 15:01:26.000000000 +0200 | ||
| 2868 | @@ -0,0 +1,39 @@ | ||
| 2869 | +/* | ||
| 2870 | + * $Id: runcmd.h,v 1.1 2005/06/05 21:55:26 exon Exp $ | ||
| 2871 | + * | ||
| 2872 | + * Author: Andreas Ericsson <ae@op5.se> | ||
| 2873 | + * | ||
| 2874 | + * Copyright: GNU GPL v2 or any later version. | ||
| 2875 | + * | ||
| 2876 | + */ | ||
| 2877 | + | ||
| 2878 | +#ifndef NAGIOSPLUG_RUNCMD_H | ||
| 2879 | +#define NAGIOSPLUG_RUNCMD_H | ||
| 2880 | + | ||
| 2881 | +#include "common.h" | ||
| 2882 | + | ||
| 2883 | +/** types **/ | ||
| 2884 | +struct output { | ||
| 2885 | + char *buf; /* output buffer */ | ||
| 2886 | + size_t buflen; /* output buffer content length */ | ||
| 2887 | + char **line; /* array of lines (points to buf) */ | ||
| 2888 | + size_t *lens; /* string lengths */ | ||
| 2889 | + size_t lines; /* lines of output */ | ||
| 2890 | +}; | ||
| 2891 | + | ||
| 2892 | +typedef struct output output; | ||
| 2893 | + | ||
| 2894 | +/** prototypes **/ | ||
| 2895 | +int np_runcmd(const char *, output *, output *, int); | ||
| 2896 | +void popen_timeout_alarm_handler(int) | ||
| 2897 | + __attribute__((__noreturn__)); | ||
| 2898 | + | ||
| 2899 | +/* only multi-threaded plugins need to bother with this */ | ||
| 2900 | +void np_runcmd_init(void); | ||
| 2901 | +#define NP_RUNCMD_INIT np_runcmd_init() | ||
| 2902 | + | ||
| 2903 | +/* possible flags for np_runcmd()'s fourth argument */ | ||
| 2904 | +#define RUNCMD_NO_ARRAYS 0x01 /* don't populate arrays at all */ | ||
| 2905 | +#define RUNCMD_NO_ASSOC 0x02 /* output.line won't point to buf */ | ||
| 2906 | + | ||
| 2907 | +#endif /* NAGIOSPLUG_RUNCMD_H */ | ||
| 2908 | diff -urN ../nplg/plugins/urlize.c ./plugins/urlize.c | ||
| 2909 | --- ../nplg/plugins/urlize.c 2005-01-05 21:53:23.000000000 +0100 | ||
| 2910 | +++ ./plugins/urlize.c 2005-06-10 15:01:26.000000000 +0200 | ||
| 2911 | @@ -14,18 +14,18 @@ | ||
| 2912 | along with this program; if not, write to the Free Software | ||
| 2913 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 2914 | |||
| 2915 | - $Id: urlize.c,v 1.17 2004/12/25 23:17:44 opensides Exp $ | ||
| 2916 | + $Id: urlize.c,v 1.2 2005/06/05 21:55:26 exon Exp $ | ||
| 2917 | |||
| 2918 | ******************************************************************************/ | ||
| 2919 | |||
| 2920 | const char *progname = "urlize"; | ||
| 2921 | -const char *revision = "$Revision: 1.17 $"; | ||
| 2922 | +const char *revision = "$Revision: 1.2 $"; | ||
| 2923 | const char *copyright = "2000-2004"; | ||
| 2924 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
| 2925 | |||
| 2926 | #include "common.h" | ||
| 2927 | #include "utils.h" | ||
| 2928 | -#include "popen.h" | ||
| 2929 | +#include "runcmd.h" | ||
| 2930 | |||
| 2931 | void print_help (void); | ||
| 2932 | void print_usage (void); | ||
| 2933 | @@ -33,10 +33,11 @@ | ||
| 2934 | int | ||
| 2935 | main (int argc, char **argv) | ||
| 2936 | { | ||
| 2937 | - int found = 0, result = STATE_UNKNOWN; | ||
| 2938 | + int result = STATE_UNKNOWN; | ||
| 2939 | char *url = NULL; | ||
| 2940 | char *cmd; | ||
| 2941 | - char *buf; | ||
| 2942 | + output chld_out, chld_err; | ||
| 2943 | + size_t i; | ||
| 2944 | |||
| 2945 | int c; | ||
| 2946 | int option = 0; | ||
| 2947 | @@ -67,55 +68,37 @@ | ||
| 2948 | exit (EXIT_SUCCESS); | ||
| 2949 | break; | ||
| 2950 | case 'u': | ||
| 2951 | - url = strdup (argv[optind]); | ||
| 2952 | + url = optarg; | ||
| 2953 | break; | ||
| 2954 | case '?': | ||
| 2955 | default: | ||
| 2956 | - usage2 (_("Unknown argument"), optarg); | ||
| 2957 | + usage3 (_("Unknown argument"), c); | ||
| 2958 | } | ||
| 2959 | } | ||
| 2960 | |||
| 2961 | - if (url == NULL) | ||
| 2962 | - url = strdup (argv[optind++]); | ||
| 2963 | + if (url == NULL && optind < argc - 1) | ||
| 2964 | + url = argv[optind++]; | ||
| 2965 | |||
| 2966 | cmd = strdup (argv[optind++]); | ||
| 2967 | for (c = optind; c < argc; c++) { | ||
| 2968 | asprintf (&cmd, "%s %s", cmd, argv[c]); | ||
| 2969 | } | ||
| 2970 | |||
| 2971 | - child_process = spopen (cmd); | ||
| 2972 | - if (child_process == NULL) { | ||
| 2973 | - printf (_("Could not open pipe: %s\n"), cmd); | ||
| 2974 | - exit (STATE_UNKNOWN); | ||
| 2975 | - } | ||
| 2976 | - | ||
| 2977 | - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 2978 | - if (child_stderr == NULL) { | ||
| 2979 | - printf (_("Could not open stderr for %s\n"), cmd); | ||
| 2980 | - } | ||
| 2981 | + result = np_runcmd(cmd, &chld_out, &chld_err, 0); | ||
| 2982 | + printf ("<A href=\"%s\">", url); | ||
| 2983 | |||
| 2984 | - buf = malloc(MAX_INPUT_BUFFER); | ||
| 2985 | - printf ("<A href=\"%s\">", argv[1]); | ||
| 2986 | - while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) { | ||
| 2987 | - found++; | ||
| 2988 | - printf ("%s", buf); | ||
| 2989 | - } | ||
| 2990 | - | ||
| 2991 | - if (!found) | ||
| 2992 | + if (!chld_out.lines) | ||
| 2993 | die (STATE_UNKNOWN, | ||
| 2994 | _("%s UNKNOWN - No data received from host\nCMD: %s</A>\n"), | ||
| 2995 | argv[0], cmd); | ||
| 2996 | |||
| 2997 | - /* close the pipe */ | ||
| 2998 | - result = spclose (child_process); | ||
| 2999 | + for(i = 0; i < chld_out.lines; i++) | ||
| 3000 | + printf("%s", chld_out.line[i]); | ||
| 3001 | |||
| 3002 | /* WARNING if output found on stderr */ | ||
| 3003 | - if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
| 3004 | + if (chld_err.buflen) | ||
| 3005 | result = max_state (result, STATE_WARNING); | ||
| 3006 | |||
| 3007 | - /* close stderr */ | ||
| 3008 | - (void) fclose (child_stderr); | ||
| 3009 | - | ||
| 3010 | printf ("</A>\n"); | ||
| 3011 | return result; | ||
| 3012 | } | ||
