check_icmp.c 36 KB

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