check_icmp.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. /******************************************************************************
  2. *
  3. * Nagios check_icmp plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2005-2006 nagios-plugins team
  7. *
  8. * Original Author : Andreas Ericsson <ae@op5.se>
  9. *
  10. * Last Modified: $Date$
  11. *
  12. * Description:
  13. *
  14. * This file contains the check_icmp plugin
  15. *
  16. * Relevant RFC's: 792 (ICMP), 791 (IP)
  17. *
  18. * This program was modeled somewhat after the check_icmp program,
  19. * which was in turn a hack of fping (www.fping.org) but has been
  20. * completely rewritten since to generate higher precision rta values,
  21. * and support several different modes as well as setting ttl to control.
  22. * redundant routes. The only remainders of fping is currently a few
  23. * function names.
  24. *
  25. * License Information:
  26. *
  27. * This program is free software; you can redistribute it and/or modify
  28. * it under the terms of the GNU General Public License as published by
  29. * the Free Software Foundation; either version 2 of the License, or
  30. * (at your option) any later version.
  31. *
  32. * This program is distributed in the hope that it will be useful,
  33. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. * GNU General Public License for more details.
  36. *
  37. * You should have received a copy of the GNU General Public License
  38. * along with this program; if not, write to the Free Software
  39. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  40. *
  41. * $Id$
  42. *
  43. *****************************************************************************/
  44. /* progname may change */
  45. /* char *progname = "check_icmp"; */
  46. char *progname;
  47. const char *revision = "$Revision$";
  48. const char *copyright = "2005-2006";
  49. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  50. /** nagios plugins basic includes */
  51. #include "common.h"
  52. #include "netutils.h"
  53. #include "utils.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 <netinet/in_systm.h>
  67. #include <netinet/in.h>
  68. #include <netinet/ip.h>
  69. #include <netinet/ip_icmp.h>
  70. #include <arpa/inet.h>
  71. #include <signal.h>
  72. /** sometimes undefined system macros (quite a few, actually) **/
  73. #ifndef MAXTTL
  74. # define MAXTTL 255
  75. #endif
  76. #ifndef INADDR_NONE
  77. # define INADDR_NONE 0xffffffU
  78. #endif
  79. #ifndef SOL_IP
  80. #define SOL_IP 0
  81. #endif
  82. /* we bundle these in one #ifndef, since they're all from BSD
  83. * Put individual #ifndef's around those that bother you */
  84. #ifndef ICMP_UNREACH_NET_UNKNOWN
  85. # define ICMP_UNREACH_NET_UNKNOWN 6
  86. # define ICMP_UNREACH_HOST_UNKNOWN 7
  87. # define ICMP_UNREACH_ISOLATED 8
  88. # define ICMP_UNREACH_NET_PROHIB 9
  89. # define ICMP_UNREACH_HOST_PROHIB 10
  90. # define ICMP_UNREACH_TOSNET 11
  91. # define ICMP_UNREACH_TOSHOST 12
  92. #endif
  93. /* tru64 has the ones above, but not these */
  94. #ifndef ICMP_UNREACH_FILTER_PROHIB
  95. # define ICMP_UNREACH_FILTER_PROHIB 13
  96. # define ICMP_UNREACH_HOST_PRECEDENCE 14
  97. # define ICMP_UNREACH_PRECEDENCE_CUTOFF 15
  98. #endif
  99. typedef unsigned short range_t; /* type for get_range() -- unimplemented */
  100. typedef struct rta_host {
  101. unsigned short id; /* id in **table, and icmp pkts */
  102. char *name; /* arg used for adding this host */
  103. char *msg; /* icmp error message, if any */
  104. struct sockaddr_in saddr_in; /* the address of this host */
  105. struct in_addr error_addr; /* stores address of error replies */
  106. unsigned long long time_waited; /* total time waited, in usecs */
  107. unsigned int icmp_sent, icmp_recv, icmp_lost; /* counters */
  108. unsigned char icmp_type, icmp_code; /* type and code from errors */
  109. unsigned short flags; /* control/status flags */
  110. double rta; /* measured RTA */
  111. unsigned char pl; /* measured packet loss */
  112. struct rta_host *next; /* linked list */
  113. } rta_host;
  114. #define FLAG_LOST_CAUSE 0x01 /* decidedly dead target. */
  115. /* threshold structure. all values are maximum allowed, exclusive */
  116. typedef struct threshold {
  117. unsigned char pl; /* max allowed packet loss in percent */
  118. unsigned int rta; /* roundtrip time average, microseconds */
  119. } threshold;
  120. /* the data structure */
  121. typedef struct icmp_ping_data {
  122. struct timeval stime; /* timestamp (saved in protocol struct as well) */
  123. unsigned short ping_id;
  124. } icmp_ping_data;
  125. /* the different modes of this program are as follows:
  126. * MODE_RTA: send all packets no matter what (mimic check_icmp and check_ping)
  127. * MODE_HOSTCHECK: Return immediately upon any sign of life
  128. * In addition, sends packets to ALL addresses assigned
  129. * to this host (as returned by gethostbyname() or
  130. * gethostbyaddr() and expects one host only to be checked at
  131. * a time. Therefore, any packet response what so ever will
  132. * count as a sign of life, even when received outside
  133. * crit.rta limit. Do not misspell any additional IP's.
  134. * MODE_ALL: Requires packets from ALL requested IP to return OK (default).
  135. * MODE_ICMP: implement something similar to check_icmp (MODE_RTA without
  136. * tcp and udp args does this)
  137. */
  138. #define MODE_RTA 0
  139. #define MODE_HOSTCHECK 1
  140. #define MODE_ALL 2
  141. #define MODE_ICMP 3
  142. /* the different ping types we can do
  143. * TODO: investigate ARP ping as well */
  144. #define HAVE_ICMP 1
  145. #define HAVE_UDP 2
  146. #define HAVE_TCP 4
  147. #define HAVE_ARP 8
  148. #define MIN_PING_DATA_SIZE sizeof(struct icmp_ping_data)
  149. #define MAX_IP_PKT_SIZE 65536 /* (theoretical) max IP packet size */
  150. #define IP_HDR_SIZE 20
  151. #define MAX_PING_DATA (MAX_IP_PKT_SIZE - IP_HDR_SIZE - ICMP_MINLEN)
  152. #define DEFAULT_PING_DATA_SIZE (MIN_PING_DATA_SIZE + 44)
  153. /* various target states */
  154. #define TSTATE_INACTIVE 0x01 /* don't ping this host anymore */
  155. #define TSTATE_WAITING 0x02 /* unanswered packets on the wire */
  156. #define TSTATE_ALIVE 0x04 /* target is alive (has answered something) */
  157. #define TSTATE_UNREACH 0x08
  158. /** prototypes **/
  159. void print_help (void);
  160. void print_usage (void);
  161. static u_int get_timevar(const char *);
  162. static u_int get_timevaldiff(struct timeval *, struct timeval *);
  163. static int wait_for_reply(int, u_int);
  164. static int recvfrom_wto(int, char *, unsigned int, struct sockaddr *, u_int *);
  165. static int send_icmp_ping(int, struct rta_host *);
  166. static int get_threshold(char *str, threshold *th);
  167. static void run_checks(void);
  168. static int add_target(char *);
  169. static int add_target_ip(char *, struct in_addr *);
  170. static int handle_random_icmp(struct icmp *, struct sockaddr_in *);
  171. static unsigned short icmp_checksum(unsigned short *, int);
  172. static void finish(int);
  173. static void crash(const char *, ...);
  174. /** external **/
  175. extern int optind, opterr, optopt;
  176. extern char *optarg;
  177. extern char **environ;
  178. /** global variables **/
  179. static struct rta_host **table, *cursor, *list;
  180. static threshold crit = {80, 500000}, warn = {40, 200000};
  181. static int mode, protocols, sockets, debug = 0, timeout = 10;
  182. static unsigned short icmp_pkt_size, icmp_data_size = DEFAULT_PING_DATA_SIZE;
  183. static unsigned int icmp_sent = 0, icmp_recv = 0, icmp_lost = 0;
  184. #define icmp_pkts_en_route (icmp_sent - (icmp_recv + icmp_lost))
  185. static unsigned short targets_down = 0, targets = 0, packets = 0;
  186. #define targets_alive (targets - targets_down)
  187. static unsigned int retry_interval, pkt_interval, target_interval;
  188. static int icmp_sock, tcp_sock, udp_sock, status = STATE_OK;
  189. static pid_t pid;
  190. static struct timezone tz;
  191. static struct timeval prog_start;
  192. static unsigned long long max_completion_time = 0;
  193. static unsigned char ttl = 0; /* outgoing ttl */
  194. static unsigned int warn_down = 1, crit_down = 1; /* host down threshold values */
  195. float pkt_backoff_factor = 1.5;
  196. float target_backoff_factor = 1.5;
  197. /** code start **/
  198. static void
  199. crash(const char *fmt, ...)
  200. {
  201. va_list ap;
  202. printf("%s: ", progname);
  203. va_start(ap, fmt);
  204. vprintf(fmt, ap);
  205. va_end(ap);
  206. if(errno) printf(": %s", strerror(errno));
  207. puts("");
  208. exit(3);
  209. }
  210. static char *
  211. get_icmp_error_msg(unsigned char icmp_type, unsigned char icmp_code)
  212. {
  213. char *msg = "unreachable";
  214. if(debug > 1) printf("get_icmp_error_msg(%u, %u)\n", icmp_type, icmp_code);
  215. switch(icmp_type) {
  216. case ICMP_UNREACH:
  217. switch(icmp_code) {
  218. case ICMP_UNREACH_NET: msg = "Net unreachable"; break;
  219. case ICMP_UNREACH_HOST: msg = "Host unreachable"; break;
  220. case ICMP_UNREACH_PROTOCOL: msg = "Protocol unreachable (firewall?)"; break;
  221. case ICMP_UNREACH_PORT: msg = "Port unreachable (firewall?)"; break;
  222. case ICMP_UNREACH_NEEDFRAG: msg = "Fragmentation needed"; break;
  223. case ICMP_UNREACH_SRCFAIL: msg = "Source route failed"; break;
  224. case ICMP_UNREACH_ISOLATED: msg = "Source host isolated"; break;
  225. case ICMP_UNREACH_NET_UNKNOWN: msg = "Unknown network"; break;
  226. case ICMP_UNREACH_HOST_UNKNOWN: msg = "Unknown host"; break;
  227. case ICMP_UNREACH_NET_PROHIB: msg = "Network denied (firewall?)"; break;
  228. case ICMP_UNREACH_HOST_PROHIB: msg = "Host denied (firewall?)"; break;
  229. case ICMP_UNREACH_TOSNET: msg = "Bad TOS for network (firewall?)"; break;
  230. case ICMP_UNREACH_TOSHOST: msg = "Bad TOS for host (firewall?)"; break;
  231. case ICMP_UNREACH_FILTER_PROHIB: msg = "Prohibited by filter (firewall)"; break;
  232. case ICMP_UNREACH_HOST_PRECEDENCE: msg = "Host precedence violation"; break;
  233. case ICMP_UNREACH_PRECEDENCE_CUTOFF: msg = "Precedence cutoff"; break;
  234. default: msg = "Invalid code"; break;
  235. }
  236. break;
  237. case ICMP_TIMXCEED:
  238. /* really 'out of reach', or non-existant host behind a router serving
  239. * two different subnets */
  240. switch(icmp_code) {
  241. case ICMP_TIMXCEED_INTRANS: msg = "Time to live exceeded in transit"; break;
  242. case ICMP_TIMXCEED_REASS: msg = "Fragment reassembly time exceeded"; break;
  243. default: msg = "Invalid code"; break;
  244. }
  245. break;
  246. case ICMP_SOURCEQUENCH: msg = "Transmitting too fast"; break;
  247. case ICMP_REDIRECT: msg = "Redirect (change route)"; break;
  248. case ICMP_PARAMPROB: msg = "Bad IP header (required option absent)"; break;
  249. /* the following aren't error messages, so ignore */
  250. case ICMP_TSTAMP:
  251. case ICMP_TSTAMPREPLY:
  252. case ICMP_IREQ:
  253. case ICMP_IREQREPLY:
  254. case ICMP_MASKREQ:
  255. case ICMP_MASKREPLY:
  256. default: msg = ""; break;
  257. }
  258. return msg;
  259. }
  260. static int
  261. handle_random_icmp(struct icmp *p, struct sockaddr_in *addr)
  262. {
  263. struct icmp *sent_icmp = NULL;
  264. struct rta_host *host = NULL;
  265. unsigned char *ptr;
  266. if(p->icmp_type == ICMP_ECHO && p->icmp_id == pid) {
  267. /* echo request from us to us (pinging localhost) */
  268. return 0;
  269. }
  270. ptr = (unsigned char *)p;
  271. if(debug) printf("handle_random_icmp(%p, %p)\n", (void *)p, (void *)addr);
  272. /* only handle a few types, since others can't possibly be replies to
  273. * us in a sane network (if it is anyway, it will be counted as lost
  274. * at summary time, but not as quickly as a proper response */
  275. /* TIMXCEED can be an unreach from a router with multiple IP's which
  276. * serves two different subnets on the same interface and a dead host
  277. * on one net is pinged from the other. The router will respond to
  278. * itself and thus set TTL=0 so as to not loop forever. Even when
  279. * TIMXCEED actually sends a proper icmp response we will have passed
  280. * too many hops to have a hope of reaching it later, in which case it
  281. * indicates overconfidence in the network, poor routing or both. */
  282. if(p->icmp_type != ICMP_UNREACH && p->icmp_type != ICMP_TIMXCEED &&
  283. p->icmp_type != ICMP_SOURCEQUENCH && p->icmp_type != ICMP_PARAMPROB)
  284. {
  285. return 0;
  286. }
  287. /* might be for us. At least it holds the original package (according
  288. * to RFC 792). If it isn't, just ignore it */
  289. sent_icmp = (struct icmp *)(ptr + 28);
  290. if(sent_icmp->icmp_type != ICMP_ECHO || sent_icmp->icmp_id != pid ||
  291. sent_icmp->icmp_seq >= targets)
  292. {
  293. if(debug) printf("Packet is no response to a packet we sent\n");
  294. return 0;
  295. }
  296. /* it is indeed a response for us */
  297. host = table[sent_icmp->icmp_seq];
  298. if(debug) {
  299. printf("Received \"%s\" from %s for ICMP ECHO sent to %s.\n",
  300. get_icmp_error_msg(p->icmp_type, p->icmp_code),
  301. inet_ntoa(addr->sin_addr), host->name);
  302. }
  303. icmp_lost++;
  304. host->icmp_lost++;
  305. /* don't spend time on lost hosts any more */
  306. if(host->flags & FLAG_LOST_CAUSE) return 0;
  307. /* source quench means we're sending too fast, so increase the
  308. * interval and mark this packet lost */
  309. if(p->icmp_type == ICMP_SOURCEQUENCH) {
  310. pkt_interval *= pkt_backoff_factor;
  311. target_interval *= target_backoff_factor;
  312. }
  313. else {
  314. targets_down++;
  315. host->flags |= FLAG_LOST_CAUSE;
  316. }
  317. host->icmp_type = p->icmp_type;
  318. host->icmp_code = p->icmp_code;
  319. host->error_addr.s_addr = addr->sin_addr.s_addr;
  320. return 0;
  321. }
  322. int
  323. main(int argc, char **argv)
  324. {
  325. int i;
  326. char *ptr;
  327. long int arg;
  328. int icmp_sockerrno, udp_sockerrno, tcp_sockerrno;
  329. int result;
  330. struct rta_host *host;
  331. /* we only need to be setsuid when we get the sockets, so do
  332. * that before pointer magic (esp. on network data) */
  333. icmp_sockerrno = udp_sockerrno = tcp_sockerrno = sockets = 0;
  334. if((icmp_sock = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) != -1)
  335. sockets |= HAVE_ICMP;
  336. else icmp_sockerrno = errno;
  337. /* if((udp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) != -1) */
  338. /* sockets |= HAVE_UDP; */
  339. /* else udp_sockerrno = errno; */
  340. /* if((tcp_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) */
  341. /* sockets |= HAVE_TCP; */
  342. /* else tcp_sockerrno = errno; */
  343. /* now drop privileges (no effect if not setsuid or geteuid() == 0) */
  344. setuid(getuid());
  345. /* POSIXLY_CORRECT might break things, so unset it (the portable way) */
  346. environ = NULL;
  347. /* use the pid to mark packets as ours */
  348. pid = getpid();
  349. /* printf("pid = %u\n", pid); */
  350. /* get calling name the old-fashioned way for portability instead
  351. * of relying on the glibc-ism __progname */
  352. ptr = strrchr(argv[0], '/');
  353. if(ptr) progname = &ptr[1];
  354. else progname = argv[0];
  355. /* now set defaults. Use progname to set them initially (allows for
  356. * superfast check_host program when target host is up */
  357. cursor = list = NULL;
  358. table = NULL;
  359. mode = MODE_RTA;
  360. crit.rta = 500000;
  361. crit.pl = 80;
  362. warn.rta = 200000;
  363. warn.pl = 40;
  364. protocols = HAVE_ICMP | HAVE_UDP | HAVE_TCP;
  365. pkt_interval = 80000; /* 80 msec packet interval by default */
  366. packets = 5;
  367. if(!strcmp(progname, "check_icmp") || !strcmp(progname, "check_ping")) {
  368. mode = MODE_ICMP;
  369. protocols = HAVE_ICMP;
  370. }
  371. else if(!strcmp(progname, "check_host")) {
  372. mode = MODE_HOSTCHECK;
  373. pkt_interval = 1000000;
  374. packets = 5;
  375. crit.rta = warn.rta = 1000000;
  376. crit.pl = warn.pl = 100;
  377. }
  378. else if(!strcmp(progname, "check_rta_multi")) {
  379. mode = MODE_ALL;
  380. target_interval = 0;
  381. pkt_interval = 50000;
  382. packets = 5;
  383. }
  384. /* parse the arguments */
  385. for(i = 1; i < argc; i++) {
  386. while((arg = getopt(argc, argv, "vhVw:c:n:p:t:H:i:b:I:l:")) != EOF) {
  387. switch(arg) {
  388. case 'v':
  389. debug++;
  390. break;
  391. case 'b':
  392. /* silently ignored for now */
  393. break;
  394. case 'i':
  395. pkt_interval = get_timevar(optarg);
  396. break;
  397. case 'I':
  398. target_interval = get_timevar(optarg);
  399. break;
  400. case 'w':
  401. get_threshold(optarg, &warn);
  402. break;
  403. case 'c':
  404. get_threshold(optarg, &crit);
  405. break;
  406. case 'n':
  407. case 'p':
  408. packets = strtoul(optarg, NULL, 0);
  409. break;
  410. case 't':
  411. timeout = strtoul(optarg, NULL, 0);
  412. if(!timeout) timeout = 10;
  413. break;
  414. case 'H':
  415. add_target(optarg);
  416. break;
  417. case 'l':
  418. ttl = (unsigned char)strtoul(optarg, NULL, 0);
  419. break;
  420. case 'd': /* implement later, for cluster checks */
  421. warn_down = (unsigned char)strtoul(optarg, &ptr, 0);
  422. if(ptr) {
  423. crit_down = (unsigned char)strtoul(ptr + 1, NULL, 0);
  424. }
  425. break;
  426. case 'V': /* version */
  427. /*print_revision (progname, revision);*/ /* FIXME: Why? */
  428. exit (STATE_OK);
  429. case 'h': /* help */
  430. print_help ();
  431. exit (STATE_OK);
  432. }
  433. }
  434. }
  435. argv = &argv[optind];
  436. while(*argv) {
  437. add_target(*argv);
  438. argv++;
  439. }
  440. if(!targets) {
  441. errno = 0;
  442. crash("No hosts to check");
  443. exit(3);
  444. }
  445. if(!sockets) {
  446. if(icmp_sock == -1) {
  447. errno = icmp_sockerrno;
  448. crash("Failed to obtain ICMP socket");
  449. return -1;
  450. }
  451. /* if(udp_sock == -1) { */
  452. /* errno = icmp_sockerrno; */
  453. /* crash("Failed to obtain UDP socket"); */
  454. /* return -1; */
  455. /* } */
  456. /* if(tcp_sock == -1) { */
  457. /* errno = icmp_sockerrno; */
  458. /* crash("Failed to obtain TCP socker"); */
  459. /* return -1; */
  460. /* } */
  461. }
  462. if(!ttl) ttl = 64;
  463. if(icmp_sock) {
  464. result = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
  465. if(debug) {
  466. if(result == -1) printf("setsockopt failed\n");
  467. else printf("ttl set to %u\n", ttl);
  468. }
  469. }
  470. /* stupid users should be able to give whatever thresholds they want
  471. * (nothing will break if they do), but some anal plugin maintainer
  472. * will probably add some printf() thing here later, so it might be
  473. * best to at least show them where to do it. ;) */
  474. if(warn.pl > crit.pl) warn.pl = crit.pl;
  475. if(warn.rta > crit.rta) warn.rta = crit.rta;
  476. if(warn_down > crit_down) crit_down = warn_down;
  477. signal(SIGINT, finish);
  478. signal(SIGHUP, finish);
  479. signal(SIGTERM, finish);
  480. signal(SIGALRM, finish);
  481. if(debug) printf("Setting alarm timeout to %u seconds\n", timeout);
  482. alarm(timeout);
  483. /* make sure we don't wait any longer than necessary */
  484. gettimeofday(&prog_start, &tz);
  485. max_completion_time =
  486. ((targets * packets * pkt_interval) + (targets * target_interval)) +
  487. (targets * packets * crit.rta) + crit.rta;
  488. if(debug) {
  489. printf("packets: %u, targets: %u\n"
  490. "target_interval: %0.3f, pkt_interval %0.3f\n"
  491. "crit.rta: %0.3f\n"
  492. "max_completion_time: %0.3f\n",
  493. packets, targets,
  494. (float)target_interval / 1000, (float)pkt_interval / 1000,
  495. (float)crit.rta / 1000,
  496. (float)max_completion_time / 1000);
  497. }
  498. if(debug) {
  499. if(max_completion_time > (u_int)timeout * 1000000) {
  500. printf("max_completion_time: %llu timeout: %u\n",
  501. max_completion_time, timeout);
  502. printf("Timout must be at lest %llu\n",
  503. max_completion_time / 1000000 + 1);
  504. }
  505. }
  506. icmp_pkt_size = icmp_data_size + ICMP_MINLEN;
  507. if(debug > 2) printf("icmp_pkt_size = %u\n", icmp_pkt_size);
  508. if(icmp_pkt_size < sizeof(struct icmp) + sizeof(struct icmp_ping_data)) {
  509. icmp_pkt_size = sizeof(struct icmp) + sizeof(struct icmp_ping_data);
  510. }
  511. if(debug > 2) printf("icmp_pkt_size = %u\n", icmp_pkt_size);
  512. if(debug) {
  513. printf("crit = {%u, %u%%}, warn = {%u, %u%%}\n",
  514. crit.rta, crit.pl, warn.rta, warn.pl);
  515. printf("pkt_interval: %u target_interval: %u retry_interval: %u\n",
  516. pkt_interval, target_interval, retry_interval);
  517. printf("icmp_pkt_size: %u timeout: %u\n",
  518. icmp_pkt_size, timeout);
  519. }
  520. if(packets > 20) {
  521. errno = 0;
  522. crash("packets is > 20 (%d)", packets);
  523. }
  524. host = list;
  525. table = malloc(sizeof(struct rta_host **) * (argc - 1));
  526. i = 0;
  527. while(host) {
  528. host->id = i;
  529. table[i] = host;
  530. host = host->next;
  531. i++;
  532. }
  533. run_checks();
  534. errno = 0;
  535. finish(0);
  536. return(0);
  537. }
  538. static void
  539. run_checks()
  540. {
  541. u_int i, t, result;
  542. u_int final_wait, time_passed;
  543. /* this loop might actually violate the pkt_interval or target_interval
  544. * settings, but only if there aren't any packets on the wire which
  545. * indicates that the target can handle an increased packet rate */
  546. for(i = 0; i < packets; i++) {
  547. for(t = 0; t < targets; t++) {
  548. /* don't send useless packets */
  549. if(!targets_alive) finish(0);
  550. if(table[t]->flags & FLAG_LOST_CAUSE) {
  551. if(debug) printf("%s is a lost cause. not sending any more\n",
  552. table[t]->name);
  553. continue;
  554. }
  555. /* we're still in the game, so send next packet */
  556. (void)send_icmp_ping(icmp_sock, table[t]);
  557. result = wait_for_reply(icmp_sock, target_interval);
  558. }
  559. result = wait_for_reply(icmp_sock, pkt_interval * targets);
  560. }
  561. if(icmp_pkts_en_route && targets_alive) {
  562. time_passed = get_timevaldiff(NULL, NULL);
  563. final_wait = max_completion_time - time_passed;
  564. if(debug) {
  565. printf("time_passed: %u final_wait: %u max_completion_time: %llu\n",
  566. time_passed, final_wait, max_completion_time);
  567. }
  568. if(time_passed > max_completion_time) {
  569. if(debug) printf("Time passed. Finishing up\n");
  570. finish(0);
  571. }
  572. /* catch the packets that might come in within the timeframe, but
  573. * haven't yet */
  574. if(debug) printf("Waiting for %u micro-seconds (%0.3f msecs)\n",
  575. final_wait, (float)final_wait / 1000);
  576. result = wait_for_reply(icmp_sock, final_wait);
  577. }
  578. }
  579. /* response structure:
  580. * ip header : 20 bytes
  581. * icmp header : 28 bytes
  582. * icmp echo reply : the rest
  583. */
  584. static int
  585. wait_for_reply(int sock, u_int t)
  586. {
  587. int n, hlen;
  588. static char buf[4096];
  589. struct sockaddr_in resp_addr;
  590. struct ip *ip;
  591. struct icmp *icp, *sent_icmp;
  592. struct rta_host *host;
  593. struct icmp_ping_data *data;
  594. struct timeval wait_start, now;
  595. u_int tdiff, i, per_pkt_wait;
  596. /* if we can't listen or don't have anything to listen to, just return */
  597. if(!t || !icmp_pkts_en_route) return 0;
  598. gettimeofday(&wait_start, &tz);
  599. i = t;
  600. per_pkt_wait = t / icmp_pkts_en_route;
  601. while(icmp_pkts_en_route && get_timevaldiff(&wait_start, NULL) < i) {
  602. t = per_pkt_wait;
  603. /* wrap up if all targets are declared dead */
  604. if(!targets_alive ||
  605. get_timevaldiff(&prog_start, NULL) >= max_completion_time ||
  606. (mode == MODE_HOSTCHECK && targets_down))
  607. {
  608. finish(0);
  609. }
  610. /* reap responses until we hit a timeout */
  611. n = recvfrom_wto(sock, buf, sizeof(buf),
  612. (struct sockaddr *)&resp_addr, &t);
  613. if(!n) {
  614. if(debug > 1) {
  615. printf("recvfrom_wto() timed out during a %u usecs wait\n",
  616. per_pkt_wait);
  617. }
  618. continue; /* timeout for this one, so keep trying */
  619. }
  620. if(n < 0) {
  621. if(debug) printf("recvfrom_wto() returned errors\n");
  622. return n;
  623. }
  624. ip = (struct ip *)buf;
  625. if(debug > 1) printf("received %u bytes from %s\n",
  626. ntohs(ip->ip_len), inet_ntoa(resp_addr.sin_addr));
  627. /* obsolete. alpha on tru64 provides the necessary defines, but isn't broken */
  628. /* #if defined( __alpha__ ) && __STDC__ && !defined( __GLIBC__ ) */
  629. /* alpha headers are decidedly broken. Using an ansi compiler,
  630. * they provide ip_vhl instead of ip_hl and ip_v, so we mask
  631. * off the bottom 4 bits */
  632. /* hlen = (ip->ip_vhl & 0x0f) << 2; */
  633. /* #else */
  634. hlen = ip->ip_hl << 2;
  635. /* #endif */
  636. if(n < (hlen + ICMP_MINLEN)) {
  637. crash("received packet too short for ICMP (%d bytes, expected %d) from %s\n",
  638. n, hlen + icmp_pkt_size, inet_ntoa(resp_addr.sin_addr));
  639. }
  640. /* else if(debug) { */
  641. /* printf("ip header size: %u, packet size: %u (expected %u, %u)\n", */
  642. /* hlen, ntohs(ip->ip_len) - hlen, */
  643. /* sizeof(struct ip), icmp_pkt_size); */
  644. /* } */
  645. /* check the response */
  646. icp = (struct icmp *)(buf + hlen);
  647. sent_icmp = (struct icmp *)(buf + hlen + ICMP_MINLEN);
  648. /* printf("buf: %p, icp: %p, distance: %u (expected %u)\n", */
  649. /* buf, icp, */
  650. /* (u_int)icp - (u_int)buf, hlen); */
  651. /* printf("buf: %p, sent_icmp: %p, distance: %u (expected %u)\n", */
  652. /* buf, sent_icmp, */
  653. /* (u_int)sent_icmp - (u_int)buf, hlen + ICMP_MINLEN); */
  654. if(icp->icmp_id != pid) {
  655. handle_random_icmp(icp, &resp_addr);
  656. continue;
  657. }
  658. if(icp->icmp_type != ICMP_ECHOREPLY || icp->icmp_seq >= targets) {
  659. if(debug > 2) printf("not a proper ICMP_ECHOREPLY\n");
  660. handle_random_icmp(icp, &resp_addr);
  661. continue;
  662. }
  663. /* this is indeed a valid response */
  664. data = (struct icmp_ping_data *)(icp->icmp_data);
  665. host = table[icp->icmp_seq];
  666. gettimeofday(&now, &tz);
  667. tdiff = get_timevaldiff(&data->stime, &now);
  668. host->time_waited += tdiff;
  669. host->icmp_recv++;
  670. icmp_recv++;
  671. if(debug) {
  672. printf("%0.3f ms rtt from %s, outgoing ttl: %u, incoming ttl: %u\n",
  673. (float)tdiff / 1000, inet_ntoa(resp_addr.sin_addr),
  674. ttl, ip->ip_ttl);
  675. }
  676. /* if we're in hostcheck mode, exit with limited printouts */
  677. if(mode == MODE_HOSTCHECK) {
  678. printf("OK - %s responds to ICMP. Packet %u, rta %0.3fms|"
  679. "pkt=%u;;0;%u rta=%0.3f;%0.3f;%0.3f;;\n",
  680. host->name, icmp_recv, (float)tdiff / 1000,
  681. icmp_recv, packets, (float)tdiff / 1000,
  682. (float)warn.rta / 1000, (float)crit.rta / 1000);
  683. exit(STATE_OK);
  684. }
  685. }
  686. return 0;
  687. }
  688. /* the ping functions */
  689. static int
  690. send_icmp_ping(int sock, struct rta_host *host)
  691. {
  692. static char *buf = NULL; /* re-use so we prevent leaks */
  693. long int len;
  694. struct icmp *icp;
  695. struct icmp_ping_data *data;
  696. struct timeval tv;
  697. struct sockaddr *addr;
  698. if(sock == -1) {
  699. errno = 0;
  700. crash("Attempt to send on bogus socket");
  701. return -1;
  702. }
  703. addr = (struct sockaddr *)&host->saddr_in;
  704. if(!buf) {
  705. buf = (char *)malloc(icmp_pkt_size + sizeof(struct ip));
  706. if(!buf) {
  707. crash("send_icmp_ping(): failed to malloc %d bytes for send buffer",
  708. icmp_pkt_size);
  709. return -1; /* might be reached if we're in debug mode */
  710. }
  711. }
  712. memset(buf, 0, icmp_pkt_size + sizeof(struct ip));
  713. if((gettimeofday(&tv, &tz)) == -1) return -1;
  714. icp = (struct icmp *)buf;
  715. icp->icmp_type = ICMP_ECHO;
  716. icp->icmp_code = 0;
  717. icp->icmp_cksum = 0;
  718. icp->icmp_id = pid;
  719. icp->icmp_seq = host->id;
  720. data = (struct icmp_ping_data *)icp->icmp_data;
  721. data->ping_id = 10; /* host->icmp.icmp_sent; */
  722. memcpy(&data->stime, &tv, sizeof(struct timeval));
  723. icp->icmp_cksum = icmp_checksum((u_short *)icp, icmp_pkt_size);
  724. len = sendto(sock, buf, icmp_pkt_size, 0, (struct sockaddr *)addr,
  725. sizeof(struct sockaddr));
  726. if(len < 0 || (unsigned int)len != icmp_pkt_size) {
  727. if(debug) printf("Failed to send ping to %s\n",
  728. inet_ntoa(host->saddr_in.sin_addr));
  729. return -1;
  730. }
  731. icmp_sent++;
  732. host->icmp_sent++;
  733. return 0;
  734. }
  735. static int
  736. recvfrom_wto(int sock, char *buf, unsigned int len, struct sockaddr *saddr,
  737. u_int *timo)
  738. {
  739. u_int slen;
  740. int n;
  741. struct timeval to, then, now;
  742. fd_set rd, wr;
  743. if(!*timo) {
  744. if(debug) printf("*timo is not\n");
  745. return 0;
  746. }
  747. to.tv_sec = *timo / 1000000;
  748. to.tv_usec = (*timo - (to.tv_sec * 1000000));
  749. FD_ZERO(&rd);
  750. FD_ZERO(&wr);
  751. FD_SET(sock, &rd);
  752. errno = 0;
  753. gettimeofday(&then, &tz);
  754. n = select(sock + 1, &rd, &wr, NULL, &to);
  755. if(n < 0) crash("select() in recvfrom_wto");
  756. gettimeofday(&now, &tz);
  757. *timo = get_timevaldiff(&then, &now);
  758. if(!n) return 0; /* timeout */
  759. slen = sizeof(struct sockaddr);
  760. return recvfrom(sock, buf, len, 0, saddr, &slen);
  761. }
  762. static void
  763. finish(int sig)
  764. {
  765. u_int i = 0;
  766. unsigned char pl;
  767. double rta;
  768. struct rta_host *host;
  769. char *status_string[] =
  770. {"OK", "WARNING", "CRITICAL", "UNKNOWN", "DEPENDENT"};
  771. alarm(0);
  772. if(debug > 1) printf("finish(%d) called\n", sig);
  773. if(icmp_sock != -1) close(icmp_sock);
  774. if(udp_sock != -1) close(udp_sock);
  775. if(tcp_sock != -1) close(tcp_sock);
  776. if(debug) {
  777. printf("icmp_sent: %u icmp_recv: %u icmp_lost: %u\n",
  778. icmp_sent, icmp_recv, icmp_lost);
  779. printf("targets: %u targets_alive: %u\n", targets, targets_alive);
  780. }
  781. /* iterate thrice to calculate values, give output, and print perfparse */
  782. host = list;
  783. while(host) {
  784. if(!host->icmp_recv) {
  785. /* rta 0 is ofcourse not entirely correct, but will still show up
  786. * conspicuosly as missing entries in perfparse and cacti */
  787. pl = 100;
  788. rta = 0;
  789. status = STATE_CRITICAL;
  790. /* up the down counter if not already counted */
  791. if(!(host->flags & FLAG_LOST_CAUSE) && targets_alive) targets_down++;
  792. }
  793. else {
  794. pl = ((host->icmp_sent - host->icmp_recv) * 100) / host->icmp_sent;
  795. rta = (double)host->time_waited / host->icmp_recv;
  796. }
  797. host->pl = pl;
  798. host->rta = rta;
  799. if(!status && (pl >= warn.pl || rta >= warn.rta)) status = STATE_WARNING;
  800. if(pl >= crit.pl || rta >= crit.rta) status = STATE_CRITICAL;
  801. host = host->next;
  802. }
  803. /* this is inevitable */
  804. if(!targets_alive) status = STATE_CRITICAL;
  805. printf("%s - ", status_string[status]);
  806. host = list;
  807. while(host) {
  808. if(debug) puts("");
  809. if(i) {
  810. if(i < targets) printf(" :: ");
  811. else printf("\n");
  812. }
  813. i++;
  814. if(!host->icmp_recv) {
  815. status = STATE_CRITICAL;
  816. if(host->flags & FLAG_LOST_CAUSE) {
  817. printf("%s: %s @ %s. rta nan, lost %d%%",
  818. host->name,
  819. get_icmp_error_msg(host->icmp_type, host->icmp_code),
  820. inet_ntoa(host->error_addr),
  821. 100);
  822. }
  823. else { /* not marked as lost cause, so we have no flags for it */
  824. printf("%s: rta nan, lost 100%%", host->name);
  825. }
  826. }
  827. else { /* !icmp_recv */
  828. printf("%s: rta %0.3fms, lost %u%%",
  829. host->name, host->rta / 1000, host->pl);
  830. }
  831. host = host->next;
  832. }
  833. /* iterate once more for pretty perfparse output */
  834. printf("|");
  835. i = 0;
  836. host = list;
  837. while(host) {
  838. if(debug) puts("");
  839. printf("%srta=%0.3fms;%0.3f;%0.3f;0; %spl=%u%%;%u;%u;; ",
  840. (targets > 1) ? host->name : "",
  841. host->rta / 1000, (float)warn.rta / 1000, (float)crit.rta / 1000,
  842. (targets > 1) ? host->name : "",
  843. host->pl, warn.pl, crit.pl);
  844. host = host->next;
  845. }
  846. /* finish with an empty line */
  847. puts("");
  848. if(debug) printf("targets: %u, targets_alive: %u\n",
  849. targets, targets_alive);
  850. exit(status);
  851. }
  852. static u_int
  853. get_timevaldiff(struct timeval *early, struct timeval *later)
  854. {
  855. u_int ret;
  856. struct timeval now;
  857. if(!later) {
  858. gettimeofday(&now, &tz);
  859. later = &now;
  860. }
  861. if(!early) early = &prog_start;
  862. /* if early > later we return 0 so as to indicate a timeout */
  863. if(early->tv_sec > early->tv_sec ||
  864. (early->tv_sec == later->tv_sec && early->tv_usec > later->tv_usec))
  865. {
  866. return 0;
  867. }
  868. ret = (later->tv_sec - early->tv_sec) * 1000000;
  869. ret += later->tv_usec - early->tv_usec;
  870. return ret;
  871. }
  872. static int
  873. add_target_ip(char *arg, struct in_addr *in)
  874. {
  875. struct rta_host *host;
  876. /* disregard obviously stupid addresses */
  877. if(in->s_addr == INADDR_NONE || in->s_addr == INADDR_ANY)
  878. return -1;
  879. /* no point in adding two identical IP's, so don't. ;) */
  880. host = list;
  881. while(host) {
  882. if(host->saddr_in.sin_addr.s_addr == in->s_addr) {
  883. if(debug) printf("Identical IP already exists. Not adding %s\n", arg);
  884. return -1;
  885. }
  886. host = host->next;
  887. }
  888. /* add the fresh ip */
  889. host = malloc(sizeof(struct rta_host));
  890. if(!host) {
  891. crash("add_target_ip(%s, %s): malloc(%d) failed",
  892. arg, inet_ntoa(*in), sizeof(struct rta_host));
  893. }
  894. memset(host, 0, sizeof(struct rta_host));
  895. /* set the values. use calling name for output */
  896. host->name = strdup(arg);
  897. /* fill out the sockaddr_in struct */
  898. host->saddr_in.sin_family = AF_INET;
  899. host->saddr_in.sin_addr.s_addr = in->s_addr;
  900. if(!list) list = cursor = host;
  901. else cursor->next = host;
  902. cursor = host;
  903. targets++;
  904. return 0;
  905. }
  906. /* wrapper for add_target_ip */
  907. static int
  908. add_target(char *arg)
  909. {
  910. int i;
  911. struct hostent *he;
  912. struct in_addr *in, ip;
  913. /* don't resolve if we don't have to */
  914. if((ip.s_addr = inet_addr(arg)) != INADDR_NONE) {
  915. /* don't add all ip's if we were given a specific one */
  916. return add_target_ip(arg, &ip);
  917. /* he = gethostbyaddr((char *)in, sizeof(struct in_addr), AF_INET); */
  918. /* if(!he) return add_target_ip(arg, in); */
  919. }
  920. else {
  921. errno = 0;
  922. he = gethostbyname(arg);
  923. if(!he) {
  924. errno = 0;
  925. crash("Failed to resolve %s", arg);
  926. return -1;
  927. }
  928. }
  929. /* possibly add all the IP's as targets */
  930. for(i = 0; he->h_addr_list[i]; i++) {
  931. in = (struct in_addr *)he->h_addr_list[i];
  932. add_target_ip(arg, in);
  933. /* this is silly, but it works */
  934. if(mode == MODE_HOSTCHECK || mode == MODE_ALL) {
  935. printf("mode: %d\n", mode);
  936. continue;
  937. }
  938. break;
  939. }
  940. return 0;
  941. }
  942. /*
  943. * u = micro
  944. * m = milli
  945. * s = seconds
  946. * return value is in microseconds
  947. */
  948. static u_int
  949. get_timevar(const char *str)
  950. {
  951. char p, u, *ptr;
  952. unsigned int len;
  953. u_int i, d; /* integer and decimal, respectively */
  954. u_int factor = 1000; /* default to milliseconds */
  955. if(!str) return 0;
  956. len = strlen(str);
  957. if(!len) return 0;
  958. /* unit might be given as ms|m (millisec),
  959. * us|u (microsec) or just plain s, for seconds */
  960. u = p = '\0';
  961. u = str[len - 1];
  962. if(len >= 2 && !isdigit((int)str[len - 2])) p = str[len - 2];
  963. if(p && u == 's') u = p;
  964. else if(!p) p = u;
  965. if(debug > 2) printf("evaluating %s, u: %c, p: %c\n", str, u, p);
  966. if(u == 'u') factor = 1; /* microseconds */
  967. else if(u == 'm') factor = 1000; /* milliseconds */
  968. else if(u == 's') factor = 1000000; /* seconds */
  969. if(debug > 2) printf("factor is %u\n", factor);
  970. i = strtoul(str, &ptr, 0);
  971. if(!ptr || *ptr != '.' || strlen(ptr) < 2 || factor == 1)
  972. return i * factor;
  973. /* time specified in usecs can't have decimal points, so ignore them */
  974. if(factor == 1) return i;
  975. d = strtoul(ptr + 1, NULL, 0);
  976. /* d is decimal, so get rid of excess digits */
  977. while(d >= factor) d /= 10;
  978. /* the last parenthesis avoids floating point exceptions. */
  979. return ((i * factor) + (d * (factor / 10)));
  980. }
  981. /* not too good at checking errors, but it'll do (main() should barfe on -1) */
  982. static int
  983. get_threshold(char *str, threshold *th)
  984. {
  985. char *p = NULL, i = 0;
  986. if(!str || !strlen(str) || !th) return -1;
  987. /* pointer magic slims code by 10 lines. i is bof-stop on stupid libc's */
  988. p = &str[strlen(str) - 1];
  989. while(p != &str[1]) {
  990. if(*p == '%') *p = '\0';
  991. else if(*p == ',' && i) {
  992. *p = '\0'; /* reset it so get_timevar(str) works nicely later */
  993. th->pl = (unsigned char)strtoul(p+1, NULL, 0);
  994. break;
  995. }
  996. i = 1;
  997. p--;
  998. }
  999. th->rta = get_timevar(str);
  1000. if(!th->rta) return -1;
  1001. if(th->rta > MAXTTL * 1000000) th->rta = MAXTTL * 1000000;
  1002. if(th->pl > 100) th->pl = 100;
  1003. return 0;
  1004. }
  1005. unsigned short
  1006. icmp_checksum(unsigned short *p, int n)
  1007. {
  1008. register unsigned short cksum;
  1009. register long sum = 0;
  1010. while(n > 1) {
  1011. sum += *p++;
  1012. n -= 2;
  1013. }
  1014. /* mop up the occasional odd byte */
  1015. if(n == 1) sum += (unsigned char)*p;
  1016. sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
  1017. sum += (sum >> 16); /* add carry */
  1018. cksum = ~sum; /* ones-complement, trunc to 16 bits */
  1019. return cksum;
  1020. }
  1021. void
  1022. print_help(void)
  1023. {
  1024. /*print_revision (progname, revision);*/ /* FIXME: Why? */
  1025. printf ("Copyright (c) 2005 Andreas Ericsson <ae@op5.se>\n");
  1026. printf (COPYRIGHT, copyright, email);
  1027. printf ("\n\n");
  1028. print_usage ();
  1029. printf (_(UT_HELP_VRSN));
  1030. printf (" %s\n", "-H");
  1031. printf (" %s\n", _("specify a target"));
  1032. printf (" %s\n", "-w");
  1033. printf (" %s", _("warning threshold (currently "));
  1034. printf ("%0.3fms,%u%%)\n", (float)warn.rta / 1000 , warn.pl / 1000);
  1035. printf (" %s\n", "-c");
  1036. printf (" %s", _("critical threshold (currently "));
  1037. printf ("%0.3fms,%u%%)\n", (float)crit.rta, crit.pl);
  1038. printf (" %s\n", "-n");
  1039. printf (" %s", _("number of packets to send (currently "));
  1040. printf ("%u)\n",packets);
  1041. printf (" %s\n", "-i");
  1042. printf (" %s", _("max packet interval (currently "));
  1043. printf ("%0.3fms)\n",(float)pkt_interval / 1000);
  1044. printf (" %s\n", "-I");
  1045. printf (" %s", _("max target interval (currently "));
  1046. printf ("%0.3fms)\n", (float)target_interval / 1000);
  1047. printf (" %s\n", "-l");
  1048. printf (" %s", _("TTL on outgoing packets (currently "));
  1049. printf ("%u)", ttl);
  1050. printf (" %s\n", "-t");
  1051. printf (" %s",_("timeout value (seconds, currently "));
  1052. printf ("%u)\n", timeout);
  1053. printf (" %s\n", "-b");
  1054. printf (" %s\n", _("icmp packet size (currenly ignored)"));
  1055. printf (" %s\n", "-v");
  1056. printf (" %s\n", _("verbose"));
  1057. printf ("\n");
  1058. printf ("%s\n\n", _("The -H switch is optional. Naming a host (or several) to check is not."));
  1059. printf ("%s\n", _("Threshold format for -w and -c is 200.25,60% for 200.25 msec RTA and 60%"));
  1060. printf ("%s\n", _("packet loss. The default values should work well for most users."));
  1061. printf ("%s\n", _("You can specify different RTA factors using the standardized abbreviations"));
  1062. printf ("%s\n\n", _("us (microseconds), ms (milliseconds, default) or just plain s for seconds."));
  1063. printf ("%s\n", _("Threshold format for -d is warn,crit. 12,14 means WARNING if >= 12 hops"));
  1064. printf ("%s\n", _("are spent and CRITICAL if >= 14 hops are spent."));
  1065. printf ("%s\n\n", _("NOTE: Some systems decrease TTL when forming ICMP_ECHOREPLY, others do not."));
  1066. printf ("%s\n\n", _("The -v switch can be specified several times for increased verbosity."));
  1067. /* printf ("%s\n", _("Long options are currently unsupported."));
  1068. printf ("%s\n", _("Options marked with * require an argument"));
  1069. */
  1070. printf (_(UT_SUPPORT));
  1071. printf (_(UT_NOWARRANTY));
  1072. }
  1073. void
  1074. print_usage (void)
  1075. {
  1076. printf (_("Usage:"));
  1077. printf(" %s [options] [-H] host1 host2 hostn\n", progname);
  1078. }