check_icmp.c 36 KB

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