check_icmp.c 38 KB

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