diff options
Diffstat (limited to 'plugins/check_nagios.c')
-rw-r--r-- | plugins/check_nagios.c | 80 |
1 files changed, 33 insertions, 47 deletions
diff --git a/plugins/check_nagios.c b/plugins/check_nagios.c index 089ff669..0ae488ff 100644 --- a/plugins/check_nagios.c +++ b/plugins/check_nagios.c | |||
@@ -24,7 +24,7 @@ const char *copyright = "1999-2004"; | |||
24 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | 24 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; |
25 | 25 | ||
26 | #include "common.h" | 26 | #include "common.h" |
27 | #include "popen.h" | 27 | #include "runcmd.h" |
28 | #include "utils.h" | 28 | #include "utils.h" |
29 | 29 | ||
30 | int process_arguments (int, char **); | 30 | int process_arguments (int, char **); |
@@ -55,6 +55,8 @@ main (int argc, char **argv) | |||
55 | int procrss = 0; | 55 | int procrss = 0; |
56 | float procpcpu = 0; | 56 | float procpcpu = 0; |
57 | char procstat[8]; | 57 | char procstat[8]; |
58 | /* procetime is unused in most configurations, but may be in PS_VAR_LIST | ||
59 | * so it must be here in spite of it producing compiler warnings */ | ||
58 | char procetime[MAX_INPUT_BUFFER]; | 60 | char procetime[MAX_INPUT_BUFFER]; |
59 | char procprog[MAX_INPUT_BUFFER]; | 61 | char procprog[MAX_INPUT_BUFFER]; |
60 | char *procargs; | 62 | char *procargs; |
@@ -62,17 +64,19 @@ main (int argc, char **argv) | |||
62 | int expected_cols = PS_COLS - 1; | 64 | int expected_cols = PS_COLS - 1; |
63 | const char *zombie = "Z"; | 65 | const char *zombie = "Z"; |
64 | char *temp_string; | 66 | char *temp_string; |
67 | output chld_out, chld_err; | ||
68 | size_t i; | ||
65 | 69 | ||
66 | setlocale (LC_ALL, ""); | 70 | setlocale (LC_ALL, ""); |
67 | bindtextdomain (PACKAGE, LOCALEDIR); | 71 | bindtextdomain (PACKAGE, LOCALEDIR); |
68 | textdomain (PACKAGE); | 72 | textdomain (PACKAGE); |
69 | 73 | ||
70 | if (process_arguments (argc, argv) == ERROR) | 74 | if (process_arguments (argc, argv) == ERROR) |
71 | usage4 (_("Could not parse arguments")); | 75 | usage_va(_("Could not parse arguments")); |
72 | 76 | ||
73 | /* Set signal handling and alarm timeout */ | 77 | /* Set signal handling and alarm timeout */ |
74 | if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 78 | if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
75 | usage4 (_("Cannot catch SIGALRM")); | 79 | usage_va(_("Cannot catch SIGALRM")); |
76 | } | 80 | } |
77 | 81 | ||
78 | /* handle timeouts gracefully... */ | 82 | /* handle timeouts gracefully... */ |
@@ -99,40 +103,30 @@ main (int argc, char **argv) | |||
99 | printf(_("command: %s\n"), PS_COMMAND); | 103 | printf(_("command: %s\n"), PS_COMMAND); |
100 | 104 | ||
101 | /* run the command to check for the Nagios process.. */ | 105 | /* run the command to check for the Nagios process.. */ |
102 | child_process = spopen (PS_COMMAND); | 106 | if((result = np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0)) != 0) |
103 | if (child_process == NULL) { | 107 | result = STATE_WARNING; |
104 | printf (_("Could not open pipe: %s\n"), PS_COMMAND); | ||
105 | return STATE_UNKNOWN; | ||
106 | } | ||
107 | |||
108 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
109 | if (child_stderr == NULL) { | ||
110 | printf (_("Could not open stderr for %s\n"), PS_COMMAND); | ||
111 | } | ||
112 | |||
113 | fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); | ||
114 | 108 | ||
115 | /* count the number of matching Nagios processes... */ | 109 | /* count the number of matching Nagios processes... */ |
116 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | 110 | for(i = 0; i < chld_out.lines; i++) { |
117 | cols = sscanf (input_buffer, PS_FORMAT, PS_VARLIST); | 111 | cols = sscanf (chld_out.line[i], PS_FORMAT, PS_VARLIST); |
118 | /* Zombie processes do not give a procprog command */ | 112 | /* Zombie processes do not give a procprog command */ |
119 | if ( cols == (expected_cols - 1) && strstr(procstat, zombie) ) { | 113 | if ( cols == (expected_cols - 1) && strstr(procstat, zombie) ) { |
120 | cols = expected_cols; | 114 | cols = expected_cols; |
121 | /* Set some value for procargs for the strip command further below | 115 | /* Set some value for procargs for the strip command further below |
122 | Seen to be a problem on some Solaris 7 and 8 systems */ | 116 | * Seen to be a problem on some Solaris 7 and 8 systems */ |
123 | input_buffer[pos] = '\n'; | 117 | chld_out.line[i][pos] = '\n'; |
124 | input_buffer[pos+1] = 0x0; | 118 | chld_out.line[i][pos+1] = 0x0; |
125 | } | 119 | } |
126 | if ( cols >= expected_cols ) { | 120 | if ( cols >= expected_cols ) { |
127 | asprintf (&procargs, "%s", input_buffer + pos); | 121 | asprintf (&procargs, "%s", chld_out.line[i] + pos); |
128 | strip (procargs); | 122 | strip (procargs); |
129 | 123 | ||
130 | /* Some ps return full pathname for command. This removes path */ | 124 | /* Some ps return full pathname for command. This removes path */ |
131 | temp_string = strtok ((char *)procprog, "/"); | 125 | temp_string = strtok ((char *)procprog, "/"); |
132 | while (temp_string) { | 126 | while (temp_string) { |
133 | strcpy(procprog, temp_string); | 127 | strcpy(procprog, temp_string); |
134 | temp_string = strtok (NULL, "/"); | 128 | temp_string = strtok (NULL, "/"); |
135 | } | 129 | } |
136 | 130 | ||
137 | /* May get empty procargs */ | 131 | /* May get empty procargs */ |
138 | if (!strstr(procargs, argv[0]) && strstr(procargs, process_string) && strcmp(procargs,"")) { | 132 | if (!strstr(procargs, argv[0]) && strstr(procargs, process_string) && strcmp(procargs,"")) { |
@@ -145,14 +139,7 @@ main (int argc, char **argv) | |||
145 | } | 139 | } |
146 | 140 | ||
147 | /* If we get anything on stderr, at least set warning */ | 141 | /* If we get anything on stderr, at least set warning */ |
148 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | 142 | if(chld_err.buflen) |
149 | result = max_state (result, STATE_WARNING); | ||
150 | |||
151 | /* close stderr */ | ||
152 | (void) fclose (child_stderr); | ||
153 | |||
154 | /* close the pipe */ | ||
155 | if (spclose (child_process)) | ||
156 | result = max_state (result, STATE_WARNING); | 143 | result = max_state (result, STATE_WARNING); |
157 | 144 | ||
158 | /* reset the alarm handler */ | 145 | /* reset the alarm handler */ |
@@ -219,8 +206,6 @@ process_arguments (int argc, char **argv) | |||
219 | break; | 206 | break; |
220 | 207 | ||
221 | switch (c) { | 208 | switch (c) { |
222 | case '?': /* print short usage statement if args not parsable */ | ||
223 | usage2 (_("Unknown argument"), optarg); | ||
224 | case 'h': /* help */ | 209 | case 'h': /* help */ |
225 | print_help (); | 210 | print_help (); |
226 | exit (STATE_OK); | 211 | exit (STATE_OK); |
@@ -243,16 +228,17 @@ process_arguments (int argc, char **argv) | |||
243 | case 'v': | 228 | case 'v': |
244 | verbose++; | 229 | verbose++; |
245 | break; | 230 | break; |
231 | default: /* print short usage_va statement if args not parsable */ | ||
232 | usage_va(_("Unknown argument - %s"), optarg); | ||
246 | } | 233 | } |
247 | } | 234 | } |
248 | 235 | ||
249 | 236 | ||
250 | if (status_log == NULL) | 237 | if (status_log == NULL) |
251 | die (STATE_UNKNOWN, | 238 | die (STATE_UNKNOWN, _("You must provide the status_log\n")); |
252 | _("You must provide the status_log\n")); | 239 | |
253 | else if (process_string == NULL) | 240 | if (process_string == NULL) |
254 | die (STATE_UNKNOWN, | 241 | die (STATE_UNKNOWN, _("You must provide a process string\n")); |
255 | _("You must provide a process string\n")); | ||
256 | 242 | ||
257 | return OK; | 243 | return OK; |
258 | } | 244 | } |