check_icmp.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  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. * Jitter, MOS and Score support added by Alessandro Ren <alessandro.ren@opservices.com>
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_icmp plugin
  13. *
  14. * Relevant RFC's: 792 (ICMP), 791 (IP)
  15. *
  16. * This program was modeled somewhat after the check_icmp program,
  17. * which was in turn a hack of fping (www.fping.org) but has been
  18. * completely rewritten since to generate higher precision rta values,
  19. * and support several different modes as well as setting ttl to control.
  20. * redundant routes. The only remainders of fping is currently a few
  21. * function names.
  22. *
  23. *
  24. * This program is free software: you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation, either version 3 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU General Public License
  35. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  36. *
  37. *
  38. *****************************************************************************/
  39. /* progname may change */
  40. /* char *progname = "check_icmp"; */
  41. char *progname;
  42. const char *copyright = "2005-2008";
  43. const char *email = "devel@nagios-plugins.org";
  44. #ifdef __sun
  45. #define _XPG4_2
  46. #endif
  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. #include <float.h>
  75. /** sometimes undefined system macros (quite a few, actually) **/
  76. #ifndef MAXTTL
  77. # define MAXTTL 255
  78. #endif
  79. #ifndef INADDR_NONE
  80. # define INADDR_NONE (in_addr_t)(-1)
  81. #endif
  82. #ifndef SOL_IP
  83. #define SOL_IP 0
  84. #endif
  85. /* we bundle these in one #ifndef, since they're all from BSD
  86. * Put individual #ifndef's around those that bother you */
  87. #ifndef ICMP_UNREACH_NET_UNKNOWN
  88. # define ICMP_UNREACH_NET_UNKNOWN 6
  89. # define ICMP_UNREACH_HOST_UNKNOWN 7
  90. # define ICMP_UNREACH_ISOLATED 8
  91. # define ICMP_UNREACH_NET_PROHIB 9
  92. # define ICMP_UNREACH_HOST_PROHIB 10
  93. # define ICMP_UNREACH_TOSNET 11
  94. # define ICMP_UNREACH_TOSHOST 12
  95. #endif
  96. /* tru64 has the ones above, but not these */
  97. #ifndef ICMP_UNREACH_FILTER_PROHIB
  98. # define ICMP_UNREACH_FILTER_PROHIB 13
  99. # define ICMP_UNREACH_HOST_PRECEDENCE 14
  100. # define ICMP_UNREACH_PRECEDENCE_CUTOFF 15
  101. #endif
  102. #ifndef DBL_MAX
  103. # define DBL_MAX 9.9999999999e999
  104. #endif
  105. typedef unsigned short range_t; /* type for get_range() -- unimplemented */
  106. typedef struct rta_host {
  107. unsigned short id; /* id in **table, and icmp pkts */
  108. char *name; /* arg used for adding this host */
  109. char *msg; /* icmp error message, if any */
  110. struct sockaddr_in saddr_in; /* the address of this host */
  111. struct in_addr error_addr; /* stores address of error replies */
  112. unsigned long long time_waited; /* total time waited, in usecs */
  113. unsigned int icmp_sent, icmp_recv, icmp_lost; /* counters */
  114. unsigned char icmp_type, icmp_code; /* type and code from errors */
  115. unsigned short flags; /* control/status flags */
  116. double rta; /* measured RTA */
  117. int rta_status;
  118. double rtmax; /* max rtt */
  119. double rtmin; /* min rtt */
  120. double jitter; /* measured jitter */
  121. int jitter_status;
  122. double jitter_max; /* jitter rtt */
  123. double jitter_min; /* jitter rtt */
  124. double EffectiveLatency;
  125. double mos; /* Mean opnion score */
  126. int mos_status;
  127. double score; /* score */
  128. int score_status;
  129. u_int last_tdiff;
  130. u_int last_icmp_seq; /* Last ICMP_SEQ to check out of order pkts */
  131. unsigned char pl; /* measured packet loss */
  132. int pl_status;
  133. struct rta_host *next; /* linked list */
  134. int order_status;
  135. } rta_host;
  136. #define FLAG_LOST_CAUSE 0x01 /* decidedly dead target. */
  137. /* threshold structure. all values are maximum allowed, exclusive */
  138. typedef struct threshold {
  139. unsigned char pl; /* max allowed packet loss in percent */
  140. unsigned int rta; /* roundtrip time average, microseconds */
  141. double jitter; /* jitter time average, microseconds */
  142. double mos; /* MOS */
  143. double score; /* Score */
  144. } threshold;
  145. /* the data structure */
  146. typedef struct icmp_ping_data {
  147. struct timeval stime; /* timestamp (saved in protocol struct as well) */
  148. unsigned short ping_id;
  149. } icmp_ping_data;
  150. /* the different modes of this program are as follows:
  151. * MODE_RTA: send all packets no matter what (mimic check_icmp and check_ping)
  152. * MODE_HOSTCHECK: Return immediately upon any sign of life
  153. * In addition, sends packets to ALL addresses assigned
  154. * to this host (as returned by gethostbyname() or
  155. * gethostbyaddr() and expects one host only to be checked at
  156. * a time. Therefore, any packet response what so ever will
  157. * count as a sign of life, even when received outside
  158. * crit.rta limit. Do not misspell any additional IP's.
  159. * MODE_ALL: Requires packets from ALL requested IP to return OK (default).
  160. * MODE_ICMP: implement something similar to check_icmp (MODE_RTA without
  161. * tcp and udp args does this)
  162. */
  163. #define MODE_RTA 0
  164. #define MODE_HOSTCHECK 1
  165. #define MODE_ALL 2
  166. #define MODE_ICMP 3
  167. /* the different ping types we can do
  168. * TODO: investigate ARP ping as well */
  169. #define HAVE_ICMP 1
  170. #define HAVE_UDP 2
  171. #define HAVE_TCP 4
  172. #define HAVE_ARP 8
  173. #define MIN_PING_DATA_SIZE sizeof(struct icmp_ping_data)
  174. #define MAX_IP_PKT_SIZE 65536 /* (theoretical) max IP packet size */
  175. #define IP_HDR_SIZE 20
  176. #define MAX_PING_DATA (MAX_IP_PKT_SIZE - IP_HDR_SIZE - ICMP_MINLEN)
  177. #define DEFAULT_PING_DATA_SIZE (MIN_PING_DATA_SIZE + 44)
  178. /* various target states */
  179. #define TSTATE_INACTIVE 0x01 /* don't ping this host anymore */
  180. #define TSTATE_WAITING 0x02 /* unanswered packets on the wire */
  181. #define TSTATE_ALIVE 0x04 /* target is alive (has answered something) */
  182. #define TSTATE_UNREACH 0x08
  183. /** prototypes **/
  184. void print_help (void);
  185. void print_usage (void);
  186. static u_int get_timevar(const char *);
  187. static u_int get_timevaldiff(struct timeval *, struct timeval *);
  188. static in_addr_t get_ip_address(const char *);
  189. static int wait_for_reply(int, u_int);
  190. static int recvfrom_wto(int, void *, unsigned int, struct sockaddr *, u_int *, struct timeval*);
  191. static int send_icmp_ping(int, struct rta_host *);
  192. static int get_threshold(char *str, threshold *th);
  193. static int get_threshold2(char *str, threshold *, threshold *, int type);
  194. static void run_checks(void);
  195. static void set_source_ip(char *);
  196. static int add_target(char *);
  197. static int add_target_ip(char *, struct in_addr *);
  198. static int handle_random_icmp(unsigned char *, struct sockaddr_in *);
  199. static unsigned short icmp_checksum(unsigned short *, int);
  200. static void finish(int);
  201. static void crash(const char *, ...);
  202. /** external **/
  203. extern int optind, opterr, optopt;
  204. extern char *optarg;
  205. extern char **environ;
  206. /** global variables **/
  207. static struct rta_host **table, *cursor, *list;
  208. static threshold crit = {80, 500000}, warn = {40, 200000};
  209. static int mode, protocols, sockets, debug = 0, timeout = 10;
  210. static unsigned short icmp_data_size = DEFAULT_PING_DATA_SIZE;
  211. static unsigned short icmp_pkt_size = DEFAULT_PING_DATA_SIZE + ICMP_MINLEN;
  212. static unsigned int icmp_sent = 0, icmp_recv = 0, icmp_lost = 0;
  213. #define icmp_pkts_en_route (icmp_sent - (icmp_recv + icmp_lost))
  214. static unsigned short targets_down = 0, targets = 0, packets = 0;
  215. #define targets_alive (targets - targets_down)
  216. static unsigned int retry_interval, pkt_interval, target_interval;
  217. static int icmp_sock, tcp_sock, udp_sock, status = STATE_OK;
  218. static pid_t pid;
  219. static struct timezone tz;
  220. static struct timeval prog_start;
  221. static unsigned long long max_completion_time = 0;
  222. static unsigned char ttl = 0; /* outgoing ttl */
  223. static unsigned int warn_down = 1, crit_down = 1; /* host down threshold values */
  224. static int min_hosts_alive = -1;
  225. float pkt_backoff_factor = 1.5;
  226. float target_backoff_factor = 1.5;
  227. int rta_mode=0;
  228. int pl_mode=0;
  229. int jitter_mode=0;
  230. int score_mode=0;
  231. int mos_mode=0;
  232. int order_mode=0;
  233. /** code start **/
  234. static void
  235. crash(const char *fmt, ...)
  236. {
  237. va_list ap;
  238. printf("%s: ", progname);
  239. va_start(ap, fmt);
  240. vprintf(fmt, ap);
  241. va_end(ap);
  242. if(errno) printf(": %s", strerror(errno));
  243. puts("");
  244. exit(3);
  245. }
  246. static const char *
  247. get_icmp_error_msg(unsigned char icmp_type, unsigned char icmp_code)
  248. {
  249. const char *msg = "unreachable";
  250. if(debug > 1) printf("get_icmp_error_msg(%u, %u)\n", icmp_type, icmp_code);
  251. switch(icmp_type) {
  252. case ICMP_UNREACH:
  253. switch(icmp_code) {
  254. case ICMP_UNREACH_NET: msg = "Net unreachable"; break;
  255. case ICMP_UNREACH_HOST: msg = "Host unreachable"; break;
  256. case ICMP_UNREACH_PROTOCOL: msg = "Protocol unreachable (firewall?)"; break;
  257. case ICMP_UNREACH_PORT: msg = "Port unreachable (firewall?)"; break;
  258. case ICMP_UNREACH_NEEDFRAG: msg = "Fragmentation needed"; break;
  259. case ICMP_UNREACH_SRCFAIL: msg = "Source route failed"; break;
  260. case ICMP_UNREACH_ISOLATED: msg = "Source host isolated"; break;
  261. case ICMP_UNREACH_NET_UNKNOWN: msg = "Unknown network"; break;
  262. case ICMP_UNREACH_HOST_UNKNOWN: msg = "Unknown host"; break;
  263. case ICMP_UNREACH_NET_PROHIB: msg = "Network denied (firewall?)"; break;
  264. case ICMP_UNREACH_HOST_PROHIB: msg = "Host denied (firewall?)"; break;
  265. case ICMP_UNREACH_TOSNET: msg = "Bad TOS for network (firewall?)"; break;
  266. case ICMP_UNREACH_TOSHOST: msg = "Bad TOS for host (firewall?)"; break;
  267. case ICMP_UNREACH_FILTER_PROHIB: msg = "Prohibited by filter (firewall)"; break;
  268. case ICMP_UNREACH_HOST_PRECEDENCE: msg = "Host precedence violation"; break;
  269. case ICMP_UNREACH_PRECEDENCE_CUTOFF: msg = "Precedence cutoff"; break;
  270. default: msg = "Invalid code"; break;
  271. }
  272. break;
  273. case ICMP_TIMXCEED:
  274. /* really 'out of reach', or non-existant host behind a router serving
  275. * two different subnets */
  276. switch(icmp_code) {
  277. case ICMP_TIMXCEED_INTRANS: msg = "Time to live exceeded in transit"; break;
  278. case ICMP_TIMXCEED_REASS: msg = "Fragment reassembly time exceeded"; break;
  279. default: msg = "Invalid code"; break;
  280. }
  281. break;
  282. case ICMP_SOURCEQUENCH: msg = "Transmitting too fast"; break;
  283. case ICMP_REDIRECT: msg = "Redirect (change route)"; break;
  284. case ICMP_PARAMPROB: msg = "Bad IP header (required option absent)"; break;
  285. /* the following aren't error messages, so ignore */
  286. case ICMP_TSTAMP:
  287. case ICMP_TSTAMPREPLY:
  288. case ICMP_IREQ:
  289. case ICMP_IREQREPLY:
  290. case ICMP_MASKREQ:
  291. case ICMP_MASKREPLY:
  292. default: msg = ""; break;
  293. }
  294. return msg;
  295. }
  296. static int
  297. handle_random_icmp(unsigned char *packet, struct sockaddr_in *addr)
  298. {
  299. struct icmp p, sent_icmp;
  300. struct rta_host *host = NULL;
  301. memcpy(&p, packet, sizeof(p));
  302. if(p.icmp_type == ICMP_ECHO && ntohs(p.icmp_id) == pid) {
  303. /* echo request from us to us (pinging localhost) */
  304. return 0;
  305. }
  306. if(debug) printf("handle_random_icmp(%p, %p)\n", (void *)&p, (void *)addr);
  307. /* only handle a few types, since others can't possibly be replies to
  308. * us in a sane network (if it is anyway, it will be counted as lost
  309. * at summary time, but not as quickly as a proper response */
  310. /* TIMXCEED can be an unreach from a router with multiple IP's which
  311. * serves two different subnets on the same interface and a dead host
  312. * on one net is pinged from the other. The router will respond to
  313. * itself and thus set TTL=0 so as to not loop forever. Even when
  314. * TIMXCEED actually sends a proper icmp response we will have passed
  315. * too many hops to have a hope of reaching it later, in which case it
  316. * indicates overconfidence in the network, poor routing or both. */
  317. if(p.icmp_type != ICMP_UNREACH && p.icmp_type != ICMP_TIMXCEED &&
  318. p.icmp_type != ICMP_SOURCEQUENCH && p.icmp_type != ICMP_PARAMPROB)
  319. {
  320. return 0;
  321. }
  322. /* might be for us. At least it holds the original package (according
  323. * to RFC 792). If it isn't, just ignore it */
  324. memcpy(&sent_icmp, packet + 28, sizeof(sent_icmp));
  325. if(sent_icmp.icmp_type != ICMP_ECHO || ntohs(sent_icmp.icmp_id) != pid ||
  326. ntohs(sent_icmp.icmp_seq) >= targets*packets)
  327. {
  328. if(debug) printf("Packet is no response to a packet we sent\n");
  329. return 0;
  330. }
  331. /* it is indeed a response for us */
  332. host = table[ntohs(sent_icmp.icmp_seq)/packets];
  333. if(debug) {
  334. printf("Received \"%s\" from %s for ICMP ECHO sent to %s.\n",
  335. get_icmp_error_msg(p.icmp_type, p.icmp_code),
  336. inet_ntoa(addr->sin_addr), host->name);
  337. }
  338. icmp_lost++;
  339. host->icmp_lost++;
  340. /* don't spend time on lost hosts any more */
  341. if(host->flags & FLAG_LOST_CAUSE) return 0;
  342. /* source quench means we're sending too fast, so increase the
  343. * interval and mark this packet lost */
  344. if(p.icmp_type == ICMP_SOURCEQUENCH) {
  345. pkt_interval *= pkt_backoff_factor;
  346. target_interval *= target_backoff_factor;
  347. }
  348. else {
  349. targets_down++;
  350. host->flags |= FLAG_LOST_CAUSE;
  351. }
  352. host->icmp_type = p.icmp_type;
  353. host->icmp_code = p.icmp_code;
  354. host->error_addr.s_addr = addr->sin_addr.s_addr;
  355. return 0;
  356. }
  357. int
  358. main(int argc, char **argv)
  359. {
  360. int i;
  361. char *ptr;
  362. long int arg;
  363. int icmp_sockerrno, udp_sockerrno, tcp_sockerrno;
  364. int result;
  365. struct rta_host *host;
  366. #ifdef HAVE_SIGACTION
  367. struct sigaction sig_action;
  368. #endif
  369. #ifdef SO_TIMESTAMP
  370. int on = 1;
  371. #endif
  372. setlocale (LC_ALL, "");
  373. bindtextdomain (PACKAGE, LOCALEDIR);
  374. textdomain (PACKAGE);
  375. /* print a helpful error message if geteuid != 0 */
  376. np_warn_if_not_root();
  377. /* Set default address_family to AF_INET (IPv4) */
  378. address_family = AF_INET;
  379. /* we only need to be setsuid when we get the sockets, so do
  380. * that before pointer magic (esp. on network data) */
  381. icmp_sockerrno = udp_sockerrno = tcp_sockerrno = sockets = 0;
  382. if((icmp_sock = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) != -1)
  383. sockets |= HAVE_ICMP;
  384. else icmp_sockerrno = errno;
  385. /* if((udp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) != -1) */
  386. /* sockets |= HAVE_UDP; */
  387. /* else udp_sockerrno = errno; */
  388. /* if((tcp_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) */
  389. /* sockets |= HAVE_TCP; */
  390. /* else tcp_sockerrno = errno; */
  391. /* now drop privileges (no effect if not setsuid or geteuid() == 0) */
  392. setuid(getuid());
  393. #ifdef SO_TIMESTAMP
  394. if(setsockopt(icmp_sock, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)))
  395. if(debug) printf("Warning: no SO_TIMESTAMP support\n");
  396. #endif // SO_TIMESTAMP
  397. /* POSIXLY_CORRECT might break things, so unset it (the portable way) */
  398. environ = NULL;
  399. /* use the pid to mark packets as ours */
  400. /* Some systems have 32-bit pid_t so mask off only 16 bits */
  401. pid = getpid() & 0xffff;
  402. /* printf("pid = %u\n", pid); */
  403. /* get calling name the old-fashioned way for portability instead
  404. * of relying on the glibc-ism __progname */
  405. ptr = strrchr(argv[0], '/');
  406. if(ptr) progname = &ptr[1];
  407. else progname = argv[0];
  408. /* now set defaults. Use progname to set them initially (allows for
  409. * superfast check_host program when target host is up */
  410. cursor = list = NULL;
  411. table = NULL;
  412. mode = MODE_RTA;
  413. /* Default critical thresholds */
  414. crit.rta = 500000;
  415. crit.pl = 80;
  416. crit.jitter = 50;
  417. crit.mos= 3;
  418. crit.score=70;
  419. /* Default warning thresholds */
  420. warn.rta = 200000;
  421. warn.pl = 40;
  422. warn.jitter = 40;
  423. warn.mos= 3.5;
  424. warn.score=80;
  425. protocols = HAVE_ICMP | HAVE_UDP | HAVE_TCP;
  426. pkt_interval = 80000; /* 80 msec packet interval by default */
  427. packets = 5;
  428. if(!strcmp(progname, "check_icmp") || !strcmp(progname, "check_ping")) {
  429. mode = MODE_ICMP;
  430. protocols = HAVE_ICMP;
  431. }
  432. else if(!strcmp(progname, "check_host")) {
  433. mode = MODE_HOSTCHECK;
  434. pkt_interval = 1000000;
  435. packets = 5;
  436. crit.rta = warn.rta = 1000000;
  437. crit.pl = warn.pl = 100;
  438. }
  439. else if(!strcmp(progname, "check_rta_multi")) {
  440. mode = MODE_ALL;
  441. target_interval = 0;
  442. pkt_interval = 50000;
  443. packets = 5;
  444. }
  445. /* Parse extra opts if any */
  446. argv=np_extra_opts(&argc, argv, progname);
  447. /* support "--help" and "--version" */
  448. if(argc == 2) {
  449. if(!strcmp(argv[1], "--help"))
  450. strcpy(argv[1], "-h");
  451. if(!strcmp(argv[1], "--version"))
  452. strcpy(argv[1], "-V");
  453. }
  454. /* parse the arguments */
  455. for(i = 1; i < argc; i++) {
  456. while((arg = getopt(argc, argv, "vhVw:c:n:p:t:6:H:s:i:b:I:l:m:P:R:J:S:M:O")) != EOF) {
  457. long size;
  458. switch(arg) {
  459. case 'v':
  460. debug++;
  461. break;
  462. case 'b':
  463. size = strtol(optarg,NULL,0);
  464. if (size >= (sizeof(struct icmp) + sizeof(struct icmp_ping_data)) &&
  465. size < MAX_PING_DATA) {
  466. icmp_data_size = size;
  467. icmp_pkt_size = size + ICMP_MINLEN;
  468. } else
  469. usage_va("ICMP data length must be between: %d and %d",
  470. sizeof(struct icmp) + sizeof(struct icmp_ping_data),
  471. MAX_PING_DATA - 1);
  472. break;
  473. case 'i':
  474. pkt_interval = get_timevar(optarg);
  475. break;
  476. case 'I':
  477. target_interval = get_timevar(optarg);
  478. break;
  479. case 'w':
  480. get_threshold(optarg, &warn);
  481. break;
  482. case 'c':
  483. get_threshold(optarg, &crit);
  484. break;
  485. case 'n':
  486. case 'p':
  487. packets = strtoul(optarg, NULL, 0);
  488. break;
  489. case 't':
  490. timeout = strtoul(optarg, NULL, 0);
  491. if(!timeout) timeout = 10;
  492. break;
  493. case '6':
  494. address_family = AF_INET6;
  495. break;
  496. case 'H':
  497. add_target(optarg);
  498. break;
  499. case 'l':
  500. ttl = (unsigned char)strtoul(optarg, NULL, 0);
  501. break;
  502. case 'm':
  503. min_hosts_alive = (int)strtoul(optarg, NULL, 0);
  504. break;
  505. case 'd': /* implement later, for cluster checks */
  506. warn_down = (unsigned char)strtoul(optarg, &ptr, 0);
  507. if(ptr) {
  508. crit_down = (unsigned char)strtoul(ptr + 1, NULL, 0);
  509. }
  510. break;
  511. case 's': /* specify source IP address */
  512. set_source_ip(optarg);
  513. break;
  514. case 'V': /* version */
  515. print_revision (progname, NP_VERSION);
  516. exit (STATE_OK);
  517. case 'h': /* help */
  518. print_help ();
  519. exit (STATE_OK);
  520. case 'R': /* RTA mode */
  521. get_threshold2(optarg, &warn, &crit,1);
  522. rta_mode=1;
  523. break;
  524. case 'P': /* packet loss mode */
  525. get_threshold2(optarg, &warn, &crit,2);
  526. pl_mode=1;
  527. break;
  528. case 'J': /* packet loss mode */
  529. get_threshold2(optarg, &warn, &crit,3);
  530. jitter_mode=1;
  531. break;
  532. case 'M': /* MOS mode */
  533. get_threshold2(optarg, &warn, &crit,4);
  534. mos_mode=1;
  535. break;
  536. case 'S': /* score mode */
  537. get_threshold2(optarg, &warn, &crit,5);
  538. score_mode=1;
  539. break;
  540. case 'O': /* out of order mode */
  541. order_mode=1;
  542. break;
  543. }
  544. }
  545. }
  546. if(debug > 0)
  547. printf("address_family: %i (IPv4 = 2; IPv6 = 10)\n", address_family);
  548. /* If not AF_INET6 (IPv6) then set default to AF_INET (IPv4) */
  549. /* if(address_family == AF_UNSPEC) {*/
  550. /* address_family = AF_INET;*/
  551. /* if(debug) {*/
  552. /* printf("address_family: %i (IPv4)\n", address_family);*/
  553. /* }*/
  554. /* }*/
  555. /* else {*/
  556. /* if(debug) {*/
  557. /* printf("address_family: %i (IPv6)\n", address_family);*/
  558. /* }*/
  559. /* }*/
  560. argv = &argv[optind];
  561. while(*argv) {
  562. add_target(*argv);
  563. argv++;
  564. }
  565. if(!targets) {
  566. errno = 0;
  567. crash("No hosts to check");
  568. exit(3);
  569. }
  570. if(!sockets) {
  571. if(icmp_sock == -1) {
  572. errno = icmp_sockerrno;
  573. crash("Failed to obtain ICMP socket");
  574. return -1;
  575. }
  576. /* if(udp_sock == -1) { */
  577. /* errno = icmp_sockerrno; */
  578. /* crash("Failed to obtain UDP socket"); */
  579. /* return -1; */
  580. /* } */
  581. /* if(tcp_sock == -1) { */
  582. /* errno = icmp_sockerrno; */
  583. /* crash("Failed to obtain TCP socker"); */
  584. /* return -1; */
  585. /* } */
  586. }
  587. if(!ttl) ttl = 64;
  588. if(icmp_sock) {
  589. result = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
  590. if(debug) {
  591. if(result == -1) printf("setsockopt failed\n");
  592. else printf("ttl set to %u\n", ttl);
  593. }
  594. }
  595. /* stupid users should be able to give whatever thresholds they want
  596. * (nothing will break if they do), but some anal plugin maintainer
  597. * will probably add some printf() thing here later, so it might be
  598. * best to at least show them where to do it. ;) */
  599. if(warn.pl > crit.pl) warn.pl = crit.pl;
  600. if(warn.rta > crit.rta) warn.rta = crit.rta;
  601. if(warn_down > crit_down) crit_down = warn_down;
  602. if(warn.jitter > crit.jitter) crit.jitter = warn.jitter;
  603. if(warn.mos < crit.mos) warn.mos = crit.mos;
  604. if(warn.score < crit.score) warn.score = crit.score;
  605. #ifdef HAVE_SIGACTION
  606. sig_action.sa_sigaction = NULL;
  607. sig_action.sa_handler = finish;
  608. sigfillset(&sig_action.sa_mask);
  609. sig_action.sa_flags = SA_NODEFER|SA_RESTART;
  610. sigaction(SIGINT, &sig_action, NULL);
  611. sigaction(SIGHUP, &sig_action, NULL);
  612. sigaction(SIGTERM, &sig_action, NULL);
  613. sigaction(SIGALRM, &sig_action, NULL);
  614. #else /* HAVE_SIGACTION */
  615. signal(SIGINT, finish);
  616. signal(SIGHUP, finish);
  617. signal(SIGTERM, finish);
  618. signal(SIGALRM, finish);
  619. #endif /* HAVE_SIGACTION */
  620. if(debug) printf("Setting alarm timeout to %u seconds\n", timeout);
  621. alarm(timeout);
  622. /* make sure we don't wait any longer than necessary */
  623. gettimeofday(&prog_start, &tz);
  624. max_completion_time =
  625. ((targets * packets * pkt_interval) + (targets * target_interval)) +
  626. (targets * packets * crit.rta) + crit.rta;
  627. if(debug) {
  628. printf("packets: %u, targets: %u\n"
  629. "target_interval: %0.3f, pkt_interval %0.3f\n"
  630. "crit.rta: %0.3f\n"
  631. "max_completion_time: %0.3f\n",
  632. packets, targets,
  633. (float)target_interval / 1000, (float)pkt_interval / 1000,
  634. (float)crit.rta / 1000,
  635. (float)max_completion_time / 1000);
  636. }
  637. if(debug) {
  638. if(max_completion_time > (u_int)timeout * 1000000) {
  639. printf("max_completion_time: %llu timeout: %u\n",
  640. max_completion_time, timeout);
  641. printf("Timout must be at lest %llu\n",
  642. max_completion_time / 1000000 + 1);
  643. }
  644. }
  645. if(debug) {
  646. printf("crit = {%u, %u%%}, warn = {%u, %u%%}\n",
  647. crit.rta, crit.pl, warn.rta, warn.pl);
  648. printf("pkt_interval: %u target_interval: %u retry_interval: %u\n",
  649. pkt_interval, target_interval, retry_interval);
  650. printf("icmp_pkt_size: %u timeout: %u\n",
  651. icmp_pkt_size, timeout);
  652. }
  653. if(packets > 20) {
  654. errno = 0;
  655. crash("packets is > 20 (%d)", packets);
  656. }
  657. if(min_hosts_alive < -1) {
  658. errno = 0;
  659. crash("minimum alive hosts is negative (%i)", min_hosts_alive);
  660. }
  661. host = list;
  662. table = malloc(sizeof(struct rta_host *) * targets);
  663. i = 0;
  664. while(host) {
  665. host->id = i*packets;
  666. table[i] = host;
  667. host = host->next;
  668. i++;
  669. }
  670. run_checks();
  671. errno = 0;
  672. finish(0);
  673. return(0);
  674. }
  675. static void
  676. run_checks()
  677. {
  678. u_int i, t, result;
  679. u_int final_wait, time_passed;
  680. /* this loop might actually violate the pkt_interval or target_interval
  681. * settings, but only if there aren't any packets on the wire which
  682. * indicates that the target can handle an increased packet rate */
  683. for(i = 0; i < packets; i++) {
  684. for(t = 0; t < targets; t++) {
  685. /* don't send useless packets */
  686. if(!targets_alive) finish(0);
  687. if(table[t]->flags & FLAG_LOST_CAUSE) {
  688. if(debug) printf("%s is a lost cause. not sending any more\n",
  689. table[t]->name);
  690. continue;
  691. }
  692. /* we're still in the game, so send next packet */
  693. (void)send_icmp_ping(icmp_sock, table[t]);
  694. (void)wait_for_reply(icmp_sock, target_interval);
  695. }
  696. (void)wait_for_reply(icmp_sock, pkt_interval * targets);
  697. }
  698. if(icmp_pkts_en_route && targets_alive) {
  699. time_passed = get_timevaldiff(NULL, NULL);
  700. final_wait = max_completion_time - time_passed;
  701. if(debug) {
  702. printf("time_passed: %u final_wait: %u max_completion_time: %llu\n",
  703. time_passed, final_wait, max_completion_time);
  704. }
  705. if(time_passed > max_completion_time) {
  706. if(debug) printf("Time passed. Finishing up\n");
  707. finish(0);
  708. }
  709. /* catch the packets that might come in within the timeframe, but
  710. * haven't yet */
  711. if(debug) printf("Waiting for %u micro-seconds (%0.3f msecs)\n",
  712. final_wait, (float)final_wait / 1000);
  713. (void)wait_for_reply(icmp_sock, final_wait);
  714. }
  715. }
  716. /* response structure:
  717. * ip header : 20 bytes
  718. * icmp header : 28 bytes
  719. * icmp echo reply : the rest
  720. */
  721. static int
  722. wait_for_reply(int sock, u_int t)
  723. {
  724. int n, hlen;
  725. static unsigned char buf[4096];
  726. struct sockaddr_in resp_addr;
  727. struct ip *ip;
  728. struct icmp icp;
  729. struct rta_host *host;
  730. struct icmp_ping_data data;
  731. struct timeval wait_start, now;
  732. u_int tdiff, i, per_pkt_wait;
  733. double jitter_tmp;
  734. /* if we can't listen or don't have anything to listen to, just return */
  735. if(!t || !icmp_pkts_en_route) return 0;
  736. gettimeofday(&wait_start, &tz);
  737. i = t;
  738. per_pkt_wait = t / icmp_pkts_en_route;
  739. while(icmp_pkts_en_route && get_timevaldiff(&wait_start, NULL) < i) {
  740. t = per_pkt_wait;
  741. /* wrap up if all targets are declared dead */
  742. if(!targets_alive ||
  743. get_timevaldiff(&prog_start, NULL) >= max_completion_time ||
  744. (mode == MODE_HOSTCHECK && targets_down))
  745. {
  746. finish(0);
  747. }
  748. /* reap responses until we hit a timeout */
  749. n = recvfrom_wto(sock, buf, sizeof(buf),
  750. (struct sockaddr *)&resp_addr, &t, &now);
  751. if(!n) {
  752. if(debug > 1) {
  753. printf("recvfrom_wto() timed out during a %u usecs wait\n",
  754. per_pkt_wait);
  755. }
  756. continue; /* timeout for this one, so keep trying */
  757. }
  758. if(n < 0) {
  759. if(debug) printf("recvfrom_wto() returned errors\n");
  760. return n;
  761. }
  762. ip = (struct ip *)buf;
  763. if(debug > 1) printf("received %u bytes from %s\n",
  764. ntohs(ip->ip_len), inet_ntoa(resp_addr.sin_addr));
  765. /* obsolete. alpha on tru64 provides the necessary defines, but isn't broken */
  766. /* #if defined( __alpha__ ) && __STDC__ && !defined( __GLIBC__ ) */
  767. /* alpha headers are decidedly broken. Using an ansi compiler,
  768. * they provide ip_vhl instead of ip_hl and ip_v, so we mask
  769. * off the bottom 4 bits */
  770. /* hlen = (ip->ip_vhl & 0x0f) << 2; */
  771. /* #else */
  772. hlen = ip->ip_hl << 2;
  773. /* #endif */
  774. if(n < (hlen + ICMP_MINLEN)) {
  775. crash("received packet too short for ICMP (%d bytes, expected %d) from %s\n",
  776. n, hlen + icmp_pkt_size, inet_ntoa(resp_addr.sin_addr));
  777. }
  778. /* else if(debug) { */
  779. /* printf("ip header size: %u, packet size: %u (expected %u, %u)\n", */
  780. /* hlen, ntohs(ip->ip_len) - hlen, */
  781. /* sizeof(struct ip), icmp_pkt_size); */
  782. /* } */
  783. /* check the response */
  784. memcpy(&icp, buf + hlen, sizeof(icp));
  785. if(ntohs(icp.icmp_id) != pid || icp.icmp_type != ICMP_ECHOREPLY ||
  786. ntohs(icp.icmp_seq) >= targets*packets) {
  787. if(debug > 2) printf("not a proper ICMP_ECHOREPLY\n");
  788. handle_random_icmp(buf + hlen, &resp_addr);
  789. continue;
  790. }
  791. /* this is indeed a valid response */
  792. memcpy(&data, icp.icmp_data, sizeof(data));
  793. if (debug > 2)
  794. printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n",
  795. (unsigned long)sizeof(data), ntohs(icp.icmp_id),
  796. ntohs(icp.icmp_seq), icp.icmp_cksum);
  797. host = table[ntohs(icp.icmp_seq)/packets];
  798. tdiff = get_timevaldiff(&data.stime, &now);
  799. if (host->last_tdiff>0) {
  800. /* Calculate jitter */
  801. if (host->last_tdiff > tdiff) {
  802. jitter_tmp = host->last_tdiff - tdiff;
  803. }
  804. else {
  805. jitter_tmp = tdiff - host->last_tdiff;
  806. }
  807. if (host->jitter==0) {
  808. host->jitter=jitter_tmp;
  809. host->jitter_max=jitter_tmp;
  810. host->jitter_min=jitter_tmp;
  811. }
  812. else {
  813. host->jitter+=jitter_tmp;
  814. if (jitter_tmp < host->jitter_min)
  815. host->jitter_min=jitter_tmp;
  816. if (jitter_tmp > host->jitter_max)
  817. host->jitter_max=jitter_tmp;
  818. }
  819. /* Check if packets in order */
  820. if (host->last_icmp_seq >= icp.icmp_seq)
  821. host->order_status=STATE_CRITICAL;
  822. }
  823. host->last_tdiff=tdiff;
  824. host->last_icmp_seq=icp.icmp_seq;
  825. //printf("%d tdiff %d host->jitter %u host->last_tdiff %u\n", icp.icmp_seq, tdiff, host->jitter, host->last_tdiff);
  826. host->time_waited += tdiff;
  827. host->icmp_recv++;
  828. icmp_recv++;
  829. if (tdiff > (int)host->rtmax)
  830. host->rtmax = tdiff;
  831. if (tdiff < (int)host->rtmin)
  832. host->rtmin = tdiff;
  833. if(debug) {
  834. printf("%0.3f ms rtt from %s, outgoing ttl: %u, incoming ttl: %u, max: %0.3f, min: %0.3f\n",
  835. (float)tdiff / 1000, inet_ntoa(resp_addr.sin_addr),
  836. ttl, ip->ip_ttl, (float)host->rtmax / 1000, (float)host->rtmin / 1000);
  837. }
  838. /* if we're in hostcheck mode, exit with limited printouts */
  839. if(mode == MODE_HOSTCHECK) {
  840. printf("OK - %s responds to ICMP. Packet %u, rta %0.3fms|"
  841. "pkt=%u;;0;%u rta=%0.3f;%0.3f;%0.3f;;\n",
  842. host->name, icmp_recv, (float)tdiff / 1000,
  843. icmp_recv, packets, (float)tdiff / 1000,
  844. (float)warn.rta / 1000, (float)crit.rta / 1000);
  845. exit(STATE_OK);
  846. }
  847. }
  848. return 0;
  849. }
  850. /* the ping functions */
  851. static int
  852. send_icmp_ping(int sock, struct rta_host *host)
  853. {
  854. static union {
  855. void *buf; /* re-use so we prevent leaks */
  856. struct icmp *icp;
  857. u_short *cksum_in;
  858. } packet = { NULL };
  859. long int len;
  860. struct icmp_ping_data data;
  861. struct msghdr hdr;
  862. struct iovec iov;
  863. struct timeval tv;
  864. struct sockaddr *addr;
  865. if(sock == -1) {
  866. errno = 0;
  867. crash("Attempt to send on bogus socket");
  868. return -1;
  869. }
  870. addr = (struct sockaddr *)&host->saddr_in;
  871. if(!packet.buf) {
  872. if (!(packet.buf = malloc(icmp_pkt_size))) {
  873. crash("send_icmp_ping(): failed to malloc %d bytes for send buffer",
  874. icmp_pkt_size);
  875. return -1; /* might be reached if we're in debug mode */
  876. }
  877. }
  878. memset(packet.buf, 0, icmp_pkt_size);
  879. if((gettimeofday(&tv, &tz)) == -1) return -1;
  880. data.ping_id = 10; /* host->icmp.icmp_sent; */
  881. memcpy(&data.stime, &tv, sizeof(tv));
  882. memcpy(&packet.icp->icmp_data, &data, sizeof(data));
  883. packet.icp->icmp_type = ICMP_ECHO;
  884. packet.icp->icmp_code = 0;
  885. packet.icp->icmp_cksum = 0;
  886. packet.icp->icmp_id = htons(pid);
  887. packet.icp->icmp_seq = htons(host->id++);
  888. packet.icp->icmp_cksum = icmp_checksum(packet.cksum_in, icmp_pkt_size);
  889. if (debug > 2)
  890. printf("Sending ICMP echo-request of len %lu, id %u, seq %u, cksum 0x%X to host %s\n",
  891. (unsigned long)sizeof(data), ntohs(packet.icp->icmp_id),
  892. ntohs(packet.icp->icmp_seq), packet.icp->icmp_cksum,
  893. host->name);
  894. memset(&iov, 0, sizeof(iov));
  895. iov.iov_base = packet.buf;
  896. iov.iov_len = icmp_pkt_size;
  897. memset(&hdr, 0, sizeof(hdr));
  898. hdr.msg_name = addr;
  899. hdr.msg_namelen = sizeof(struct sockaddr);
  900. hdr.msg_iov = &iov;
  901. hdr.msg_iovlen = 1;
  902. /* MSG_CONFIRM is a linux thing and only available on linux kernels >= 2.3.15, see send(2) */
  903. #ifdef MSG_CONFIRM
  904. len = sendmsg(sock, &hdr, MSG_CONFIRM);
  905. #else
  906. len = sendmsg(sock, &hdr, 0);
  907. #endif
  908. if(len < 0 || (unsigned int)len != icmp_pkt_size) {
  909. if(debug) printf("Failed to send ping to %s\n",
  910. inet_ntoa(host->saddr_in.sin_addr));
  911. return -1;
  912. }
  913. icmp_sent++;
  914. host->icmp_sent++;
  915. return 0;
  916. }
  917. static int
  918. recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr *saddr,
  919. u_int *timo, struct timeval* tv)
  920. {
  921. u_int slen;
  922. int n, ret;
  923. struct timeval to, then, now;
  924. fd_set rd, wr;
  925. char ans_data[4096];
  926. struct msghdr hdr;
  927. struct iovec iov;
  928. #ifdef SO_TIMESTAMP
  929. struct cmsghdr* chdr;
  930. #endif
  931. if(!*timo) {
  932. if(debug) printf("*timo is not\n");
  933. return 0;
  934. }
  935. to.tv_sec = *timo / 1000000;
  936. to.tv_usec = (*timo - (to.tv_sec * 1000000));
  937. FD_ZERO(&rd);
  938. FD_ZERO(&wr);
  939. FD_SET(sock, &rd);
  940. errno = 0;
  941. gettimeofday(&then, &tz);
  942. n = select(sock + 1, &rd, &wr, NULL, &to);
  943. if(n < 0) crash("select() in recvfrom_wto");
  944. gettimeofday(&now, &tz);
  945. *timo = get_timevaldiff(&then, &now);
  946. if(!n) return 0; /* timeout */
  947. slen = sizeof(struct sockaddr);
  948. memset(&iov, 0, sizeof(iov));
  949. iov.iov_base = buf;
  950. iov.iov_len = len;
  951. memset(&hdr, 0, sizeof(hdr));
  952. hdr.msg_name = saddr;
  953. hdr.msg_namelen = slen;
  954. hdr.msg_iov = &iov;
  955. hdr.msg_iovlen = 1;
  956. #ifdef HAVE_MSGHDR_MSG_CONTROL
  957. hdr.msg_control = ans_data;
  958. hdr.msg_controllen = sizeof(ans_data);
  959. #endif
  960. ret = recvmsg(sock, &hdr, 0);
  961. #ifdef SO_TIMESTAMP
  962. for(chdr = CMSG_FIRSTHDR(&hdr); chdr; chdr = CMSG_NXTHDR(&hdr, chdr)) {
  963. if(chdr->cmsg_level == SOL_SOCKET
  964. && chdr->cmsg_type == SO_TIMESTAMP
  965. && chdr->cmsg_len >= CMSG_LEN(sizeof(struct timeval))) {
  966. memcpy(tv, CMSG_DATA(chdr), sizeof(*tv));
  967. break ;
  968. }
  969. }
  970. if (!chdr)
  971. #endif // SO_TIMESTAMP
  972. gettimeofday(tv, &tz);
  973. return (ret);
  974. }
  975. static void
  976. finish(int sig)
  977. {
  978. u_int i = 0;
  979. unsigned char pl;
  980. double rta;
  981. struct rta_host *host;
  982. const char *status_string[] =
  983. {"OK", "WARNING", "CRITICAL", "UNKNOWN", "DEPENDENT"};
  984. int hosts_ok = 0;
  985. int hosts_warn = 0;
  986. double R;
  987. alarm(0);
  988. if(debug > 1) printf("finish(%d) called\n", sig);
  989. if(icmp_sock != -1) close(icmp_sock);
  990. if(udp_sock != -1) close(udp_sock);
  991. if(tcp_sock != -1) close(tcp_sock);
  992. if(debug) {
  993. printf("icmp_sent: %u icmp_recv: %u icmp_lost: %u\n",
  994. icmp_sent, icmp_recv, icmp_lost);
  995. printf("targets: %u targets_alive: %u\n", targets, targets_alive);
  996. }
  997. /* iterate thrice to calculate values, give output, and print perfparse */
  998. status=STATE_OK;
  999. host = list;
  1000. while(host) {
  1001. if(!host->icmp_recv) {
  1002. /* rta 0 is ofcourse not entirely correct, but will still show up
  1003. * conspicuosly as missing entries in perfparse and cacti */
  1004. pl = 100;
  1005. rta = 0;
  1006. status = STATE_CRITICAL;
  1007. /* up the down counter if not already counted */
  1008. if(!(host->flags & FLAG_LOST_CAUSE) && targets_alive) targets_down++;
  1009. }
  1010. else {
  1011. pl = ((host->icmp_sent - host->icmp_recv) * 100) / host->icmp_sent;
  1012. rta = (double)host->time_waited / host->icmp_recv;
  1013. }
  1014. if (host->icmp_recv>1) {
  1015. host->jitter=(host->jitter / (host->icmp_recv - 1)/1000);
  1016. host->EffectiveLatency = (rta/1000) + host->jitter * 2 + 10;
  1017. if (host->EffectiveLatency < 160)
  1018. R = 93.2 - (host->EffectiveLatency / 40);
  1019. else
  1020. R = 93.2 - ((host->EffectiveLatency - 120) / 10);
  1021. R = R - (pl * 2.5);
  1022. if (R<0) R=0;
  1023. host->score = R;
  1024. host->mos= 1 + ((0.035) * R) + ((.000007) * R * (R-60) * (100-R));
  1025. }
  1026. else {
  1027. host->jitter=0;
  1028. host->jitter_min=0;
  1029. host->jitter_max=0;
  1030. host->mos=0;
  1031. }
  1032. host->pl = pl;
  1033. host->rta = rta;
  1034. /* if no new mode selected, use old schema */
  1035. if (!rta_mode && !pl_mode && !jitter_mode && !score_mode && !mos_mode && !order_mode) {
  1036. rta_mode=1;
  1037. pl_mode=1;
  1038. }
  1039. /* Check which mode is on and do the warn / Crit stuff */
  1040. if (rta_mode) {
  1041. if(rta >= crit.rta) {
  1042. status = STATE_CRITICAL;
  1043. host->rta_status=STATE_CRITICAL;
  1044. }
  1045. else if(status!=STATE_CRITICAL && (rta >= warn.rta)) {
  1046. status = STATE_WARNING;
  1047. hosts_warn++;
  1048. host->rta_status=STATE_WARNING;
  1049. }
  1050. else {
  1051. hosts_ok++;
  1052. }
  1053. }
  1054. if (pl_mode) {
  1055. if(pl >= crit.pl) {
  1056. status = STATE_CRITICAL;
  1057. host->pl_status=STATE_CRITICAL;
  1058. }
  1059. else if(status!=STATE_CRITICAL && (pl >= warn.pl)) {
  1060. status = STATE_WARNING;
  1061. hosts_warn++;
  1062. host->pl_status=STATE_WARNING;
  1063. }
  1064. else {
  1065. hosts_ok++;
  1066. }
  1067. }
  1068. if (jitter_mode) {
  1069. if(host->jitter >= crit.jitter) {
  1070. status = STATE_CRITICAL;
  1071. host->jitter_status=STATE_CRITICAL;
  1072. }
  1073. else if(status!=STATE_CRITICAL && (host->jitter >= warn.jitter)) {
  1074. status = STATE_WARNING;
  1075. hosts_warn++;
  1076. host->jitter_status=STATE_WARNING;
  1077. }
  1078. else {
  1079. hosts_ok++;
  1080. }
  1081. }
  1082. if (mos_mode) {
  1083. if(host->mos <= crit.mos) {
  1084. status = STATE_CRITICAL;
  1085. host->mos_status=STATE_CRITICAL;
  1086. }
  1087. else if(status!=STATE_CRITICAL && (host->mos <= warn.mos)) {
  1088. status = STATE_WARNING;
  1089. hosts_warn++;
  1090. host->mos_status=STATE_WARNING;
  1091. }
  1092. else {
  1093. hosts_ok++;
  1094. }
  1095. }
  1096. if (score_mode) {
  1097. if(host->score <= crit.score) {
  1098. status = STATE_CRITICAL;
  1099. host->score_status=STATE_CRITICAL;
  1100. }
  1101. else if(status!=STATE_CRITICAL && (host->score <= warn.score)) {
  1102. status = STATE_WARNING;
  1103. score_mode++;
  1104. host->score_status=STATE_WARNING;
  1105. }
  1106. else {
  1107. hosts_ok++;
  1108. }
  1109. }
  1110. host = host->next;
  1111. }
  1112. /* this is inevitable */
  1113. if(!targets_alive) status = STATE_CRITICAL;
  1114. if(min_hosts_alive > -1) {
  1115. if(hosts_ok >= min_hosts_alive) status = STATE_OK;
  1116. else if((hosts_ok + hosts_warn) >= min_hosts_alive) status = STATE_WARNING;
  1117. }
  1118. printf("%s - ", status_string[status]);
  1119. host = list;
  1120. while(host) {
  1121. if(debug) puts("");
  1122. if(i) {
  1123. if(i < targets) printf(" :: ");
  1124. else printf("\n");
  1125. }
  1126. i++;
  1127. if(!host->icmp_recv) {
  1128. status = STATE_CRITICAL;
  1129. host->rtmin=0;
  1130. host->jitter_min=0;
  1131. if(host->flags & FLAG_LOST_CAUSE) {
  1132. printf("%s: %s @ %s. rta nan, lost %d%%",
  1133. host->name,
  1134. get_icmp_error_msg(host->icmp_type, host->icmp_code),
  1135. inet_ntoa(host->error_addr),
  1136. 100);
  1137. }
  1138. else { /* not marked as lost cause, so we have no flags for it */
  1139. printf("%s: rta nan, lost 100%%", host->name);
  1140. }
  1141. }
  1142. else { /* !icmp_recv */
  1143. printf("%s", host->name);
  1144. /* rta text output */
  1145. if (rta_mode) {
  1146. if (status == STATE_OK)
  1147. printf(" rta %0.3fms", host->rta / 1000);
  1148. else if (status==STATE_WARNING && host->rta_status==status)
  1149. printf(" rta %0.3fms > %0.3fms", (float)host->rta / 1000, (float)warn.rta/1000);
  1150. else if (status==STATE_CRITICAL && host->rta_status==status)
  1151. printf(" rta %0.3fms > %0.3fms", (float)host->rta / 1000, (float)crit.rta/1000);
  1152. }
  1153. /* pl text output */
  1154. if (pl_mode) {
  1155. if (status == STATE_OK)
  1156. printf(" lost %u%%", host->pl);
  1157. else if (status==STATE_WARNING && host->pl_status==status)
  1158. printf(" lost %u%% > %u%%", host->pl, warn.pl);
  1159. else if (status==STATE_CRITICAL && host->pl_status==status)
  1160. printf(" lost %u%% > %u%%", host->pl, crit.pl);
  1161. }
  1162. /* jitter text output */
  1163. if (jitter_mode) {
  1164. if (status == STATE_OK)
  1165. printf(" jitter %0.3fms", (float)host->jitter);
  1166. else if (status==STATE_WARNING && host->jitter_status==status)
  1167. printf(" jitter %0.3fms > %0.3fms", (float)host->jitter, warn.jitter);
  1168. else if (status==STATE_CRITICAL && host->jitter_status==status)
  1169. printf(" jitter %0.3fms > %0.3fms", (float)host->jitter, crit.jitter);
  1170. }
  1171. /* mos text output */
  1172. if (mos_mode) {
  1173. if (status == STATE_OK)
  1174. printf(" MOS %0.1f", (float)host->mos);
  1175. else if (status==STATE_WARNING && host->mos_status==status)
  1176. printf(" MOS %0.1f < %0.1f", (float)host->mos, (float)warn.mos);
  1177. else if (status==STATE_CRITICAL && host->mos_status==status)
  1178. printf(" MOS %0.1f < %0.1f", (float)host->mos, (float)crit.mos);
  1179. }
  1180. /* score text output */
  1181. if (score_mode) {
  1182. if (status == STATE_OK)
  1183. printf(" Score %u", (int)host->score);
  1184. else if (status==STATE_WARNING && host->score_status==status )
  1185. printf(" Score %u < %u", (int)host->score, (int)warn.score);
  1186. else if (status==STATE_CRITICAL && host->score_status==status )
  1187. printf(" Score %u < %u", (int)host->score, (int)crit.score);
  1188. }
  1189. /* order statis text output */
  1190. if (order_mode) {
  1191. if (status == STATE_OK)
  1192. printf(" Packets in order");
  1193. else if (status==STATE_CRITICAL && host->order_status==status)
  1194. printf(" Packets out of order");
  1195. }
  1196. }
  1197. host = host->next;
  1198. }
  1199. /* iterate once more for pretty perfparse output */
  1200. if (!(!rta_mode && !pl_mode && !jitter_mode && !score_mode && !mos_mode && order_mode)) {
  1201. printf("|");
  1202. }
  1203. i = 0;
  1204. host = list;
  1205. while(host) {
  1206. if(debug) puts("");
  1207. if (rta_mode && host->pl<100) {
  1208. printf("%srta=%0.3fms;%0.3f;%0.3f;0; %srtmax=%0.3fms;;;; %srtmin=%0.3fms;;;; ",(targets > 1) ? host->name : "", (float)host->rta / 1000, (float)warn.rta / 1000, (float)crit.rta / 1000, (targets > 1) ? host->name : "", (float)host->rtmax / 1000, (targets > 1) ? host->name : "", (float)host->rtmin / 1000);
  1209. }
  1210. if (pl_mode) {
  1211. printf("%spl=%u%%;%u;%u;0;100 ", (targets > 1) ? host->name : "", host->pl, warn.pl, crit.pl);
  1212. }
  1213. if (jitter_mode && host->pl<100) {
  1214. printf("%sjitter_avg=%0.3fms;%0.3f;%0.3f;0; %sjitter_max=%0.3fms;;;; %sjitter_min=%0.3fms;;;; ", (targets > 1) ? host->name : "", (float)host->jitter, (float)warn.jitter, (float)crit.jitter, (targets > 1) ? host->name : "", (float)host->jitter_max / 1000, (targets > 1) ? host->name : "",(float)host->jitter_min / 1000);
  1215. }
  1216. if (mos_mode && host->pl<100) {
  1217. printf("%smos=%0.1f;%0.1f;%0.1f;0;5 ", (targets > 1) ? host->name : "", (float)host->mos, (float)warn.mos, (float)crit.mos);
  1218. }
  1219. if (score_mode && host->pl<100) {
  1220. printf("%sscore=%u;%u;%u;0;100 ", (targets > 1) ? host->name : "", (int)host->score, (int)warn.score, (int)crit.score);
  1221. }
  1222. host = host->next;
  1223. }
  1224. if(min_hosts_alive > -1) {
  1225. if(hosts_ok >= min_hosts_alive) status = STATE_OK;
  1226. else if((hosts_ok + hosts_warn) >= min_hosts_alive) status = STATE_WARNING;
  1227. }
  1228. /* finish with an empty line */
  1229. puts("");
  1230. if(debug) printf("targets: %u, targets_alive: %u, hosts_ok: %u, hosts_warn: %u, min_hosts_alive: %i\n",
  1231. targets, targets_alive, hosts_ok, hosts_warn, min_hosts_alive);
  1232. exit(status);
  1233. }
  1234. static u_int
  1235. get_timevaldiff(struct timeval *early, struct timeval *later)
  1236. {
  1237. u_int ret;
  1238. struct timeval now;
  1239. if(!later) {
  1240. gettimeofday(&now, &tz);
  1241. later = &now;
  1242. }
  1243. if(!early) early = &prog_start;
  1244. /* if early > later we return 0 so as to indicate a timeout */
  1245. if(early->tv_sec > later->tv_sec ||
  1246. (early->tv_sec == later->tv_sec && early->tv_usec > later->tv_usec))
  1247. {
  1248. return 0;
  1249. }
  1250. ret = (later->tv_sec - early->tv_sec) * 1000000;
  1251. ret += later->tv_usec - early->tv_usec;
  1252. return ret;
  1253. }
  1254. static int
  1255. add_target_ip(char *arg, struct in_addr *in)
  1256. {
  1257. struct rta_host *host;
  1258. /* disregard obviously stupid addresses */
  1259. if(in->s_addr == INADDR_NONE || in->s_addr == INADDR_ANY)
  1260. return -1;
  1261. /* no point in adding two identical IP's, so don't. ;) */
  1262. host = list;
  1263. while(host) {
  1264. if(host->saddr_in.sin_addr.s_addr == in->s_addr) {
  1265. if(debug) printf("Identical IP already exists. Not adding %s\n", arg);
  1266. return -1;
  1267. }
  1268. host = host->next;
  1269. }
  1270. /* add the fresh ip */
  1271. host = malloc(sizeof(struct rta_host));
  1272. if(!host) {
  1273. crash("add_target_ip(%s, %s): malloc(%d) failed",
  1274. arg, inet_ntoa(*in), sizeof(struct rta_host));
  1275. }
  1276. memset(host, 0, sizeof(struct rta_host));
  1277. /* set the values. use calling name for output */
  1278. host->name = strdup(arg);
  1279. /* fill out the sockaddr_in struct */
  1280. host->saddr_in.sin_family = AF_INET;
  1281. host->saddr_in.sin_addr.s_addr = in->s_addr;
  1282. host->rtmin = DBL_MAX;
  1283. host->rtmax = 0;
  1284. host->jitter=0;
  1285. host->jitter_max=0;
  1286. host->jitter_min=DBL_MAX;
  1287. host->last_tdiff=0;
  1288. host->order_status=STATE_OK;
  1289. host->last_icmp_seq=0;
  1290. host->rta_status=0;
  1291. host->pl_status=0;
  1292. host->jitter_status=0;
  1293. host->mos_status=0;
  1294. host->score_status=0;
  1295. host->pl_status=0;
  1296. if(!list) list = cursor = host;
  1297. else cursor->next = host;
  1298. cursor = host;
  1299. targets++;
  1300. return 0;
  1301. }
  1302. /* wrapper for add_target_ip */
  1303. static int
  1304. add_target(char *arg)
  1305. {
  1306. int i;
  1307. struct hostent *he;
  1308. struct in_addr *in, ip;
  1309. inet_pton(address_family, arg, &(ip.s_addr));
  1310. /* don't resolve if we don't have to */
  1311. if(ip.s_addr != INADDR_NONE) {
  1312. /* don't resolve if we don't have to */
  1313. /*if((ip.s_addr = inet_addr(arg)) != INADDR_NONE) {*/
  1314. /* don't add all ip's if we were given a specific one */
  1315. return add_target_ip(arg, &ip);
  1316. /* he = gethostbyaddr((char *)in, sizeof(struct in_addr), AF_INET); */
  1317. /* if(!he) return add_target_ip(arg, in); */
  1318. }
  1319. else {
  1320. errno = 0;
  1321. he = gethostbyname(arg);
  1322. if(!he) {
  1323. errno = 0;
  1324. crash("Failed to resolve %s", arg);
  1325. return -1;
  1326. }
  1327. }
  1328. /* possibly add all the IP's as targets */
  1329. for(i = 0; he->h_addr_list[i]; i++) {
  1330. in = (struct in_addr *)he->h_addr_list[i];
  1331. add_target_ip(arg, in);
  1332. /* this is silly, but it works */
  1333. if(mode == MODE_HOSTCHECK || mode == MODE_ALL) {
  1334. if(debug > 2) printf("mode: %d\n", mode);
  1335. continue;
  1336. }
  1337. break;
  1338. }
  1339. return 0;
  1340. }
  1341. static void
  1342. set_source_ip(char *arg)
  1343. {
  1344. struct sockaddr_in src;
  1345. memset(&src, 0, sizeof(src));
  1346. src.sin_family = AF_INET;
  1347. if((src.sin_addr.s_addr = inet_addr(arg)) == INADDR_NONE)
  1348. src.sin_addr.s_addr = get_ip_address(arg);
  1349. if(bind(icmp_sock, (struct sockaddr *)&src, sizeof(src)) == -1)
  1350. crash("Cannot bind to IP address %s", arg);
  1351. }
  1352. /* TODO: Move this to netutils.c and also change check_dhcp to use that. */
  1353. static in_addr_t
  1354. get_ip_address(const char *ifname)
  1355. {
  1356. #if defined(SIOCGIFADDR)
  1357. struct ifreq ifr;
  1358. struct sockaddr_in ip;
  1359. strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  1360. ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
  1361. if(ioctl(icmp_sock, SIOCGIFADDR, &ifr) == -1)
  1362. crash("Cannot determine IP address of interface %s", ifname);
  1363. memcpy(&ip, &ifr.ifr_addr, sizeof(ip));
  1364. return ip.sin_addr.s_addr;
  1365. #else
  1366. errno = 0;
  1367. crash("Cannot get interface IP address on this platform.");
  1368. #endif
  1369. }
  1370. /*
  1371. * u = micro
  1372. * m = milli
  1373. * s = seconds
  1374. * return value is in microseconds
  1375. */
  1376. static u_int
  1377. get_timevar(const char *str)
  1378. {
  1379. char p, u, *ptr;
  1380. size_t len;
  1381. u_int i, d; /* integer and decimal, respectively */
  1382. u_int factor = 1000; /* default to milliseconds */
  1383. if(!str) return 0;
  1384. len = strlen(str);
  1385. if(!len) return 0;
  1386. /* unit might be given as ms|m (millisec),
  1387. * us|u (microsec) or just plain s, for seconds */
  1388. p = '\0';
  1389. u = str[len - 1];
  1390. if(len >= 2 && !isdigit((int)str[len - 2])) p = str[len - 2];
  1391. if(p && u == 's') u = p;
  1392. else if(!p) p = u;
  1393. if(debug > 2) printf("evaluating %s, u: %c, p: %c\n", str, u, p);
  1394. if(u == 'u') factor = 1; /* microseconds */
  1395. else if(u == 'm') factor = 1000; /* milliseconds */
  1396. else if(u == 's') factor = 1000000; /* seconds */
  1397. if(debug > 2) printf("factor is %u\n", factor);
  1398. i = strtoul(str, &ptr, 0);
  1399. if(!ptr || *ptr != '.' || strlen(ptr) < 2 || factor == 1)
  1400. return i * factor;
  1401. /* time specified in usecs can't have decimal points, so ignore them */
  1402. if(factor == 1) return i;
  1403. d = strtoul(ptr + 1, NULL, 0);
  1404. /* d is decimal, so get rid of excess digits */
  1405. while(d >= factor) d /= 10;
  1406. /* the last parenthesis avoids floating point exceptions. */
  1407. return ((i * factor) + (d * (factor / 10)));
  1408. }
  1409. /* not too good at checking errors, but it'll do (main() should barfe on -1) */
  1410. static int
  1411. get_threshold(char *str, threshold *th)
  1412. {
  1413. char *p = NULL, i = 0;
  1414. if(!str || !strlen(str) || !th) return -1;
  1415. /* pointer magic slims code by 10 lines. i is bof-stop on stupid libc's */
  1416. p = &str[strlen(str) - 1];
  1417. while(p != &str[1]) {
  1418. if(*p == '%') *p = '\0';
  1419. else if(*p == ',' && i) {
  1420. *p = '\0'; /* reset it so get_timevar(str) works nicely later */
  1421. th->pl = (unsigned char)strtoul(p+1, NULL, 0);
  1422. break;
  1423. }
  1424. i = 1;
  1425. p--;
  1426. }
  1427. th->rta = get_timevar(str);
  1428. if(!th->rta) return -1;
  1429. if(th->rta > MAXTTL * 1000000) th->rta = MAXTTL * 1000000;
  1430. if(th->pl > 100) th->pl = 100;
  1431. return 0;
  1432. }
  1433. /* not too good at checking errors, but it'll do (main() should barfe on -1) */
  1434. static int
  1435. get_threshold2(char *str, threshold *warn, threshold *crit, int type)
  1436. {
  1437. char *p = NULL, i = 0;
  1438. if(!str || !strlen(str) || !warn || !crit) return -1;
  1439. /* pointer magic slims code by 10 lines. i is bof-stop on stupid libc's */
  1440. p = &str[strlen(str) - 1];
  1441. while(p != &str[0]) {
  1442. if( (*p == 'm') || (*p == '%') ) *p = '\0';
  1443. else if(*p == ',' && i) {
  1444. *p = '\0'; /* reset it so get_timevar(str) works nicely later */
  1445. if (type==1)
  1446. crit->rta = atof(p+1)*1000;
  1447. else if (type==2)
  1448. crit->pl = (unsigned char)strtoul(p+1, NULL, 0);
  1449. else if (type==3)
  1450. crit->jitter = atof(p+1);
  1451. else if (type==4)
  1452. crit->mos = atof(p+1);
  1453. else if (type==5)
  1454. crit->score = atof(p+1);
  1455. }
  1456. i = 1;
  1457. p--;
  1458. }
  1459. if (type==1)
  1460. warn->rta = atof(p)*1000;
  1461. else if (type==2)
  1462. warn->pl = (unsigned char)strtoul(p, NULL, 0);
  1463. if (type==3)
  1464. warn->jitter = atof(p);
  1465. else if (type==4)
  1466. warn->mos = atof(p);
  1467. else if (type==5)
  1468. warn->score = atof(p);
  1469. return 0;
  1470. }
  1471. unsigned short
  1472. icmp_checksum(unsigned short *p, int n)
  1473. {
  1474. register unsigned short cksum;
  1475. register long sum = 0;
  1476. while(n > 1) {
  1477. sum += *p++;
  1478. n -= 2;
  1479. }
  1480. /* mop up the occasional odd byte */
  1481. if(n == 1) sum += (unsigned char)*p;
  1482. sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
  1483. sum += (sum >> 16); /* add carry */
  1484. cksum = ~sum; /* ones-complement, trunc to 16 bits */
  1485. return cksum;
  1486. }
  1487. void
  1488. print_help(void)
  1489. {
  1490. /*print_revision (progname);*/ /* FIXME: Why? */
  1491. printf ("Copyright (c) 2005 Andreas Ericsson <ae@op5.se>\n");
  1492. printf ("Jiiter, MOS and Score support added by Alesssandro Ren <alessandro.ren@opservices.com.br>\n");
  1493. printf (COPYRIGHT, copyright, email);
  1494. printf ("\n\n");
  1495. print_usage ();
  1496. printf (UT_HELP_VRSN);
  1497. printf (UT_EXTRA_OPTS);
  1498. printf (" %s\n", "-w");
  1499. printf (" %s", _("warning threshold (currently "));
  1500. printf ("%0.3fms,%u%%)\n", (float)warn.rta / 1000, warn.pl);
  1501. printf (" %s\n", "-c");
  1502. printf (" %s", _("critical threshold (currently "));
  1503. printf ("%0.3fms,%u%%)\n", (float)crit.rta / 1000, crit.pl);
  1504. printf (" %s\n", "-R");
  1505. printf (" %s\n", _("RTA, round trip average, mode warning,critical, ex. 100ms,200ms unit in ms"));
  1506. printf (" %s\n", "-P");
  1507. printf (" %s\n", _("packet loss mode, ex. 40%,50% , unit in %"));
  1508. printf (" %s\n", "-J");
  1509. printf (" %s\n", _("jitter mode warning,critical, ex. 40.000ms,50.000ms , unit in ms "));
  1510. printf (" %s\n", "-M");
  1511. printf (" %s\n", _("MOS mode, between 0 and 4.4 warning,critical, ex. 3.5,3.0"));
  1512. printf (" %s\n", "-S");
  1513. printf (" %s\n", _("score mode, max value 100 warning,critical, ex. 80,70 "));
  1514. printf (" %s\n", "-O");
  1515. printf (" %s\n", _("detect out of order ICMP packts "));
  1516. printf (" %s\n", "-H");
  1517. printf (" %s\n", _("specify a target"));
  1518. printf (" %s\n", "-s");
  1519. printf (" %s\n", _("specify a source IP address or device name"));
  1520. printf (" %s\n", "-n");
  1521. printf (" %s", _("number of packets to send (currently "));
  1522. printf ("%u)\n",packets);
  1523. printf (" %s\n", "-p");
  1524. printf (" %s", _("number of packets to send (currently "));
  1525. printf ("%u)\n",packets);
  1526. printf (" %s\n", "-i");
  1527. printf (" %s", _("max packet interval (currently "));
  1528. printf ("%0.3fms)\n",(float)pkt_interval / 1000);
  1529. printf (" %s\n", "-I");
  1530. printf (" %s", _("max target interval (currently "));
  1531. printf ("%0.3fms)\n", (float)target_interval / 1000);
  1532. printf (" %s\n", "-m");
  1533. printf (" %s",_("number of alive hosts required for success"));
  1534. printf ("\n");
  1535. printf (" %s\n", "-l");
  1536. printf (" %s", _("TTL on outgoing packets (currently "));
  1537. printf ("%u)\n", ttl);
  1538. printf (" %s\n", "-t");
  1539. printf (" %s",_("timeout value (seconds, currently "));
  1540. printf ("%u)\n", timeout);
  1541. printf (" %s\n", "-b");
  1542. printf (" %s\n", _("Number of icmp data bytes to send"));
  1543. printf (" %s %u + %d)\n", _("Packet size will be data bytes + icmp header (currently"),icmp_data_size, ICMP_MINLEN);
  1544. printf (" %s\n", "-v");
  1545. printf (" %s\n", _("verbose"));
  1546. printf ("\n");
  1547. printf ("%s\n", _("Notes:"));
  1548. printf ("%s\n", _("If not mode R,P,J,M,S or O is informed, default icmp behavior, RTA and packet loss"));
  1549. printf (" %s\n", _("The -H switch is optional. Naming a host (or several) to check is not."));
  1550. printf ("\n");
  1551. printf (" %s\n", _("Threshold format for -w and -c is 200.25,60% for 200.25 msec RTA and 60%"));
  1552. printf (" %s\n", _("packet loss. The default values should work well for most users."));
  1553. printf (" %s\n", _("You can specify different RTA factors using the standardized abbreviations"));
  1554. printf (" %s\n", _("us (microseconds), ms (milliseconds, default) or just plain s for seconds."));
  1555. /* -d not yet implemented */
  1556. /* printf ("%s\n", _("Threshold format for -d is warn,crit. 12,14 means WARNING if >= 12 hops"));
  1557. printf ("%s\n", _("are spent and CRITICAL if >= 14 hops are spent."));
  1558. printf ("%s\n\n", _("NOTE: Some systems decrease TTL when forming ICMP_ECHOREPLY, others do not."));*/
  1559. printf ("\n");
  1560. printf (" %s\n", _("The -v switch can be specified several times for increased verbosity."));
  1561. /* printf ("%s\n", _("Long options are currently unsupported."));
  1562. printf ("%s\n", _("Options marked with * require an argument"));
  1563. */
  1564. printf (UT_SUPPORT);
  1565. }
  1566. void
  1567. print_usage (void)
  1568. {
  1569. printf ("%s\n", _("Usage:"));
  1570. printf(" %s [options] [-H] host1 host2 hostN\n", progname);
  1571. }