Code

Making messages more consistent
[nagiosplug.git] / plugins / check_fping.c
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 ******************************************************************************/
19 const char *progname = "check_fping";
20 const char *revision = "$Revision$";
21 const char *copyright = "2000-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "popen.h"
26 #include "netutils.h"
27 #include "utils.h"
29 enum {
30         PACKET_COUNT = 1,
31         PACKET_SIZE = 56,
32         PL = 0,
33         RTA = 1
34 };
36 int textscan (char *buf);
37 int process_arguments (int, char **);
38 int get_threshold (char *arg, char *rv[2]);
39 void print_help (void);
40 void print_usage (void);
42 char *server_name = NULL;
43 int packet_size = PACKET_SIZE;
44 int packet_count = PACKET_COUNT;
45 int verbose = FALSE;
46 int cpl;
47 int wpl;
48 double crta;
49 double wrta;
50 int cpl_p = FALSE;
51 int wpl_p = FALSE;
52 int crta_p = FALSE;
53 int wrta_p = FALSE;
55 int
56 main (int argc, char **argv)
57 {
58         int status = STATE_UNKNOWN;
59         char *server = NULL;
60         char *command_line = NULL;
61         char *input_buffer = NULL;
62         input_buffer = malloc (MAX_INPUT_BUFFER);
64         setlocale (LC_ALL, "");
65         bindtextdomain (PACKAGE, LOCALEDIR);
66         textdomain (PACKAGE);
68         if (process_arguments (argc, argv) == ERROR)
69                 usage (_("Could not parse arguments\n"));
71         server = strscpy (server, server_name);
73         /* compose the command */
74         asprintf (&command_line, "%s -b %d -c %d %s", PATH_TO_FPING,
75                   packet_size, packet_count, server);
77         if (verbose)
78                 printf ("%s\n", command_line);
80         /* run the command */
81         child_process = spopen (command_line);
82         if (child_process == NULL) {
83                 printf (_("Could not open pipe: %s\n"), command_line);
84                 return STATE_UNKNOWN;
85         }
87         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
88         if (child_stderr == NULL) {
89                 printf (_("Could not open stderr for %s\n"), command_line);
90         }
92         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
93                 if (verbose)
94                         printf ("%s", input_buffer);
95                 status = max_state (status, textscan (input_buffer));
96         }
98         /* If we get anything on STDERR, at least set warning */
99         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
100                 status = max_state (status, STATE_WARNING);
101                 if (verbose)
102                         printf ("%s", input_buffer);
103                 status = max_state (status, textscan (input_buffer));
104         }
105         (void) fclose (child_stderr);
107         /* close the pipe */
108         if (spclose (child_process))
109                 /* need to use max_state not max */
110                 status = max_state (status, STATE_WARNING);
112         printf ("FPING %s - %s\n", state_text (status), server_name);
114         return status;
120 int
121 textscan (char *buf)
123         char *rtastr = NULL;
124         char *losstr = NULL;
125         double loss;
126         double rta;
127         int status = STATE_UNKNOWN;
129         if (strstr (buf, "not found")) {
130                 die (STATE_CRITICAL, _("FPING unknown - %s not found\n"), server_name);
132         }
133         else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
134                 die (STATE_CRITICAL, _("FPING critical - %s is unreachable\n"),
135                                                          "host");
137         }
138         else if (strstr (buf, "is down")) {
139                 die (STATE_CRITICAL, _("FPING critical - %s is down\n"), server_name);
141         }
142         else if (strstr (buf, "is alive")) {
143                 status = STATE_OK;
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_p == TRUE && loss > cpl)
156                         status = STATE_CRITICAL;
157                 else if (crta_p == TRUE  && rta > crta)
158                         status = STATE_CRITICAL;
159                 else if (wpl_p == TRUE && loss > wpl)
160                         status = STATE_WARNING;
161                 else if (wrta_p == TRUE && rta > wrta)
162                         status = STATE_WARNING;
163                 else
164                         status = STATE_OK;
165                 die (status,
166                       _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"),
167                                  state_text (status), server_name, loss, rta,
168                      perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100),
169                      fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0));
171         }
172         else if(strstr (buf, "xmt/rcv/%loss") ) {
173                 /* no min/max/avg if host was unreachable in fping v2.2.b1 */
174                 losstr = strstr (buf, "=");
175                 losstr = 1 + strstr (losstr, "/");
176                 losstr = 1 + strstr (losstr, "/");
177                 loss = strtod (losstr, NULL);
178                 if (atoi(losstr) == 100)
179                         status = STATE_CRITICAL;
180                 else if (cpl_p == TRUE && loss > cpl)
181                         status = STATE_CRITICAL;
182                 else if (wpl_p == TRUE && loss > wpl)
183                         status = STATE_WARNING;
184                 else
185                         status = STATE_OK;
186                 /* loss=%.0f%%;%d;%d;0;100 */
187                 die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"),
188                      state_text (status), server_name, loss ,
189                      perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100));
190         
191         }
192         else {
193                 status = max_state (status, STATE_WARNING);
194         }
196         return status;
198 \f
202 /* process command-line arguments */
203 int
204 process_arguments (int argc, char **argv)
206         int c;
207         char *rv[2];
209         int option = 0;
210         static struct option longopts[] = {
211                 {"hostname", required_argument, 0, 'H'},
212                 {"critical", required_argument, 0, 'c'},
213                 {"warning", required_argument, 0, 'w'},
214                 {"bytes", required_argument, 0, 'b'},
215                 {"number", required_argument, 0, 'n'},
216                 {"verbose", no_argument, 0, 'v'},
217                 {"version", no_argument, 0, 'V'},
218                 {"help", no_argument, 0, 'h'},
219                 {0, 0, 0, 0}
220         };
222         rv[PL] = NULL;
223         rv[RTA] = NULL;
225         if (argc < 2)
226                 return ERROR;
228         if (!is_option (argv[1])) {
229                 server_name = argv[1];
230                 argv[1] = argv[0];
231                 argv = &argv[1];
232                 argc--;
233         }
235         while (1) {
236                 c = getopt_long (argc, argv, "+hVvH:c:w:b:n:", longopts, &option);
238                 if (c == -1 || c == EOF || c == 1)
239                         break;
241                 switch (c) {
242                 case '?':                                                                       /* print short usage statement if args not parsable */
243                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
244                         print_usage ();
245                         exit (STATE_UNKNOWN);
246                 case 'h':                                                                       /* help */
247                         print_help ();
248                         exit (STATE_OK);
249                 case 'V':                                                                       /* version */
250                         print_revision (progname, revision);
251                         exit (STATE_OK);
252                 case 'v':                                                                       /* verbose mode */
253                         verbose = TRUE;
254                         break;
255                 case 'H':                                                                       /* hostname */
256                         if (is_host (optarg) == FALSE) {
257                                 usage2 (_("Invalid host name/address"), optarg);
258                         }
259                         server_name = strscpy (server_name, optarg);
260                         break;
261                 case 'c':
262                         get_threshold (optarg, rv);
263                         if (rv[RTA]) {
264                                 crta = strtod (rv[RTA], NULL);
265                                 crta_p = TRUE;
266                                 rv[RTA] = NULL;
267                         }
268                         if (rv[PL]) {
269                                 cpl = atoi (rv[PL]);
270                                 cpl_p = TRUE;
271                                 rv[PL] = NULL;
272                         }
273                         break;
274                 case 'w':
275                         get_threshold (optarg, rv);
276                         if (rv[RTA]) {
277                                 wrta = strtod (rv[RTA], NULL);
278                                 wrta_p = TRUE;
279                                 rv[RTA] = NULL;
280                         }
281                         if (rv[PL]) {
282                                 wpl = atoi (rv[PL]);
283                                 wpl_p = TRUE;
284                                 rv[PL] = NULL;
285                         }
286                         break;
287                 case 'b':                                                                       /* bytes per packet */
288                         if (is_intpos (optarg))
289                                 packet_size = atoi (optarg);
290                         else
291                                 usage (_("Packet size must be a positive integer"));
292                         break;
293                 case 'n':                                                                       /* number of packets */
294                         if (is_intpos (optarg))
295                                 packet_count = atoi (optarg);
296                         else
297                                 usage (_("Packet count must be a positive integer"));
298                         break;
299                 }
300         }
303         if (server_name == NULL)
304                 usage (_("Host name was not supplied\n\n"));
306         return OK;
313 int
314 get_threshold (char *arg, char *rv[2])
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;
356 \f
357 void
358 print_help (void)
361         print_revision (progname, "$Revision$");
363         printf ("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n");
364         printf (COPYRIGHT, copyright, email);
366         printf (_("\
367 This plugin will use the /bin/fping command (from saint) to ping the\n\
368 specified host for a fast check if the host is alive. Note that it is\n\
369 necessary to set the suid flag on fping.\n\n"));
371         print_usage ();
373         printf (_(UT_HELP_VRSN));
375         printf (_("\
376  -H, --hostname=HOST\n\
377     Name or IP Address of host to ping (IP Address bypasses name lookup,\n\
378     reducing system load)\n\
379  -w, --warning=THRESHOLD\n\
380     warning threshold pair\n\
381  -c, --critical=THRESHOLD\n\
382     critical threshold pair\n\
383  -b, --bytes=INTEGER\n\
384     Size of ICMP packet (default: %d)\n\
385  -n, --number=INTEGER\n\
386     Number of ICMP packets to send (default: %d)\n"),
387                 PACKET_SIZE, PACKET_COUNT);
389         printf (_(UT_VERBOSE));
391         printf (_("\n\
392 THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n\
393 time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n\
394 percentage of packet loss to trigger an alarm state.\n"));
396         printf (_(UT_SUPPORT));
402 void
403 print_usage (void)
405         printf (_("Usage: %s <host_address>\n"), progname);
406         printf (_(UT_HLP_VRS), progname, progname);