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