check_icmp.c 61 KB

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