b27389d723974b7d171f077e456fe7fe13289750
1 /*****************************************************************************
2 *
3 * Nagios check_fping plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
7 *
8 * Description:
9 *
10 * This file contains the check_disk plugin
11 *
12 * This plugin will use the fping command to ping the specified host for a
13 * fast check
14 *
15 *
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 *
29 *
30 *****************************************************************************/
32 const char *progname = "check_fping";
33 const char *copyright = "2000-2007";
34 const char *email = "nagiosplug-devel@lists.sourceforge.net";
36 #include "common.h"
37 #include "popen.h"
38 #include "netutils.h"
39 #include "utils.h"
41 enum {
42 PACKET_COUNT = 1,
43 PACKET_SIZE = 56,
44 PL = 0,
45 RTA = 1
46 };
48 int textscan (char *buf);
49 int process_arguments (int, char **);
50 int get_threshold (char *arg, char *rv[2]);
51 void print_help (void);
52 void print_usage (void);
54 char *server_name = NULL;
55 int packet_size = PACKET_SIZE;
56 int packet_count = PACKET_COUNT;
57 int target_timeout = 0;
58 int packet_interval = 0;
59 int verbose = FALSE;
60 int cpl;
61 int wpl;
62 double crta;
63 double wrta;
64 int cpl_p = FALSE;
65 int wpl_p = FALSE;
66 int crta_p = FALSE;
67 int wrta_p = FALSE;
69 int
70 main (int argc, char **argv)
71 {
72 /* normaly should be int result = STATE_UNKNOWN; */
74 int status = STATE_UNKNOWN;
75 char *server = NULL;
76 char *command_line = NULL;
77 char *input_buffer = NULL;
78 char *option_string = "";
79 input_buffer = malloc (MAX_INPUT_BUFFER);
81 setlocale (LC_ALL, "");
82 bindtextdomain (PACKAGE, LOCALEDIR);
83 textdomain (PACKAGE);
85 /* Parse extra opts if any */
86 argv=np_extra_opts (&argc, argv, progname);
88 if (process_arguments (argc, argv) == ERROR)
89 usage4 (_("Could not parse arguments"));
91 server = strscpy (server, server_name);
93 /* compose the command */
94 if (target_timeout)
95 asprintf(&option_string, "%s-t %d ", option_string, target_timeout);
96 if (packet_interval)
97 asprintf(&option_string, "%s-p %d ", option_string, packet_interval);
99 asprintf (&command_line, "%s %s-b %d -c %d %s", PATH_TO_FPING,
100 option_string, packet_size, packet_count, server);
102 if (verbose)
103 printf ("%s\n", command_line);
105 /* run the command */
106 child_process = spopen (command_line);
107 if (child_process == NULL) {
108 printf (_("Could not open pipe: %s\n"), command_line);
109 return STATE_UNKNOWN;
110 }
112 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
113 if (child_stderr == NULL) {
114 printf (_("Could not open stderr for %s\n"), command_line);
115 }
117 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
118 if (verbose)
119 printf ("%s", input_buffer);
120 status = max_state (status, textscan (input_buffer));
121 }
123 /* If we get anything on STDERR, at least set warning */
124 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
125 status = max_state (status, STATE_WARNING);
126 if (verbose)
127 printf ("%s", input_buffer);
128 status = max_state (status, textscan (input_buffer));
129 }
130 (void) fclose (child_stderr);
132 /* close the pipe */
133 if (spclose (child_process))
134 /* need to use max_state not max */
135 status = max_state (status, STATE_WARNING);
137 printf ("FPING %s - %s\n", state_text (status), server_name);
139 return status;
140 }
144 int
145 textscan (char *buf)
146 {
147 char *rtastr = NULL;
148 char *losstr = NULL;
149 double loss;
150 double rta;
151 int status = STATE_UNKNOWN;
153 if (strstr (buf, "not found")) {
154 die (STATE_CRITICAL, _("FPING UNKNOW - %s not found\n"), server_name);
156 }
157 else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
158 die (STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"),
159 "host");
161 }
162 else if (strstr (buf, "is down")) {
163 die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
165 }
166 else if (strstr (buf, "is alive")) {
167 status = STATE_OK;
169 }
170 else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
171 losstr = strstr (buf, "=");
172 losstr = 1 + strstr (losstr, "/");
173 losstr = 1 + strstr (losstr, "/");
174 rtastr = strstr (buf, "min/avg/max");
175 rtastr = strstr (rtastr, "=");
176 rtastr = 1 + index (rtastr, '/');
177 loss = strtod (losstr, NULL);
178 rta = strtod (rtastr, NULL);
179 if (cpl_p == TRUE && loss > cpl)
180 status = STATE_CRITICAL;
181 else if (crta_p == TRUE && rta > crta)
182 status = STATE_CRITICAL;
183 else if (wpl_p == TRUE && loss > wpl)
184 status = STATE_WARNING;
185 else if (wrta_p == TRUE && rta > wrta)
186 status = STATE_WARNING;
187 else
188 status = STATE_OK;
189 die (status,
190 _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"),
191 state_text (status), server_name, loss, rta,
192 perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100),
193 fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0));
195 }
196 else if(strstr (buf, "xmt/rcv/%loss") ) {
197 /* no min/max/avg if host was unreachable in fping v2.2.b1 */
198 losstr = strstr (buf, "=");
199 losstr = 1 + strstr (losstr, "/");
200 losstr = 1 + strstr (losstr, "/");
201 loss = strtod (losstr, NULL);
202 if (atoi(losstr) == 100)
203 status = STATE_CRITICAL;
204 else if (cpl_p == TRUE && loss > cpl)
205 status = STATE_CRITICAL;
206 else if (wpl_p == TRUE && loss > wpl)
207 status = STATE_WARNING;
208 else
209 status = STATE_OK;
210 /* loss=%.0f%%;%d;%d;0;100 */
211 die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"),
212 state_text (status), server_name, loss ,
213 perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100));
215 }
216 else {
217 status = max_state (status, STATE_WARNING);
218 }
220 return status;
221 }
225 /* process command-line arguments */
226 int
227 process_arguments (int argc, char **argv)
228 {
229 int c;
230 char *rv[2];
232 int option = 0;
233 static struct option longopts[] = {
234 {"hostname", required_argument, 0, 'H'},
235 {"critical", required_argument, 0, 'c'},
236 {"warning", required_argument, 0, 'w'},
237 {"bytes", required_argument, 0, 'b'},
238 {"number", required_argument, 0, 'n'},
239 {"target-timeout", required_argument, 0, 'T'},
240 {"interval", required_argument, 0, 'i'},
241 {"verbose", no_argument, 0, 'v'},
242 {"version", no_argument, 0, 'V'},
243 {"help", no_argument, 0, 'h'},
244 {0, 0, 0, 0}
245 };
247 rv[PL] = NULL;
248 rv[RTA] = NULL;
250 if (argc < 2)
251 return ERROR;
253 if (!is_option (argv[1])) {
254 server_name = argv[1];
255 argv[1] = argv[0];
256 argv = &argv[1];
257 argc--;
258 }
260 while (1) {
261 c = getopt_long (argc, argv, "+hVvH:c:w:b:n:T:i:", longopts, &option);
263 if (c == -1 || c == EOF || c == 1)
264 break;
266 switch (c) {
267 case '?': /* print short usage statement if args not parsable */
268 usage5 ();
269 case 'h': /* help */
270 print_help ();
271 exit (STATE_OK);
272 case 'V': /* version */
273 print_revision (progname, NP_VERSION);
274 exit (STATE_OK);
275 case 'v': /* verbose mode */
276 verbose = TRUE;
277 break;
278 case 'H': /* hostname */
279 if (is_host (optarg) == FALSE) {
280 usage2 (_("Invalid hostname/address"), optarg);
281 }
282 server_name = strscpy (server_name, optarg);
283 break;
284 case 'c':
285 get_threshold (optarg, rv);
286 if (rv[RTA]) {
287 crta = strtod (rv[RTA], NULL);
288 crta_p = TRUE;
289 rv[RTA] = NULL;
290 }
291 if (rv[PL]) {
292 cpl = atoi (rv[PL]);
293 cpl_p = TRUE;
294 rv[PL] = NULL;
295 }
296 break;
297 case 'w':
298 get_threshold (optarg, rv);
299 if (rv[RTA]) {
300 wrta = strtod (rv[RTA], NULL);
301 wrta_p = TRUE;
302 rv[RTA] = NULL;
303 }
304 if (rv[PL]) {
305 wpl = atoi (rv[PL]);
306 wpl_p = TRUE;
307 rv[PL] = NULL;
308 }
309 break;
310 case 'b': /* bytes per packet */
311 if (is_intpos (optarg))
312 packet_size = atoi (optarg);
313 else
314 usage (_("Packet size must be a positive integer"));
315 break;
316 case 'n': /* number of packets */
317 if (is_intpos (optarg))
318 packet_count = atoi (optarg);
319 else
320 usage (_("Packet count must be a positive integer"));
321 break;
322 case 'T': /* timeout in msec */
323 if (is_intpos (optarg))
324 target_timeout = atoi (optarg);
325 else
326 usage (_("Target timeout must be a positive integer"));
327 break;
328 case 'i': /* interval in msec */
329 if (is_intpos (optarg))
330 packet_interval = atoi (optarg);
331 else
332 usage (_("Interval must be a positive integer"));
333 break;
334 }
335 }
337 if (server_name == NULL)
338 usage4 (_("Hostname was not supplied"));
340 return OK;
341 }
344 int
345 get_threshold (char *arg, char *rv[2])
346 {
347 char *arg1 = NULL;
348 char *arg2 = NULL;
350 arg1 = strscpy (arg1, arg);
351 if (strpbrk (arg1, ",:"))
352 arg2 = 1 + strpbrk (arg1, ",:");
354 if (arg2) {
355 arg1[strcspn (arg1, ",:")] = 0;
356 if (strstr (arg1, "%") && strstr (arg2, "%"))
357 die (STATE_UNKNOWN,
358 _("%s: Only one threshold may be packet loss (%s)\n"), progname,
359 arg);
360 if (!strstr (arg1, "%") && !strstr (arg2, "%"))
361 die (STATE_UNKNOWN,
362 _("%s: Only one threshold must be packet loss (%s)\n"),
363 progname, arg);
364 }
366 if (arg2 && strstr (arg2, "%")) {
367 rv[PL] = arg2;
368 rv[RTA] = arg1;
369 }
370 else if (arg2) {
371 rv[PL] = arg1;
372 rv[RTA] = arg2;
373 }
374 else if (strstr (arg1, "%")) {
375 rv[PL] = arg1;
376 }
377 else {
378 rv[RTA] = arg1;
379 }
381 return OK;
382 }
385 void
386 print_help (void)
387 {
389 print_revision (progname, NP_VERSION);
391 printf ("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n");
392 printf (COPYRIGHT, copyright, email);
394 printf ("%s\n", _("This plugin will use the fping command to ping the specified host for a fast check"));
396 printf ("%s\n", _("Note that it is necessary to set the suid flag on fping."));
398 printf ("\n\n");
400 print_usage ();
402 printf (_(UT_HELP_VRSN));
403 printf (_(UT_EXTRA_OPTS));
405 printf (" %s\n", "-H, --hostname=HOST");
406 printf (" %s\n", _("name or IP Address of host to ping (IP Address bypasses name lookup, reducing system load)"));
407 printf (" %s\n", "-w, --warning=THRESHOLD");
408 printf (" %s\n", _("warning threshold pair"));
409 printf (" %s\n", "-c, --critical=THRESHOLD");
410 printf (" %s\n", _("critical threshold pair"));
411 printf (" %s\n", "-b, --bytes=INTEGER");
412 printf (" %s (default: %d)\n", _("size of ICMP packet"),PACKET_SIZE);
413 printf (" %s\n", "-n, --number=INTEGER");
414 printf (" %s (default: %d)\n", _("number of ICMP packets to send"),PACKET_COUNT);
415 printf (" %s\n", "-T, --target-timeout=INTEGER");
416 printf (" %s (default: fping's default for -t)\n", _("Target timeout (ms)"),PACKET_COUNT);
417 printf (" %s\n", "-i, --interval=INTEGER");
418 printf (" %s (default: fping's default for -p)\n", _("Interval (ms) between sending packets"),PACKET_COUNT);
419 printf (_(UT_VERBOSE));
420 printf ("\n");
421 printf (" %s\n", _("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel time (ms)"));
422 printf (" %s\n", _("which triggers a WARNING or CRITICAL state, and <pl> is the percentage of"));
423 printf (" %s\n", _("packet loss to trigger an alarm state."));
425 #ifdef NP_EXTRA_OPTS
426 printf ("\n");
427 printf ("%s\n", _("Notes:"));
428 printf (_(UT_EXTRA_OPTS_NOTES));
429 #endif
431 printf (_(UT_SUPPORT));
432 }
435 void
436 print_usage (void)
437 {
438 printf (_("Usage:"));
439 printf (" %s <host_address> -w limit -c limit [-b size] [-n number] [-T number] [-i number]\n", progname);
440 }