diff options
author | Ethan Galstad <egalstad@users.sourceforge.net> | 2002-02-28 06:42:51 (GMT) |
---|---|---|
committer | Ethan Galstad <egalstad@users.sourceforge.net> | 2002-02-28 06:42:51 (GMT) |
commit | 44a321cb8a42d6c0ea2d96a1086a17f2134c89cc (patch) | |
tree | a1a4d9f7b92412a17ab08f34f04eec45433048b7 /plugins/check_fping.c | |
parent | 54fd5d7022ff2d6a59bc52b8869182f3fc77a058 (diff) | |
download | monitoring-plugins-44a321cb8a42d6c0ea2d96a1086a17f2134c89cc.tar.gz |
Initial revision
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@2 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/check_fping.c')
-rw-r--r-- | plugins/check_fping.c | 386 |
1 files changed, 386 insertions, 0 deletions
diff --git a/plugins/check_fping.c b/plugins/check_fping.c new file mode 100644 index 0000000..f6531a5 --- /dev/null +++ b/plugins/check_fping.c | |||
@@ -0,0 +1,386 @@ | |||
1 | /****************************************************************************** | ||
2 | * | ||
3 | * CHECK_FPING.C | ||
4 | * | ||
5 | * Program: Fping plugin for Nagios | ||
6 | * License: GPL | ||
7 | * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at) | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Modifications: | ||
11 | * | ||
12 | * 08-24-1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at) | ||
13 | * Intial Coding | ||
14 | * 09-11-1999 Karl DeBisschop (kdebiss@alum.mit.edu) | ||
15 | * Change to spopen | ||
16 | * Fix so that state unknown is returned by default | ||
17 | * (formerly would give state ok if no fping specified) | ||
18 | * Add server_name to output | ||
19 | * Reformat to 80-character standard screen | ||
20 | * 11-18-1999 Karl DeBisschop (kdebiss@alum.mit.edu) | ||
21 | * set STATE_WARNING of stderr written or nonzero status returned | ||
22 | * | ||
23 | * Description: | ||
24 | * | ||
25 | * This plugin will use the /bin/fping command (form saint) to ping | ||
26 | * the specified host for a fast check if the host is alive. Note that | ||
27 | * it is necessary to set the suid flag on fping. | ||
28 | ******************************************************************************/ | ||
29 | |||
30 | #include "config.h" | ||
31 | #include "common.h" | ||
32 | #include "popen.h" | ||
33 | #include "utils.h" | ||
34 | |||
35 | #define PROGNAME "check_fping" | ||
36 | #define PACKET_COUNT 1 | ||
37 | #define PACKET_SIZE 56 | ||
38 | #define UNKNOWN_PACKET_LOSS 200 /* 200% */ | ||
39 | #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */ | ||
40 | |||
41 | #define PL 0 | ||
42 | #define RTA 1 | ||
43 | |||
44 | int textscan (char *buf); | ||
45 | int process_arguments (int, char **); | ||
46 | int get_threshold (char *arg, char *rv[2]); | ||
47 | void print_usage (void); | ||
48 | void print_help (void); | ||
49 | |||
50 | char *server_name = NULL; | ||
51 | int cpl = UNKNOWN_PACKET_LOSS; | ||
52 | int wpl = UNKNOWN_PACKET_LOSS; | ||
53 | double crta = UNKNOWN_TRIP_TIME; | ||
54 | double wrta = UNKNOWN_TRIP_TIME; | ||
55 | int packet_size = PACKET_SIZE; | ||
56 | int packet_count = PACKET_COUNT; | ||
57 | int verbose = FALSE; | ||
58 | |||
59 | int | ||
60 | main (int argc, char **argv) | ||
61 | { | ||
62 | int status = STATE_UNKNOWN; | ||
63 | char *server = NULL; | ||
64 | char *command_line = NULL; | ||
65 | char *input_buffer = NULL; | ||
66 | input_buffer = malloc (MAX_INPUT_BUFFER); | ||
67 | |||
68 | if (process_arguments (argc, argv) == ERROR) | ||
69 | usage ("Could not parse arguments\n"); | ||
70 | |||
71 | server = strscpy (server, server_name); | ||
72 | |||
73 | /* compose the command */ | ||
74 | command_line = ssprintf | ||
75 | (command_line, "%s -b %d -c %d %s", | ||
76 | PATH_TO_FPING, packet_size, packet_count, server); | ||
77 | |||
78 | if (verbose) | ||
79 | printf ("%s\n", command_line); | ||
80 | |||
81 | /* run the command */ | ||
82 | child_process = spopen (command_line); | ||
83 | if (child_process == NULL) { | ||
84 | printf ("Unable to open pipe: %s\n", command_line); | ||
85 | return STATE_UNKNOWN; | ||
86 | } | ||
87 | |||
88 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
89 | if (child_stderr == NULL) { | ||
90 | printf ("Could not open stderr for %s\n", command_line); | ||
91 | } | ||
92 | |||
93 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
94 | if (verbose) | ||
95 | printf ("%s", input_buffer); | ||
96 | status = max (status, textscan (input_buffer)); | ||
97 | } | ||
98 | |||
99 | /* If we get anything on STDERR, at least set warning */ | ||
100 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | ||
101 | status = max (status, STATE_WARNING); | ||
102 | if (verbose) | ||
103 | printf ("%s", input_buffer); | ||
104 | status = max (status, textscan (input_buffer)); | ||
105 | } | ||
106 | (void) fclose (child_stderr); | ||
107 | |||
108 | /* close the pipe */ | ||
109 | if (spclose (child_process)) | ||
110 | status = max (status, STATE_WARNING); | ||
111 | |||
112 | printf ("FPING %s - %s\n", state_text (status), server_name); | ||
113 | |||
114 | return status; | ||
115 | } | ||
116 | |||
117 | |||
118 | |||
119 | |||
120 | int | ||
121 | textscan (char *buf) | ||
122 | { | ||
123 | char *rtastr = NULL; | ||
124 | char *losstr = NULL; | ||
125 | double loss; | ||
126 | double rta; | ||
127 | int status = STATE_UNKNOWN; | ||
128 | |||
129 | if (strstr (buf, "not found")) { | ||
130 | terminate (STATE_CRITICAL, "FPING unknown - %s not found\n", server_name); | ||
131 | |||
132 | } | ||
133 | else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) { | ||
134 | terminate (STATE_CRITICAL, "FPING critical - %s is unreachable\n", | ||
135 | "host"); | ||
136 | |||
137 | } | ||
138 | else if (strstr (buf, "is down")) { | ||
139 | terminate (STATE_CRITICAL, "FPING critical - %s is down\n", server_name); | ||
140 | |||
141 | } | ||
142 | else if (strstr (buf, "is alive")) { | ||
143 | status = STATE_OK; | ||
144 | |||
145 | } | ||
146 | else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) { | ||
147 | losstr = strstr (buf, "="); | ||
148 | losstr = 1 + strstr (losstr, "/"); | ||
149 | losstr = 1 + strstr (losstr, "/"); | ||
150 | rtastr = strstr (buf, "min/avg/max"); | ||
151 | rtastr = strstr (rtastr, "="); | ||
152 | rtastr = 1 + index (rtastr, '/'); | ||
153 | loss = strtod (losstr, NULL); | ||
154 | rta = strtod (rtastr, NULL); | ||
155 | if (cpl != UNKNOWN_PACKET_LOSS && loss > cpl) | ||
156 | status = STATE_CRITICAL; | ||
157 | else if (crta != UNKNOWN_TRIP_TIME && rta > crta) | ||
158 | status = STATE_CRITICAL; | ||
159 | else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl) | ||
160 | status = STATE_WARNING; | ||
161 | else if (wrta != UNKNOWN_TRIP_TIME && rta > wrta) | ||
162 | status = STATE_WARNING; | ||
163 | else | ||
164 | status = STATE_OK; | ||
165 | terminate (status, "FPING %s - %s (loss=%f%%, rta=%f ms)\n", | ||
166 | state_text (status), server_name, loss, rta); | ||
167 | |||
168 | } | ||
169 | else { | ||
170 | status = max (status, STATE_WARNING); | ||
171 | } | ||
172 | |||
173 | return status; | ||
174 | } | ||
175 | |||
176 | |||
177 | |||
178 | |||
179 | /* process command-line arguments */ | ||
180 | int | ||
181 | process_arguments (int argc, char **argv) | ||
182 | { | ||
183 | int c; | ||
184 | char *rv[2]; | ||
185 | |||
186 | #ifdef HAVE_GETOPT_H | ||
187 | int option_index = 0; | ||
188 | static struct option long_options[] = { | ||
189 | {"hostname", required_argument, 0, 'H'}, | ||
190 | {"critical", required_argument, 0, 'c'}, | ||
191 | {"warning", required_argument, 0, 'w'}, | ||
192 | {"bytes", required_argument, 0, 'b'}, | ||
193 | {"number", required_argument, 0, 'n'}, | ||
194 | {"verbose", no_argument, 0, 'v'}, | ||
195 | {"version", no_argument, 0, 'V'}, | ||
196 | {"help", no_argument, 0, 'h'}, | ||
197 | {0, 0, 0, 0} | ||
198 | }; | ||
199 | #endif | ||
200 | |||
201 | rv[PL] = NULL; | ||
202 | rv[RTA] = NULL; | ||
203 | |||
204 | if (argc < 2) | ||
205 | return ERROR; | ||
206 | |||
207 | if (!is_option (argv[1])) { | ||
208 | server_name = argv[1]; | ||
209 | argv[1] = argv[0]; | ||
210 | argv = &argv[1]; | ||
211 | argc--; | ||
212 | } | ||
213 | |||
214 | while (1) { | ||
215 | #ifdef HAVE_GETOPT_H | ||
216 | c = | ||
217 | getopt_long (argc, argv, "+hVvH:c:w:b:n:", long_options, &option_index); | ||
218 | #else | ||
219 | c = getopt (argc, argv, "+hVvH:c:w:b:n:"); | ||
220 | #endif | ||
221 | |||
222 | if (c == -1 || c == EOF || c == 1) | ||
223 | break; | ||
224 | |||
225 | switch (c) { | ||
226 | case '?': /* print short usage statement if args not parsable */ | ||
227 | printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg); | ||
228 | print_usage (); | ||
229 | exit (STATE_UNKNOWN); | ||
230 | case 'h': /* help */ | ||
231 | print_help (); | ||
232 | exit (STATE_OK); | ||
233 | case 'V': /* version */ | ||
234 | print_revision (my_basename (argv[0]), "$Revision$"); | ||
235 | exit (STATE_OK); | ||
236 | case 'v': /* verbose mode */ | ||
237 | verbose = TRUE; | ||
238 | break; | ||
239 | case 'H': /* hostname */ | ||
240 | if (is_host (optarg) == FALSE) { | ||
241 | printf ("Invalid host name/address\n\n"); | ||
242 | print_usage (); | ||
243 | exit (STATE_UNKNOWN); | ||
244 | } | ||
245 | server_name = strscpy (server_name, optarg); | ||
246 | break; | ||
247 | case 'c': | ||
248 | get_threshold (optarg, rv); | ||
249 | if (rv[RTA]) { | ||
250 | crta = strtod (rv[RTA], NULL); | ||
251 | rv[RTA] = NULL; | ||
252 | } | ||
253 | if (rv[PL]) { | ||
254 | cpl = atoi (rv[PL]); | ||
255 | rv[PL] = NULL; | ||
256 | } | ||
257 | break; | ||
258 | case 'w': | ||
259 | get_threshold (optarg, rv); | ||
260 | if (rv[RTA]) { | ||
261 | wrta = strtod (rv[RTA], NULL); | ||
262 | rv[RTA] = NULL; | ||
263 | } | ||
264 | if (rv[PL]) { | ||
265 | wpl = atoi (rv[PL]); | ||
266 | rv[PL] = NULL; | ||
267 | } | ||
268 | break; | ||
269 | case 'b': /* bytes per packet */ | ||
270 | if (is_intpos (optarg)) | ||
271 | packet_size = atoi (optarg); | ||
272 | else | ||
273 | usage ("Packet size must be a positive integer"); | ||
274 | break; | ||
275 | case 'n': /* number of packets */ | ||
276 | if (is_intpos (optarg)) | ||
277 | packet_count = atoi (optarg); | ||
278 | else | ||
279 | usage ("Packet count must be a positive integer"); | ||
280 | break; | ||
281 | } | ||
282 | } | ||
283 | |||
284 | |||
285 | if (server_name == NULL) | ||
286 | usage ("Host name was not supplied\n\n"); | ||
287 | |||
288 | return OK; | ||
289 | } | ||
290 | |||
291 | |||
292 | |||
293 | |||
294 | |||
295 | int | ||
296 | get_threshold (char *arg, char *rv[2]) | ||
297 | { | ||
298 | char *arg1 = NULL; | ||
299 | char *arg2 = NULL; | ||
300 | |||
301 | arg1 = strscpy (arg1, arg); | ||
302 | if (strpbrk (arg1, ",:")) | ||
303 | arg2 = 1 + strpbrk (arg1, ",:"); | ||
304 | |||
305 | if (arg2) { | ||
306 | arg1[strcspn (arg1, ",:")] = 0; | ||
307 | if (strstr (arg1, "%") && strstr (arg2, "%")) | ||
308 | terminate (STATE_UNKNOWN, | ||
309 | "%s: Only one threshold may be packet loss (%s)\n", PROGNAME, | ||
310 | arg); | ||
311 | if (!strstr (arg1, "%") && !strstr (arg2, "%")) | ||
312 | terminate (STATE_UNKNOWN, | ||
313 | "%s: Only one threshold must be packet loss (%s)\n", | ||
314 | PROGNAME, arg); | ||
315 | } | ||
316 | |||
317 | if (arg2 && strstr (arg2, "%")) { | ||
318 | rv[PL] = arg2; | ||
319 | rv[RTA] = arg1; | ||
320 | } | ||
321 | else if (arg2) { | ||
322 | rv[PL] = arg1; | ||
323 | rv[RTA] = arg2; | ||
324 | } | ||
325 | else if (strstr (arg1, "%")) { | ||
326 | rv[PL] = arg1; | ||
327 | } | ||
328 | else { | ||
329 | rv[RTA] = arg1; | ||
330 | } | ||
331 | |||
332 | return OK; | ||
333 | } | ||
334 | |||
335 | |||
336 | |||
337 | |||
338 | |||
339 | void | ||
340 | print_usage (void) | ||
341 | { | ||
342 | printf ("Usage: %s <host_address>\n", PROGNAME); | ||
343 | } | ||
344 | |||
345 | |||
346 | |||
347 | |||
348 | |||
349 | void | ||
350 | print_help (void) | ||
351 | { | ||
352 | |||
353 | print_revision (PROGNAME, "$Revision$"); | ||
354 | |||
355 | printf | ||
356 | ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n\n" | ||
357 | "This plugin will use the /bin/fping command (from saint) to ping the\n" | ||
358 | "specified host for a fast check if the host is alive. Note that it is\n" | ||
359 | "necessary to set the suid flag on fping.\n\n"); | ||
360 | |||
361 | print_usage (); | ||
362 | |||
363 | printf | ||
364 | ("\nOptions:\n" | ||
365 | "-H, --hostname=HOST\n" | ||
366 | " Name or IP Address of host to ping (IP Address bypasses name lookup,\n" | ||
367 | " reducing system load)\n" | ||
368 | "-w, --warning=THRESHOLD\n" | ||
369 | " warning threshold pair\n" | ||
370 | "-c, --critical=THRESHOLD\n" | ||
371 | " critical threshold pair\n" | ||
372 | "-b, --bytes=INTEGER\n" | ||
373 | " Size of ICMP packet (default: %d)\n" | ||
374 | "-n, --number=INTEGER\n" | ||
375 | " Number of ICMP packets to send (default: %d)\n" | ||
376 | "-v, --verbose\n" | ||
377 | " Show details for command-line debugging (do not use with nagios server)\n" | ||
378 | "-h, --help\n" | ||
379 | " Print this help screen\n" | ||
380 | "-V, --version\n" | ||
381 | " Print version information\n" | ||
382 | "THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n" | ||
383 | "time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n" | ||
384 | "percentage of packet loss to trigger an alarm state.\n", | ||
385 | PACKET_SIZE, PACKET_COUNT); | ||
386 | } | ||