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