check_icmp.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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. pid = getpid();
  350. /* printf("pid = %u\n", pid); */
  351. /* get calling name the old-fashioned way for portability instead
  352. * of relying on the glibc-ism __progname */
  353. ptr = strrchr(argv[0], '/');
  354. if(ptr) progname = &ptr[1];
  355. else progname = argv[0];
  356. /* now set defaults. Use progname to set them initially (allows for
  357. * superfast check_host program when target host is up */
  358. cursor = list = NULL;
  359. table = NULL;
  360. mode = MODE_RTA;
  361. crit.rta = 500000;
  362. crit.pl = 80;
  363. warn.rta = 200000;
  364. warn.pl = 40;
  365. protocols = HAVE_ICMP | HAVE_UDP | HAVE_TCP;
  366. pkt_interval = 80000; /* 80 msec packet interval by default */
  367. packets = 5;
  368. if(!strcmp(progname, "check_icmp") || !strcmp(progname, "check_ping")) {
  369. mode = MODE_ICMP;
  370. protocols = HAVE_ICMP;
  371. }
  372. else if(!strcmp(progname, "check_host")) {
  373. mode = MODE_HOSTCHECK;
  374. pkt_interval = 1000000;
  375. packets = 5;
  376. crit.rta = warn.rta = 1000000;
  377. crit.pl = warn.pl = 100;
  378. }
  379. else if(!strcmp(progname, "check_rta_multi")) {
  380. mode = MODE_ALL;
  381. target_interval = 0;
  382. pkt_interval = 50000;
  383. packets = 5;
  384. }
  385. /* parse the arguments */
  386. for(i = 1; i < argc; i++) {
  387. while((arg = getopt(argc, argv, "vhVw:c:n:p:t:H:i:b:I:l:m:")) != EOF) {
  388. switch(arg) {
  389. case 'v':
  390. debug++;
  391. break;
  392. case 'b':
  393. /* silently ignored for now */
  394. break;
  395. case 'i':
  396. pkt_interval = get_timevar(optarg);
  397. break;
  398. case 'I':
  399. target_interval = get_timevar(optarg);
  400. break;
  401. case 'w':
  402. get_threshold(optarg, &warn);
  403. break;
  404. case 'c':
  405. get_threshold(optarg, &crit);
  406. break;
  407. case 'n':
  408. case 'p':
  409. packets = strtoul(optarg, NULL, 0);
  410. break;
  411. case 't':
  412. timeout = strtoul(optarg, NULL, 0);
  413. if(!timeout) timeout = 10;
  414. break;
  415. case 'H':
  416. add_target(optarg);
  417. break;
  418. case 'l':
  419. ttl = (unsigned char)strtoul(optarg, NULL, 0);
  420. break;
  421. case 'm':
  422. min_hosts_alive = (int)strtoul(optarg, NULL, 0);
  423. break;
  424. case 'd': /* implement later, for cluster checks */
  425. warn_down = (unsigned char)strtoul(optarg, &ptr, 0);
  426. if(ptr) {
  427. crit_down = (unsigned char)strtoul(ptr + 1, NULL, 0);
  428. }
  429. break;
  430. case 'V': /* version */
  431. /*print_revision (progname, revision);*/ /* FIXME: Why? */
  432. exit (STATE_OK);
  433. case 'h': /* help */
  434. print_help ();
  435. exit (STATE_OK);
  436. }
  437. }
  438. }
  439. argv = &argv[optind];
  440. while(*argv) {
  441. add_target(*argv);
  442. argv++;
  443. }
  444. if(!targets) {
  445. errno = 0;
  446. crash("No hosts to check");
  447. exit(3);
  448. }
  449. if(!sockets) {
  450. if(icmp_sock == -1) {
  451. errno = icmp_sockerrno;
  452. crash("Failed to obtain ICMP socket");
  453. return -1;
  454. }
  455. /* if(udp_sock == -1) { */
  456. /* errno = icmp_sockerrno; */
  457. /* crash("Failed to obtain UDP socket"); */
  458. /* return -1; */
  459. /* } */
  460. /* if(tcp_sock == -1) { */
  461. /* errno = icmp_sockerrno; */
  462. /* crash("Failed to obtain TCP socker"); */
  463. /* return -1; */
  464. /* } */
  465. }
  466. if(!ttl) ttl = 64;
  467. if(icmp_sock) {
  468. result = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
  469. if(debug) {
  470. if(result == -1) printf("setsockopt failed\n");
  471. else printf("ttl set to %u\n", ttl);
  472. }
  473. }
  474. /* stupid users should be able to give whatever thresholds they want
  475. * (nothing will break if they do), but some anal plugin maintainer
  476. * will probably add some printf() thing here later, so it might be
  477. * best to at least show them where to do it. ;) */
  478. if(warn.pl > crit.pl) warn.pl = crit.pl;
  479. if(warn.rta > crit.rta) warn.rta = crit.rta;
  480. if(warn_down > crit_down) crit_down = warn_down;
  481. signal(SIGINT, finish);
  482. signal(SIGHUP, finish);
  483. signal(SIGTERM, finish);
  484. signal(SIGALRM, finish);
  485. if(debug) printf("Setting alarm timeout to %u seconds\n", timeout);
  486. alarm(timeout);
  487. /* make sure we don't wait any longer than necessary */
  488. gettimeofday(&prog_start, &tz);
  489. max_completion_time =
  490. ((targets * packets * pkt_interval) + (targets * target_interval)) +
  491. (targets * packets * crit.rta) + crit.rta;
  492. if(debug) {
  493. printf("packets: %u, targets: %u\n"
  494. "target_interval: %0.3f, pkt_interval %0.3f\n"
  495. "crit.rta: %0.3f\n"
  496. "max_completion_time: %0.3f\n",
  497. packets, targets,
  498. (float)target_interval / 1000, (float)pkt_interval / 1000,
  499. (float)crit.rta / 1000,
  500. (float)max_completion_time / 1000);
  501. }
  502. if(debug) {
  503. if(max_completion_time > (u_int)timeout * 1000000) {
  504. printf("max_completion_time: %llu timeout: %u\n",
  505. max_completion_time, timeout);
  506. printf("Timout must be at lest %llu\n",
  507. max_completion_time / 1000000 + 1);
  508. }
  509. }
  510. icmp_pkt_size = icmp_data_size + ICMP_MINLEN;
  511. if(debug > 2) printf("icmp_pkt_size = %u\n", icmp_pkt_size);
  512. if(icmp_pkt_size < sizeof(struct icmp) + sizeof(struct icmp_ping_data)) {
  513. icmp_pkt_size = sizeof(struct icmp) + sizeof(struct icmp_ping_data);
  514. }
  515. if(debug > 2) printf("icmp_pkt_size = %u\n", icmp_pkt_size);
  516. if(debug) {
  517. printf("crit = {%u, %u%%}, warn = {%u, %u%%}\n",
  518. crit.rta, crit.pl, warn.rta, warn.pl);
  519. printf("pkt_interval: %u target_interval: %u retry_interval: %u\n",
  520. pkt_interval, target_interval, retry_interval);
  521. printf("icmp_pkt_size: %u timeout: %u\n",
  522. icmp_pkt_size, timeout);
  523. }
  524. if(packets > 20) {
  525. errno = 0;
  526. crash("packets is > 20 (%d)", packets);
  527. }
  528. if(min_hosts_alive < -1) {
  529. errno = 0;
  530. crash("minimum alive hosts is negative (%i)", min_hosts_alive);
  531. }
  532. host = list;
  533. table = malloc(sizeof(struct rta_host **) * (argc - 1));
  534. i = 0;
  535. while(host) {
  536. host->id = i;
  537. table[i] = host;
  538. host = host->next;
  539. i++;
  540. }
  541. run_checks();
  542. errno = 0;
  543. finish(0);
  544. return(0);
  545. }
  546. static void
  547. run_checks()
  548. {
  549. u_int i, t, result;
  550. u_int final_wait, time_passed;
  551. /* this loop might actually violate the pkt_interval or target_interval
  552. * settings, but only if there aren't any packets on the wire which
  553. * indicates that the target can handle an increased packet rate */
  554. for(i = 0; i < packets; i++) {
  555. for(t = 0; t < targets; t++) {
  556. /* don't send useless packets */
  557. if(!targets_alive) finish(0);
  558. if(table[t]->flags & FLAG_LOST_CAUSE) {
  559. if(debug) printf("%s is a lost cause. not sending any more\n",
  560. table[t]->name);
  561. continue;
  562. }
  563. /* we're still in the game, so send next packet */
  564. (void)send_icmp_ping(icmp_sock, table[t]);
  565. result = wait_for_reply(icmp_sock, target_interval);
  566. }
  567. result = wait_for_reply(icmp_sock, pkt_interval * targets);
  568. }
  569. if(icmp_pkts_en_route && targets_alive) {
  570. time_passed = get_timevaldiff(NULL, NULL);
  571. final_wait = max_completion_time - time_passed;
  572. if(debug) {
  573. printf("time_passed: %u final_wait: %u max_completion_time: %llu\n",
  574. time_passed, final_wait, max_completion_time);
  575. }
  576. if(time_passed > max_completion_time) {
  577. if(debug) printf("Time passed. Finishing up\n");
  578. finish(0);
  579. }
  580. /* catch the packets that might come in within the timeframe, but
  581. * haven't yet */
  582. if(debug) printf("Waiting for %u micro-seconds (%0.3f msecs)\n",
  583. final_wait, (float)final_wait / 1000);
  584. result = wait_for_reply(icmp_sock, final_wait);
  585. }
  586. }
  587. /* response structure:
  588. * ip header : 20 bytes
  589. * icmp header : 28 bytes
  590. * icmp echo reply : the rest
  591. */
  592. static int
  593. wait_for_reply(int sock, u_int t)
  594. {
  595. int n, hlen;
  596. static char buf[4096];
  597. struct sockaddr_in resp_addr;
  598. struct ip *ip;
  599. struct icmp *icp, *sent_icmp;
  600. struct rta_host *host;
  601. struct icmp_ping_data *data;
  602. struct timeval wait_start, now;
  603. u_int tdiff, i, per_pkt_wait;
  604. /* if we can't listen or don't have anything to listen to, just return */
  605. if(!t || !icmp_pkts_en_route) return 0;
  606. gettimeofday(&wait_start, &tz);
  607. i = t;
  608. per_pkt_wait = t / icmp_pkts_en_route;
  609. while(icmp_pkts_en_route && get_timevaldiff(&wait_start, NULL) < i) {
  610. t = per_pkt_wait;
  611. /* wrap up if all targets are declared dead */
  612. if(!targets_alive ||
  613. get_timevaldiff(&prog_start, NULL) >= max_completion_time ||
  614. (mode == MODE_HOSTCHECK && targets_down))
  615. {
  616. finish(0);
  617. }
  618. /* reap responses until we hit a timeout */
  619. n = recvfrom_wto(sock, buf, sizeof(buf),
  620. (struct sockaddr *)&resp_addr, &t);
  621. if(!n) {
  622. if(debug > 1) {
  623. printf("recvfrom_wto() timed out during a %u usecs wait\n",
  624. per_pkt_wait);
  625. }
  626. continue; /* timeout for this one, so keep trying */
  627. }
  628. if(n < 0) {
  629. if(debug) printf("recvfrom_wto() returned errors\n");
  630. return n;
  631. }
  632. ip = (struct ip *)buf;
  633. if(debug > 1) printf("received %u bytes from %s\n",
  634. ntohs(ip->ip_len), inet_ntoa(resp_addr.sin_addr));
  635. /* obsolete. alpha on tru64 provides the necessary defines, but isn't broken */
  636. /* #if defined( __alpha__ ) && __STDC__ && !defined( __GLIBC__ ) */
  637. /* alpha headers are decidedly broken. Using an ansi compiler,
  638. * they provide ip_vhl instead of ip_hl and ip_v, so we mask
  639. * off the bottom 4 bits */
  640. /* hlen = (ip->ip_vhl & 0x0f) << 2; */
  641. /* #else */
  642. hlen = ip->ip_hl << 2;
  643. /* #endif */
  644. if(n < (hlen + ICMP_MINLEN)) {
  645. crash("received packet too short for ICMP (%d bytes, expected %d) from %s\n",
  646. n, hlen + icmp_pkt_size, inet_ntoa(resp_addr.sin_addr));
  647. }
  648. /* else if(debug) { */
  649. /* printf("ip header size: %u, packet size: %u (expected %u, %u)\n", */
  650. /* hlen, ntohs(ip->ip_len) - hlen, */
  651. /* sizeof(struct ip), icmp_pkt_size); */
  652. /* } */
  653. /* check the response */
  654. icp = (struct icmp *)(buf + hlen);
  655. sent_icmp = (struct icmp *)(buf + hlen + ICMP_MINLEN);
  656. /* printf("buf: %p, icp: %p, distance: %u (expected %u)\n", */
  657. /* buf, icp, */
  658. /* (u_int)icp - (u_int)buf, hlen); */
  659. /* printf("buf: %p, sent_icmp: %p, distance: %u (expected %u)\n", */
  660. /* buf, sent_icmp, */
  661. /* (u_int)sent_icmp - (u_int)buf, hlen + ICMP_MINLEN); */
  662. if(icp->icmp_id != pid) {
  663. handle_random_icmp(icp, &resp_addr);
  664. continue;
  665. }
  666. if(icp->icmp_type != ICMP_ECHOREPLY || icp->icmp_seq >= targets) {
  667. if(debug > 2) printf("not a proper ICMP_ECHOREPLY\n");
  668. handle_random_icmp(icp, &resp_addr);
  669. continue;
  670. }
  671. /* this is indeed a valid response */
  672. data = (struct icmp_ping_data *)(icp->icmp_data);
  673. host = table[icp->icmp_seq];
  674. gettimeofday(&now, &tz);
  675. tdiff = get_timevaldiff(&data->stime, &now);
  676. host->time_waited += tdiff;
  677. host->icmp_recv++;
  678. icmp_recv++;
  679. if(debug) {
  680. printf("%0.3f ms rtt from %s, outgoing ttl: %u, incoming ttl: %u\n",
  681. (float)tdiff / 1000, inet_ntoa(resp_addr.sin_addr),
  682. ttl, ip->ip_ttl);
  683. }
  684. /* if we're in hostcheck mode, exit with limited printouts */
  685. if(mode == MODE_HOSTCHECK) {
  686. printf("OK - %s responds to ICMP. Packet %u, rta %0.3fms|"
  687. "pkt=%u;;0;%u rta=%0.3f;%0.3f;%0.3f;;\n",
  688. host->name, icmp_recv, (float)tdiff / 1000,
  689. icmp_recv, packets, (float)tdiff / 1000,
  690. (float)warn.rta / 1000, (float)crit.rta / 1000);
  691. exit(STATE_OK);
  692. }
  693. }
  694. return 0;
  695. }
  696. /* the ping functions */
  697. static int
  698. send_icmp_ping(int sock, struct rta_host *host)
  699. {
  700. static char *buf = NULL; /* re-use so we prevent leaks */
  701. long int len;
  702. struct icmp *icp;
  703. struct icmp_ping_data *data;
  704. struct timeval tv;
  705. struct sockaddr *addr;
  706. if(sock == -1) {
  707. errno = 0;
  708. crash("Attempt to send on bogus socket");
  709. return -1;
  710. }
  711. addr = (struct sockaddr *)&host->saddr_in;
  712. if(!buf) {
  713. buf = (char *)malloc(icmp_pkt_size + sizeof(struct ip));
  714. if(!buf) {
  715. crash("send_icmp_ping(): failed to malloc %d bytes for send buffer",
  716. icmp_pkt_size);
  717. return -1; /* might be reached if we're in debug mode */
  718. }
  719. }
  720. memset(buf, 0, icmp_pkt_size + sizeof(struct ip));
  721. if((gettimeofday(&tv, &tz)) == -1) return -1;
  722. icp = (struct icmp *)buf;
  723. icp->icmp_type = ICMP_ECHO;
  724. icp->icmp_code = 0;
  725. icp->icmp_cksum = 0;
  726. icp->icmp_id = pid;
  727. icp->icmp_seq = host->id;
  728. data = (struct icmp_ping_data *)icp->icmp_data;
  729. data->ping_id = 10; /* host->icmp.icmp_sent; */
  730. memcpy(&data->stime, &tv, sizeof(struct timeval));
  731. icp->icmp_cksum = icmp_checksum((u_short *)icp, icmp_pkt_size);
  732. len = sendto(sock, 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. }