Code

Increment per-host sequence in check_icmp
[nagiosplug.git] / plugins-root / check_icmp.c
1 /*****************************************************************************
2
3 * Nagios check_icmp plugin
4
5 * License: GPL
6 * Copyright (c) 2005-2008 Nagios Plugins Development Team
7 * Original Author : Andreas Ericsson <ae@op5.se>
8
9 * Description:
10
11 * This file contains the check_icmp plugin
12
13 * Relevant RFC's: 792 (ICMP), 791 (IP)
14
15 * This program was modeled somewhat after the check_icmp program,
16 * which was in turn a hack of fping (www.fping.org) but has been
17 * completely rewritten since to generate higher precision rta values,
18 * and support several different modes as well as setting ttl to control.
19 * redundant routes. The only remainders of fping is currently a few
20 * function names.
21
22
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
27
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31 * GNU General Public License for more details.
32
33 * You should have received a copy of the GNU General Public License
34 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35
36
37 *****************************************************************************/
39 /* progname may change */
40 /* char *progname = "check_icmp"; */
41 char *progname;
42 const char *copyright = "2005-2008";
43 const char *email = "nagiosplug-devel@lists.sourceforge.net";
45 /** nagios plugins basic includes */
46 #include "common.h"
47 #include "netutils.h"
48 #include "utils.h"
50 #if HAVE_SYS_SOCKIO_H
51 #include <sys/sockio.h>
52 #endif
53 #include <sys/ioctl.h>
54 #include <sys/time.h>
55 #include <sys/types.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <stdarg.h>
59 #include <unistd.h>
60 #include <stddef.h>
61 #include <errno.h>
62 #include <string.h>
63 #include <ctype.h>
64 #include <netdb.h>
65 #include <sys/socket.h>
66 #include <net/if.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_icmp.h>
71 #include <arpa/inet.h>
72 #include <signal.h>
73 #include <float.h>
76 /** sometimes undefined system macros (quite a few, actually) **/
77 #ifndef MAXTTL
78 # define MAXTTL 255
79 #endif
80 #ifndef INADDR_NONE
81 # define INADDR_NONE (in_addr_t)(-1)
82 #endif
84 #ifndef SOL_IP
85 #define SOL_IP 0
86 #endif
88 /* we bundle these in one #ifndef, since they're all from BSD
89  * Put individual #ifndef's around those that bother you */
90 #ifndef ICMP_UNREACH_NET_UNKNOWN
91 # define ICMP_UNREACH_NET_UNKNOWN 6
92 # define ICMP_UNREACH_HOST_UNKNOWN 7
93 # define ICMP_UNREACH_ISOLATED 8
94 # define ICMP_UNREACH_NET_PROHIB 9
95 # define ICMP_UNREACH_HOST_PROHIB 10
96 # define ICMP_UNREACH_TOSNET 11
97 # define ICMP_UNREACH_TOSHOST 12
98 #endif
99 /* tru64 has the ones above, but not these */
100 #ifndef ICMP_UNREACH_FILTER_PROHIB
101 # define ICMP_UNREACH_FILTER_PROHIB 13
102 # define ICMP_UNREACH_HOST_PRECEDENCE 14
103 # define ICMP_UNREACH_PRECEDENCE_CUTOFF 15
104 #endif
106 #ifndef DBL_MAX
107 # define DBL_MAX 9.9999999999e999
108 #endif
110 typedef unsigned short range_t;  /* type for get_range() -- unimplemented */
112 typedef struct rta_host {
113         unsigned short id;           /* id in **table, and icmp pkts */
114         char *name;                  /* arg used for adding this host */
115         char *msg;                   /* icmp error message, if any */
116         struct sockaddr_in saddr_in; /* the address of this host */
117         struct in_addr error_addr;   /* stores address of error replies */
118         unsigned long long time_waited; /* total time waited, in usecs */
119         unsigned int icmp_sent, icmp_recv, icmp_lost; /* counters */
120         unsigned char icmp_type, icmp_code; /* type and code from errors */
121         unsigned short flags;        /* control/status flags */
122         double rta;                  /* measured RTA */
123         double rtmax;                /* max rtt */
124         double rtmin;                /* min rtt */
125         unsigned char pl;            /* measured packet loss */
126         struct rta_host *next;       /* linked list */
127 } rta_host;
129 #define FLAG_LOST_CAUSE 0x01  /* decidedly dead target. */
131 /* threshold structure. all values are maximum allowed, exclusive */
132 typedef struct threshold {
133         unsigned char pl;    /* max allowed packet loss in percent */
134         unsigned int rta;  /* roundtrip time average, microseconds */
135 } threshold;
137 /* the data structure */
138 typedef struct icmp_ping_data {
139         struct timeval stime;   /* timestamp (saved in protocol struct as well) */
140         unsigned short ping_id;
141 } icmp_ping_data;
143 /* the different modes of this program are as follows:
144  * MODE_RTA: send all packets no matter what (mimic check_icmp and check_ping)
145  * MODE_HOSTCHECK: Return immediately upon any sign of life
146  *                 In addition, sends packets to ALL addresses assigned
147  *                 to this host (as returned by gethostbyname() or
148  *                 gethostbyaddr() and expects one host only to be checked at
149  *                 a time.  Therefore, any packet response what so ever will
150  *                 count as a sign of life, even when received outside
151  *                 crit.rta limit. Do not misspell any additional IP's.
152  * MODE_ALL:  Requires packets from ALL requested IP to return OK (default).
153  * MODE_ICMP: implement something similar to check_icmp (MODE_RTA without
154  *            tcp and udp args does this)
155  */
156 #define MODE_RTA 0
157 #define MODE_HOSTCHECK 1
158 #define MODE_ALL 2
159 #define MODE_ICMP 3
161 /* the different ping types we can do
162  * TODO: investigate ARP ping as well */
163 #define HAVE_ICMP 1
164 #define HAVE_UDP 2
165 #define HAVE_TCP 4
166 #define HAVE_ARP 8
168 #define MIN_PING_DATA_SIZE sizeof(struct icmp_ping_data)
169 #define MAX_IP_PKT_SIZE 65536   /* (theoretical) max IP packet size */
170 #define IP_HDR_SIZE 20
171 #define MAX_PING_DATA (MAX_IP_PKT_SIZE - IP_HDR_SIZE - ICMP_MINLEN)
172 #define DEFAULT_PING_DATA_SIZE (MIN_PING_DATA_SIZE + 44)
174 /* various target states */
175 #define TSTATE_INACTIVE 0x01    /* don't ping this host anymore */
176 #define TSTATE_WAITING 0x02             /* unanswered packets on the wire */
177 #define TSTATE_ALIVE 0x04       /* target is alive (has answered something) */
178 #define TSTATE_UNREACH 0x08
180 /** prototypes **/
181 void print_help (void);
182 void print_usage (void);
183 static u_int get_timevar(const char *);
184 static u_int get_timevaldiff(struct timeval *, struct timeval *);
185 static in_addr_t get_ip_address(const char *);
186 static int wait_for_reply(int, u_int);
187 static int recvfrom_wto(int, void *, unsigned int, struct sockaddr *, u_int *);
188 static int send_icmp_ping(int, struct rta_host *);
189 static int get_threshold(char *str, threshold *th);
190 static void run_checks(void);
191 static void set_source_ip(char *);
192 static int add_target(char *);
193 static int add_target_ip(char *, struct in_addr *);
194 static int handle_random_icmp(unsigned char *, struct sockaddr_in *);
195 static unsigned short icmp_checksum(unsigned short *, int);
196 static void finish(int);
197 static void crash(const char *, ...);
199 /** external **/
200 extern int optind, opterr, optopt;
201 extern char *optarg;
202 extern char **environ;
204 /** global variables **/
205 static struct rta_host **table, *cursor, *list;
206 static threshold crit = {80, 500000}, warn = {40, 200000};
207 static int mode, protocols, sockets, debug = 0, timeout = 10;
208 static unsigned short icmp_data_size = DEFAULT_PING_DATA_SIZE;
209 static unsigned short icmp_pkt_size = DEFAULT_PING_DATA_SIZE + ICMP_MINLEN;
211 static unsigned int icmp_sent = 0, icmp_recv = 0, icmp_lost = 0;
212 #define icmp_pkts_en_route (icmp_sent - (icmp_recv + icmp_lost))
213 static unsigned short targets_down = 0, targets = 0, packets = 0;
214 #define targets_alive (targets - targets_down)
215 static unsigned int retry_interval, pkt_interval, target_interval;
216 static int icmp_sock, tcp_sock, udp_sock, status = STATE_OK;
217 static pid_t pid;
218 static struct timezone tz;
219 static struct timeval prog_start;
220 static unsigned long long max_completion_time = 0;
221 static unsigned char ttl = 0;   /* outgoing ttl */
222 static unsigned int warn_down = 1, crit_down = 1; /* host down threshold values */
223 static int min_hosts_alive = -1;
224 float pkt_backoff_factor = 1.5;
225 float target_backoff_factor = 1.5;
227 /** code start **/
228 static void
229 crash(const char *fmt, ...)
231         va_list ap;
233         printf("%s: ", progname);
235         va_start(ap, fmt);
236         vprintf(fmt, ap);
237         va_end(ap);
239         if(errno) printf(": %s", strerror(errno));
240         puts("");
242         exit(3);
246 static const char *
247 get_icmp_error_msg(unsigned char icmp_type, unsigned char icmp_code)
249         const char *msg = "unreachable";
251         if(debug > 1) printf("get_icmp_error_msg(%u, %u)\n", icmp_type, icmp_code);
252         switch(icmp_type) {
253         case ICMP_UNREACH:
254                 switch(icmp_code) {
255                 case ICMP_UNREACH_NET: msg = "Net unreachable"; break;
256                 case ICMP_UNREACH_HOST: msg = "Host unreachable"; break;
257                 case ICMP_UNREACH_PROTOCOL: msg = "Protocol unreachable (firewall?)"; break;
258                 case ICMP_UNREACH_PORT: msg = "Port unreachable (firewall?)"; break;
259                 case ICMP_UNREACH_NEEDFRAG: msg = "Fragmentation needed"; break;
260                 case ICMP_UNREACH_SRCFAIL: msg = "Source route failed"; break;
261                 case ICMP_UNREACH_ISOLATED: msg = "Source host isolated"; break;
262                 case ICMP_UNREACH_NET_UNKNOWN: msg = "Unknown network"; break;
263                 case ICMP_UNREACH_HOST_UNKNOWN: msg = "Unknown host"; break;
264                 case ICMP_UNREACH_NET_PROHIB: msg = "Network denied (firewall?)"; break;
265                 case ICMP_UNREACH_HOST_PROHIB: msg = "Host denied (firewall?)"; break;
266                 case ICMP_UNREACH_TOSNET: msg = "Bad TOS for network (firewall?)"; break;
267                 case ICMP_UNREACH_TOSHOST: msg = "Bad TOS for host (firewall?)"; break;
268                 case ICMP_UNREACH_FILTER_PROHIB: msg = "Prohibited by filter (firewall)"; break;
269                 case ICMP_UNREACH_HOST_PRECEDENCE: msg = "Host precedence violation"; break;
270                 case ICMP_UNREACH_PRECEDENCE_CUTOFF: msg = "Precedence cutoff"; break;
271                 default: msg = "Invalid code"; break;
272                 }
273                 break;
275         case ICMP_TIMXCEED:
276                 /* really 'out of reach', or non-existant host behind a router serving
277                  * two different subnets */
278                 switch(icmp_code) {
279                 case ICMP_TIMXCEED_INTRANS: msg = "Time to live exceeded in transit"; break;
280                 case ICMP_TIMXCEED_REASS: msg = "Fragment reassembly time exceeded"; break;
281                 default: msg = "Invalid code"; break;
282                 }
283                 break;
285         case ICMP_SOURCEQUENCH: msg = "Transmitting too fast"; break;
286         case ICMP_REDIRECT: msg = "Redirect (change route)"; break;
287         case ICMP_PARAMPROB: msg = "Bad IP header (required option absent)"; break;
289                 /* the following aren't error messages, so ignore */
290         case ICMP_TSTAMP:
291         case ICMP_TSTAMPREPLY:
292         case ICMP_IREQ:
293         case ICMP_IREQREPLY:
294         case ICMP_MASKREQ:
295         case ICMP_MASKREPLY:
296         default: msg = ""; break;
297         }
299         return msg;
302 static int
303 handle_random_icmp(unsigned char *packet, struct sockaddr_in *addr)
305         struct icmp p, sent_icmp;
306         struct rta_host *host = NULL;
308         memcpy(&p, packet, sizeof(p));
309         if(p.icmp_type == ICMP_ECHO && p.icmp_id == pid) {
310                 /* echo request from us to us (pinging localhost) */
311                 return 0;
312         }
314         if(debug) printf("handle_random_icmp(%p, %p)\n", (void *)&p, (void *)addr);
316         /* only handle a few types, since others can't possibly be replies to
317          * us in a sane network (if it is anyway, it will be counted as lost
318          * at summary time, but not as quickly as a proper response */
319         /* TIMXCEED can be an unreach from a router with multiple IP's which
320          * serves two different subnets on the same interface and a dead host
321          * on one net is pinged from the other. The router will respond to
322          * itself and thus set TTL=0 so as to not loop forever.  Even when
323          * TIMXCEED actually sends a proper icmp response we will have passed
324          * too many hops to have a hope of reaching it later, in which case it
325          * indicates overconfidence in the network, poor routing or both. */
326         if(p.icmp_type != ICMP_UNREACH && p.icmp_type != ICMP_TIMXCEED &&
327            p.icmp_type != ICMP_SOURCEQUENCH && p.icmp_type != ICMP_PARAMPROB)
328         {
329                 return 0;
330         }
332         /* might be for us. At least it holds the original package (according
333          * to RFC 792). If it isn't, just ignore it */
334         memcpy(&sent_icmp, packet + 28, sizeof(sent_icmp));
335         if(sent_icmp.icmp_type != ICMP_ECHO || sent_icmp.icmp_id != pid ||
336            sent_icmp.icmp_seq >= targets*packets)
337         {
338                 if(debug) printf("Packet is no response to a packet we sent\n");
339                 return 0;
340         }
342         /* it is indeed a response for us */
343         host = table[sent_icmp.icmp_seq/packets];
344         if(debug) {
345                 printf("Received \"%s\" from %s for ICMP ECHO sent to %s.\n",
346                            get_icmp_error_msg(p.icmp_type, p.icmp_code),
347                            inet_ntoa(addr->sin_addr), host->name);
348         }
350         icmp_lost++;
351         host->icmp_lost++;
352         /* don't spend time on lost hosts any more */
353         if(host->flags & FLAG_LOST_CAUSE) return 0;
355         /* source quench means we're sending too fast, so increase the
356          * interval and mark this packet lost */
357         if(p.icmp_type == ICMP_SOURCEQUENCH) {
358                 pkt_interval *= pkt_backoff_factor;
359                 target_interval *= target_backoff_factor;
360         }
361         else {
362                 targets_down++;
363                 host->flags |= FLAG_LOST_CAUSE;
364         }
365         host->icmp_type = p.icmp_type;
366         host->icmp_code = p.icmp_code;
367         host->error_addr.s_addr = addr->sin_addr.s_addr;
369         return 0;
372 int
373 main(int argc, char **argv)
375         int i;
376         char *ptr;
377         long int arg;
378         int icmp_sockerrno, udp_sockerrno, tcp_sockerrno;
379         int result;
380         struct rta_host *host;
382         setlocale (LC_ALL, "");
383         bindtextdomain (PACKAGE, LOCALEDIR);
384         textdomain (PACKAGE);
386         /* print a helpful error message if geteuid != 0 */
387         np_warn_if_not_root();
389         /* we only need to be setsuid when we get the sockets, so do
390          * that before pointer magic (esp. on network data) */
391         icmp_sockerrno = udp_sockerrno = tcp_sockerrno = sockets = 0;
393         if((icmp_sock = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) != -1)
394                 sockets |= HAVE_ICMP;
395         else icmp_sockerrno = errno;
397         /* if((udp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) != -1) */
398         /*      sockets |= HAVE_UDP; */
399         /* else udp_sockerrno = errno; */
401         /* if((tcp_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) */
402         /*      sockets |= HAVE_TCP; */
403         /* else tcp_sockerrno = errno; */
405         /* now drop privileges (no effect if not setsuid or geteuid() == 0) */
406         setuid(getuid());
408         /* POSIXLY_CORRECT might break things, so unset it (the portable way) */
409         environ = NULL;
411         /* use the pid to mark packets as ours */
412         /* Some systems have 32-bit pid_t so mask off only 16 bits */
413         pid = getpid() & 0xffff;
414         /* printf("pid = %u\n", pid); */
416         /* get calling name the old-fashioned way for portability instead
417          * of relying on the glibc-ism __progname */
418         ptr = strrchr(argv[0], '/');
419         if(ptr) progname = &ptr[1];
420         else progname = argv[0];
422         /* now set defaults. Use progname to set them initially (allows for
423          * superfast check_host program when target host is up */
424         cursor = list = NULL;
425         table = NULL;
427         mode = MODE_RTA;
428         crit.rta = 500000;
429         crit.pl = 80;
430         warn.rta = 200000;
431         warn.pl = 40;
432         protocols = HAVE_ICMP | HAVE_UDP | HAVE_TCP;
433         pkt_interval = 80000;  /* 80 msec packet interval by default */
434         packets = 5;
436         if(!strcmp(progname, "check_icmp") || !strcmp(progname, "check_ping")) {
437                 mode = MODE_ICMP;
438                 protocols = HAVE_ICMP;
439         }
440         else if(!strcmp(progname, "check_host")) {
441                 mode = MODE_HOSTCHECK;
442                 pkt_interval = 1000000;
443                 packets = 5;
444                 crit.rta = warn.rta = 1000000;
445                 crit.pl = warn.pl = 100;
446         }
447         else if(!strcmp(progname, "check_rta_multi")) {
448                 mode = MODE_ALL;
449                 target_interval = 0;
450                 pkt_interval = 50000;
451                 packets = 5;
452         }
454         /* Parse extra opts if any */
455         argv=np_extra_opts(&argc, argv, progname);
457         /* parse the arguments */
458         for(i = 1; i < argc; i++) {
459                 while((arg = getopt(argc, argv, "vhVw:c:n:p:t:H:s:i:b:I:l:m:")) != EOF) {
460                         long size;
461                         switch(arg) {
462                         case 'v':
463                                 debug++;
464                                 break;
465                         case 'b':
466                                 size = strtol(optarg,NULL,0);
467                                 if (size >= (sizeof(struct icmp) + sizeof(struct icmp_ping_data)) &&
468                                     size < MAX_PING_DATA) {
469                                         icmp_data_size = size;
470                                         icmp_pkt_size = size + ICMP_MINLEN;
471                                 } else
472                                         usage_va("ICMP data length must be between: %d and %d",
473                                                  sizeof(struct icmp) + sizeof(struct icmp_ping_data),
474                                                  MAX_PING_DATA - 1);
476                                 break;
477                         case 'i':
478                                 pkt_interval = get_timevar(optarg);
479                                 break;
480                         case 'I':
481                                 target_interval = get_timevar(optarg);
482                                 break;
483                         case 'w':
484                                 get_threshold(optarg, &warn);
485                                 break;
486                         case 'c':
487                                 get_threshold(optarg, &crit);
488                                 break;
489                         case 'n':
490                         case 'p':
491                                 packets = strtoul(optarg, NULL, 0);
492                                 break;
493                         case 't':
494                                 timeout = strtoul(optarg, NULL, 0);
495                                 if(!timeout) timeout = 10;
496                                 break;
497                         case 'H':
498                                 add_target(optarg);
499                                 break;
500                         case 'l':
501                                 ttl = (unsigned char)strtoul(optarg, NULL, 0);
502                                 break;
503                         case 'm':
504                                 min_hosts_alive = (int)strtoul(optarg, NULL, 0);
505                                 break;
506                         case 'd': /* implement later, for cluster checks */
507                                 warn_down = (unsigned char)strtoul(optarg, &ptr, 0);
508                                 if(ptr) {
509                                         crit_down = (unsigned char)strtoul(ptr + 1, NULL, 0);
510                                 }
511                                 break;
512                         case 's': /* specify source IP address */
513                                 set_source_ip(optarg);
514                                 break;
515       case 'V':                 /* version */
516         print_revision (progname, NP_VERSION);
517         exit (STATE_OK);
518       case 'h':                 /* help */
519         print_help ();
520         exit (STATE_OK);
521                         }
522                 }
523         }
525         argv = &argv[optind];
526         while(*argv) {
527                 add_target(*argv);
528                 argv++;
529         }
530         if(!targets) {
531                 errno = 0;
532                 crash("No hosts to check");
533                 exit(3);
534         }
536         if(!sockets) {
537                 if(icmp_sock == -1) {
538                         errno = icmp_sockerrno;
539                         crash("Failed to obtain ICMP socket");
540                         return -1;
541                 }
542                 /* if(udp_sock == -1) { */
543                 /*      errno = icmp_sockerrno; */
544                 /*      crash("Failed to obtain UDP socket"); */
545                 /*      return -1; */
546                 /* } */
547                 /* if(tcp_sock == -1) { */
548                 /*      errno = icmp_sockerrno; */
549                 /*      crash("Failed to obtain TCP socker"); */
550                 /*      return -1; */
551                 /* } */
552         }
553         if(!ttl) ttl = 64;
555         if(icmp_sock) {
556                 result = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
557                 if(debug) {
558                         if(result == -1) printf("setsockopt failed\n");
559                         else printf("ttl set to %u\n", ttl);
560                 }
561         }
563         /* stupid users should be able to give whatever thresholds they want
564          * (nothing will break if they do), but some anal plugin maintainer
565          * will probably add some printf() thing here later, so it might be
566          * best to at least show them where to do it. ;) */
567         if(warn.pl > crit.pl) warn.pl = crit.pl;
568         if(warn.rta > crit.rta) warn.rta = crit.rta;
569         if(warn_down > crit_down) crit_down = warn_down;
571         signal(SIGINT, finish);
572         signal(SIGHUP, finish);
573         signal(SIGTERM, finish);
574         signal(SIGALRM, finish);
575         if(debug) printf("Setting alarm timeout to %u seconds\n", timeout);
576         alarm(timeout);
578         /* make sure we don't wait any longer than necessary */
579         gettimeofday(&prog_start, &tz);
580         max_completion_time =
581                 ((targets * packets * pkt_interval) + (targets * target_interval)) +
582                 (targets * packets * crit.rta) + crit.rta;
584         if(debug) {
585                 printf("packets: %u, targets: %u\n"
586                            "target_interval: %0.3f, pkt_interval %0.3f\n"
587                            "crit.rta: %0.3f\n"
588                            "max_completion_time: %0.3f\n",
589                            packets, targets,
590                            (float)target_interval / 1000, (float)pkt_interval / 1000,
591                            (float)crit.rta / 1000,
592                            (float)max_completion_time / 1000);
593         }
595         if(debug) {
596                 if(max_completion_time > (u_int)timeout * 1000000) {
597                         printf("max_completion_time: %llu  timeout: %u\n",
598                                    max_completion_time, timeout);
599                         printf("Timout must be at lest %llu\n",
600                                    max_completion_time / 1000000 + 1);
601                 }
602         }
604         if(debug) {
605                 printf("crit = {%u, %u%%}, warn = {%u, %u%%}\n",
606                            crit.rta, crit.pl, warn.rta, warn.pl);
607                 printf("pkt_interval: %u  target_interval: %u  retry_interval: %u\n",
608                            pkt_interval, target_interval, retry_interval);
609                 printf("icmp_pkt_size: %u  timeout: %u\n",
610                            icmp_pkt_size, timeout);
611         }
613         if(packets > 20) {
614                 errno = 0;
615                 crash("packets is > 20 (%d)", packets);
616         }
618         if(min_hosts_alive < -1) {
619                 errno = 0;
620                 crash("minimum alive hosts is negative (%i)", min_hosts_alive);
621         }
623         host = list;
624         table = malloc(sizeof(struct rta_host **) * (argc - 1));
625         i = 0;
626         while(host) {
627                 host->id = i*packets;
628                 table[i] = host;
629                 host = host->next;
630                 i++;
631         }
633         run_checks();
635         errno = 0;
636         finish(0);
638         return(0);
641 static void
642 run_checks()
644         u_int i, t, result;
645         u_int final_wait, time_passed;
647         /* this loop might actually violate the pkt_interval or target_interval
648          * settings, but only if there aren't any packets on the wire which
649          * indicates that the target can handle an increased packet rate */
650         for(i = 0; i < packets; i++) {
651                 for(t = 0; t < targets; t++) {
652                         /* don't send useless packets */
653                         if(!targets_alive) finish(0);
654                         if(table[t]->flags & FLAG_LOST_CAUSE) {
655                                 if(debug) printf("%s is a lost cause. not sending any more\n",
656                                                                  table[t]->name);
657                                 continue;
658                         }
660                         /* we're still in the game, so send next packet */
661                         (void)send_icmp_ping(icmp_sock, table[t]);
662                         result = wait_for_reply(icmp_sock, target_interval);
663                 }
664                 result = wait_for_reply(icmp_sock, pkt_interval * targets);
665         }
667         if(icmp_pkts_en_route && targets_alive) {
668                 time_passed = get_timevaldiff(NULL, NULL);
669                 final_wait = max_completion_time - time_passed;
671                 if(debug) {
672                         printf("time_passed: %u  final_wait: %u  max_completion_time: %llu\n",
673                                    time_passed, final_wait, max_completion_time);
674                 }
675                 if(time_passed > max_completion_time) {
676                         if(debug) printf("Time passed. Finishing up\n");
677                         finish(0);
678                 }
680                 /* catch the packets that might come in within the timeframe, but
681                  * haven't yet */
682                 if(debug) printf("Waiting for %u micro-seconds (%0.3f msecs)\n",
683                                                  final_wait, (float)final_wait / 1000);
684                 result = wait_for_reply(icmp_sock, final_wait);
685         }
688 /* response structure:
689  * ip header   : 20 bytes
690  * icmp header : 28 bytes
691  * icmp echo reply : the rest
692  */
693 static int
694 wait_for_reply(int sock, u_int t)
696         int n, hlen;
697         static unsigned char buf[4096];
698         struct sockaddr_in resp_addr;
699         struct ip *ip;
700         struct icmp icp;
701         struct rta_host *host;
702         struct icmp_ping_data data;
703         struct timeval wait_start, now;
704         u_int tdiff, i, per_pkt_wait;
706         /* if we can't listen or don't have anything to listen to, just return */
707         if(!t || !icmp_pkts_en_route) return 0;
709         gettimeofday(&wait_start, &tz);
711         i = t;
712         per_pkt_wait = t / icmp_pkts_en_route;
713         while(icmp_pkts_en_route && get_timevaldiff(&wait_start, NULL) < i) {
714                 t = per_pkt_wait;
716                 /* wrap up if all targets are declared dead */
717                 if(!targets_alive ||
718                    get_timevaldiff(&prog_start, NULL) >= max_completion_time ||
719                    (mode == MODE_HOSTCHECK && targets_down))
720                 {
721                         finish(0);
722                 }
724                 /* reap responses until we hit a timeout */
725                 n = recvfrom_wto(sock, buf, sizeof(buf),
726                                                  (struct sockaddr *)&resp_addr, &t);
727                 if(!n) {
728                         if(debug > 1) {
729                                 printf("recvfrom_wto() timed out during a %u usecs wait\n",
730                                            per_pkt_wait);
731                         }
732                         continue;       /* timeout for this one, so keep trying */
733                 }
734                 if(n < 0) {
735                         if(debug) printf("recvfrom_wto() returned errors\n");
736                         return n;
737                 }
739                 ip = (struct ip *)buf;
740                 if(debug > 1) printf("received %u bytes from %s\n",
741                                                  ntohs(ip->ip_len), inet_ntoa(resp_addr.sin_addr));
743 /* obsolete. alpha on tru64 provides the necessary defines, but isn't broken */
744 /* #if defined( __alpha__ ) && __STDC__ && !defined( __GLIBC__ ) */
745                 /* alpha headers are decidedly broken. Using an ansi compiler,
746                  * they provide ip_vhl instead of ip_hl and ip_v, so we mask
747                  * off the bottom 4 bits */
748 /*              hlen = (ip->ip_vhl & 0x0f) << 2; */
749 /* #else */
750                 hlen = ip->ip_hl << 2;
751 /* #endif */
753                 if(n < (hlen + ICMP_MINLEN)) {
754                         crash("received packet too short for ICMP (%d bytes, expected %d) from %s\n",
755                                   n, hlen + icmp_pkt_size, inet_ntoa(resp_addr.sin_addr));
756                 }
757                 /* else if(debug) { */
758                 /*      printf("ip header size: %u, packet size: %u (expected %u, %u)\n", */
759                 /*                 hlen, ntohs(ip->ip_len) - hlen, */
760                 /*                 sizeof(struct ip), icmp_pkt_size); */
761                 /* } */
763                 /* check the response */
764                 memcpy(&icp, buf + hlen, sizeof(icp));
766                 if(icp.icmp_id != pid || icp.icmp_type != ICMP_ECHOREPLY || icp.icmp_seq >= targets*packets) {
767                         if(debug > 2) printf("not a proper ICMP_ECHOREPLY\n");
768                         handle_random_icmp(buf + hlen, &resp_addr);
769                         continue;
770                 }
772                 /* this is indeed a valid response */
773                 memcpy(&data, icp.icmp_data, sizeof(data));
774                 if (debug > 2)
775                         printf("ICMP echo-reply of len %u, id %u, seq %u, cksum 0x%X\n",
776                                sizeof(data), icp.icmp_id, icp.icmp_seq, icp.icmp_cksum);
778                 host = table[icp.icmp_seq/packets];
779                 gettimeofday(&now, &tz);
780                 tdiff = get_timevaldiff(&data.stime, &now);
782                 host->time_waited += tdiff;
783                 host->icmp_recv++;
784                 icmp_recv++;
785                 if (tdiff > host->rtmax)
786                         host->rtmax = tdiff;
787                 if (tdiff < host->rtmin)
788                         host->rtmin = tdiff;
790                 if(debug) {
791                         printf("%0.3f ms rtt from %s, outgoing ttl: %u, incoming ttl: %u, max: %0.3f, min: %0.3f\n",
792                                    (float)tdiff / 1000, inet_ntoa(resp_addr.sin_addr),
793                                    ttl, ip->ip_ttl, (float)host->rtmax / 1000, (float)host->rtmin / 1000);
794                 }
796                 /* if we're in hostcheck mode, exit with limited printouts */
797                 if(mode == MODE_HOSTCHECK) {
798                         printf("OK - %s responds to ICMP. Packet %u, rta %0.3fms|"
799                                    "pkt=%u;;0;%u rta=%0.3f;%0.3f;%0.3f;;\n",
800                                    host->name, icmp_recv, (float)tdiff / 1000,
801                                    icmp_recv, packets, (float)tdiff / 1000,
802                                    (float)warn.rta / 1000, (float)crit.rta / 1000);
803                         exit(STATE_OK);
804                 }
805         }
807         return 0;
810 /* the ping functions */
811 static int
812 send_icmp_ping(int sock, struct rta_host *host)
814         static union {
815                 void *buf; /* re-use so we prevent leaks */
816                 struct icmp *icp;
817                 u_short *cksum_in;
818         } packet = { NULL };
819         long int len;
820         struct icmp_ping_data data;
821         struct timeval tv;
822         struct sockaddr *addr;
824         if(sock == -1) {
825                 errno = 0;
826                 crash("Attempt to send on bogus socket");
827                 return -1;
828         }
829         addr = (struct sockaddr *)&host->saddr_in;
831         if(!packet.buf) {
832                 if (!(packet.buf = malloc(icmp_pkt_size))) {
833                         crash("send_icmp_ping(): failed to malloc %d bytes for send buffer",
834                                   icmp_pkt_size);
835                         return -1;      /* might be reached if we're in debug mode */
836                 }
837         }
838         memset(packet.buf, 0, icmp_pkt_size);
840         if((gettimeofday(&tv, &tz)) == -1) return -1;
842         data.ping_id = 10; /* host->icmp.icmp_sent; */
843         memcpy(&data.stime, &tv, sizeof(tv));
844         memcpy(&packet.icp->icmp_data, &data, sizeof(data));
845         packet.icp->icmp_type = ICMP_ECHO;
846         packet.icp->icmp_code = 0;
847         packet.icp->icmp_cksum = 0;
848         packet.icp->icmp_id = pid;
849         packet.icp->icmp_seq = host->id++;
850         packet.icp->icmp_cksum = icmp_checksum(packet.cksum_in, icmp_pkt_size);
852         if (debug > 2)
853                 printf("Sending ICMP echo-request of len %u, id %u, seq %u, cksum 0x%X to host %s\n",
854                        sizeof(data), packet.icp->icmp_id, packet.icp->icmp_seq, packet.icp->icmp_cksum, host->name);
856         len = sendto(sock, packet.buf, icmp_pkt_size, 0, (struct sockaddr *)addr,
857                                  sizeof(struct sockaddr));
859         if(len < 0 || (unsigned int)len != icmp_pkt_size) {
860                 if(debug) printf("Failed to send ping to %s\n",
861                                                  inet_ntoa(host->saddr_in.sin_addr));
862                 return -1;
863         }
865         icmp_sent++;
866         host->icmp_sent++;
868         return 0;
871 static int
872 recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr *saddr,
873                          u_int *timo)
875         u_int slen;
876         int n;
877         struct timeval to, then, now;
878         fd_set rd, wr;
880         if(!*timo) {
881                 if(debug) printf("*timo is not\n");
882                 return 0;
883         }
885         to.tv_sec = *timo / 1000000;
886         to.tv_usec = (*timo - (to.tv_sec * 1000000));
888         FD_ZERO(&rd);
889         FD_ZERO(&wr);
890         FD_SET(sock, &rd);
891         errno = 0;
892         gettimeofday(&then, &tz);
893         n = select(sock + 1, &rd, &wr, NULL, &to);
894         if(n < 0) crash("select() in recvfrom_wto");
895         gettimeofday(&now, &tz);
896         *timo = get_timevaldiff(&then, &now);
898         if(!n) return 0;                                /* timeout */
900         slen = sizeof(struct sockaddr);
902         return recvfrom(sock, buf, len, 0, saddr, &slen);
905 static void
906 finish(int sig)
908         u_int i = 0;
909         unsigned char pl;
910         double rta;
911         struct rta_host *host;
912         const char *status_string[] =
913         {"OK", "WARNING", "CRITICAL", "UNKNOWN", "DEPENDENT"};
914         int hosts_ok = 0;
915         int hosts_warn = 0;
917         alarm(0);
918         if(debug > 1) printf("finish(%d) called\n", sig);
920         if(icmp_sock != -1) close(icmp_sock);
921         if(udp_sock != -1) close(udp_sock);
922         if(tcp_sock != -1) close(tcp_sock);
924         if(debug) {
925                 printf("icmp_sent: %u  icmp_recv: %u  icmp_lost: %u\n",
926                            icmp_sent, icmp_recv, icmp_lost);
927                 printf("targets: %u  targets_alive: %u\n", targets, targets_alive);
928         }
930         /* iterate thrice to calculate values, give output, and print perfparse */
931         host = list;
932         while(host) {
933                 if(!host->icmp_recv) {
934                         /* rta 0 is ofcourse not entirely correct, but will still show up
935                          * conspicuosly as missing entries in perfparse and cacti */
936                         pl = 100;
937                         rta = 0;
938                         status = STATE_CRITICAL;
939                         /* up the down counter if not already counted */
940                         if(!(host->flags & FLAG_LOST_CAUSE) && targets_alive) targets_down++;
941                 }
942                 else {
943                         pl = ((host->icmp_sent - host->icmp_recv) * 100) / host->icmp_sent;
944                         rta = (double)host->time_waited / host->icmp_recv;
945                 }
946                 host->pl = pl;
947                 host->rta = rta;
948                 if(pl >= crit.pl || rta >= crit.rta) {
949                         status = STATE_CRITICAL;
950                 }
951                 else if(!status && (pl >= warn.pl || rta >= warn.rta)) {
952                         status = STATE_WARNING;
953                         hosts_warn++;
954                 }
955                 else {
956                         hosts_ok++;
957                 }
959                 host = host->next;
960         }
961         /* this is inevitable */
962         if(!targets_alive) status = STATE_CRITICAL;
963         if(min_hosts_alive > -1) {
964                 if(hosts_ok >= min_hosts_alive) status = STATE_OK;
965                 else if((hosts_ok + hosts_warn) >= min_hosts_alive) status = STATE_WARNING;
966         }
967         printf("%s - ", status_string[status]);
969         host = list;
970         while(host) {
971                 if(debug) puts("");
972                 if(i) {
973                         if(i < targets) printf(" :: ");
974                         else printf("\n");
975                 }
976                 i++;
977                 if(!host->icmp_recv) {
978                         status = STATE_CRITICAL;
979                         if(host->flags & FLAG_LOST_CAUSE) {
980                                 printf("%s: %s @ %s. rta nan, lost %d%%",
981                                            host->name,
982                                            get_icmp_error_msg(host->icmp_type, host->icmp_code),
983                                            inet_ntoa(host->error_addr),
984                                            100);
985                         }
986                         else { /* not marked as lost cause, so we have no flags for it */
987                                 printf("%s: rta nan, lost 100%%", host->name);
988                         }
989                 }
990                 else {  /* !icmp_recv */
991                         printf("%s: rta %0.3fms, lost %u%%",
992                                    host->name, host->rta / 1000, host->pl);
993                 }
995                 host = host->next;
996         }
998         /* iterate once more for pretty perfparse output */
999         printf("|");
1000         i = 0;
1001         host = list;
1002         while(host) {
1003                 if(debug) puts("");
1004                 printf("%srta=%0.3fms;%0.3f;%0.3f;0; %spl=%u%%;%u;%u;; %srtmax=%0.3fms;;;; %srtmin=%0.3fms;;;; ",
1005                            (targets > 1) ? host->name : "",
1006                            host->rta / 1000, (float)warn.rta / 1000, (float)crit.rta / 1000,
1007                            (targets > 1) ? host->name : "", host->pl, warn.pl, crit.pl,
1008                            (targets > 1) ? host->name : "", (float)host->rtmax / 1000,
1009                            (targets > 1) ? host->name : "", (host->rtmin < DBL_MAX) ? (float)host->rtmin / 1000 : (float)0);
1011                 host = host->next;
1012         }
1014         if(min_hosts_alive > -1) {
1015                 if(hosts_ok >= min_hosts_alive) status = STATE_OK;
1016                 else if((hosts_ok + hosts_warn) >= min_hosts_alive) status = STATE_WARNING;
1017         }
1019         /* finish with an empty line */
1020         puts("");
1021         if(debug) printf("targets: %u, targets_alive: %u, hosts_ok: %u, hosts_warn: %u, min_hosts_alive: %i\n",
1022                                          targets, targets_alive, hosts_ok, hosts_warn, min_hosts_alive);
1024         exit(status);
1027 static u_int
1028 get_timevaldiff(struct timeval *early, struct timeval *later)
1030         u_int ret;
1031         struct timeval now;
1033         if(!later) {
1034                 gettimeofday(&now, &tz);
1035                 later = &now;
1036         }
1037         if(!early) early = &prog_start;
1039         /* if early > later we return 0 so as to indicate a timeout */
1040         if(early->tv_sec > later->tv_sec ||
1041            (early->tv_sec == later->tv_sec && early->tv_usec > later->tv_usec))
1042         {
1043                 return 0;
1044         }
1046         ret = (later->tv_sec - early->tv_sec) * 1000000;
1047         ret += later->tv_usec - early->tv_usec;
1049         return ret;
1052 static int
1053 add_target_ip(char *arg, struct in_addr *in)
1055         struct rta_host *host;
1057         /* disregard obviously stupid addresses */
1058         if(in->s_addr == INADDR_NONE || in->s_addr == INADDR_ANY)
1059                 return -1;
1061         /* no point in adding two identical IP's, so don't. ;) */
1062         host = list;
1063         while(host) {
1064                 if(host->saddr_in.sin_addr.s_addr == in->s_addr) {
1065                         if(debug) printf("Identical IP already exists. Not adding %s\n", arg);
1066                         return -1;
1067                 }
1068                 host = host->next;
1069         }
1071         /* add the fresh ip */
1072         host = malloc(sizeof(struct rta_host));
1073         if(!host) {
1074                 crash("add_target_ip(%s, %s): malloc(%d) failed",
1075                           arg, inet_ntoa(*in), sizeof(struct rta_host));
1076         }
1077         memset(host, 0, sizeof(struct rta_host));
1079         /* set the values. use calling name for output */
1080         host->name = strdup(arg);
1082         /* fill out the sockaddr_in struct */
1083         host->saddr_in.sin_family = AF_INET;
1084         host->saddr_in.sin_addr.s_addr = in->s_addr;
1086         host->rtmin = DBL_MAX;
1088         if(!list) list = cursor = host;
1089         else cursor->next = host;
1091         cursor = host;
1092         targets++;
1094         return 0;
1097 /* wrapper for add_target_ip */
1098 static int
1099 add_target(char *arg)
1101         int i;
1102         struct hostent *he;
1103         struct in_addr *in, ip;
1105         /* don't resolve if we don't have to */
1106         if((ip.s_addr = inet_addr(arg)) != INADDR_NONE) {
1107                 /* don't add all ip's if we were given a specific one */
1108                 return add_target_ip(arg, &ip);
1109                 /* he = gethostbyaddr((char *)in, sizeof(struct in_addr), AF_INET); */
1110                 /* if(!he) return add_target_ip(arg, in); */
1111         }
1112         else {
1113                 errno = 0;
1114                 he = gethostbyname(arg);
1115                 if(!he) {
1116                         errno = 0;
1117                         crash("Failed to resolve %s", arg);
1118                         return -1;
1119                 }
1120         }
1122         /* possibly add all the IP's as targets */
1123         for(i = 0; he->h_addr_list[i]; i++) {
1124                 in = (struct in_addr *)he->h_addr_list[i];
1125                 add_target_ip(arg, in);
1127                 /* this is silly, but it works */
1128                 if(mode == MODE_HOSTCHECK || mode == MODE_ALL) {
1129                         if(debug > 2) printf("mode: %d\n", mode);
1130                         continue;
1131                 }
1132                 break;
1133         }
1135         return 0;
1138 static void
1139 set_source_ip(char *arg)
1141         struct sockaddr_in src;
1143         memset(&src, 0, sizeof(src));
1144         src.sin_family = AF_INET;
1145         if((src.sin_addr.s_addr = inet_addr(arg)) == INADDR_NONE)
1146                 src.sin_addr.s_addr = get_ip_address(arg);
1147         if(bind(icmp_sock, (struct sockaddr *)&src, sizeof(src)) == -1)
1148                 crash("Cannot bind to IP address %s", arg);
1151 /* TODO: Move this to netutils.c and also change check_dhcp to use that. */
1152 static in_addr_t
1153 get_ip_address(const char *ifname)
1155 #if defined(SIOCGIFADDR)
1156         struct ifreq ifr;
1157         struct sockaddr_in ip;
1159         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
1160         ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
1161         if(ioctl(icmp_sock, SIOCGIFADDR, &ifr) == -1)
1162                 crash("Cannot determine IP address of interface %s", ifname);
1163         memcpy(&ip, &ifr.ifr_addr, sizeof(ip));
1164         return ip.sin_addr.s_addr;
1165 #else
1166         errno = 0;
1167         crash("Cannot get interface IP address on this platform.");
1168 #endif
1171 /*
1172  * u = micro
1173  * m = milli
1174  * s = seconds
1175  * return value is in microseconds
1176  */
1177 static u_int
1178 get_timevar(const char *str)
1180         char p, u, *ptr;
1181         unsigned int len;
1182         u_int i, d;                 /* integer and decimal, respectively */
1183         u_int factor = 1000;    /* default to milliseconds */
1185         if(!str) return 0;
1186         len = strlen(str);
1187         if(!len) return 0;
1189         /* unit might be given as ms|m (millisec),
1190          * us|u (microsec) or just plain s, for seconds */
1191         u = p = '\0';
1192         u = str[len - 1];
1193         if(len >= 2 && !isdigit((int)str[len - 2])) p = str[len - 2];
1194         if(p && u == 's') u = p;
1195         else if(!p) p = u;
1196         if(debug > 2) printf("evaluating %s, u: %c, p: %c\n", str, u, p);
1198         if(u == 'u') factor = 1;            /* microseconds */
1199         else if(u == 'm') factor = 1000;        /* milliseconds */
1200         else if(u == 's') factor = 1000000;     /* seconds */
1201         if(debug > 2) printf("factor is %u\n", factor);
1203         i = strtoul(str, &ptr, 0);
1204         if(!ptr || *ptr != '.' || strlen(ptr) < 2 || factor == 1)
1205                 return i * factor;
1207         /* time specified in usecs can't have decimal points, so ignore them */
1208         if(factor == 1) return i;
1210         d = strtoul(ptr + 1, NULL, 0);
1212         /* d is decimal, so get rid of excess digits */
1213         while(d >= factor) d /= 10;
1215         /* the last parenthesis avoids floating point exceptions. */
1216         return ((i * factor) + (d * (factor / 10)));
1219 /* not too good at checking errors, but it'll do (main() should barfe on -1) */
1220 static int
1221 get_threshold(char *str, threshold *th)
1223         char *p = NULL, i = 0;
1225         if(!str || !strlen(str) || !th) return -1;
1227         /* pointer magic slims code by 10 lines. i is bof-stop on stupid libc's */
1228         p = &str[strlen(str) - 1];
1229         while(p != &str[1]) {
1230                 if(*p == '%') *p = '\0';
1231                 else if(*p == ',' && i) {
1232                         *p = '\0';      /* reset it so get_timevar(str) works nicely later */
1233                         th->pl = (unsigned char)strtoul(p+1, NULL, 0);
1234                         break;
1235                 }
1236                 i = 1;
1237                 p--;
1238         }
1239         th->rta = get_timevar(str);
1241         if(!th->rta) return -1;
1243         if(th->rta > MAXTTL * 1000000) th->rta = MAXTTL * 1000000;
1244         if(th->pl > 100) th->pl = 100;
1246         return 0;
1249 unsigned short
1250 icmp_checksum(unsigned short *p, int n)
1252         register unsigned short cksum;
1253         register long sum = 0;
1255         while(n > 1) {
1256                 sum += *p++;
1257                 n -= 2;
1258         }
1260         /* mop up the occasional odd byte */
1261         if(n == 1) sum += (unsigned char)*p;
1263         sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
1264         sum += (sum >> 16);                     /* add carry */
1265         cksum = ~sum;                           /* ones-complement, trunc to 16 bits */
1267         return cksum;
1270 void
1271 print_help(void)
1274   /*print_revision (progname);*/ /* FIXME: Why? */
1276   printf ("Copyright (c) 2005 Andreas Ericsson <ae@op5.se>\n");
1277   printf (COPYRIGHT, copyright, email);
1279   printf ("\n\n");
1281   print_usage ();
1283   printf (_(UT_HELP_VRSN));
1284   printf (_(UT_EXTRA_OPTS));
1286   printf (" %s\n", "-H");
1287   printf ("    %s\n", _("specify a target"));
1288   printf (" %s\n", "-w");
1289   printf ("    %s", _("warning threshold (currently "));
1290   printf ("%0.3fms,%u%%)\n", (float)warn.rta / 1000, warn.pl);
1291   printf (" %s\n", "-c");
1292   printf ("    %s", _("critical threshold (currently "));
1293   printf ("%0.3fms,%u%%)\n", (float)crit.rta / 1000, crit.pl);
1294   printf (" %s\n", "-s");
1295   printf ("    %s\n", _("specify a source IP address or device name"));
1296   printf (" %s\n", "-n");
1297   printf ("    %s", _("number of packets to send (currently "));
1298   printf ("%u)\n",packets);
1299   printf (" %s\n", "-i");
1300   printf ("    %s", _("max packet interval (currently "));
1301   printf ("%0.3fms)\n",(float)pkt_interval / 1000);
1302   printf (" %s\n", "-I");
1303   printf ("    %s", _("max target interval (currently "));
1304   printf ("%0.3fms)\n", (float)target_interval / 1000);
1305   printf (" %s\n", "-m");
1306   printf ("    %s",_("number of alive hosts required for success"));
1307   printf ("\n");
1308   printf (" %s\n", "-l");
1309   printf ("    %s", _("TTL on outgoing packets (currently "));
1310   printf ("%u)\n", ttl);
1311   printf (" %s\n", "-t");
1312   printf ("    %s",_("timeout value (seconds, currently  "));
1313   printf ("%u)\n", timeout);
1314   printf (" %s\n", "-b");
1315   printf ("    %s\n", _("Number of icmp data bytes to send"));
1316   printf ("    %s %u + %d)\n", _("Packet size will be data bytes + icmp header (currently"),icmp_data_size, ICMP_MINLEN);
1317   printf (" %s\n", "-v");
1318   printf ("    %s\n", _("verbose"));
1320   printf ("\n");
1321   printf ("%s\n", _("Notes:"));
1322   printf (" %s\n", _("The -H switch is optional. Naming a host (or several) to check is not."));
1323   printf ("\n");
1324   printf (" %s\n", _("Threshold format for -w and -c is 200.25,60% for 200.25 msec RTA and 60%"));
1325   printf (" %s\n", _("packet loss.  The default values should work well for most users."));
1326   printf (" %s\n", _("You can specify different RTA factors using the standardized abbreviations"));
1327   printf (" %s\n", _("us (microseconds), ms (milliseconds, default) or just plain s for seconds."));
1328 /* -d not yet implemented */
1329 /*  printf ("%s\n", _("Threshold format for -d is warn,crit.  12,14 means WARNING if >= 12 hops"));
1330   printf ("%s\n", _("are spent and CRITICAL if >= 14 hops are spent."));
1331   printf ("%s\n\n", _("NOTE: Some systems decrease TTL when forming ICMP_ECHOREPLY, others do not."));*/
1332   printf ("\n");
1333   printf (" %s\n", _("The -v switch can be specified several times for increased verbosity."));
1334 /*  printf ("%s\n", _("Long options are currently unsupported."));
1335   printf ("%s\n", _("Options marked with * require an argument"));
1336 */
1337 #ifdef NP_EXTRA_OPTS
1338   printf ("\n");
1339   printf (_(UT_EXTRA_OPTS_NOTES));
1340 #endif
1342   printf (_(UT_SUPPORT));
1347 void
1348 print_usage (void)
1350   printf (_("Usage:"));
1351   printf(" %s [options] [-H] host1 host2 hostN\n", progname);