check_icmp.c 61 KB

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