aeb2f936169fd43427b30bfc6bd893b11cfc29b0
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 /* Parse extra opts if any */
87 argv=np_extra_opts (&argc, argv, progname);
89 if (process_arguments (argc, argv) == ERROR)
90 usage4 (_("Could not parse arguments"));
92 server = strscpy (server, server_name);
94 /* compose the command */
95 asprintf (&command_line, "%s -b %d -c %d %s", PATH_TO_FPING,
96 packet_size, packet_count, server);
98 if (verbose)
99 printf ("%s\n", command_line);
101 /* run the command */
102 child_process = spopen (command_line);
103 if (child_process == NULL) {
104 printf (_("Could not open pipe: %s\n"), command_line);
105 return STATE_UNKNOWN;
106 }
108 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
109 if (child_stderr == NULL) {
110 printf (_("Could not open stderr for %s\n"), command_line);
111 }
113 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
114 if (verbose)
115 printf ("%s", input_buffer);
116 status = max_state (status, textscan (input_buffer));
117 }
119 /* If we get anything on STDERR, at least set warning */
120 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
121 status = max_state (status, STATE_WARNING);
122 if (verbose)
123 printf ("%s", input_buffer);
124 status = max_state (status, textscan (input_buffer));
125 }
126 (void) fclose (child_stderr);
128 /* close the pipe */
129 if (spclose (child_process))
130 /* need to use max_state not max */
131 status = max_state (status, STATE_WARNING);
133 printf ("FPING %s - %s\n", state_text (status), server_name);
135 return status;
136 }
140 int
141 textscan (char *buf)
142 {
143 char *rtastr = NULL;
144 char *losstr = NULL;
145 double loss;
146 double rta;
147 int status = STATE_UNKNOWN;
149 if (strstr (buf, "not found")) {
150 die (STATE_CRITICAL, _("FPING UNKNOW - %s not found\n"), server_name);
152 }
153 else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
154 die (STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"),
155 "host");
157 }
158 else if (strstr (buf, "is down")) {
159 die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
161 }
162 else if (strstr (buf, "is alive")) {
163 status = STATE_OK;
165 }
166 else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
167 losstr = strstr (buf, "=");
168 losstr = 1 + strstr (losstr, "/");
169 losstr = 1 + strstr (losstr, "/");
170 rtastr = strstr (buf, "min/avg/max");
171 rtastr = strstr (rtastr, "=");
172 rtastr = 1 + index (rtastr, '/');
173 loss = strtod (losstr, NULL);
174 rta = strtod (rtastr, NULL);
175 if (cpl_p == TRUE && loss > cpl)
176 status = STATE_CRITICAL;
177 else if (crta_p == TRUE && rta > crta)
178 status = STATE_CRITICAL;
179 else if (wpl_p == TRUE && loss > wpl)
180 status = STATE_WARNING;
181 else if (wrta_p == TRUE && rta > wrta)
182 status = STATE_WARNING;
183 else
184 status = STATE_OK;
185 die (status,
186 _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"),
187 state_text (status), server_name, loss, rta,
188 perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100),
189 fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0));
191 }
192 else if(strstr (buf, "xmt/rcv/%loss") ) {
193 /* no min/max/avg if host was unreachable in fping v2.2.b1 */
194 losstr = strstr (buf, "=");
195 losstr = 1 + strstr (losstr, "/");
196 losstr = 1 + strstr (losstr, "/");
197 loss = strtod (losstr, NULL);
198 if (atoi(losstr) == 100)
199 status = STATE_CRITICAL;
200 else if (cpl_p == TRUE && loss > cpl)
201 status = STATE_CRITICAL;
202 else if (wpl_p == TRUE && loss > wpl)
203 status = STATE_WARNING;
204 else
205 status = STATE_OK;
206 /* loss=%.0f%%;%d;%d;0;100 */
207 die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"),
208 state_text (status), server_name, loss ,
209 perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100));
211 }
212 else {
213 status = max_state (status, STATE_WARNING);
214 }
216 return status;
217 }
221 /* process command-line arguments */
222 int
223 process_arguments (int argc, char **argv)
224 {
225 int c;
226 char *rv[2];
228 int option = 0;
229 static struct option longopts[] = {
230 {"hostname", required_argument, 0, 'H'},
231 {"critical", required_argument, 0, 'c'},
232 {"warning", required_argument, 0, 'w'},
233 {"bytes", required_argument, 0, 'b'},
234 {"number", required_argument, 0, 'n'},
235 {"verbose", no_argument, 0, 'v'},
236 {"version", no_argument, 0, 'V'},
237 {"help", no_argument, 0, 'h'},
238 {0, 0, 0, 0}
239 };
241 rv[PL] = NULL;
242 rv[RTA] = NULL;
244 if (argc < 2)
245 return ERROR;
247 if (!is_option (argv[1])) {
248 server_name = argv[1];
249 argv[1] = argv[0];
250 argv = &argv[1];
251 argc--;
252 }
254 while (1) {
255 c = getopt_long (argc, argv, "+hVvH:c:w:b:n:", longopts, &option);
257 if (c == -1 || c == EOF || c == 1)
258 break;
260 switch (c) {
261 case '?': /* print short usage statement if args not parsable */
262 usage5 ();
263 case 'h': /* help */
264 print_help ();
265 exit (STATE_OK);
266 case 'V': /* version */
267 print_revision (progname, revision);
268 exit (STATE_OK);
269 case 'v': /* verbose mode */
270 verbose = TRUE;
271 break;
272 case 'H': /* hostname */
273 if (is_host (optarg) == FALSE) {
274 usage2 (_("Invalid hostname/address"), optarg);
275 }
276 server_name = strscpy (server_name, optarg);
277 break;
278 case 'c':
279 get_threshold (optarg, rv);
280 if (rv[RTA]) {
281 crta = strtod (rv[RTA], NULL);
282 crta_p = TRUE;
283 rv[RTA] = NULL;
284 }
285 if (rv[PL]) {
286 cpl = atoi (rv[PL]);
287 cpl_p = TRUE;
288 rv[PL] = NULL;
289 }
290 break;
291 case 'w':
292 get_threshold (optarg, rv);
293 if (rv[RTA]) {
294 wrta = strtod (rv[RTA], NULL);
295 wrta_p = TRUE;
296 rv[RTA] = NULL;
297 }
298 if (rv[PL]) {
299 wpl = atoi (rv[PL]);
300 wpl_p = TRUE;
301 rv[PL] = NULL;
302 }
303 break;
304 case 'b': /* bytes per packet */
305 if (is_intpos (optarg))
306 packet_size = atoi (optarg);
307 else
308 usage (_("Packet size must be a positive integer"));
309 break;
310 case 'n': /* number of packets */
311 if (is_intpos (optarg))
312 packet_count = atoi (optarg);
313 else
314 usage (_("Packet count must be a positive integer"));
315 break;
316 }
317 }
319 if (server_name == NULL)
320 usage4 (_("Hostname was not supplied"));
322 return OK;
323 }
326 int
327 get_threshold (char *arg, char *rv[2])
328 {
329 char *arg1 = NULL;
330 char *arg2 = NULL;
332 arg1 = strscpy (arg1, arg);
333 if (strpbrk (arg1, ",:"))
334 arg2 = 1 + strpbrk (arg1, ",:");
336 if (arg2) {
337 arg1[strcspn (arg1, ",:")] = 0;
338 if (strstr (arg1, "%") && strstr (arg2, "%"))
339 die (STATE_UNKNOWN,
340 _("%s: Only one threshold may be packet loss (%s)\n"), progname,
341 arg);
342 if (!strstr (arg1, "%") && !strstr (arg2, "%"))
343 die (STATE_UNKNOWN,
344 _("%s: Only one threshold must be packet loss (%s)\n"),
345 progname, arg);
346 }
348 if (arg2 && strstr (arg2, "%")) {
349 rv[PL] = arg2;
350 rv[RTA] = arg1;
351 }
352 else if (arg2) {
353 rv[PL] = arg1;
354 rv[RTA] = arg2;
355 }
356 else if (strstr (arg1, "%")) {
357 rv[PL] = arg1;
358 }
359 else {
360 rv[RTA] = arg1;
361 }
363 return OK;
364 }
367 void
368 print_help (void)
369 {
371 print_revision (progname, revision);
373 printf ("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n");
374 printf (COPYRIGHT, copyright, email);
376 printf ("%s\n", _("This plugin will use the fping command to ping the specified host for a fast check"));
378 printf ("%s\n", _("Note that it is necessary to set the suid flag on fping."));
380 printf ("\n\n");
382 print_usage ();
384 printf (_(UT_HELP_VRSN));
385 printf (_(UT_EXTRA_OPTS));
387 printf (" %s\n", "-H, --hostname=HOST");
388 printf (" %s\n", _("name or IP Address of host to ping (IP Address bypasses name lookup, reducing system load)"));
389 printf (" %s\n", "-w, --warning=THRESHOLD");
390 printf (" %s\n", _("warning threshold pair"));
391 printf (" %s\n", "-c, --critical=THRESHOLD");
392 printf (" %s\n", _("critical threshold pair"));
393 printf (" %s\n", "-b, --bytes=INTEGER");
394 printf (" %s (default: %d)\n", _("size of ICMP packet"),PACKET_SIZE);
395 printf (" %s\n", "-n, --number=INTEGER");
396 printf (" %s (default: %d)\n", _("number of ICMP packets to send"),PACKET_COUNT);
397 printf (_(UT_VERBOSE));
398 printf ("\n");
399 printf (" %s\n", _("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel time (ms)"));
400 printf (" %s\n", _("which triggers a WARNING or CRITICAL state, and <pl> is the percentage of"));
401 printf (" %s\n", _("packet loss to trigger an alarm state."));
403 #ifdef NP_EXTRA_OPTS
404 printf ("\n");
405 printf ("%s\n", _("Notes:"));
406 printf (_(UT_EXTRA_OPTS_NOTES));
407 #endif
409 printf (_(UT_SUPPORT));
410 }
413 void
414 print_usage (void)
415 {
416 printf (_("Usage:"));
417 printf (" %s <host_address> -w limit -c limit [-b size] [-n number]\n", progname);
418 }