diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_by_ssh.c | 169 |
1 files changed, 72 insertions, 97 deletions
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c index 59264dcd..97c8672f 100644 --- a/plugins/check_by_ssh.c +++ b/plugins/check_by_ssh.c | |||
@@ -43,13 +43,14 @@ void print_help (char *command_name); | |||
43 | void print_usage (void); | 43 | void print_usage (void); |
44 | 44 | ||
45 | 45 | ||
46 | int commands; | 46 | int commands = 0; |
47 | char *remotecmd = NULL; | 47 | int services = 0; |
48 | char *comm = NULL; | 48 | char *remotecmd = ""; |
49 | char *comm = SSH_COMMAND; | ||
49 | char *hostname = NULL; | 50 | char *hostname = NULL; |
50 | char *outputfile = NULL; | 51 | char *outputfile = NULL; |
51 | char *host_shortname = NULL; | 52 | char *host_shortname = NULL; |
52 | char *servicelist = NULL; | 53 | char **service; |
53 | int passive = FALSE; | 54 | int passive = FALSE; |
54 | int verbose = FALSE; | 55 | int verbose = FALSE; |
55 | 56 | ||
@@ -59,9 +60,10 @@ main (int argc, char **argv) | |||
59 | { | 60 | { |
60 | 61 | ||
61 | char input_buffer[MAX_INPUT_BUFFER] = ""; | 62 | char input_buffer[MAX_INPUT_BUFFER] = ""; |
62 | char *result_text = NULL; | 63 | char *result_text = ""; |
63 | char *status_text; | 64 | char *status_text; |
64 | char *output = NULL; | 65 | char *output = ""; |
66 | char *summary = ""; | ||
65 | char *eol = NULL; | 67 | char *eol = NULL; |
66 | char *srvc_desc = NULL; | 68 | char *srvc_desc = NULL; |
67 | int cresult; | 69 | int cresult; |
@@ -104,10 +106,8 @@ main (int argc, char **argv) | |||
104 | 106 | ||
105 | 107 | ||
106 | /* get results from remote command */ | 108 | /* get results from remote command */ |
107 | result_text = realloc (result_text, 1); | ||
108 | result_text[0] = 0; | ||
109 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) | 109 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) |
110 | result_text = strscat (result_text, input_buffer); | 110 | asprintf (&result_text, "%s%s", result_text, input_buffer); |
111 | 111 | ||
112 | 112 | ||
113 | /* WARNING if output found on stderr */ | 113 | /* WARNING if output found on stderr */ |
@@ -131,24 +131,23 @@ main (int argc, char **argv) | |||
131 | } | 131 | } |
132 | 132 | ||
133 | time (&local_time); | 133 | time (&local_time); |
134 | srvc_desc = strtok (servicelist, ":"); | 134 | commands = 0; |
135 | while (result_text != NULL) { | 135 | while (result_text && strlen(result_text) > 0) { |
136 | status_text = (strstr (result_text, "STATUS CODE: ")); | 136 | status_text = (strstr (result_text, "STATUS CODE: ")); |
137 | if (status_text == NULL) { | 137 | if (status_text == NULL) { |
138 | printf ("%s", result_text); | 138 | printf ("%s", result_text); |
139 | return result; | 139 | return result; |
140 | } | 140 | } |
141 | output = result_text; | 141 | asprintf (&output, "%s", result_text); |
142 | result_text = strnl (status_text); | 142 | result_text = strnl (status_text); |
143 | eol = strpbrk (output, "\r\n"); | 143 | eol = strpbrk (output, "\r\n"); |
144 | if (eol != NULL) | 144 | if (eol != NULL) |
145 | eol[0] = 0; | 145 | eol[0] = 0; |
146 | if (srvc_desc && status_text | 146 | if (service[commands] && status_text |
147 | && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1) { | 147 | && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1) { |
148 | fprintf (fp, "%d PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", | 148 | fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", |
149 | (int) local_time, host_shortname, srvc_desc, cresult, | 149 | (int) local_time, host_shortname, service[commands++], cresult, |
150 | output); | 150 | output); |
151 | srvc_desc = strtok (NULL, ":"); | ||
152 | } | 151 | } |
153 | } | 152 | } |
154 | 153 | ||
@@ -156,14 +155,12 @@ main (int argc, char **argv) | |||
156 | 155 | ||
157 | /* print the first line from the remote command */ | 156 | /* print the first line from the remote command */ |
158 | else { | 157 | else { |
159 | eol = strpbrk (result_text, "\r\n"); | 158 | eol = strpbrk (result_text, "\r\n"); |
160 | if (eol) | 159 | if (eol) |
161 | eol[0] = 0; | 160 | eol[0] = 0; |
162 | printf ("%s\n", result_text); | 161 | printf ("%s\n", result_text); |
163 | |||
164 | } | 162 | } |
165 | 163 | ||
166 | |||
167 | /* return error status from remote command */ | 164 | /* return error status from remote command */ |
168 | return result; | 165 | return result; |
169 | } | 166 | } |
@@ -176,60 +173,9 @@ main (int argc, char **argv) | |||
176 | int | 173 | int |
177 | process_arguments (int argc, char **argv) | 174 | process_arguments (int argc, char **argv) |
178 | { | 175 | { |
179 | int c; | 176 | int c, i; |
180 | 177 | char *p1, *p2; | |
181 | if (argc < 2) | 178 | size_t len; |
182 | return ERROR; | ||
183 | |||
184 | remotecmd = realloc (remotecmd, 1); | ||
185 | remotecmd[0] = 0; | ||
186 | |||
187 | for (c = 1; c < argc; c++) | ||
188 | if (strcmp ("-to", argv[c]) == 0) | ||
189 | strcpy (argv[c], "-t"); | ||
190 | |||
191 | comm = strscpy (comm, SSH_COMMAND); | ||
192 | |||
193 | c = 0; | ||
194 | while (c += (call_getopt (argc - c, &argv[c]))) { | ||
195 | |||
196 | if (argc <= c) | ||
197 | break; | ||
198 | |||
199 | if (hostname == NULL) { | ||
200 | if (!is_host (argv[c])) | ||
201 | terminate (STATE_UNKNOWN, "%s: Invalid host name %s\n", PROGNAME, | ||
202 | argv[c]); | ||
203 | hostname = argv[c]; | ||
204 | } | ||
205 | else if (remotecmd == NULL) { | ||
206 | remotecmd = strscpy (remotecmd, argv[c++]); | ||
207 | for (; c < argc; c++) | ||
208 | remotecmd = ssprintf (remotecmd, "%s %s", remotecmd, argv[c]); | ||
209 | } | ||
210 | |||
211 | } | ||
212 | |||
213 | if (commands > 1) | ||
214 | remotecmd = strscat (remotecmd, ";echo STATUS CODE: $?;"); | ||
215 | |||
216 | if (remotecmd == NULL || strlen (remotecmd) <= 1) | ||
217 | usage ("No remotecmd\n"); | ||
218 | |||
219 | comm = ssprintf (comm, "%s %s '%s'", comm, hostname, remotecmd); | ||
220 | |||
221 | return validate_arguments (); | ||
222 | } | ||
223 | |||
224 | |||
225 | |||
226 | |||
227 | |||
228 | /* Call getopt */ | ||
229 | int | ||
230 | call_getopt (int argc, char **argv) | ||
231 | { | ||
232 | int c, i = 1; | ||
233 | 179 | ||
234 | #ifdef HAVE_GETOPT_H | 180 | #ifdef HAVE_GETOPT_H |
235 | int option_index = 0; | 181 | int option_index = 0; |
@@ -254,6 +200,13 @@ call_getopt (int argc, char **argv) | |||
254 | }; | 200 | }; |
255 | #endif | 201 | #endif |
256 | 202 | ||
203 | if (argc < 2) | ||
204 | return ERROR; | ||
205 | |||
206 | for (c = 1; c < argc; c++) | ||
207 | if (strcmp ("-to", argv[c]) == 0) | ||
208 | strcpy (argv[c], "-t"); | ||
209 | |||
257 | while (1) { | 210 | while (1) { |
258 | #ifdef HAVE_GETOPT_H | 211 | #ifdef HAVE_GETOPT_H |
259 | c = | 212 | c = |
@@ -266,20 +219,6 @@ call_getopt (int argc, char **argv) | |||
266 | if (c == -1 || c == EOF) | 219 | if (c == -1 || c == EOF) |
267 | break; | 220 | break; |
268 | 221 | ||
269 | i++; | ||
270 | switch (c) { | ||
271 | case 't': | ||
272 | case 'H': | ||
273 | case 'O': | ||
274 | case 'p': | ||
275 | case 'i': | ||
276 | case 'u': | ||
277 | case 'l': | ||
278 | case 'n': | ||
279 | case 's': | ||
280 | i++; | ||
281 | } | ||
282 | |||
283 | switch (c) { | 222 | switch (c) { |
284 | case '?': /* help */ | 223 | case '?': /* help */ |
285 | print_usage (); | 224 | print_usage (); |
@@ -306,14 +245,22 @@ call_getopt (int argc, char **argv) | |||
306 | case 'p': /* port number */ | 245 | case 'p': /* port number */ |
307 | if (!is_integer (optarg)) | 246 | if (!is_integer (optarg)) |
308 | usage2 ("port must be an integer", optarg); | 247 | usage2 ("port must be an integer", optarg); |
309 | comm = ssprintf (comm,"%s -p %s", comm, optarg); | 248 | asprintf (&comm,"%s -p %s", comm, optarg); |
310 | break; | 249 | break; |
311 | case 'O': /* output file */ | 250 | case 'O': /* output file */ |
312 | outputfile = optarg; | 251 | outputfile = optarg; |
313 | passive = TRUE; | 252 | passive = TRUE; |
314 | break; | 253 | break; |
315 | case 's': /* description of service to check */ | 254 | case 's': /* description of service to check */ |
316 | servicelist = optarg; | 255 | service = realloc (service, ++services); |
256 | p1 = optarg; | ||
257 | while (p2 = index (p1, ':')) { | ||
258 | *p2 = '\0'; | ||
259 | asprintf (&service[services-1], "%s", p1); | ||
260 | service = realloc (service, ++services); | ||
261 | p1 = p2 + 1; | ||
262 | } | ||
263 | asprintf (&service[services-1], "%s", p1); | ||
317 | break; | 264 | break; |
318 | case 'n': /* short name of host in nagios configuration */ | 265 | case 'n': /* short name of host in nagios configuration */ |
319 | host_shortname = optarg; | 266 | host_shortname = optarg; |
@@ -322,21 +269,42 @@ call_getopt (int argc, char **argv) | |||
322 | c = 'l'; | 269 | c = 'l'; |
323 | case 'l': /* login name */ | 270 | case 'l': /* login name */ |
324 | case 'i': /* identity */ | 271 | case 'i': /* identity */ |
325 | comm = ssprintf (comm, "%s -%c %s", comm, c, optarg); | 272 | asprintf (&comm, "%s -%c %s", comm, c, optarg); |
326 | break; | 273 | break; |
327 | case '4': /* Pass these switches directly to ssh */ | 274 | case '4': /* Pass these switches directly to ssh */ |
328 | case '6': /* -4 for IPv4, -6 for IPv6 */ | 275 | case '6': /* -4 for IPv4, -6 for IPv6 */ |
329 | case 'f': /* fork to background */ | 276 | case 'f': /* fork to background */ |
330 | comm = ssprintf (comm, "%s -%c", comm, c); | 277 | asprintf (&comm, "%s -%c", comm, c); |
331 | break; | 278 | break; |
332 | case 'C': /* Command for remote machine */ | 279 | case 'C': /* Command for remote machine */ |
333 | commands++; | 280 | commands++; |
334 | if (commands > 1) | 281 | if (commands > 1) |
335 | remotecmd = strscat (remotecmd, ";echo STATUS CODE: $?;"); | 282 | asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); |
336 | remotecmd = strscat (remotecmd, optarg); | 283 | asprintf (&remotecmd, "%s%s", remotecmd, optarg); |
337 | } | 284 | } |
338 | } | 285 | } |
339 | return i; | 286 | |
287 | c = optind; | ||
288 | if (hostname == NULL) { | ||
289 | if (!is_host (argv[c])) | ||
290 | terminate (STATE_UNKNOWN, "%s: Invalid host name %s\n", PROGNAME, argv[c]); | ||
291 | hostname = argv[c++]; | ||
292 | } | ||
293 | |||
294 | if (strlen(remotecmd) == 0) { | ||
295 | for (; c < argc; c++) | ||
296 | asprintf (&remotecmd, "%s %s", remotecmd, argv[c]); | ||
297 | } | ||
298 | |||
299 | if (commands > 1) | ||
300 | remotecmd = strscat (remotecmd, ";echo STATUS CODE: $?;"); | ||
301 | |||
302 | if (remotecmd == NULL || strlen (remotecmd) <= 1) | ||
303 | usage ("No remotecmd\n"); | ||
304 | |||
305 | asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd); | ||
306 | |||
307 | return validate_arguments (); | ||
340 | } | 308 | } |
341 | 309 | ||
342 | 310 | ||
@@ -348,6 +316,13 @@ validate_arguments (void) | |||
348 | { | 316 | { |
349 | if (remotecmd == NULL || hostname == NULL) | 317 | if (remotecmd == NULL || hostname == NULL) |
350 | return ERROR; | 318 | return ERROR; |
319 | |||
320 | if (passive && commands != services) | ||
321 | terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide a service name for each command.\n", PROGNAME); | ||
322 | |||
323 | if (passive && host_shortname == NULL) | ||
324 | terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide the host short name from the nagios configs.\n", PROGNAME); | ||
325 | |||
351 | return OK; | 326 | return OK; |
352 | } | 327 | } |
353 | 328 | ||