check_icmp.c 51 KB

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