check_icmp.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  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 && host->pl < 100) {
  1409. printf("%srta=%0.3fms;%0.3f;%0.3f;0; %srtmax=%0.3fms;;;; "
  1410. "%srtmin=%0.3fms;;;; ",
  1411. (targets > 1) ? host->name : "", (float)host->rta / 1000,
  1412. (float)warn.rta / 1000, (float)crit.rta / 1000,
  1413. (targets > 1) ? host->name : "", (float)host->rtmax / 1000,
  1414. (targets > 1) ? host->name : "", (float)host->rtmin / 1000);
  1415. }
  1416. if (pl_mode) {
  1417. printf("%spl=%u%%;%u;%u;0;100 ", (targets > 1) ? host->name : "",
  1418. host->pl, warn.pl, crit.pl);
  1419. }
  1420. if (jitter_mode && host->pl < 100) {
  1421. printf("%sjitter_avg=%0.3fms;%0.3f;%0.3f;0; %sjitter_max=%0.3fms;;;; "
  1422. "%sjitter_min=%0.3fms;;;; ",
  1423. (targets > 1) ? host->name : "", (float)host->jitter,
  1424. (float)warn.jitter, (float)crit.jitter,
  1425. (targets > 1) ? host->name : "", (float)host->jitter_max / 1000,
  1426. (targets > 1) ? host->name : "", (float)host->jitter_min / 1000);
  1427. }
  1428. if (mos_mode && host->pl < 100) {
  1429. printf("%smos=%0.1f;%0.1f;%0.1f;0;5 ", (targets > 1) ? host->name : "",
  1430. (float)host->mos, (float)warn.mos, (float)crit.mos);
  1431. }
  1432. if (score_mode && host->pl < 100) {
  1433. printf("%sscore=%u;%u;%u;0;100 ", (targets > 1) ? host->name : "",
  1434. (int)host->score, (int)warn.score, (int)crit.score);
  1435. }
  1436. host = host->next;
  1437. }
  1438. if (min_hosts_alive > -1) {
  1439. if (hosts_ok >= min_hosts_alive) {
  1440. status = STATE_OK;
  1441. } else if ((hosts_ok + hosts_warn) >= min_hosts_alive) {
  1442. status = STATE_WARNING;
  1443. }
  1444. }
  1445. /* finish with an empty line */
  1446. puts("");
  1447. if (debug) {
  1448. printf("targets: %u, targets_alive: %u, hosts_ok: %u, hosts_warn: %u, "
  1449. "min_hosts_alive: %i\n",
  1450. targets, targets_alive, hosts_ok, hosts_warn, min_hosts_alive);
  1451. }
  1452. exit(status);
  1453. }
  1454. static u_int get_timevaldiff(struct timeval *early, struct timeval *later) {
  1455. u_int ret;
  1456. struct timeval now;
  1457. if (!later) {
  1458. gettimeofday(&now, &tz);
  1459. later = &now;
  1460. }
  1461. if (!early)
  1462. early = &prog_start;
  1463. /* if early > later we return 0 so as to indicate a timeout */
  1464. if (early->tv_sec > later->tv_sec ||
  1465. (early->tv_sec == later->tv_sec && early->tv_usec > later->tv_usec)) {
  1466. return 0;
  1467. }
  1468. ret = (later->tv_sec - early->tv_sec) * 1000000;
  1469. ret += later->tv_usec - early->tv_usec;
  1470. return ret;
  1471. }
  1472. static int add_target_ip(char *arg, struct sockaddr_storage *in) {
  1473. struct rta_host *host;
  1474. struct sockaddr_in *sin, *host_sin;
  1475. struct sockaddr_in6 *sin6, *host_sin6;
  1476. if (address_family == AF_INET) {
  1477. sin = (struct sockaddr_in *)in;
  1478. } else {
  1479. sin6 = (struct sockaddr_in6 *)in;
  1480. }
  1481. /* disregard some addresses */
  1482. if (((address_family == AF_INET && (sin->sin_addr.s_addr == INADDR_NONE ||
  1483. sin->sin_addr.s_addr == INADDR_ANY))) ||
  1484. (address_family == AF_INET6 &&
  1485. sin6->sin6_addr.s6_addr == in6addr_any.s6_addr)) {
  1486. return -1;
  1487. }
  1488. /* no point in adding two identical IP's, so don't. ;) */
  1489. host = list;
  1490. while (host) {
  1491. host_sin = (struct sockaddr_in *)&host->saddr_in;
  1492. host_sin6 = (struct sockaddr_in6 *)&host->saddr_in;
  1493. if ((address_family == AF_INET &&
  1494. host_sin->sin_addr.s_addr == sin->sin_addr.s_addr) ||
  1495. (address_family == AF_INET6 &&
  1496. host_sin6->sin6_addr.s6_addr == sin6->sin6_addr.s6_addr)) {
  1497. if (debug) {
  1498. printf("Identical IP already exists. Not adding %s\n", arg);
  1499. }
  1500. return -1;
  1501. }
  1502. host = host->next;
  1503. }
  1504. /* add the fresh ip */
  1505. host = (struct rta_host *)malloc(sizeof(struct rta_host));
  1506. if (!host) {
  1507. char address[address_length(address_family)];
  1508. parse_address_string(address_family, (struct sockaddr_storage *)&in,
  1509. address, sizeof(address));
  1510. crash("add_target_ip(%s, %s): malloc(%d) failed", arg, address,
  1511. sizeof(struct rta_host));
  1512. }
  1513. memset(host, 0, sizeof(struct rta_host));
  1514. /* set the values. use calling name for output */
  1515. host->name = strdup(arg);
  1516. /* fill out the sockaddr_storage struct */
  1517. if (address_family == AF_INET) {
  1518. host_sin = (struct sockaddr_in *)&host->saddr_in;
  1519. host_sin->sin_family = AF_INET;
  1520. host_sin->sin_addr.s_addr = sin->sin_addr.s_addr;
  1521. } else if (address_family == AF_INET6) {
  1522. host_sin6 = (struct sockaddr_in6 *)&host->saddr_in;
  1523. host_sin6->sin6_family = AF_INET6;
  1524. memcpy(host_sin6->sin6_addr.s6_addr, sin6->sin6_addr.s6_addr,
  1525. sizeof host_sin6->sin6_addr.s6_addr);
  1526. }
  1527. host->rtmin = DBL_MAX;
  1528. host->rtmax = 0;
  1529. host->jitter = 0;
  1530. host->jitter_max = 0;
  1531. host->jitter_min = DBL_MAX;
  1532. host->last_tdiff = 0;
  1533. host->order_status = STATE_OK;
  1534. host->last_icmp_seq = 0;
  1535. host->rta_status = 0;
  1536. host->pl_status = 0;
  1537. host->jitter_status = 0;
  1538. host->mos_status = 0;
  1539. host->score_status = 0;
  1540. host->pl_status = 0;
  1541. if (!list) {
  1542. list = cursor = host;
  1543. } else {
  1544. cursor->next = host;
  1545. }
  1546. cursor = host;
  1547. targets++;
  1548. return 0;
  1549. }
  1550. /* wrapper for add_target_ip */
  1551. static int add_target(char *arg) {
  1552. int error, result;
  1553. struct sockaddr_storage ip;
  1554. struct addrinfo hints, *res, *p;
  1555. struct sockaddr_in *sin;
  1556. struct sockaddr_in6 *sin6;
  1557. switch (address_family) {
  1558. case AF_INET:
  1559. sin = (struct sockaddr_in *)&ip;
  1560. result = inet_pton(address_family, arg, &sin->sin_addr);
  1561. break;
  1562. case AF_INET6:
  1563. sin6 = (struct sockaddr_in6 *)&ip;
  1564. result = inet_pton(address_family, arg, &sin6->sin6_addr);
  1565. break;
  1566. default:
  1567. crash("Address family not supported");
  1568. }
  1569. /* don't resolve if we don't have to */
  1570. if (result == 1) {
  1571. /* don't add all ip's if we were given a specific one */
  1572. return add_target_ip(arg, &ip);
  1573. } else {
  1574. errno = 0;
  1575. memset(&hints, 0, sizeof(hints));
  1576. hints.ai_family = address_family == AF_INET ? PF_INET : PF_INET6;
  1577. hints.ai_socktype = SOCK_RAW;
  1578. if ((error = getaddrinfo(arg, NULL, &hints, &res)) != 0) {
  1579. errno = 0;
  1580. crash("Failed to resolve %s: %s", arg, gai_strerror(error));
  1581. return -1;
  1582. }
  1583. }
  1584. /* possibly add all the IP's as targets */
  1585. for (p = res; p != NULL; p = p->ai_next) {
  1586. memcpy(&ip, p->ai_addr, p->ai_addrlen);
  1587. add_target_ip(arg, &ip);
  1588. /* this is silly, but it works */
  1589. if (mode == MODE_HOSTCHECK || mode == MODE_ALL) {
  1590. if (debug > 2) {
  1591. printf("mode: %d\n", mode);
  1592. }
  1593. continue;
  1594. }
  1595. break;
  1596. }
  1597. freeaddrinfo(res);
  1598. return 0;
  1599. }
  1600. static void set_source_ip(char *arg) {
  1601. struct sockaddr_in src;
  1602. memset(&src, 0, sizeof(src));
  1603. src.sin_family = address_family;
  1604. if ((src.sin_addr.s_addr = inet_addr(arg)) == INADDR_NONE) {
  1605. src.sin_addr.s_addr = get_ip_address(arg);
  1606. }
  1607. if (bind(icmp_sock, (struct sockaddr *)&src, sizeof(src)) == -1) {
  1608. crash("Cannot bind to IP address %s", arg);
  1609. }
  1610. }
  1611. /* TODO: Move this to netutils.c and also change check_dhcp to use that. */
  1612. static in_addr_t get_ip_address(const char *ifname) {
  1613. #if defined(SIOCGIFADDR)
  1614. struct ifreq ifr;
  1615. struct sockaddr_in ip;
  1616. strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  1617. ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
  1618. if (ioctl(icmp_sock, SIOCGIFADDR, &ifr) == -1) {
  1619. crash("Cannot determine IP address of interface %s", ifname);
  1620. }
  1621. memcpy(&ip, &ifr.ifr_addr, sizeof(ip));
  1622. return ip.sin_addr.s_addr;
  1623. #else
  1624. errno = 0;
  1625. crash("Cannot get interface IP address on this platform.");
  1626. #endif
  1627. }
  1628. /*
  1629. * u = micro
  1630. * m = milli
  1631. * s = seconds
  1632. * return value is in microseconds
  1633. */
  1634. static u_int get_timevar(const char *str) {
  1635. char p, u, *ptr;
  1636. size_t len;
  1637. u_int i, d; /* integer and decimal, respectively */
  1638. u_int factor = 1000; /* default to milliseconds */
  1639. if (!str) {
  1640. return 0;
  1641. }
  1642. len = strlen(str);
  1643. if (!len) {
  1644. return 0;
  1645. }
  1646. /* unit might be given as ms|m (millisec),
  1647. * us|u (microsec) or just plain s, for seconds */
  1648. p = '\0';
  1649. u = str[len - 1];
  1650. if (len >= 2 && !isdigit((int)str[len - 2])) {
  1651. p = str[len - 2];
  1652. }
  1653. if (p && u == 's') {
  1654. u = p;
  1655. } else if (!p) {
  1656. p = u;
  1657. }
  1658. if (debug > 2) {
  1659. printf("evaluating %s, u: %c, p: %c\n", str, u, p);
  1660. }
  1661. if (u == 'u') {
  1662. /* microseconds */
  1663. factor = 1;
  1664. } else if (u == 'm') {
  1665. /* milliseconds */
  1666. factor = 1000;
  1667. } else if (u == 's') {
  1668. /* seconds */
  1669. factor = 1000000;
  1670. }
  1671. if (debug > 2) {
  1672. printf("factor is %u\n", factor);
  1673. }
  1674. i = strtoul(str, &ptr, 0);
  1675. if (!ptr || *ptr != '.' || strlen(ptr) < 2 || factor == 1) {
  1676. return i * factor;
  1677. }
  1678. /* time specified in usecs can't have decimal points, so ignore them */
  1679. if (factor == 1) {
  1680. return i;
  1681. }
  1682. d = strtoul(ptr + 1, NULL, 0);
  1683. /* d is decimal, so get rid of excess digits */
  1684. while (d >= factor)
  1685. d /= 10;
  1686. /* the last parenthesis avoids floating point exceptions. */
  1687. return ((i * factor) + (d * (factor / 10)));
  1688. }
  1689. /* not too good at checking errors, but it'll do (main() should barfe on -1) */
  1690. static int get_threshold(char *str, threshold *th) {
  1691. char *p = NULL, i = 0;
  1692. if (!str || !strlen(str) || !th) {
  1693. return -1;
  1694. }
  1695. /* pointer magic slims code by 10 lines. i is bof-stop on stupid libc's */
  1696. p = &str[strlen(str) - 1];
  1697. while (p != &str[1]) {
  1698. if (*p == '%') {
  1699. *p = '\0';
  1700. } else if (*p == ',' && i) {
  1701. *p = '\0'; /* reset it so get_timevar(str) works nicely later */
  1702. th->pl = (unsigned char)strtoul(p + 1, NULL, 0);
  1703. break;
  1704. }
  1705. i = 1;
  1706. p--;
  1707. }
  1708. th->rta = get_timevar(str);
  1709. if (!th->rta) {
  1710. return -1;
  1711. }
  1712. if (th->rta > MAXTTL * 1000000) {
  1713. th->rta = MAXTTL * 1000000;
  1714. }
  1715. if (th->pl > 100) {
  1716. th->pl = 100;
  1717. }
  1718. return 0;
  1719. }
  1720. /* not too good at checking errors, but it'll do (main() should barfe on -1) */
  1721. static int get_threshold2(char *str, threshold *warn, threshold *crit,
  1722. int type) {
  1723. char *p = NULL, i = 0;
  1724. if (!str || !strlen(str) || !warn || !crit) {
  1725. return -1;
  1726. }
  1727. /* pointer magic slims code by 10 lines. i is bof-stop on stupid libc's */
  1728. p = &str[strlen(str) - 1];
  1729. while (p != &str[0]) {
  1730. if ((*p == 'm') || (*p == '%')) {
  1731. *p = '\0';
  1732. } else if (*p == ',' && i) {
  1733. *p = '\0'; /* reset it so get_timevar(str) works nicely later */
  1734. if (type == 1) {
  1735. crit->rta = atof(p + 1) * 1000;
  1736. } else if (type == 2) {
  1737. crit->pl = (unsigned char)strtoul(p + 1, NULL, 0);
  1738. } else if (type == 3) {
  1739. crit->jitter = atof(p + 1);
  1740. } else if (type == 4) {
  1741. crit->mos = atof(p + 1);
  1742. } else if (type == 5) {
  1743. crit->score = atof(p + 1);
  1744. }
  1745. }
  1746. i = 1;
  1747. p--;
  1748. }
  1749. if (type == 1) {
  1750. warn->rta = atof(p) * 1000;
  1751. } else if (type == 2) {
  1752. warn->pl = (unsigned char)strtoul(p, NULL, 0);
  1753. }
  1754. if (type == 3) {
  1755. warn->jitter = atof(p);
  1756. } else if (type == 4) {
  1757. warn->mos = atof(p);
  1758. } else if (type == 5) {
  1759. warn->score = atof(p);
  1760. }
  1761. return 0;
  1762. }
  1763. unsigned short icmp_checksum(unsigned short *p, int n) {
  1764. unsigned short cksum;
  1765. long sum = 0;
  1766. while (n > 2) {
  1767. sum += *p++;
  1768. n -= sizeof(unsigned short);
  1769. }
  1770. /* mop up the occasional odd byte */
  1771. if (n == 1) {
  1772. sum += (unsigned char)*p;
  1773. }
  1774. sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
  1775. sum += (sum >> 16); /* add carry */
  1776. cksum = ~sum; /* ones-complement, trunc to 16 bits */
  1777. return cksum;
  1778. }
  1779. void print_help(void) {
  1780. print_revision(progname, NP_VERSION);
  1781. printf(COPYRIGHT, copyright, email);
  1782. print_usage();
  1783. printf(UT_HELP_VRSN);
  1784. printf(UT_EXTRA_OPTS);
  1785. printf(" %s\n", "-w");
  1786. printf(" %s", _("warning threshold (currently "));
  1787. printf("%0.3fms,%u%%)\n", (float)warn.rta / 1000, warn.pl);
  1788. printf(" %s\n", "-c");
  1789. printf(" %s", _("critical threshold (currently "));
  1790. printf("%0.3fms,%u%%)\n", (float)crit.rta / 1000, crit.pl);
  1791. printf(" %s\n", "-R");
  1792. printf(" %s\n", _("RTA, round trip average, mode warning,critical, ex. "
  1793. "100ms,200ms unit in ms"));
  1794. printf(" %s\n", "-P");
  1795. printf(" %s\n", _("packet loss mode, ex. 40%,50% , unit in %"));
  1796. printf(" %s\n", "-J");
  1797. printf(
  1798. " %s\n",
  1799. _("jitter mode warning,critical, ex. 40.000ms,50.000ms , unit in ms "));
  1800. printf(" %s\n", "-M");
  1801. printf(" %s\n",
  1802. _("MOS mode, between 0 and 4.4 warning,critical, ex. 3.5,3.0"));
  1803. printf(" %s\n", "-S");
  1804. printf(" %s\n",
  1805. _("score mode, max value 100 warning,critical, ex. 80,70 "));
  1806. printf(" %s\n", "-O");
  1807. printf(" %s\n", _("detect out of order ICMP packts "));
  1808. printf(" %s\n", "-4");
  1809. printf(" %s\n", _("target address(es) are IPv4 and packets are ICMPv4"));
  1810. printf(" %s\n", "-6");
  1811. printf(" %s\n", _("target address(es) are IPv6 and packets are ICMPv6"));
  1812. printf(" %s\n", "-H");
  1813. printf(" %s\n", _("specify a target"));
  1814. printf(" %s\n", "-s");
  1815. printf(" %s\n", _("specify a source IP address or device name"));
  1816. printf(" %s\n", "-n");
  1817. printf(" %s", _("number of packets to send (currently "));
  1818. printf("%u)\n", packets);
  1819. printf(" %s\n", "-p");
  1820. printf(" %s", _("number of packets to send (currently "));
  1821. printf("%u)\n", packets);
  1822. printf(" %s\n", "-i");
  1823. printf(" %s", _("max packet interval (currently "));
  1824. printf("%0.3fms)\n", (float)pkt_interval / 1000);
  1825. printf(" %s\n", "-I");
  1826. printf(" %s", _("max target interval (currently "));
  1827. printf("%0.3fms)\n", (float)target_interval / 1000);
  1828. printf(" %s\n", "-m");
  1829. printf(" %s", _("number of alive hosts required for success"));
  1830. printf("\n");
  1831. printf(" %s\n", "-l");
  1832. printf(" %s", _("TTL on outgoing packets (currently "));
  1833. printf("%u)\n", ttl);
  1834. printf(" %s\n", "-t");
  1835. printf(" %s", _("timeout value (seconds, currently "));
  1836. printf("%u)\n", timeout);
  1837. printf(" %s\n", "-b");
  1838. printf(" %s\n", _("Number of icmp data bytes to send"));
  1839. printf(" %s %u + %d)\n",
  1840. _("Packet size will be data bytes + icmp header (currently"),
  1841. icmp_data_size, ICMP_MINLEN);
  1842. printf(" %s\n", "-v");
  1843. printf(" %s\n", _("verbose"));
  1844. printf("\n");
  1845. printf("%s\n", _("Notes:"));
  1846. printf(" %s\n", _("If not mode R,P,J,M,S or O is informed, default icmp "
  1847. "behavior, RTA and packet loss"));
  1848. printf("\n");
  1849. printf(" %s\n", _("The -H switch is optional. Naming a host (or several) to "
  1850. "check is not."));
  1851. printf("\n");
  1852. printf(" %s\n", _("When defining multiple addresses they must be provided as "
  1853. "the last argument."));
  1854. printf("\n");
  1855. printf(" %s\n", _("Threshold format for -w and -c is 200.25,60% for 200.25 "
  1856. "msec RTA and 60%"));
  1857. printf(
  1858. " %s\n",
  1859. _("packet loss. The default values should work well for most users."));
  1860. printf(" %s\n", _("You can specify different RTA factors using the "
  1861. "standardized abbreviations"));
  1862. printf(" %s\n", _("us (microseconds), ms (milliseconds, default) or just "
  1863. "plain s for seconds."));
  1864. /* -d not yet implemented */
  1865. /* printf ("%s\n", _("Threshold format for -d is warn,crit. 12,14 means
  1866. WARNING if >= 12 hops")); printf ("%s\n", _("are spent and CRITICAL if >= 14
  1867. hops are spent.")); printf ("%s\n\n", _("NOTE: Some systems decrease TTL
  1868. when forming ICMP_ECHOREPLY, others do not."));*/
  1869. printf("\n");
  1870. printf(" %s\n", _("The -v switch can be specified several times for "
  1871. "increased verbosity."));
  1872. /* printf ("%s\n", _("Long options are currently unsupported."));*/
  1873. /* printf ("%s\n", _("Options marked with * require an argument"));*/
  1874. printf(UT_SUPPORT);
  1875. }
  1876. void print_usage(void) {
  1877. printf("%s\n", _("Usage:"));
  1878. printf(" %s [options] [-H] host1 host2 hostN\n", progname);
  1879. }