Code

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