1 /*****************************************************************************
2 *
3 * Nagios check_fping plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_disk plugin
13 *
14 * This plugin will use the fping command to ping the specified host for a
15 * fast check
16 *
17 *
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 *
31 * $Id$
32 *
33 *****************************************************************************/
35 const char *progname = "check_fping";
36 const char *revision = "$Revision$";
37 const char *copyright = "2000-2007";
38 const char *email = "nagiosplug-devel@lists.sourceforge.net";
40 #include "common.h"
41 #include "popen.h"
42 #include "netutils.h"
43 #include "utils.h"
45 enum {
46 PACKET_COUNT = 1,
47 PACKET_SIZE = 56,
48 PL = 0,
49 RTA = 1
50 };
52 int textscan (char *buf);
53 int process_arguments (int, char **);
54 int get_threshold (char *arg, char *rv[2]);
55 void print_help (void);
56 void print_usage (void);
58 char *server_name = NULL;
59 int packet_size = PACKET_SIZE;
60 int packet_count = PACKET_COUNT;
61 int verbose = FALSE;
62 int cpl;
63 int wpl;
64 double crta;
65 double wrta;
66 int cpl_p = FALSE;
67 int wpl_p = FALSE;
68 int crta_p = FALSE;
69 int wrta_p = FALSE;
71 int
72 main (int argc, char **argv)
73 {
74 /* normaly should be int result = STATE_UNKNOWN; */
76 int status = STATE_UNKNOWN;
77 char *server = NULL;
78 char *command_line = NULL;
79 char *input_buffer = NULL;
80 input_buffer = malloc (MAX_INPUT_BUFFER);
82 setlocale (LC_ALL, "");
83 bindtextdomain (PACKAGE, LOCALEDIR);
84 textdomain (PACKAGE);
86 if (process_arguments (argc, argv) == ERROR)
87 usage4 (_("Could not parse arguments"));
89 server = strscpy (server, server_name);
91 /* compose the command */
92 asprintf (&command_line, "%s -b %d -c %d %s", PATH_TO_FPING,
93 packet_size, packet_count, server);
95 if (verbose)
96 printf ("%s\n", command_line);
98 /* run the command */
99 child_process = spopen (command_line);
100 if (child_process == NULL) {
101 printf (_("Could not open pipe: %s\n"), command_line);
102 return STATE_UNKNOWN;
103 }
105 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
106 if (child_stderr == NULL) {
107 printf (_("Could not open stderr for %s\n"), command_line);
108 }
110 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
111 if (verbose)
112 printf ("%s", input_buffer);
113 status = max_state (status, textscan (input_buffer));
114 }
116 /* If we get anything on STDERR, at least set warning */
117 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
118 status = max_state (status, STATE_WARNING);
119 if (verbose)
120 printf ("%s", input_buffer);
121 status = max_state (status, textscan (input_buffer));
122 }
123 (void) fclose (child_stderr);
125 /* close the pipe */
126 if (spclose (child_process))
127 /* need to use max_state not max */
128 status = max_state (status, STATE_WARNING);
130 printf ("FPING %s - %s\n", state_text (status), server_name);
132 return status;
133 }
137 int
138 textscan (char *buf)
139 {
140 char *rtastr = NULL;
141 char *losstr = NULL;
142 double loss;
143 double rta;
144 int status = STATE_UNKNOWN;
146 if (strstr (buf, "not found")) {
147 die (STATE_CRITICAL, _("FPING UNKNOW - %s not found\n"), server_name);
149 }
150 else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
151 die (STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"),
152 "host");
154 }
155 else if (strstr (buf, "is down")) {
156 die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
158 }
159 else if (strstr (buf, "is alive")) {
160 status = STATE_OK;
162 }
163 else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
164 losstr = strstr (buf, "=");
165 losstr = 1 + strstr (losstr, "/");
166 losstr = 1 + strstr (losstr, "/");
167 rtastr = strstr (buf, "min/avg/max");
168 rtastr = strstr (rtastr, "=");
169 rtastr = 1 + index (rtastr, '/');
170 loss = strtod (losstr, NULL);
171 rta = strtod (rtastr, NULL);
172 if (cpl_p == TRUE && loss > cpl)
173 status = STATE_CRITICAL;
174 else if (crta_p == TRUE && rta > crta)
175 status = STATE_CRITICAL;
176 else if (wpl_p == TRUE && loss > wpl)
177 status = STATE_WARNING;
178 else if (wrta_p == TRUE && rta > wrta)
179 status = STATE_WARNING;
180 else
181 status = STATE_OK;
182 die (status,
183 _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"),
184 state_text (status), server_name, loss, rta,
185 perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100),
186 fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0));
188 }
189 else if(strstr (buf, "xmt/rcv/%loss") ) {
190 /* no min/max/avg if host was unreachable in fping v2.2.b1 */
191 losstr = strstr (buf, "=");
192 losstr = 1 + strstr (losstr, "/");
193 losstr = 1 + strstr (losstr, "/");
194 loss = strtod (losstr, NULL);
195 if (atoi(losstr) == 100)
196 status = STATE_CRITICAL;
197 else if (cpl_p == TRUE && loss > cpl)
198 status = STATE_CRITICAL;
199 else if (wpl_p == TRUE && loss > wpl)
200 status = STATE_WARNING;
201 else
202 status = STATE_OK;
203 /* loss=%.0f%%;%d;%d;0;100 */
204 die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"),
205 state_text (status), server_name, loss ,
206 perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100));
208 }
209 else {
210 status = max_state (status, STATE_WARNING);
211 }
213 return status;
214 }
218 /* process command-line arguments */
219 int
220 process_arguments (int argc, char **argv)
221 {
222 int c;
223 char *rv[2];
225 int option = 0;
226 static struct option longopts[] = {
227 {"hostname", required_argument, 0, 'H'},
228 {"critical", required_argument, 0, 'c'},
229 {"warning", required_argument, 0, 'w'},
230 {"bytes", required_argument, 0, 'b'},
231 {"number", required_argument, 0, 'n'},
232 {"verbose", no_argument, 0, 'v'},
233 {"version", no_argument, 0, 'V'},
234 {"help", no_argument, 0, 'h'},
235 {0, 0, 0, 0}
236 };
238 rv[PL] = NULL;
239 rv[RTA] = NULL;
241 if (argc < 2)
242 return ERROR;
244 if (!is_option (argv[1])) {
245 server_name = argv[1];
246 argv[1] = argv[0];
247 argv = &argv[1];
248 argc--;
249 }
251 while (1) {
252 c = getopt_long (argc, argv, "+hVvH:c:w:b:n:", longopts, &option);
254 if (c == -1 || c == EOF || c == 1)
255 break;
257 switch (c) {
258 case '?': /* print short usage statement if args not parsable */
259 usage5 ();
260 case 'h': /* help */
261 print_help ();
262 exit (STATE_OK);
263 case 'V': /* version */
264 print_revision (progname, revision);
265 exit (STATE_OK);
266 case 'v': /* verbose mode */
267 verbose = TRUE;
268 break;
269 case 'H': /* hostname */
270 if (is_host (optarg) == FALSE) {
271 usage2 (_("Invalid hostname/address"), optarg);
272 }
273 server_name = strscpy (server_name, optarg);
274 break;
275 case 'c':
276 get_threshold (optarg, rv);
277 if (rv[RTA]) {
278 crta = strtod (rv[RTA], NULL);
279 crta_p = TRUE;
280 rv[RTA] = NULL;
281 }
282 if (rv[PL]) {
283 cpl = atoi (rv[PL]);
284 cpl_p = TRUE;
285 rv[PL] = NULL;
286 }
287 break;
288 case 'w':
289 get_threshold (optarg, rv);
290 if (rv[RTA]) {
291 wrta = strtod (rv[RTA], NULL);
292 wrta_p = TRUE;
293 rv[RTA] = NULL;
294 }
295 if (rv[PL]) {
296 wpl = atoi (rv[PL]);
297 wpl_p = TRUE;
298 rv[PL] = NULL;
299 }
300 break;
301 case 'b': /* bytes per packet */
302 if (is_intpos (optarg))
303 packet_size = atoi (optarg);
304 else
305 usage (_("Packet size must be a positive integer"));
306 break;
307 case 'n': /* number of packets */
308 if (is_intpos (optarg))
309 packet_count = atoi (optarg);
310 else
311 usage (_("Packet count must be a positive integer"));
312 break;
313 }
314 }
316 if (server_name == NULL)
317 usage4 (_("Hostname was not supplied"));
319 return OK;
320 }
323 int
324 get_threshold (char *arg, char *rv[2])
325 {
326 char *arg1 = NULL;
327 char *arg2 = NULL;
329 arg1 = strscpy (arg1, arg);
330 if (strpbrk (arg1, ",:"))
331 arg2 = 1 + strpbrk (arg1, ",:");
333 if (arg2) {
334 arg1[strcspn (arg1, ",:")] = 0;
335 if (strstr (arg1, "%") && strstr (arg2, "%"))
336 die (STATE_UNKNOWN,
337 _("%s: Only one threshold may be packet loss (%s)\n"), progname,
338 arg);
339 if (!strstr (arg1, "%") && !strstr (arg2, "%"))
340 die (STATE_UNKNOWN,
341 _("%s: Only one threshold must be packet loss (%s)\n"),
342 progname, arg);
343 }
345 if (arg2 && strstr (arg2, "%")) {
346 rv[PL] = arg2;
347 rv[RTA] = arg1;
348 }
349 else if (arg2) {
350 rv[PL] = arg1;
351 rv[RTA] = arg2;
352 }
353 else if (strstr (arg1, "%")) {
354 rv[PL] = arg1;
355 }
356 else {
357 rv[RTA] = arg1;
358 }
360 return OK;
361 }
364 void
365 print_help (void)
366 {
368 print_revision (progname, revision);
370 printf ("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n");
371 printf (COPYRIGHT, copyright, email);
373 printf ("%s\n", _("This plugin will use the fping command to ping the specified host for a fast check"));
375 printf ("%s\n", _("Note that it is necessary to set the suid flag on fping."));
377 printf ("\n\n");
379 print_usage ();
381 printf (_(UT_HELP_VRSN));
383 printf (" %s\n", "-H, --hostname=HOST");
384 printf (" %s\n", _("name or IP Address of host to ping (IP Address bypasses name lookup, reducing system load)"));
385 printf (" %s\n", "-w, --warning=THRESHOLD");
386 printf (" %s\n", _("warning threshold pair"));
387 printf (" %s\n", "-c, --critical=THRESHOLD");
388 printf (" %s\n", _("critical threshold pair"));
389 printf (" %s\n", "-b, --bytes=INTEGER");
390 printf (" %s\n", _("size of ICMP packet (default: %d)"),PACKET_SIZE);
391 printf (" %s\n", "-n, --number=INTEGER");
392 printf (" %s\n", _("number of ICMP packets to send (default: %d)"),PACKET_COUNT);
393 printf (_(UT_VERBOSE));
394 printf ("\n");
395 printf (" %s\n", _("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel time (ms)"));
396 printf (" %s\n", _("which triggers a WARNING or CRITICAL state, and <pl> is the percentage of"));
397 printf (" %s\n", _("packet loss to trigger an alarm state."));
398 printf (_(UT_SUPPORT));
399 }
402 void
403 print_usage (void)
404 {
405 printf (_("Usage:"));
406 printf (" %s <host_address> -w limit -c limit [-b size] [-n number]\n", progname);
407 }