check_icmp.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178
  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, perfdata_num=-1, ttl = 0;
  224. static char *perfdata_sep = NULL;
  225. static unsigned short icmp_data_size = DEFAULT_PING_DATA_SIZE;
  226. static unsigned short icmp_pkt_size = DEFAULT_PING_DATA_SIZE + ICMP_MINLEN;
  227. static unsigned int icmp_sent = 0, icmp_recv = 0, icmp_lost = 0;
  228. #define icmp_pkts_en_route (icmp_sent - (icmp_recv + icmp_lost))
  229. static unsigned short targets_down = 0, targets = 0, packets = 0;
  230. #define targets_alive (targets - targets_down)
  231. static unsigned int retry_interval, pkt_interval, target_interval;
  232. static int icmp_sock, tcp_sock, udp_sock, status = STATE_OK;
  233. static pid_t pid;
  234. static struct timezone tz;
  235. static struct timeval prog_start;
  236. static unsigned long long max_completion_time = 0;
  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. char *bind_address = NULL;
  428. long int arg;
  429. int icmp_sockerrno, udp_sockerrno, tcp_sockerrno;
  430. int result;
  431. struct rta_host *host;
  432. #ifdef HAVE_SIGACTION
  433. struct sigaction sig_action;
  434. #endif
  435. #ifdef SO_TIMESTAMP
  436. int on = 1;
  437. #endif
  438. setlocale(LC_ALL, "");
  439. bindtextdomain(PACKAGE, LOCALEDIR);
  440. textdomain(PACKAGE);
  441. /* print a helpful error message if geteuid != 0 */
  442. np_warn_if_not_root();
  443. /* Set default address_family to AF_INET (IPv4) */
  444. /* It will be changed to AF_INET6 later if required */
  445. address_family = AF_INET;
  446. /* Set default ip_protocol to IPPROTO_ICMP (IPv4) */
  447. /* It will be changed to IPPROTO_ICMPV6 later if required */
  448. int ip_protocol = IPPROTO_ICMP;
  449. /* we only need to be setsuid when we get the sockets, so do
  450. * that before pointer magic (esp. on network data) */
  451. icmp_sockerrno = udp_sockerrno = tcp_sockerrno = sockets = 0;
  452. /* get calling name the old-fashioned way for portability instead
  453. * of relying on the glibc-ism __progname */
  454. ptr = strrchr(argv[0], '/');
  455. if (ptr) {
  456. progname = &ptr[1];
  457. } else {
  458. progname = argv[0];
  459. }
  460. /* now set defaults. Use progname to set them initially (allows for
  461. * superfast check_host program when target host is up */
  462. cursor = list = NULL;
  463. table = NULL;
  464. mode = MODE_RTA;
  465. /* Default critical thresholds */
  466. crit.rta = 500000;
  467. crit.pl = 80;
  468. crit.jitter = 50;
  469. crit.mos = 3;
  470. crit.score = 70;
  471. /* Default warning thresholds */
  472. warn.rta = 200000;
  473. warn.pl = 40;
  474. warn.jitter = 40;
  475. warn.mos = 3.5;
  476. warn.score = 80;
  477. protocols = HAVE_ICMP | HAVE_UDP | HAVE_TCP;
  478. pkt_interval = 80000; /* 80 msec packet interval by default */
  479. packets = 5;
  480. if (!strcmp(progname, "check_icmp") || !strcmp(progname, "check_ping")) {
  481. mode = MODE_ICMP;
  482. protocols = HAVE_ICMP;
  483. } else if (!strcmp(progname, "check_host")) {
  484. mode = MODE_HOSTCHECK;
  485. pkt_interval = 1000000;
  486. packets = 5;
  487. crit.rta = warn.rta = 1000000;
  488. crit.pl = warn.pl = 100;
  489. } else if (!strcmp(progname, "check_rta_multi")) {
  490. mode = MODE_ALL;
  491. target_interval = 0;
  492. pkt_interval = 50000;
  493. packets = 5;
  494. }
  495. /* parse the arguments */
  496. for (i = 1; i < argc; i++) {
  497. while ((arg = getopt(argc, argv,
  498. "vhVw:c:n:p:t:H:s:i:b:f:F:I:l:m:P:R:J:S:M:O:64")) != EOF) {
  499. long size;
  500. switch (arg) {
  501. case 'v':
  502. debug++;
  503. break;
  504. case 'b':
  505. size = strtol(optarg, NULL, 0);
  506. if (size >= (sizeof(struct icmp) + sizeof(struct icmp_ping_data)) &&
  507. size < MAX_PING_DATA) {
  508. icmp_data_size = size;
  509. icmp_pkt_size = size + ICMP_MINLEN;
  510. } else {
  511. usage_va("ICMP data length must be between: %d and %d",
  512. sizeof(struct icmp) + sizeof(struct icmp_ping_data),
  513. MAX_PING_DATA - 1);
  514. }
  515. break;
  516. case 'f':
  517. perfdata_sep = optarg;
  518. break;
  519. case 'F':
  520. perfdata_num = strtoul(optarg, NULL, 0);
  521. break;
  522. case 'i':
  523. pkt_interval = get_timevar(optarg);
  524. break;
  525. case 'I':
  526. target_interval = get_timevar(optarg);
  527. break;
  528. case 'w':
  529. get_threshold(optarg, &warn);
  530. break;
  531. case 'c':
  532. get_threshold(optarg, &crit);
  533. break;
  534. case 'n':
  535. case 'p':
  536. packets = strtoul(optarg, NULL, 0);
  537. break;
  538. case 't':
  539. timeout = strtoul(optarg, NULL, 0);
  540. if (!timeout) {
  541. timeout = 10;
  542. }
  543. break;
  544. case '4':
  545. address_family = AF_INET;
  546. ip_protocol = IPPROTO_ICMP;
  547. break;
  548. case '6':
  549. #ifdef USE_IPV6
  550. address_family = AF_INET6;
  551. ip_protocol = IPPROTO_ICMPV6;
  552. #else
  553. usage(_("IPv6 support not available\n"));
  554. #endif
  555. break;
  556. case 'H':
  557. add_target(optarg);
  558. break;
  559. case 'l':
  560. ttl = (int)strtoul(optarg, NULL, 0);
  561. break;
  562. case 'm':
  563. min_hosts_alive = (int)strtoul(optarg, NULL, 0);
  564. break;
  565. case 'd':
  566. /* implement later, for cluster checks */
  567. warn_down = (unsigned char)strtoul(optarg, &ptr, 0);
  568. if (ptr) {
  569. crit_down = (unsigned char)strtoul(ptr + 1, NULL, 0);
  570. }
  571. break;
  572. case 's':
  573. /* specify source IP address */
  574. bind_address = optarg;
  575. break;
  576. case 'V':
  577. /* version */
  578. print_revision(progname, NP_VERSION);
  579. exit(STATE_OK);
  580. case 'h':
  581. /* help */
  582. print_help();
  583. exit(STATE_OK);
  584. case 'R':
  585. /* RTA mode */
  586. get_threshold2(optarg, &warn, &crit, 1);
  587. rta_mode = 1;
  588. break;
  589. case 'P':
  590. /* packet loss mode */
  591. get_threshold2(optarg, &warn, &crit, 2);
  592. pl_mode = 1;
  593. break;
  594. case 'J':
  595. /* packet loss mode */
  596. get_threshold2(optarg, &warn, &crit, 3);
  597. jitter_mode = 1;
  598. break;
  599. case 'M':
  600. /* MOS mode */
  601. get_threshold2(optarg, &warn, &crit, 4);
  602. mos_mode = 1;
  603. break;
  604. case 'S':
  605. /* score mode */
  606. get_threshold2(optarg, &warn, &crit, 5);
  607. score_mode = 1;
  608. break;
  609. case 'O':
  610. /* out of order mode */
  611. order_mode = 1;
  612. break;
  613. }
  614. }
  615. }
  616. if ((icmp_sock = socket(address_family, SOCK_RAW, ip_protocol)) != -1) {
  617. sockets |= HAVE_ICMP;
  618. } else {
  619. icmp_sockerrno = errno;
  620. }
  621. if (bind_address != NULL) {
  622. set_source_ip(bind_address);
  623. }
  624. /* now drop privileges (no effect if not setsuid or geteuid() == 0) */
  625. setuid(getuid());
  626. #ifdef SO_TIMESTAMP
  627. if (setsockopt(icmp_sock, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on))) {
  628. if (debug) {
  629. printf("Warning: no SO_TIMESTAMP support\n");
  630. }
  631. }
  632. #endif /* SO_TIMESTAMP */
  633. /* POSIXLY_CORRECT might break things, so unset it (the portable way) */
  634. environ = NULL;
  635. /* use the pid to mark packets as ours */
  636. /* Some systems have 32-bit pid_t so mask off only 16 bits */
  637. pid = getpid() & 0xffff;
  638. /* printf("pid = %u\n", pid); */
  639. /* Parse extra opts if any */
  640. argv = np_extra_opts(&argc, argv, progname);
  641. /* support "--help" and "--version" */
  642. if (argc == 2) {
  643. if (!strcmp(argv[1], "--help")) {
  644. strcpy(argv[1], "-h");
  645. }
  646. if (!strcmp(argv[1], "--version")) {
  647. strcpy(argv[1], "-V");
  648. }
  649. }
  650. if (debug) {
  651. printf("address_family: %i (IPv4 = 2; IPv6 = 10)\n", address_family);
  652. }
  653. argv = &argv[optind];
  654. while (*argv) {
  655. add_target(*argv);
  656. argv++;
  657. }
  658. if (!targets) {
  659. errno = 0;
  660. crash("No hosts to check");
  661. exit(3);
  662. }
  663. if (!sockets) {
  664. if (icmp_sock == -1) {
  665. errno = icmp_sockerrno;
  666. crash("Failed to obtain ICMP socket");
  667. return -1;
  668. }
  669. /* if(udp_sock == -1) { */
  670. /* errno = icmp_sockerrno; */
  671. /* crash("Failed to obtain UDP socket"); */
  672. /* return -1; */
  673. /* } */
  674. /* if(tcp_sock == -1) { */
  675. /* errno = icmp_sockerrno; */
  676. /* crash("Failed to obtain TCP socket"); */
  677. /* return -1; */
  678. /* } */
  679. }
  680. if (!ttl) {
  681. ttl = 64;
  682. }
  683. if (icmp_sock) {
  684. result = setsockopt(icmp_sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl));
  685. if (debug) {
  686. if (result == -1) {
  687. printf("setsockopt failed\n");
  688. } else {
  689. printf("ttl set to %u\n", ttl);
  690. }
  691. }
  692. }
  693. /* Users should be able to give whatever thresholds they want */
  694. /* (nothing will break if they do), but some plugin maintainer */
  695. /* will probably add some printf() thing here later, so it might be */
  696. /* best to at least show them where to do it */
  697. if (warn.pl > crit.pl) {
  698. warn.pl = crit.pl;
  699. }
  700. if (warn.rta > crit.rta) {
  701. warn.rta = crit.rta;
  702. }
  703. if (warn_down > crit_down) {
  704. crit_down = warn_down;
  705. }
  706. if (warn.jitter > crit.jitter) {
  707. crit.jitter = warn.jitter;
  708. }
  709. if (warn.mos < crit.mos) {
  710. warn.mos = crit.mos;
  711. }
  712. if (warn.score < crit.score) {
  713. warn.score = crit.score;
  714. }
  715. #ifdef HAVE_SIGACTION
  716. sig_action.sa_sigaction = NULL;
  717. sig_action.sa_handler = finish;
  718. sigfillset(&sig_action.sa_mask);
  719. sig_action.sa_flags = SA_NODEFER | SA_RESTART;
  720. sigaction(SIGINT, &sig_action, NULL);
  721. sigaction(SIGHUP, &sig_action, NULL);
  722. sigaction(SIGTERM, &sig_action, NULL);
  723. sigaction(SIGALRM, &sig_action, NULL);
  724. #else /* HAVE_SIGACTION */
  725. signal(SIGINT, finish);
  726. signal(SIGHUP, finish);
  727. signal(SIGTERM, finish);
  728. signal(SIGALRM, finish);
  729. #endif /* HAVE_SIGACTION */
  730. if (debug) {
  731. printf("Setting alarm timeout to %u seconds\n", timeout);
  732. }
  733. alarm(timeout);
  734. /* make sure we don't wait any longer than necessary */
  735. gettimeofday(&prog_start, &tz);
  736. max_completion_time =
  737. ((targets * packets * pkt_interval) + (targets * target_interval)) +
  738. (targets * packets * crit.rta) + crit.rta;
  739. if (debug) {
  740. printf("packets: %u, targets: %u\n"
  741. "target_interval: %0.3f, pkt_interval %0.3f\n"
  742. "crit.rta: %0.3f\n"
  743. "max_completion_time: %0.3f\n",
  744. packets, targets, (float)target_interval / 1000,
  745. (float)pkt_interval / 1000, (float)crit.rta / 1000,
  746. (float)max_completion_time / 1000);
  747. }
  748. if (debug) {
  749. if (max_completion_time > (u_int)timeout * 1000000) {
  750. printf("max_completion_time: %llu timeout: %u\n", max_completion_time,
  751. timeout);
  752. printf("Timout must be at least %llu\n",
  753. max_completion_time / 1000000 + 1);
  754. }
  755. }
  756. if (debug) {
  757. printf("crit = {%u, %u%%}, warn = {%u, %u%%}\n", crit.rta, crit.pl,
  758. warn.rta, warn.pl);
  759. printf("pkt_interval: %u target_interval: %u retry_interval: %u\n",
  760. pkt_interval, target_interval, retry_interval);
  761. printf("icmp_pkt_size: %u timeout: %u\n", icmp_pkt_size, timeout);
  762. }
  763. if (packets > 20) {
  764. errno = 0;
  765. crash("packets is > 20 (%d)", packets);
  766. }
  767. if (min_hosts_alive < -1) {
  768. errno = 0;
  769. crash("minimum alive hosts is negative (%i)", min_hosts_alive);
  770. }
  771. host = list;
  772. table = (struct rta_host **)malloc(sizeof(struct rta_host **) * targets);
  773. i = 0;
  774. while (host) {
  775. host->id = i * packets;
  776. table[i] = host;
  777. host = host->next;
  778. i++;
  779. }
  780. run_checks();
  781. errno = 0;
  782. finish(0);
  783. return (0);
  784. }
  785. static void run_checks() {
  786. u_int i, t, result;
  787. u_int final_wait, time_passed;
  788. /* this loop might actually violate the pkt_interval or target_interval
  789. * settings, but only if there aren't any packets on the wire which
  790. * indicates that the target can handle an increased packet rate */
  791. for (i = 0; i < packets; i++) {
  792. for (t = 0; t < targets; t++) {
  793. /* don't send useless packets */
  794. if (!targets_alive) {
  795. finish(0);
  796. }
  797. if (table[t]->flags & FLAG_LOST_CAUSE) {
  798. if (debug) {
  799. printf("%s is a lost cause. not sending any more\n", table[t]->name);
  800. }
  801. continue;
  802. }
  803. /* we're still in the game, so send next packet */
  804. (void)send_icmp_ping(icmp_sock, table[t]);
  805. result = wait_for_reply(icmp_sock, target_interval);
  806. }
  807. result = wait_for_reply(icmp_sock, pkt_interval * targets);
  808. }
  809. if (icmp_pkts_en_route && targets_alive) {
  810. time_passed = get_timevaldiff(NULL, NULL);
  811. final_wait = max_completion_time - time_passed;
  812. if (debug) {
  813. printf("time_passed: %u final_wait: %u max_completion_time: %llu\n",
  814. time_passed, final_wait, max_completion_time);
  815. }
  816. if (time_passed > max_completion_time) {
  817. if (debug) {
  818. printf("Time passed. Finishing up\n");
  819. }
  820. finish(0);
  821. }
  822. /* catch the packets that might come in within the timeframe, but
  823. * haven't yet */
  824. if (debug) {
  825. printf("Waiting for %u micro-seconds (%0.3f msecs)\n", final_wait,
  826. (float)final_wait / 1000);
  827. }
  828. result = wait_for_reply(icmp_sock, final_wait);
  829. }
  830. }
  831. /* Response Structure: */
  832. /* IPv4: */
  833. /* ip header (total length) : 20 bytes */
  834. /* IPv6: */
  835. /* ip header (payload length) : 40 bytes */
  836. /* Both: */
  837. /* icmp header : 28 bytes */
  838. /* icmp echo reply : the rest */
  839. static int wait_for_reply(int sock, u_int t) {
  840. int n, hlen = 0;
  841. static unsigned char buf[4096];
  842. struct sockaddr_storage resp_addr;
  843. union ip_hdr *ip;
  844. union icmp_packet packet;
  845. struct rta_host *host = NULL;
  846. struct icmp_ping_data data;
  847. struct timeval wait_start, now;
  848. u_int tdiff, i, per_pkt_wait;
  849. double jitter_tmp;
  850. if (!(packet.buf = malloc(icmp_pkt_size))) {
  851. crash("send_icmp_ping(): failed to malloc %d bytes for send buffer",
  852. icmp_pkt_size);
  853. return -1; /* might be reached if we're in debug mode */
  854. }
  855. memset(packet.buf, 0, icmp_pkt_size);
  856. /* if we can't listen or don't have anything to listen to, just return */
  857. if (!t || !icmp_pkts_en_route) {
  858. free(packet.buf);
  859. return 0;
  860. }
  861. gettimeofday(&wait_start, &tz);
  862. i = t;
  863. per_pkt_wait = t / icmp_pkts_en_route;
  864. while (icmp_pkts_en_route && get_timevaldiff(&wait_start, NULL) < i) {
  865. t = per_pkt_wait;
  866. /* wrap up if all targets are declared dead */
  867. if (!targets_alive ||
  868. get_timevaldiff(&prog_start, NULL) >= max_completion_time ||
  869. (mode == MODE_HOSTCHECK && targets_down)) {
  870. finish(0);
  871. }
  872. /* reap responses until we hit a timeout */
  873. n = recvfrom_wto(sock, buf, sizeof(buf), (struct sockaddr *)&resp_addr, &t,
  874. &now);
  875. if (!n) {
  876. if (debug > 1) {
  877. printf("recvfrom_wto() timed out during a %u usecs wait\n",
  878. per_pkt_wait);
  879. }
  880. continue; /* timeout for this one, so keep trying */
  881. }
  882. if (n < 0) {
  883. if (debug) {
  884. printf("recvfrom_wto() returned errors\n");
  885. }
  886. return n;
  887. }
  888. ip = (union ip_hdr *)buf;
  889. if (debug > 1) {
  890. char address[address_length(address_family)];
  891. parse_address_string(address_family, &resp_addr, address,
  892. sizeof(address));
  893. if (address_family == AF_INET) {
  894. printf("received %u bytes from %s\n", ntohs(ip->ip.ip_len), address);
  895. } else if (address_family == AF_INET6) {
  896. printf("received %u bytes from %s\n", ntohs(ip->ip6.ip6_plen), address);
  897. }
  898. }
  899. /* IPv6 doesn't have a header length, it's a payload length */
  900. if (address_family == AF_INET) {
  901. hlen = ip->ip.ip_hl << 2;
  902. } else if (address_family == AF_INET6) {
  903. hlen = 0;
  904. }
  905. if (n < (hlen + ICMP_MINLEN)) {
  906. char address[address_length(address_family)];
  907. parse_address_string(address_family, &resp_addr, address,
  908. sizeof(address));
  909. crash("received packet too short for ICMP (%d bytes, expected %d) from "
  910. "%s\n",
  911. n, hlen + icmp_pkt_size, address);
  912. }
  913. /* check the response */
  914. memcpy(packet.buf, buf + hlen, icmp_pkt_size);
  915. if ((address_family == AF_INET &&
  916. (ntohs(packet.icp->icmp_id) != pid ||
  917. packet.icp->icmp_type != ICMP_ECHOREPLY ||
  918. ntohs(packet.icp->icmp_seq) >= targets * packets)) ||
  919. (address_family == AF_INET6 &&
  920. (ntohs(packet.icp6->icmp6_id) != pid ||
  921. packet.icp6->icmp6_type != ICMP6_ECHO_REPLY ||
  922. ntohs(packet.icp6->icmp6_seq) >= targets * packets))) {
  923. if (debug > 2) {
  924. printf("not a proper ICMP_ECHOREPLY\n");
  925. }
  926. handle_random_icmp(buf + hlen, &resp_addr);
  927. continue;
  928. }
  929. /* this is indeed a valid response */
  930. if (address_family == AF_INET) {
  931. memcpy(&data, packet.icp->icmp_data, sizeof(data));
  932. if (debug > 2) {
  933. printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n",
  934. (unsigned long)sizeof(data), ntohs(packet.icp->icmp_id),
  935. ntohs(packet.icp->icmp_seq), packet.icp->icmp_cksum);
  936. }
  937. host = table[ntohs(packet.icp->icmp_seq) / packets];
  938. } else if (address_family == AF_INET6) {
  939. memcpy(&data, &packet.icp6->icmp6_dataun.icmp6_un_data8[4], sizeof(data));
  940. if (debug > 2) {
  941. printf("ICMP echo-reply of len %lu, id %u, seq %u, cksum 0x%X\n",
  942. (unsigned long)sizeof(data), ntohs(packet.icp6->icmp6_id),
  943. ntohs(packet.icp6->icmp6_seq), packet.icp6->icmp6_cksum);
  944. }
  945. host = table[ntohs(packet.icp6->icmp6_seq) / packets];
  946. }
  947. tdiff = get_timevaldiff(&data.stime, &now);
  948. if (host->last_tdiff > 0) {
  949. /* Calculate jitter */
  950. if (host->last_tdiff > tdiff) {
  951. jitter_tmp = host->last_tdiff - tdiff;
  952. } else {
  953. jitter_tmp = tdiff - host->last_tdiff;
  954. }
  955. if (host->jitter == 0) {
  956. host->jitter = jitter_tmp;
  957. host->jitter_max = jitter_tmp;
  958. host->jitter_min = jitter_tmp;
  959. } else {
  960. host->jitter += jitter_tmp;
  961. if (jitter_tmp < host->jitter_min) {
  962. host->jitter_min = jitter_tmp;
  963. }
  964. if (jitter_tmp > host->jitter_max) {
  965. host->jitter_max = jitter_tmp;
  966. }
  967. }
  968. /* Check if packets in order */
  969. if (host->last_icmp_seq >= packet.icp->icmp_seq) {
  970. host->order_status = STATE_CRITICAL;
  971. }
  972. }
  973. host->last_tdiff = tdiff;
  974. host->last_icmp_seq = packet.icp->icmp_seq;
  975. host->time_waited += tdiff;
  976. host->icmp_recv++;
  977. icmp_recv++;
  978. if (tdiff > (int)host->rtmax) {
  979. host->rtmax = tdiff;
  980. }
  981. if (tdiff < (int)host->rtmin) {
  982. host->rtmin = tdiff;
  983. }
  984. if (debug) {
  985. char address[address_length(address_family)];
  986. parse_address_string(address_family, &resp_addr, address,
  987. sizeof(address));
  988. printf("%0.3f ms rtt from %s, outgoing ttl: %u, incoming ttl: %u, max: "
  989. "%0.3f, min: %0.3f\n",
  990. (float)tdiff / 1000, address, ttl, ip->ip.ip_ttl,
  991. (float)host->rtmax / 1000, (float)host->rtmin / 1000);
  992. }
  993. /* if we're in hostcheck mode, exit with limited printouts */
  994. if (mode == MODE_HOSTCHECK) {
  995. printf("OK - %s responds to ICMP. Packet %u, rta %0.3fms|"
  996. "pkt=%u;;0;%u rta=%0.3f;%0.3f;%0.3f;;\n",
  997. host->name, icmp_recv, (float)tdiff / 1000, icmp_recv, packets,
  998. (float)tdiff / 1000, (float)warn.rta / 1000,
  999. (float)crit.rta / 1000);
  1000. exit(STATE_OK);
  1001. }
  1002. }
  1003. return 0;
  1004. }
  1005. /* the ping functions */
  1006. static int send_icmp_ping(int sock, struct rta_host *host) {
  1007. long int len;
  1008. struct icmp_ping_data data;
  1009. struct msghdr hdr;
  1010. struct iovec iov;
  1011. struct timeval tv;
  1012. size_t addrlen;
  1013. void *buf = NULL;
  1014. if (sock == -1) {
  1015. errno = 0;
  1016. crash("Attempt to send on bogus socket");
  1017. return -1;
  1018. }
  1019. if (!buf) {
  1020. if (!(buf = malloc(icmp_pkt_size))) {
  1021. crash("send_icmp_ping(): failed to malloc %d bytes for send buffer",
  1022. icmp_pkt_size);
  1023. return -1; /* might be reached if we're in debug mode */
  1024. }
  1025. }
  1026. memset(buf, 0, icmp_pkt_size);
  1027. if ((gettimeofday(&tv, &tz)) == -1) {
  1028. free(buf);
  1029. return -1;
  1030. }
  1031. data.ping_id = 10; /* host->icmp.icmp_sent; */
  1032. memcpy(&data.stime, &tv, sizeof(tv));
  1033. if (address_family == AF_INET) {
  1034. struct icmp *icp = (struct icmp *)buf;
  1035. addrlen = sizeof(struct sockaddr_in);
  1036. memcpy(&icp->icmp_data, &data, sizeof(data));
  1037. icp->icmp_type = ICMP_ECHO;
  1038. icp->icmp_code = 0;
  1039. icp->icmp_cksum = 0;
  1040. icp->icmp_id = htons(pid);
  1041. icp->icmp_seq = htons(host->id++);
  1042. icp->icmp_cksum = icmp_checksum((unsigned short *)buf, icmp_pkt_size);
  1043. if (debug > 2) {
  1044. printf("Sending ICMPv4 echo-request of len %lu, id %u, seq %u, cksum "
  1045. "0x%X to host %s\n",
  1046. (unsigned long)sizeof(data), ntohs(icp->icmp_id),
  1047. ntohs(icp->icmp_seq), icp->icmp_cksum, host->name);
  1048. }
  1049. } else if (address_family == AF_INET6) {
  1050. struct icmp6_hdr *icp6 = (struct icmp6_hdr *)buf;
  1051. addrlen = sizeof(struct sockaddr_in6);
  1052. memcpy(&icp6->icmp6_dataun.icmp6_un_data8[4], &data, sizeof(data));
  1053. icp6->icmp6_type = ICMP6_ECHO_REQUEST;
  1054. icp6->icmp6_code = 0;
  1055. icp6->icmp6_cksum = 0;
  1056. icp6->icmp6_id = htons(pid);
  1057. icp6->icmp6_seq = htons(host->id++);
  1058. /* checksum is calculated automatically */
  1059. if (debug > 2) {
  1060. printf("Sending ICMPv6 echo-request of len %lu, id %u, seq %u, cksum "
  1061. "0x%X to host %s\n",
  1062. (unsigned long)sizeof(data), ntohs(icp6->icmp6_id),
  1063. ntohs(icp6->icmp6_seq), icp6->icmp6_cksum, host->name);
  1064. }
  1065. }
  1066. memset(&iov, 0, sizeof(iov));
  1067. iov.iov_base = buf;
  1068. iov.iov_len = icmp_pkt_size;
  1069. memset(&hdr, 0, sizeof(hdr));
  1070. hdr.msg_name = (struct sockaddr *)&host->saddr_in;
  1071. hdr.msg_namelen = addrlen;
  1072. hdr.msg_iov = &iov;
  1073. hdr.msg_iovlen = 1;
  1074. errno = 0;
  1075. /* MSG_CONFIRM is a linux thing and only available on linux kernels >= 2.3.15,
  1076. * see send(2) */
  1077. #ifdef MSG_CONFIRM
  1078. len = sendmsg(sock, &hdr, MSG_CONFIRM);
  1079. #else
  1080. len = sendmsg(sock, &hdr, 0);
  1081. #endif
  1082. free(buf);
  1083. if (len < 0 || (unsigned int)len != icmp_pkt_size) {
  1084. if (debug) {
  1085. char address[address_length(address_family)];
  1086. parse_address_string(address_family,
  1087. (struct sockaddr_storage *)&host->saddr_in, address,
  1088. sizeof(address));
  1089. printf("Failed to send ping to %s = %s\n", address, strerror(errno));
  1090. }
  1091. errno = 0;
  1092. return -1;
  1093. }
  1094. icmp_sent++;
  1095. host->icmp_sent++;
  1096. return 0;
  1097. }
  1098. static int recvfrom_wto(int sock, void *buf, unsigned int len,
  1099. struct sockaddr *saddr, u_int *timo,
  1100. struct timeval *tv) {
  1101. u_int slen;
  1102. int n, ret;
  1103. struct timeval to, then, now;
  1104. fd_set rd, wr;
  1105. char ans_data[4096];
  1106. struct msghdr hdr;
  1107. struct iovec iov;
  1108. #ifdef SO_TIMESTAMP
  1109. struct cmsghdr *chdr;
  1110. #endif
  1111. if (!*timo) {
  1112. if (debug)
  1113. printf("*timo is not\n");
  1114. return 0;
  1115. }
  1116. to.tv_sec = *timo / 1000000;
  1117. to.tv_usec = (*timo - (to.tv_sec * 1000000));
  1118. FD_ZERO(&rd);
  1119. FD_ZERO(&wr);
  1120. FD_SET(sock, &rd);
  1121. errno = 0;
  1122. gettimeofday(&then, &tz);
  1123. n = select(sock + 1, &rd, &wr, NULL, &to);
  1124. if (n < 0) {
  1125. crash("select() in recvfrom_wto");
  1126. }
  1127. gettimeofday(&now, &tz);
  1128. *timo = get_timevaldiff(&then, &now);
  1129. if (!n) {
  1130. /* timeout */
  1131. return 0;
  1132. }
  1133. slen = sizeof(struct sockaddr_storage);
  1134. memset(&iov, 0, sizeof(iov));
  1135. iov.iov_base = buf;
  1136. iov.iov_len = len;
  1137. memset(&hdr, 0, sizeof(hdr));
  1138. hdr.msg_name = saddr;
  1139. hdr.msg_namelen = slen;
  1140. hdr.msg_iov = &iov;
  1141. hdr.msg_iovlen = 1;
  1142. #ifdef HAVE_MSGHDR_MSG_CONTROL
  1143. hdr.msg_control = ans_data;
  1144. hdr.msg_controllen = sizeof(ans_data);
  1145. #endif
  1146. ret = recvmsg(sock, &hdr, 0);
  1147. #ifdef SO_TIMESTAMP
  1148. for (chdr = CMSG_FIRSTHDR(&hdr); chdr; chdr = CMSG_NXTHDR(&hdr, chdr)) {
  1149. if (chdr->cmsg_level == SOL_SOCKET && chdr->cmsg_type == SO_TIMESTAMP &&
  1150. chdr->cmsg_len >= CMSG_LEN(sizeof(struct timeval))) {
  1151. memcpy(tv, CMSG_DATA(chdr), sizeof(*tv));
  1152. break;
  1153. }
  1154. }
  1155. #endif /* SO_TIMESTAMP */
  1156. if (!chdr) {
  1157. gettimeofday(tv, &tz);
  1158. }
  1159. return (ret);
  1160. }
  1161. static void finish(int sig) {
  1162. u_int i = 0;
  1163. unsigned char pl;
  1164. double rta;
  1165. struct rta_host *host;
  1166. const char *status_string[] = {"OK", "WARNING", "CRITICAL", "UNKNOWN",
  1167. "DEPENDENT"};
  1168. int hosts_ok = 0;
  1169. int hosts_warn = 0;
  1170. int this_status;
  1171. double R;
  1172. alarm(0);
  1173. if (debug > 1) {
  1174. printf("finish(%d) called\n", sig);
  1175. }
  1176. if (icmp_sock != -1) {
  1177. close(icmp_sock);
  1178. }
  1179. if (udp_sock != -1) {
  1180. close(udp_sock);
  1181. }
  1182. if (tcp_sock != -1) {
  1183. close(tcp_sock);
  1184. }
  1185. if (debug) {
  1186. printf("icmp_sent: %u icmp_recv: %u icmp_lost: %u\n", icmp_sent,
  1187. icmp_recv, icmp_lost);
  1188. printf("targets: %u targets_alive: %u\n", targets, targets_alive);
  1189. }
  1190. /* iterate thrice to calculate values, give output, and print perfparse */
  1191. status = STATE_OK;
  1192. host = list;
  1193. while (host) {
  1194. this_status = STATE_OK;
  1195. if (!host->icmp_recv) {
  1196. /* rta 0 is ofcourse not entirely correct, but will still show up
  1197. * conspicuosly as missing entries in perfparse and cacti */
  1198. pl = 100;
  1199. rta = 0;
  1200. status = STATE_CRITICAL;
  1201. /* up the down counter if not already counted */
  1202. if (!(host->flags & FLAG_LOST_CAUSE) && targets_alive) {
  1203. targets_down++;
  1204. }
  1205. } else {
  1206. pl = ((host->icmp_sent - host->icmp_recv) * 100) / host->icmp_sent;
  1207. rta = (double)host->time_waited / host->icmp_recv;
  1208. }
  1209. if (host->icmp_recv > 1) {
  1210. host->jitter = (host->jitter / (host->icmp_recv - 1) / 1000);
  1211. host->EffectiveLatency = (rta / 1000) + host->jitter * 2 + 10;
  1212. if (host->EffectiveLatency < 160) {
  1213. R = 93.2 - (host->EffectiveLatency / 40);
  1214. } else {
  1215. R = 93.2 - ((host->EffectiveLatency - 120) / 10);
  1216. }
  1217. R = R - (pl * 2.5);
  1218. if (R < 0) {
  1219. R = 0;
  1220. }
  1221. host->score = R;
  1222. host->mos = 1 + ((0.035) * R) + ((.000007) * R * (R - 60) * (100 - R));
  1223. } else {
  1224. host->jitter = 0;
  1225. host->jitter_min = 0;
  1226. host->jitter_max = 0;
  1227. host->mos = 0;
  1228. }
  1229. host->pl = pl;
  1230. host->rta = rta;
  1231. /* if no new mode selected, use old schema */
  1232. if (!rta_mode && !pl_mode && !jitter_mode && !score_mode && !mos_mode &&
  1233. !order_mode) {
  1234. rta_mode = 1;
  1235. pl_mode = 1;
  1236. }
  1237. /* Check which mode is on and do the warn / Crit stuff */
  1238. if (rta_mode) {
  1239. if (rta >= crit.rta) {
  1240. this_status = STATE_CRITICAL;
  1241. status = STATE_CRITICAL;
  1242. host->rta_status = STATE_CRITICAL;
  1243. } else if (status != STATE_CRITICAL && (rta >= warn.rta)) {
  1244. this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
  1245. status = STATE_WARNING;
  1246. host->rta_status = STATE_WARNING;
  1247. }
  1248. }
  1249. if (pl_mode) {
  1250. if (pl >= crit.pl) {
  1251. this_status = STATE_CRITICAL;
  1252. status = STATE_CRITICAL;
  1253. host->pl_status = STATE_CRITICAL;
  1254. } else if (status != STATE_CRITICAL && (pl >= warn.pl)) {
  1255. this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
  1256. status = STATE_WARNING;
  1257. host->pl_status = STATE_WARNING;
  1258. }
  1259. }
  1260. if (jitter_mode) {
  1261. if (host->jitter >= crit.jitter) {
  1262. this_status = STATE_CRITICAL;
  1263. status = STATE_CRITICAL;
  1264. host->jitter_status = STATE_CRITICAL;
  1265. } else if (status != STATE_CRITICAL && (host->jitter >= warn.jitter)) {
  1266. this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
  1267. status = STATE_WARNING;
  1268. host->jitter_status = STATE_WARNING;
  1269. }
  1270. }
  1271. if (mos_mode) {
  1272. if (host->mos <= crit.mos) {
  1273. this_status = STATE_CRITICAL;
  1274. status = STATE_CRITICAL;
  1275. host->mos_status = STATE_CRITICAL;
  1276. } else if (status != STATE_CRITICAL && (host->mos <= warn.mos)) {
  1277. this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
  1278. status = STATE_WARNING;
  1279. host->mos_status = STATE_WARNING;
  1280. }
  1281. }
  1282. if (score_mode) {
  1283. if (host->score <= crit.score) {
  1284. this_status = STATE_CRITICAL;
  1285. status = STATE_CRITICAL;
  1286. host->score_status = STATE_CRITICAL;
  1287. } else if (status != STATE_CRITICAL && (host->score <= warn.score)) {
  1288. this_status = (this_status <= STATE_WARNING ? STATE_WARNING : this_status);
  1289. status = STATE_WARNING;
  1290. host->score_status = STATE_WARNING;
  1291. }
  1292. }
  1293. if (this_status == STATE_WARNING) {
  1294. hosts_warn++;
  1295. }
  1296. else if (this_status == STATE_OK) {
  1297. hosts_ok++;
  1298. }
  1299. host = host->next;
  1300. }
  1301. /* this is inevitable */
  1302. if (!targets_alive) {
  1303. status = STATE_CRITICAL;
  1304. }
  1305. if (min_hosts_alive > -1) {
  1306. if (hosts_ok >= min_hosts_alive) {
  1307. status = STATE_OK;
  1308. } else if ((hosts_ok + hosts_warn) >= min_hosts_alive) {
  1309. status = STATE_WARNING;
  1310. }
  1311. }
  1312. printf("%s - ", status_string[status]);
  1313. host = list;
  1314. while (host) {
  1315. if (debug) {
  1316. puts("");
  1317. }
  1318. if (i) {
  1319. if (i < targets) {
  1320. printf(" :: ");
  1321. } else {
  1322. printf("\n");
  1323. }
  1324. }
  1325. i++;
  1326. if (!host->icmp_recv) {
  1327. status = STATE_CRITICAL;
  1328. host->rtmin = 0;
  1329. host->jitter_min = 0;
  1330. if (host->flags & FLAG_LOST_CAUSE) {
  1331. char address[address_length(address_family)];
  1332. parse_address_string(address_family, &host->error_addr, address,
  1333. sizeof(address));
  1334. printf("%s: %s @ %s. rta nan, lost %d%%", host->name,
  1335. get_icmp_error_msg(host->icmp_type, host->icmp_code), address,
  1336. 100);
  1337. } else {
  1338. /* not marked as lost cause, so we have no flags for it */
  1339. printf("%s: rta nan, lost 100%%", host->name);
  1340. }
  1341. } else {
  1342. /* !icmp_recv */
  1343. printf("%s:", host->name);
  1344. /* rta text output */
  1345. if (rta_mode) {
  1346. if (status == STATE_OK) {
  1347. printf(" rta %0.3fms", host->rta / 1000);
  1348. } else if (status == STATE_WARNING && host->rta_status == status) {
  1349. printf(" rta %0.3fms >= %0.3fms", (float)host->rta / 1000,
  1350. (float)warn.rta / 1000);
  1351. } else if (status == STATE_CRITICAL && host->rta_status == status) {
  1352. printf(" rta %0.3fms >= %0.3fms", (float)host->rta / 1000,
  1353. (float)crit.rta / 1000);
  1354. }
  1355. }
  1356. /* pl text output */
  1357. if (pl_mode) {
  1358. if (status == STATE_OK) {
  1359. printf(" lost %u%%", host->pl);
  1360. } else if (status == STATE_WARNING && host->pl_status == status) {
  1361. printf(" lost %u%% >= %u%%", host->pl, warn.pl);
  1362. } else if (status == STATE_CRITICAL && host->pl_status == status) {
  1363. printf(" lost %u%% >= %u%%", host->pl, crit.pl);
  1364. }
  1365. }
  1366. /* jitter text output */
  1367. if (jitter_mode) {
  1368. if (status == STATE_OK) {
  1369. printf(" jitter %0.3fms", (float)host->jitter);
  1370. } else if (status == STATE_WARNING && host->jitter_status == status) {
  1371. printf(" jitter %0.3fms >= %0.3fms", (float)host->jitter, warn.jitter);
  1372. } else if (status == STATE_CRITICAL && host->jitter_status == status) {
  1373. printf(" jitter %0.3fms >= %0.3fms", (float)host->jitter, crit.jitter);
  1374. }
  1375. }
  1376. /* mos text output */
  1377. if (mos_mode) {
  1378. if (status == STATE_OK) {
  1379. printf(" MOS %0.1f", (float)host->mos);
  1380. } else if (status == STATE_WARNING && host->mos_status == status) {
  1381. printf(" MOS %0.1f <= %0.1f", (float)host->mos, (float)warn.mos);
  1382. } else if (status == STATE_CRITICAL && host->mos_status == status) {
  1383. printf(" MOS %0.1f <= %0.1f", (float)host->mos, (float)crit.mos);
  1384. }
  1385. }
  1386. /* score text output */
  1387. if (score_mode) {
  1388. if (status == STATE_OK) {
  1389. printf(" Score %u", (int)host->score);
  1390. } else if (status == STATE_WARNING && host->score_status == status) {
  1391. printf(" Score %u <= %u", (int)host->score, (int)warn.score);
  1392. } else if (status == STATE_CRITICAL && host->score_status == status) {
  1393. printf(" Score %u <= %u", (int)host->score, (int)crit.score);
  1394. }
  1395. }
  1396. /* order statis text output */
  1397. if (order_mode) {
  1398. if (status == STATE_OK) {
  1399. printf(" Packets in order");
  1400. } else if (status == STATE_CRITICAL && host->order_status == status) {
  1401. printf(" Packets out of order");
  1402. }
  1403. }
  1404. }
  1405. host = host->next;
  1406. }
  1407. /* iterate once more for pretty perfparse output */
  1408. if (!(!rta_mode && !pl_mode && !jitter_mode && !score_mode && !mos_mode &&
  1409. order_mode)) {
  1410. printf("|");
  1411. }
  1412. i = 0;
  1413. host = list;
  1414. while (host) {
  1415. i++;
  1416. if (i > perfdata_num && perfdata_num > -1)
  1417. break;
  1418. if (debug) {
  1419. puts("");
  1420. }
  1421. if (rta_mode) {
  1422. printf("%s%srta=%0.3fms;%0.3f;%0.3f;0; ",
  1423. (targets > 1 || perfdata_sep != NULL) ? host->name : "",
  1424. (perfdata_sep != NULL) ? perfdata_sep : "",
  1425. (float)host->rta / 1000, (float)warn.rta / 1000, (float)crit.rta / 1000);
  1426. }
  1427. if (pl_mode) {
  1428. printf("%s%spl=%u%%;%u;%u;0;100 ",
  1429. (targets > 1 || perfdata_sep != NULL) ? host->name : "",
  1430. (perfdata_sep != NULL) ? perfdata_sep : "",
  1431. host->pl, warn.pl, crit.pl);
  1432. }
  1433. if (rta_mode) {
  1434. printf("%s%srtmax=%0.3fms;;;; %s%srtmin=%0.3fms;;;; ",
  1435. (targets > 1 || perfdata_sep != NULL) ? host->name : "",
  1436. (perfdata_sep != NULL) ? perfdata_sep : "",
  1437. (float)host->rtmax / 1000,
  1438. (targets > 1 || perfdata_sep != NULL) ? host->name : "",
  1439. (perfdata_sep != NULL) ? perfdata_sep : "",
  1440. (float)host->rtmin / 1000);
  1441. }
  1442. if (jitter_mode) {
  1443. printf("%s%sjitter_avg=%0.3fms;%0.3f;%0.3f;0; %s%sjitter_max=%0.3fms;;;; "
  1444. "%s%sjitter_min=%0.3fms;;;; ",
  1445. (targets > 1 || perfdata_sep != NULL) ? host->name : "",
  1446. (perfdata_sep != NULL) ? perfdata_sep : "",
  1447. (float)host->jitter, (float)warn.jitter, (float)crit.jitter,
  1448. (targets > 1 || perfdata_sep != NULL) ? host->name : "",
  1449. (perfdata_sep != NULL) ? perfdata_sep : "",
  1450. (float)host->jitter_max / 1000,
  1451. (targets > 1 || perfdata_sep != NULL) ? host->name : "",
  1452. (perfdata_sep != NULL) ? perfdata_sep : "",
  1453. (float)host->jitter_min / 1000);
  1454. }
  1455. if (mos_mode) {
  1456. printf("%s%smos=%0.1f;%0.1f;%0.1f;0;5 ",
  1457. (targets > 1 || perfdata_sep != NULL) ? host->name : "",
  1458. (perfdata_sep != NULL) ? perfdata_sep : "",
  1459. (float)host->mos, (float)warn.mos, (float)crit.mos);
  1460. }
  1461. if (score_mode) {
  1462. printf("%s%sscore=%u;%u;%u;0;100 ",
  1463. (targets > 1 || perfdata_sep != NULL) ? host->name : "",
  1464. (perfdata_sep != NULL) ? perfdata_sep : "",
  1465. (int)host->score, (int)warn.score, (int)crit.score);
  1466. }
  1467. host = host->next;
  1468. }
  1469. if (min_hosts_alive > -1) {
  1470. if (hosts_ok >= min_hosts_alive) {
  1471. status = STATE_OK;
  1472. } else if ((hosts_ok + hosts_warn) >= min_hosts_alive) {
  1473. status = STATE_WARNING;
  1474. }
  1475. }
  1476. /* finish with an empty line */
  1477. puts("");
  1478. if (debug) {
  1479. printf("targets: %u, targets_alive: %u, hosts_ok: %u, hosts_warn: %u, "
  1480. "min_hosts_alive: %i\n",
  1481. targets, targets_alive, hosts_ok, hosts_warn, min_hosts_alive);
  1482. }
  1483. exit(status);
  1484. }
  1485. static u_int get_timevaldiff(struct timeval *early, struct timeval *later) {
  1486. u_int ret;
  1487. struct timeval now;
  1488. if (!later) {
  1489. gettimeofday(&now, &tz);
  1490. later = &now;
  1491. }
  1492. if (!early)
  1493. early = &prog_start;
  1494. /* if early > later we return 0 so as to indicate a timeout */
  1495. if (early->tv_sec > later->tv_sec ||
  1496. (early->tv_sec == later->tv_sec && early->tv_usec > later->tv_usec)) {
  1497. return 0;
  1498. }
  1499. ret = (later->tv_sec - early->tv_sec) * 1000000;
  1500. ret += later->tv_usec - early->tv_usec;
  1501. return ret;
  1502. }
  1503. static int add_target_ip(char *arg, struct sockaddr_storage *in) {
  1504. struct rta_host *host;
  1505. struct sockaddr_in *sin = NULL, *host_sin;
  1506. struct sockaddr_in6 *sin6 = NULL, *host_sin6;
  1507. if (address_family == AF_INET) {
  1508. sin = (struct sockaddr_in *)in;
  1509. } else {
  1510. sin6 = (struct sockaddr_in6 *)in;
  1511. }
  1512. /* disregard some addresses */
  1513. if (((address_family == AF_INET && (sin->sin_addr.s_addr == INADDR_NONE ||
  1514. sin->sin_addr.s_addr == INADDR_ANY))) ||
  1515. (address_family == AF_INET6 &&
  1516. sin6->sin6_addr.s6_addr == in6addr_any.s6_addr)) {
  1517. return -1;
  1518. }
  1519. /* no point in adding two identical IP's, so don't. ;) */
  1520. host = list;
  1521. while (host) {
  1522. host_sin = (struct sockaddr_in *)&host->saddr_in;
  1523. host_sin6 = (struct sockaddr_in6 *)&host->saddr_in;
  1524. if ((address_family == AF_INET &&
  1525. host_sin->sin_addr.s_addr == sin->sin_addr.s_addr) ||
  1526. (address_family == AF_INET6 &&
  1527. host_sin6->sin6_addr.s6_addr == sin6->sin6_addr.s6_addr)) {
  1528. if (debug) {
  1529. printf("Identical IP already exists. Not adding %s\n", arg);
  1530. }
  1531. return -1;
  1532. }
  1533. host = host->next;
  1534. }
  1535. /* add the fresh ip */
  1536. host = (struct rta_host *)malloc(sizeof(struct rta_host));
  1537. if (!host) {
  1538. char address[address_length(address_family)];
  1539. parse_address_string(address_family, (struct sockaddr_storage *)&in,
  1540. address, sizeof(address));
  1541. crash("add_target_ip(%s, %s): malloc(%d) failed", arg, address,
  1542. sizeof(struct rta_host));
  1543. }
  1544. memset(host, 0, sizeof(struct rta_host));
  1545. /* set the values. use calling name for output */
  1546. host->name = strdup(arg);
  1547. /* fill out the sockaddr_storage struct */
  1548. if (address_family == AF_INET) {
  1549. host_sin = (struct sockaddr_in *)&host->saddr_in;
  1550. host_sin->sin_family = AF_INET;
  1551. host_sin->sin_addr.s_addr = sin->sin_addr.s_addr;
  1552. } else if (address_family == AF_INET6) {
  1553. host_sin6 = (struct sockaddr_in6 *)&host->saddr_in;
  1554. host_sin6->sin6_family = AF_INET6;
  1555. memcpy(host_sin6->sin6_addr.s6_addr, sin6->sin6_addr.s6_addr,
  1556. sizeof host_sin6->sin6_addr.s6_addr);
  1557. }
  1558. host->rtmin = DBL_MAX;
  1559. host->rtmax = 0;
  1560. host->jitter = 0;
  1561. host->jitter_max = 0;
  1562. host->jitter_min = DBL_MAX;
  1563. host->last_tdiff = 0;
  1564. host->order_status = STATE_OK;
  1565. host->last_icmp_seq = 0;
  1566. host->rta_status = 0;
  1567. host->pl_status = 0;
  1568. host->jitter_status = 0;
  1569. host->mos_status = 0;
  1570. host->score_status = 0;
  1571. host->pl_status = 0;
  1572. if (!list) {
  1573. list = cursor = host;
  1574. } else {
  1575. cursor->next = host;
  1576. }
  1577. cursor = host;
  1578. targets++;
  1579. return 0;
  1580. }
  1581. /* wrapper for add_target_ip */
  1582. static int add_target(char *arg) {
  1583. int error, result;
  1584. struct sockaddr_storage ip;
  1585. struct addrinfo hints, *res, *p;
  1586. struct sockaddr_in *sin;
  1587. struct sockaddr_in6 *sin6;
  1588. switch (address_family) {
  1589. case AF_INET:
  1590. sin = (struct sockaddr_in *)&ip;
  1591. result = inet_pton(address_family, arg, &sin->sin_addr);
  1592. break;
  1593. case AF_INET6:
  1594. sin6 = (struct sockaddr_in6 *)&ip;
  1595. result = inet_pton(address_family, arg, &sin6->sin6_addr);
  1596. break;
  1597. default:
  1598. crash("Address family not supported");
  1599. }
  1600. /* don't resolve if we don't have to */
  1601. if (result == 1) {
  1602. /* don't add all ip's if we were given a specific one */
  1603. return add_target_ip(arg, &ip);
  1604. } else {
  1605. errno = 0;
  1606. memset(&hints, 0, sizeof(hints));
  1607. hints.ai_family = address_family == AF_INET ? PF_INET : PF_INET6;
  1608. hints.ai_socktype = SOCK_RAW;
  1609. if ((error = getaddrinfo(arg, NULL, &hints, &res)) != 0) {
  1610. errno = 0;
  1611. crash("Failed to resolve %s: %s", arg, gai_strerror(error));
  1612. return -1;
  1613. }
  1614. }
  1615. /* possibly add all the IP's as targets */
  1616. for (p = res; p != NULL; p = p->ai_next) {
  1617. memcpy(&ip, p->ai_addr, p->ai_addrlen);
  1618. add_target_ip(arg, &ip);
  1619. /* this is silly, but it works */
  1620. if (mode == MODE_HOSTCHECK || mode == MODE_ALL) {
  1621. if (debug > 2) {
  1622. printf("mode: %d\n", mode);
  1623. }
  1624. continue;
  1625. }
  1626. break;
  1627. }
  1628. freeaddrinfo(res);
  1629. return 0;
  1630. }
  1631. static void set_source_ip(char *arg) {
  1632. struct sockaddr_storage src;
  1633. int result;
  1634. void *address_offset;
  1635. memset(&src, 0, sizeof(src));
  1636. src.ss_family = address_family;
  1637. if (address_family == AF_INET) {
  1638. struct sockaddr_in *src_ipv4 = (struct sockaddr_in *) &src;
  1639. address_offset = (void *) &src_ipv4->sin_addr.s_addr;
  1640. }
  1641. else if (address_family == AF_INET6) {
  1642. struct sockaddr_in6 *src_ipv6 = (struct sockaddr_in6 *) &src;
  1643. /* Note: s6_addr is already an array, unlike s_addr */
  1644. address_offset = (void *) src_ipv6->sin6_addr.s6_addr;
  1645. }
  1646. result = inet_pton(address_family, arg, address_offset);
  1647. if (result != 1) {
  1648. struct sockaddr_in *src_ipv4 = (struct sockaddr_in *) &src;
  1649. src_ipv4->sin_addr.s_addr = get_ip_address(arg);
  1650. }
  1651. if (bind(icmp_sock, (struct sockaddr *)&src, sizeof(src)) == -1) {
  1652. crash("Cannot bind to IP address %s", arg);
  1653. }
  1654. }
  1655. /* TODO: Move this to netutils.c and also change check_dhcp to use that. */
  1656. static in_addr_t get_ip_address(const char *ifname) {
  1657. #if defined(SIOCGIFADDR)
  1658. struct ifreq ifr;
  1659. struct sockaddr_in ip;
  1660. strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  1661. ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
  1662. if (ioctl(icmp_sock, SIOCGIFADDR, &ifr) == -1) {
  1663. crash("Cannot determine IP address of interface %s", ifname);
  1664. }
  1665. memcpy(&ip, &ifr.ifr_addr, sizeof(ip));
  1666. return ip.sin_addr.s_addr;
  1667. #else
  1668. errno = 0;
  1669. crash("Cannot get interface IP address on this platform.");
  1670. #endif
  1671. }
  1672. /*
  1673. * u = micro
  1674. * m = milli
  1675. * s = seconds
  1676. * return value is in microseconds
  1677. */
  1678. static u_int get_timevar(const char *str) {
  1679. char p, u, *ptr;
  1680. size_t len;
  1681. u_int i, d; /* integer and decimal, respectively */
  1682. u_int factor = 1000; /* default to milliseconds */
  1683. if (!str) {
  1684. return 0;
  1685. }
  1686. len = strlen(str);
  1687. if (!len) {
  1688. return 0;
  1689. }
  1690. /* unit might be given as ms|m (millisec),
  1691. * us|u (microsec) or just plain s, for seconds */
  1692. p = '\0';
  1693. u = str[len - 1];
  1694. if (len >= 2 && !isdigit((int)str[len - 2])) {
  1695. p = str[len - 2];
  1696. }
  1697. if (p && u == 's') {
  1698. u = p;
  1699. } else if (!p) {
  1700. p = u;
  1701. }
  1702. if (debug > 2) {
  1703. printf("evaluating %s, u: %c, p: %c\n", str, u, p);
  1704. }
  1705. if (u == 'u') {
  1706. /* microseconds */
  1707. factor = 1;
  1708. } else if (u == 'm') {
  1709. /* milliseconds */
  1710. factor = 1000;
  1711. } else if (u == 's') {
  1712. /* seconds */
  1713. factor = 1000000;
  1714. }
  1715. if (debug > 2) {
  1716. printf("factor is %u\n", factor);
  1717. }
  1718. i = strtoul(str, &ptr, 0);
  1719. if (!ptr || *ptr != '.' || strlen(ptr) < 2 || factor == 1) {
  1720. return i * factor;
  1721. }
  1722. /* time specified in usecs can't have decimal points, so ignore them */
  1723. if (factor == 1) {
  1724. return i;
  1725. }
  1726. d = strtoul(ptr + 1, NULL, 0);
  1727. /* d is decimal, so get rid of excess digits */
  1728. while (d >= factor)
  1729. d /= 10;
  1730. /* the last parenthesis avoids floating point exceptions. */
  1731. return ((i * factor) + (d * (factor / 10)));
  1732. }
  1733. /* not too good at checking errors, but it'll do (main() should barfe on -1) */
  1734. static int get_threshold(char *str, threshold *th) {
  1735. char *p = NULL, i = 0;
  1736. if (!str || !strlen(str) || !th) {
  1737. return -1;
  1738. }
  1739. /* pointer magic slims code by 10 lines. i is bof-stop on stupid libc's */
  1740. p = &str[strlen(str) - 1];
  1741. while (p != &str[1]) {
  1742. if (*p == '%') {
  1743. *p = '\0';
  1744. } else if (*p == ',' && i) {
  1745. *p = '\0'; /* reset it so get_timevar(str) works nicely later */
  1746. th->pl = (unsigned char)strtoul(p + 1, NULL, 0);
  1747. break;
  1748. }
  1749. i = 1;
  1750. p--;
  1751. }
  1752. th->rta = get_timevar(str);
  1753. if (!th->rta) {
  1754. return -1;
  1755. }
  1756. if (th->rta > MAXTTL * 1000000) {
  1757. th->rta = MAXTTL * 1000000;
  1758. }
  1759. if (th->pl > 100) {
  1760. th->pl = 100;
  1761. }
  1762. return 0;
  1763. }
  1764. /* not too good at checking errors, but it'll do (main() should barfe on -1) */
  1765. static int get_threshold2(char *str, threshold *warn, threshold *crit,
  1766. int type) {
  1767. char *p = NULL, i = 0;
  1768. if (!str || !strlen(str) || !warn || !crit) {
  1769. return -1;
  1770. }
  1771. /* pointer magic slims code by 10 lines. i is bof-stop on stupid libc's */
  1772. p = &str[strlen(str) - 1];
  1773. while (p != &str[0]) {
  1774. if ((*p == 'm') || (*p == '%')) {
  1775. *p = '\0';
  1776. } else if (*p == ',' && i) {
  1777. *p = '\0'; /* reset it so get_timevar(str) works nicely later */
  1778. if (type == 1) {
  1779. crit->rta = atof(p + 1) * 1000;
  1780. } else if (type == 2) {
  1781. crit->pl = (unsigned char)strtoul(p + 1, NULL, 0);
  1782. } else if (type == 3) {
  1783. crit->jitter = atof(p + 1);
  1784. } else if (type == 4) {
  1785. crit->mos = atof(p + 1);
  1786. } else if (type == 5) {
  1787. crit->score = atof(p + 1);
  1788. }
  1789. }
  1790. i = 1;
  1791. p--;
  1792. }
  1793. if (type == 1) {
  1794. warn->rta = atof(p) * 1000;
  1795. } else if (type == 2) {
  1796. warn->pl = (unsigned char)strtoul(p, NULL, 0);
  1797. }
  1798. if (type == 3) {
  1799. warn->jitter = atof(p);
  1800. } else if (type == 4) {
  1801. warn->mos = atof(p);
  1802. } else if (type == 5) {
  1803. warn->score = atof(p);
  1804. }
  1805. return 0;
  1806. }
  1807. unsigned short icmp_checksum(unsigned short *p, int n) {
  1808. unsigned short cksum;
  1809. long sum = 0;
  1810. while (n > 2) {
  1811. sum += *p++;
  1812. n -= sizeof(unsigned short);
  1813. }
  1814. /* mop up the occasional odd byte */
  1815. if (n == 1) {
  1816. sum += (unsigned char)*p;
  1817. }
  1818. sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
  1819. sum += (sum >> 16); /* add carry */
  1820. cksum = ~sum; /* ones-complement, trunc to 16 bits */
  1821. return cksum;
  1822. }
  1823. void print_help(void) {
  1824. print_revision(progname, NP_VERSION);
  1825. printf(COPYRIGHT, copyright, email);
  1826. print_usage();
  1827. printf(UT_HELP_VRSN);
  1828. printf(UT_EXTRA_OPTS);
  1829. printf(" %s\n", "-w");
  1830. printf(" %s", _("warning threshold (currently "));
  1831. printf("%0.3fms,%u%%)\n", (float)warn.rta / 1000, warn.pl);
  1832. printf(" %s\n", "-c");
  1833. printf(" %s", _("critical threshold (currently "));
  1834. printf("%0.3fms,%u%%)\n", (float)crit.rta / 1000, crit.pl);
  1835. printf(" %s\n", "-R");
  1836. printf(" %s\n", _("RTA, round trip average, mode warning,critical, ex. "
  1837. "100ms,200ms unit in ms"));
  1838. printf(" %s\n", "-P");
  1839. printf(" %s\n", _("packet loss mode, ex. 40%,50% , unit in %"));
  1840. printf(" %s\n", "-J");
  1841. printf(
  1842. " %s\n",
  1843. _("jitter mode warning,critical, ex. 40.000ms,50.000ms , unit in ms "));
  1844. printf(" %s\n", "-M");
  1845. printf(" %s\n",
  1846. _("MOS mode, between 0 and 4.4 warning,critical, ex. 3.5,3.0"));
  1847. printf(" %s\n", "-S");
  1848. printf(" %s\n",
  1849. _("score mode, max value 100 warning,critical, ex. 80,70 "));
  1850. printf(" %s\n", "-O");
  1851. printf(" %s\n", _("detect out of order ICMP packts "));
  1852. printf(" %s\n", "-4");
  1853. printf(" %s\n", _("target address(es) are IPv4 and packets are ICMPv4"));
  1854. printf(" %s\n", "-6");
  1855. printf(" %s\n", _("target address(es) are IPv6 and packets are ICMPv6"));
  1856. printf(" %s\n", "-H");
  1857. printf(" %s\n", _("specify a target"));
  1858. printf(" %s\n", "-s");
  1859. printf(" %s\n", _("specify a source IP address or device name"));
  1860. printf(" %s\n", "-n");
  1861. printf(" %s", _("number of packets to send (currently "));
  1862. printf("%u)\n", packets);
  1863. printf(" %s\n", "-p");
  1864. printf(" %s", _("number of packets to send (currently "));
  1865. printf("%u)\n", packets);
  1866. printf(" %s\n", "-i");
  1867. printf(" %s", _("max packet interval (currently "));
  1868. printf("%0.3fms)\n", (float)pkt_interval / 1000);
  1869. printf(" %s\n", "-I");
  1870. printf(" %s", _("max target interval (currently "));
  1871. printf("%0.3fms)\n", (float)target_interval / 1000);
  1872. printf(" %s\n", "-m");
  1873. printf(" %s", _("number of alive hosts required for success"));
  1874. printf("\n");
  1875. printf(" %s\n", "-l");
  1876. printf(" %s", _("TTL on outgoing packets (currently "));
  1877. printf("%u)\n", ttl);
  1878. printf(" %s\n", "-t");
  1879. printf(" %s", _("timeout value (seconds, currently "));
  1880. printf("%u)\n", timeout);
  1881. printf(" %s\n", "-b");
  1882. printf(" %s\n", _("Number of icmp data bytes to send"));
  1883. printf(" %s %u + %d)\n",
  1884. _("Packet size will be data bytes + icmp header (currently"),
  1885. icmp_data_size, ICMP_MINLEN);
  1886. printf(" %s\n", "-f");
  1887. printf(" %s\n", _("separator for perfdata instance"));
  1888. printf(" %s\n", "-F");
  1889. printf(" %s\n", _("number of instances to output perfdata for"));
  1890. printf(" %s\n", "-v");
  1891. printf(" %s\n", _("verbose"));
  1892. printf("\n");
  1893. printf("%s\n", _("Notes:"));
  1894. printf(" %s\n", _("If not mode R,P,J,M,S or O is informed, default icmp "
  1895. "behavior, RTA and packet loss"));
  1896. printf("\n");
  1897. printf(" %s\n", _("The -H switch is optional. Naming a host (or several) to "
  1898. "check is not."));
  1899. printf("\n");
  1900. printf(" %s\n", _("When defining multiple addresses they must be provided as "
  1901. "the last argument."));
  1902. printf("\n");
  1903. printf(" %s\n", _("Threshold format for -w and -c is 200.25,60% for 200.25 "
  1904. "msec RTA and 60%"));
  1905. printf(
  1906. " %s\n",
  1907. _("packet loss. The default values should work well for most users."));
  1908. printf(" %s\n", _("You can specify different RTA factors using the "
  1909. "standardized abbreviations"));
  1910. printf(" %s\n", _("us (microseconds), ms (milliseconds, default) or just "
  1911. "plain s for seconds."));
  1912. /* -d not yet implemented */
  1913. /* printf ("%s\n", _("Threshold format for -d is warn,crit. 12,14 means
  1914. WARNING if >= 12 hops")); printf ("%s\n", _("are spent and CRITICAL if >= 14
  1915. hops are spent.")); printf ("%s\n\n", _("NOTE: Some systems decrease TTL
  1916. when forming ICMP_ECHOREPLY, others do not."));*/
  1917. printf("\n");
  1918. printf(" %s\n", _("The -v switch can be specified several times for "
  1919. "increased verbosity."));
  1920. /* printf ("%s\n", _("Long options are currently unsupported."));*/
  1921. /* printf ("%s\n", _("Options marked with * require an argument"));*/
  1922. printf(UT_SUPPORT);
  1923. }
  1924. void print_usage(void) {
  1925. printf("%s\n", _("Usage:"));
  1926. printf(" %s [options] [-H] host1 host2 hostN\n", progname);
  1927. }