1 /*
2 * $Id$
3 *
4 * This is a hack of fping2 made to work with nagios.
5 * It's fast and removes the necessity of parsing another programs output.
6 *
7 * VIEWING NOTES:
8 * This file was formatted with tab indents at a tab stop of 4.
9 *
10 * It is highly recommended that your editor is set to this
11 * tab stop setting for viewing and editing.
12 *
13 * COPYLEFT;
14 * This programs copyright status is currently undetermined. Much of
15 * the code in it comes from the fping2 program which used to be licensed
16 * under the Stanford General Software License (available at
17 * http://graphics.stanford.edu/software/license.html). It is presently
18 * unclear what license (if any) applies to the original code at the
19 * moment.
20 *
21 * The fping website can be found at http://www.fping.com
22 */
24 const char *progname = "check_icmp";
25 const char *revision = "$Revision$";
26 const char *copyright = "2004";
27 const char *email = "nagiosplug-devel@lists.sourceforge.net";
29 #include "common.h"
30 #include "netutils.h"
31 #include "utils.h"
33 #include <stdio.h>
34 #include <errno.h>
35 #include <time.h>
36 #include <signal.h>
38 #include <unistd.h>
40 #include <stdlib.h>
42 #include <string.h>
43 #include <stddef.h>
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <sys/socket.h>
49 #include <sys/file.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/in.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_icmp.h>
57 #include <arpa/inet.h>
58 #include <netdb.h>
60 /* RS6000 has sys/select.h */
61 #ifdef HAVE_SYS_SELECT_H
62 #include <sys/select.h>
63 #endif /* HAVE_SYS_SELECT_H */
65 /* rta threshold values can't be larger than MAXTTL seconds */
66 #ifndef MAXTTL
67 # define MAXTTL 255
68 #endif
69 #ifndef IPDEFTTL
70 # define IPDEFTTL 64
71 #endif
73 /*** externals ***/
74 extern char *optarg;
75 extern int optind, opterr;
77 /*** Constants ***/
78 //#define EMAIL "ae@op5.se"
79 //#define VERSION "0.8.1"
81 #ifndef INADDR_NONE
82 # define INADDR_NONE 0xffffffU
83 #endif
85 /*** Ping packet defines ***/
86 /* data added after ICMP header for our nefarious purposes */
87 typedef struct ping_data {
88 unsigned int ping_count; /* counts up to -[n|p] count or 1 */
89 struct timeval ping_ts; /* time sent */
90 } PING_DATA;
92 #define MIN_PING_DATA sizeof(PING_DATA)
93 #define MAX_IP_PACKET 65536 /* (theoretical) max IP packet size */
94 #define SIZE_IP_HDR 20
95 #define SIZE_ICMP_HDR ICMP_MINLEN /* from ip_icmp.h */
96 #define MAX_PING_DATA (MAX_IP_PACKET - SIZE_IP_HDR - SIZE_ICMP_HDR)
98 /*
99 * Interval is the minimum amount of time between sending a ping packet to
100 * any host.
101 *
102 * Perhost_interval is the minimum amount of time between sending a ping
103 * packet to a particular responding host
104 *
105 * Timeout is the initial amount of time between sending a ping packet to
106 * a particular non-responding host.
107 *
108 * Retry is the number of ping packets to send to a non-responding host
109 * before giving up (in is-it-alive mode).
110 *
111 * Backoff factor is how much longer to wait on successive retries.
112 */
113 #ifndef DEFAULT_INTERVAL
114 #define DEFAULT_INTERVAL 25 /* default time between packets (msec) */
115 #endif
117 #ifndef DEFAULT_RETRY
118 #define DEFAULT_RETRY 1 /* number of times to retry a host */
119 #endif
121 #ifndef DEFAULT_TIMEOUT
122 # define DEFAULT_TIMEOUT 1000
123 #endif
125 #ifndef DEFAULT_BACKOFF_FACTOR
126 #define DEFAULT_BACKOFF_FACTOR 1.5 /* exponential timeout factor */
127 #endif
128 #define MIN_BACKOFF_FACTOR 1.0 /* exponential timeout factor */
129 #define MAX_BACKOFF_FACTOR 5.0 /* exponential timeout factor */
131 #ifndef DNS_TIMEOUT
132 #define DNS_TIMEOUT 1000 /* time in usec for dns retry */
133 #endif
135 #ifndef MAX_RTA_THRESHOLD_VALUE
136 # define MAX_RTA_THRESHOLD_VALUE 120*1000000 /* 2 minutes should be enough */
137 #endif
138 #ifndef MIN_RTA_THRESHOLD_VALUE
139 # define MIN_RTA_THRESHOLD_VALUE 10000 /* minimum RTA threshold value */
140 #endif
142 /* sized so as to be like traditional ping */
143 #define DEFAULT_PING_DATA_SIZE (MIN_PING_DATA + 44)
145 /* maxima and minima */
146 #define MAX_COUNT 50 /* max count even if we're root */
147 #define MAX_RETRY 5
148 #define MIN_INTERVAL 25 /* msecs */
149 #define MIN_TIMEOUT 50 /* msecs */
151 /* response time array flags */
152 #define RESP_WAITING -1
153 #define RESP_UNUSED -2
155 #define ICMP_UNREACH_MAXTYPE 15
157 /* entry used to keep track of each host we are pinging */
158 struct host_entry {
159 int i; /* index into array */
160 char *name; /* name as given by user */
161 char *host; /* text description of host */
162 struct sockaddr_in saddr; /* internet address */
163 unsigned short **pr; /* TCP port range to check for connectivity */
164 struct timeval last_send_time; /* time of last packet sent */
165 unsigned int num_sent; /* number of ping packets sent */
166 unsigned int num_recv; /* number of pings received */
167 unsigned int total_time; /* sum of response times */
168 unsigned int status; /* this hosts status */
169 unsigned int running; /* unset when through sending */
170 unsigned int waiting; /* waiting for response */
171 int *resp_times; /* individual response times */
172 struct host_entry *prev, *next; /* doubly linked list */
173 };
175 typedef struct host_entry HOST_ENTRY;
177 struct host_name_list {
178 char *entry;
179 struct host_name_list *next;
180 };
182 /* threshold structure */
183 struct threshold {
184 unsigned int pl; /* packet loss */
185 unsigned int rta; /* roundtrip time average */
186 };
187 typedef struct threshold threshold;
189 /*****************************************************************************
190 * Global Variables *
191 *****************************************************************************/
193 HOST_ENTRY *rrlist = NULL; /* linked list of hosts be pinged */
194 HOST_ENTRY **table = NULL; /* array of pointers to items in the list */
195 HOST_ENTRY *cursor;
197 char *prog; /* our name */
198 int ident; /* our pid, for marking icmp packets */
199 int sock; /* socket */
200 u_int debug = 0;
202 /* threshold value defaults;
203 * WARNING; 60% packetloss or 200 msecs round trip average
204 * CRITICAL; 80% packetloss or 500 msecs round trip average */
205 threshold warn = {60, 200 * 1000};
206 threshold crit = {80, 500 * 1000};
208 /* times get *100 because all times are calculated in 10 usec units, not ms */
209 unsigned int retry = DEFAULT_RETRY;
210 u_int timeout = DEFAULT_TIMEOUT * 100;
211 u_int interval = DEFAULT_INTERVAL * 100;
212 float backoff = DEFAULT_BACKOFF_FACTOR;
213 u_int select_time; /* calculated using maximum threshold rta value */
214 u_int ping_data_size = DEFAULT_PING_DATA_SIZE;
215 u_int ping_pkt_size;
216 unsigned int count = 5;
217 unsigned int trials = 1;
219 /* global stats */
220 int total_replies = 0;
221 int num_jobs = 0; /* number of hosts still to do */
222 int num_hosts = 0; /* total number of hosts */
223 int num_alive = 0; /* total number alive */
224 int num_unreachable = 0; /* total number unreachable */
225 int num_noaddress = 0; /* total number of addresses not found */
226 int num_timeout = 0; /* number of timed out packets */
227 int num_pingsent = 0; /* total pings sent */
228 int num_pingreceived = 0; /* total pings received */
229 int num_othericmprcvd = 0; /* total non-echo-reply ICMP received */
231 struct timeval current_time; /* current time (pseudo) */
232 struct timeval my_start_time; /* conflict with utils.c 33, but not found ?? */
233 struct timeval my_end_time; /* conflict with utils.c 33, but not found ?? */
234 struct timeval last_send_time; /* time last ping was sent */
235 struct timezone tz;
237 /* switches */
238 int generate_flag = 0; /* flag for IP list generation */
239 int stats_flag, unreachable_flag, alive_flag;
240 int elapsed_flag, version_flag, count_flag;
241 int name_flag, addr_flag, backoff_flag;
242 int multif_flag;
244 /*** prototypes ***/
245 void add_name(char *);
246 void add_addr(char *, char *, struct in_addr);
247 char *na_cat(char *, struct in_addr);
248 char *cpystr(char *);
249 void crash(char *);
250 char *get_host_by_address(struct in_addr);
251 int in_cksum(u_short *, int);
252 void u_sleep(int);
253 int recvfrom_wto(int, char *, int, struct sockaddr *, int);
254 void remove_job(HOST_ENTRY *);
255 void send_ping(int, HOST_ENTRY *);
256 long timeval_diff(struct timeval *, struct timeval *);
257 //void usage(void);
258 int wait_for_reply(int);
259 void finish(void);
260 int handle_random_icmp(struct icmp *, struct sockaddr_in *);
261 char *sprint_tm(int);
262 int get_threshold(char *, threshold *);
264 /* common functions */
265 void print_help (void);
266 void print_usage (void);
268 /*** the various exit-states */
269 /*enum {
270 STATE_OK = 0,
271 STATE_WARNING,
272 STATE_CRITICAL,
273 STATE_UNKNOWN,
274 STATE_DEPENDANT,
275 STATE_OOB
276 };*/
278 /* the strings that correspond to them */
279 /*
280 char *status_string[STATE_OOB] = {
281 "OK",
282 "WARNING",
283 "CRITICAL",
284 "UNKNOWN",
285 "DEPENDANT"
286 };
287 */
289 int status = STATE_OK;
290 int fin_stat = STATE_OK;
292 /*****************************************************************************
293 * Code block start *
294 *****************************************************************************/
295 int main(int argc, char **argv)
296 {
297 int c;
298 u_int lt, ht;
299 int advance;
300 struct protoent *proto;
301 uid_t uid;
302 struct host_name_list *host_ptr, *host_base_ptr;
304 if(strchr(argv[0], '/')) prog = strrchr(argv[0], '/') + 1;
305 else prog = argv[0];
307 setlocale (LC_ALL, "");
308 bindtextdomain (PACKAGE, LOCALEDIR);
309 textdomain (PACKAGE);
311 /* check if we are root */
312 if(geteuid()) {
313 printf(_("Root access needed (for raw sockets)\n"));
314 exit(STATE_UNKNOWN);
315 }
317 /* confirm that ICMP is available on this machine */
318 if((proto = getprotobyname("icmp")) == NULL)
319 crash(_("icmp: unknown protocol"));
321 /* create raw socket for ICMP calls (ping) */
322 sock = socket(AF_INET, SOCK_RAW, proto->p_proto);
324 if(sock < 0)
325 crash(_("Can't create raw socket"));
327 /* drop privileges now that we have the socket */
328 if((uid = getuid())) {
329 seteuid(uid);
330 }
332 if(argc < 2) print_usage();
334 ident = getpid() & 0xFFFF;
336 if(!(host_base_ptr = malloc(sizeof(struct host_name_list)))) {
337 crash(_("Unable to allocate memory for host name list\n"));
338 }
339 host_ptr = host_base_ptr;
341 backoff_flag = 0;
342 opterr = 1;
344 /* get command line options
345 * -H denotes a host (actually ignored and picked up later)
346 * -h for help
347 * -V or -v for version
348 * -d to display hostnames rather than addresses
349 * -t sets timeout for packets and tcp connects
350 * -r defines retries (persistence)
351 * -p or -n sets packet count (5)
352 * -b sets packet size (56)
353 * -w sets warning threshhold (200,40%)
354 * -c sets critical threshhold (500,80%)
355 * -i sets interval for both packet transmissions and connect attempts
356 */
357 #define OPT_STR "amH:hvVDdAp:n:b:r:t:i:w:c:"
358 while((c = getopt(argc, argv, OPT_STR)) != EOF) {
359 switch (c) {
360 case 'H':
361 if(!(host_ptr->entry = malloc(strlen(optarg) + 1))) {
362 crash(_("Failed to allocate memory for hostname"));
363 }
364 memset(host_ptr->entry, 0, strlen(optarg) + 1);
365 host_ptr->entry = memcpy(host_ptr->entry, optarg, strlen(optarg));
366 if(!(host_ptr->next = malloc(sizeof(struct host_name_list))))
367 crash(_("Failed to allocate memory for hostname"));
368 host_ptr = host_ptr->next;
369 host_ptr->next = NULL;
370 // add_name(optarg);
371 break;
372 /* this is recognized, but silently ignored.
373 * host(s) are added later on */
375 break;
376 case 'w':
377 if(get_threshold(optarg, &warn)) {
378 printf(_("Illegal threshold pair specified for -%c"), c);
379 print_usage();
380 }
381 break;
383 case 'c':
384 if(get_threshold(optarg, &crit)) {
385 printf(_("Illegal threshold pair specified for -%c"), c);
386 print_usage();
387 }
388 break;
390 case 't':
391 if(!(timeout = (u_int) strtoul(optarg, NULL, 0) * 100)) {
392 printf(_("Option -%c requires integer argument\n"), c);
393 print_usage();
394 }
395 break;
397 case 'r':
398 if(!(retry = (u_int) strtoul(optarg, NULL, 0))) {
399 printf(_("Option -%c requires integer argument\n"), c);
400 print_usage();
401 }
402 break;
404 case 'i':
405 if(!(interval = (u_int) strtoul(optarg, NULL, 0) * 100)) {
406 printf(_("Option -%c requires positive non-zero integer argument\n"), c);
407 print_usage();
408 }
409 break;
411 case 'p':
412 case 'n':
413 if(!(count = (u_int) strtoul(optarg, NULL, 0))) {
414 printf(_("Option -%c requires positive non-zero integer argument\n"), c);
415 print_usage();
416 }
417 break;
419 case 'b':
420 if(!(ping_data_size = (u_int) strtoul(optarg, NULL, 0))) {
421 printf(_("Option -%c requires integer argument\n"), c);
422 print_usage();
423 }
424 break;
426 case 'h':
427 print_usage();
428 break;
430 case 'e':
431 elapsed_flag = 1;
432 break;
434 case 'm':
435 multif_flag = 1;
436 break;
438 case 'd':
439 name_flag = 1;
440 break;
442 case 'A':
443 addr_flag = 1;
444 break;
446 case 's':
447 stats_flag = 1;
448 break;
450 case 'u':
451 unreachable_flag = 1;
452 break;
454 case 'a':
455 alive_flag = 1;
456 break;
458 case 'v':
459 printf("%s: Version %s $Date$\n", prog, VERSION);
460 printf("%s: comments to %s\n", prog, email);
461 exit(STATE_OK);
463 case 'g':
464 /* use IP list generation */
465 /* mutex with file input or command line targets */
466 generate_flag = 1;
467 break;
469 default:
470 printf(_("Option flag -%c specified, but not recognized\n"), c);
471 print_usage();
472 break;
473 }
474 }
476 /* arguments are parsed, so now we validate them */
478 if(count > 1) count_flag = 1;
480 /* set threshold values to 10usec units (inherited from fping.c) */
481 crit.rta = crit.rta / 10;
482 warn.rta = warn.rta / 10;
483 select_time = crit.rta;
484 /* this isn't critical, but will most likely not be what the user expects
485 * so we tell him/her about it, but keep running anyways */
486 if(warn.pl > crit.pl || warn.rta > crit.rta) {
487 select_time = warn.rta;
488 printf("(WARNING threshold > CRITICAL threshold) :: ");
489 fflush(stdout);
490 }
492 /* A timeout smaller than maximum rta threshold makes no sense */
493 if(timeout < crit.rta) timeout = crit.rta;
494 else if(timeout < warn.rta) timeout = warn.rta;
496 if((interval < MIN_INTERVAL * 100 || retry > MAX_RETRY) && getuid()) {
497 printf(_("%s: these options are too risky for mere mortals.\n"), prog);
498 printf(_("%s: You need i >= %u and r < %u\n"),
499 prog, MIN_INTERVAL, MAX_RETRY);
500 printf(_("Current settings; i = %d, r = %d\n"),
501 interval / 100, retry);
502 print_usage();
503 }
505 if((ping_data_size > MAX_PING_DATA) || (ping_data_size < MIN_PING_DATA)) {
506 printf(_("%s: data size %u not valid, must be between %u and %u\n"),
507 prog, ping_data_size, MIN_PING_DATA, MAX_PING_DATA);
508 print_usage();
510 }
512 if((backoff > MAX_BACKOFF_FACTOR) || (backoff < MIN_BACKOFF_FACTOR)) {
513 printf(_("%s: backoff factor %.1f not valid, must be between %.1f and %.1f\n"),
514 prog, backoff, MIN_BACKOFF_FACTOR, MAX_BACKOFF_FACTOR);
515 print_usage();
517 }
519 if(count > MAX_COUNT) {
520 printf(_("%s: count %u not valid, must be less than %u\n"),
521 prog, count, MAX_COUNT);
522 print_usage();
523 }
525 if(count_flag) {
526 alive_flag = unreachable_flag = 0;
527 }
529 trials = (count > retry + 1) ? count : retry + 1;
531 /* handle host names supplied on command line or in a file */
532 /* if the generate_flag is on, then generate the IP list */
533 argv = &argv[optind];
535 /* cover allowable conditions */
537 /* generate requires command line parameters beyond the switches */
538 if(generate_flag && !*argv) {
539 printf(_("Generate flag requires command line parameters beyond switches\n"));
540 print_usage();
541 }
543 if(*argv && !generate_flag) {
544 while(*argv) {
545 if(!(host_ptr->entry = malloc(strlen(*argv) + 1))) {
546 crash(_("Failed to allocate memory for hostname"));
547 }
548 memset(host_ptr->entry, 0, strlen(*argv) + 1);
549 host_ptr->entry = memcpy(host_ptr->entry, *argv, strlen(*argv));
550 if(!(host_ptr->next = malloc(sizeof(struct host_name_list))))
551 crash(_("Failed to allocate memory for hostname"));
552 host_ptr = host_ptr->next;
553 host_ptr->next = NULL;
555 // add_name(*argv);
556 argv++;
557 }
558 }
560 // now add all the hosts
561 host_ptr = host_base_ptr;
562 while(host_ptr->next) {
563 add_name(host_ptr->entry);
564 host_ptr = host_ptr->next;
565 }
567 if(!num_hosts) {
568 printf(_("No hosts to work with!\n\n"));
569 print_usage();
570 }
572 /* allocate array to hold outstanding ping requests */
573 table = (HOST_ENTRY **) malloc(sizeof(HOST_ENTRY *) * num_hosts);
574 if(!table) crash(_("Can't malloc array of hosts"));
576 cursor = rrlist;
578 for(num_jobs = 0; num_jobs < num_hosts; num_jobs++) {
579 table[num_jobs] = cursor;
580 cursor->i = num_jobs;
582 cursor = cursor->next;
583 } /* FOR */
585 ping_pkt_size = ping_data_size + SIZE_ICMP_HDR;
587 signal(SIGINT, (void *)finish);
589 gettimeofday(&my_start_time, &tz);
590 current_time = my_start_time;
592 last_send_time.tv_sec = current_time.tv_sec - 10000;
594 cursor = rrlist;
595 advance = 0;
597 /* main loop */
598 while(num_jobs) {
599 /* fetch all packets that receive within time boundaries */
600 while(num_pingsent &&
601 cursor &&
602 cursor->num_sent > cursor->num_recv &&
603 wait_for_reply(sock)) ;
605 if(cursor && advance) {
606 cursor = cursor->next;
607 }
609 gettimeofday(¤t_time, &tz);
610 lt = timeval_diff(¤t_time, &last_send_time);
611 ht = timeval_diff(¤t_time, &cursor->last_send_time);
613 advance = 1;
615 /* if it's OK to send while counting or looping or starting */
616 if(lt > interval) {
617 /* send if starting or looping */
618 if((cursor->num_sent == 0)) {
619 send_ping(sock, cursor);
620 continue;
621 } /* IF */
623 /* send if counting and count not exceeded */
624 if(count_flag) {
625 if(cursor->num_sent < count) {
626 send_ping(sock, cursor);
627 continue;
628 } /* IF */
629 } /* IF */
630 } /* IF */
632 /* is-it-alive mode, and timeout exceeded while waiting for a reply */
633 /* and we haven't exceeded our retries */
634 if((lt > interval) && !count_flag && !cursor->num_recv &&
635 (ht > timeout) && (cursor->waiting < retry + 1)) {
636 num_timeout++;
638 /* try again */
639 send_ping(sock, cursor);
640 continue;
641 } /* IF */
643 /* didn't send, can we remove? */
645 /* remove if counting and count exceeded */
646 if(count_flag) {
647 if((cursor->num_sent >= count)) {
648 remove_job(cursor);
649 continue;
650 } /* IF */
651 } /* IF */
652 else {
653 /* normal mode, and we got one */
654 if(cursor->num_recv) {
655 remove_job(cursor);
656 continue;
657 } /* IF */
659 /* normal mode, and timeout exceeded while waiting for a reply */
660 /* and we've run out of retries, so node is unreachable */
661 if((ht > timeout) && (cursor->waiting >= retry + 1)) {
662 num_timeout++;
663 remove_job(cursor);
664 continue;
666 } /* IF */
667 } /* ELSE */
669 /* could send to this host, so keep considering it */
670 if(ht > interval) {
671 advance = 0;
672 }
673 } /* WHILE */
675 finish();
676 return 0;
677 } /* main() */
679 /************************************************************
680 * Description:
681 *
682 * Main program clean up and exit point
683 ************************************************************/
684 void finish()
685 {
686 int i;
687 HOST_ENTRY *h;
689 gettimeofday(&my_end_time, &tz);
691 /* tot up unreachables */
692 for(i=0; i<num_hosts; i++) {
693 h = table[i];
695 if(!h->num_recv) {
696 num_unreachable++;
697 status = fin_stat = STATE_CRITICAL;
698 if(num_hosts == 1) {
699 printf("CRITICAL - %s is down (lost 100%%)|"
700 "rta=;%d;%d;; pl=100%%;%d;%d;;\n",
701 h->host,
702 warn.rta / 100, crit.rta / 100,
703 warn.pl, crit.pl);
704 }
705 else {
706 printf(_("%s is down (lost 100%%)"), h->host);
707 }
708 }
709 else {
710 /* reset the status */
711 status = STATE_OK;
713 /* check for warning before critical, for debugging purposes */
714 if(warn.rta <= h->total_time / h->num_recv) {
715 /* printf("warn.rta exceeded\n");
716 */ status = STATE_WARNING;
717 }
718 if(warn.pl <= ((h->num_sent - h->num_recv) * 100) / h->num_sent) {
719 /* printf("warn.pl exceeded (pl=%d)\n",
720 ((h->num_sent - h->num_recv) * 100) / h->num_sent);
721 */ status = STATE_WARNING;
722 }
723 if(crit.rta <= h->total_time / h->num_recv) {
724 /* printf("crit.rta exceeded\n");
725 */ status = STATE_CRITICAL;
726 }
727 if(crit.pl <= ((h->num_sent - h->num_recv) * 100) / h->num_sent) {
728 /* printf("crit.pl exceeded (pl=%d)\n",
729 ((h->num_sent - h->num_recv) * 100) / h->num_sent);
730 */ status = STATE_CRITICAL;
731 }
733 if(num_hosts == 1 || status != STATE_OK) {
734 printf("%s - %s: rta %s ms, lost %d%%",
735 state_text(status), h->host,
736 sprint_tm(h->total_time / h->num_recv),
737 h->num_sent > 0 ? ((h->num_sent - h->num_recv) * 100) / h->num_sent : 0
738 );
739 /* perfdata only available for single-host stuff */
740 if(num_hosts == 1) {
741 printf("|rta=%sms;%d;%d;; pl=%d%%;%d;%d;;\n",
742 sprint_tm(h->total_time / h->num_recv), warn.rta / 100, crit.rta / 100,
743 h->num_sent > 0 ? ((h->num_sent - h->num_recv) * 100) / h->num_sent : 0, warn.pl, crit.pl
744 );
745 }
746 else printf(" :: ");
747 }
749 /* fin_stat should always hold the WORST state */
750 if(fin_stat != STATE_CRITICAL && status != STATE_OK) {
751 fin_stat = status;
752 }
753 }
754 }
756 if(num_noaddress) {
757 printf(_("No hostaddress specified.\n"));
758 print_usage();
759 }
760 else if(num_alive != num_hosts) {
761 /* for future multi-check support */
762 /*printf("num_alive != num_hosts (%d : %d)\n", num_alive, num_hosts);*/
763 fin_stat = STATE_CRITICAL;
764 }
766 if(num_hosts > 1) {
767 if(num_alive == num_hosts) {
768 printf(_("OK - All %d hosts are alive\n"), num_hosts);
769 }
770 else {
771 printf(_("CRITICAL - %d of %d hosts are alive\n"), num_alive, num_hosts);
772 }
773 }
774 exit(fin_stat);
775 }
778 void send_ping(int lsock, HOST_ENTRY *h)
779 {
780 char *buffer;
781 struct icmp *icp;
782 PING_DATA *pdp;
783 int n;
785 buffer = (char *)malloc((size_t) ping_pkt_size);
786 if(!buffer)
787 crash(_("Can't malloc ping packet"));
789 memset(buffer, 0, ping_pkt_size * sizeof(char));
790 icp = (struct icmp *)buffer;
792 gettimeofday(&h->last_send_time, &tz);
794 icp->icmp_type = ICMP_ECHO;
795 icp->icmp_code = 0;
796 icp->icmp_cksum = 0;
797 icp->icmp_seq = h->i;
798 icp->icmp_id = ident;
800 pdp = (PING_DATA *) (buffer + SIZE_ICMP_HDR);
801 pdp->ping_ts = h->last_send_time;
802 pdp->ping_count = h->num_sent;
804 icp->icmp_cksum = in_cksum((u_short *) icp, ping_pkt_size);
806 n = sendto(lsock, buffer, ping_pkt_size, 0,
807 (struct sockaddr *)&h->saddr, sizeof(struct sockaddr_in));
809 if(n < 0 || (unsigned int)n != ping_pkt_size) {
810 if(unreachable_flag) {
811 printf(_("%s error while sending ping: %s\n"),
812 h->host, strerror(errno));
813 } /* IF */
815 num_unreachable++;
816 remove_job(h);
817 } /* IF */
818 else {
819 /* mark this trial as outstanding */
820 h->resp_times[h->num_sent] = RESP_WAITING;
822 h->num_sent++;
823 h->waiting++;
824 num_pingsent++;
825 last_send_time = h->last_send_time;
826 } /* ELSE */
828 free(buffer);
829 } /* send_ping() */
831 int wait_for_reply(int lsock)
832 {
833 int result;
834 static char buffer[4096];
835 struct sockaddr_in response_addr;
836 struct ip *ip;
837 int hlen;
838 struct icmp *icp;
839 int n;
840 HOST_ENTRY *h = NULL;
841 long this_reply;
842 int this_count;
843 struct timeval sent_time;
845 result = recvfrom_wto(lsock, buffer, sizeof(buffer),
846 (struct sockaddr *)&response_addr, select_time);
848 if(result < 0) return 0; /* timeout */
850 ip = (struct ip *)buffer;
852 #if defined( __alpha__ ) && __STDC__ && !defined( __GLIBC__ )
853 /* The alpha headers are decidedly broken.
854 * Using an ANSI compiler, it provides ip_vhl instead of ip_hl and
855 * ip_v. So, to get ip_hl, we mask off the bottom four bits.
856 */
857 hlen = (ip->ip_vhl & 0x0F) << 2;
858 #else
859 hlen = ip->ip_hl << 2;
860 #endif /* defined(__alpha__) && __STDC__ */
862 if(result < hlen + ICMP_MINLEN) {
863 printf(_("Received packet too short for ICMP (%d bytes from %s)\n"), result,
864 inet_ntoa(response_addr.sin_addr));
866 return (1); /* too short */
867 } /* IF */
869 icp = (struct icmp *)(buffer + hlen);
870 if(icp->icmp_type != ICMP_ECHOREPLY) {
871 /* handle some problem */
872 if(handle_random_icmp(icp, &response_addr))
873 num_othericmprcvd++;
875 return 1;
876 } /* IF */
878 if(icp->icmp_id != ident)
879 return 1; /* packet received, but not the one we are looking for! */
881 num_pingreceived++;
883 if(icp->icmp_seq >= (n_short) num_hosts)
884 return(1); /* packet received, don't worry about it anymore */
886 n = icp->icmp_seq;
887 h = table[n];
889 /* received ping is cool, so process it */
891 gettimeofday(¤t_time, &tz);
892 h->waiting = 0;
893 h->num_recv++;
895 memcpy(&sent_time, icp->icmp_data + offsetof(PING_DATA, ping_ts),
896 sizeof(sent_time));
897 memcpy(&this_count, icp->icmp_data, sizeof(this_count));
899 this_reply = timeval_diff(¤t_time, &sent_time);
900 h->total_time += this_reply;
901 total_replies++;
903 /* note reply time in array, probably */
904 if((this_count >= 0) && ((unsigned int)this_count < trials)) {
905 if(h->resp_times[this_count] != RESP_WAITING) {
906 printf(_("%s : duplicate for [%d], %d bytes, %s ms"),
907 h->host, this_count, result, sprint_tm(this_reply));
909 if(response_addr.sin_addr.s_addr != h->saddr.sin_addr.s_addr)
910 printf(" [<- %s]\n", inet_ntoa(response_addr.sin_addr));
911 } /* IF */
912 else h->resp_times[this_count] = this_reply;
913 } /* IF */
914 else {
915 /* count is out of bounds?? */
916 printf(_("%s : duplicate for [%d], %d bytes, %s ms\n"),
917 h->host, this_count, result, sprint_tm(this_reply));
918 } /* ELSE */
920 if(h->num_recv == 1) {
921 num_alive++;
922 } /* IF */
924 return num_jobs;
925 } /* wait_for_reply() */
927 int handle_random_icmp(struct icmp *p, struct sockaddr_in *addr)
928 {
929 struct icmp *sent_icmp;
930 u_char *c;
931 HOST_ENTRY *h;
933 c = (u_char *) p;
934 switch (p->icmp_type) {
935 case ICMP_UNREACH:
936 sent_icmp = (struct icmp *)(c + 28);
938 if((sent_icmp->icmp_type == ICMP_ECHO) &&
939 (sent_icmp->icmp_id == ident) &&
940 (sent_icmp->icmp_seq < (n_short) num_hosts)) {
941 /* this is a response to a ping we sent */
942 h = table[sent_icmp->icmp_seq];
944 if(p->icmp_code > ICMP_UNREACH_MAXTYPE) {
945 printf(_("ICMP Unreachable (Invalid Code) from %s for ICMP Echo sent to %s"),
946 inet_ntoa(addr->sin_addr), h->host);
948 } /* IF */
949 else {
950 printf(_("ICMP Unreachable from %s for ICMP Echo sent to %s"),
951 inet_ntoa(addr->sin_addr), h->host);
953 } /* ELSE */
955 if(inet_addr(h->host) == INADDR_NONE)
956 printf(" (%s)", inet_ntoa(h->saddr.sin_addr));
958 printf("\n");
960 } /* IF */
962 return 1;
964 case ICMP_SOURCEQUENCH:
965 case ICMP_REDIRECT:
966 case ICMP_TIMXCEED:
967 case ICMP_PARAMPROB:
968 sent_icmp = (struct icmp *)(c + 28);
969 if((sent_icmp->icmp_type = ICMP_ECHO) &&
970 (sent_icmp->icmp_id = ident) &&
971 (sent_icmp->icmp_seq < (n_short) num_hosts)) {
972 /* this is a response to a ping we sent */
973 h = table[sent_icmp->icmp_seq];
974 printf(_("ICMP Unreachable from %s for ICMP Echo sent to %s"),
975 inet_ntoa(addr->sin_addr), h->host);
977 if(inet_addr(h->host) == INADDR_NONE)
978 printf(" (%s)", inet_ntoa(h->saddr.sin_addr));
980 printf("\n");
981 } /* IF */
983 return 2;
985 /* no way to tell whether any of these are sent due to our ping */
986 /* or not (shouldn't be, of course), so just discard */
987 case ICMP_TSTAMP:
988 case ICMP_TSTAMPREPLY:
989 case ICMP_IREQ:
990 case ICMP_IREQREPLY:
991 case ICMP_MASKREQ:
992 case ICMP_MASKREPLY:
993 default:
994 return 0;
996 } /* SWITCH */
998 } /* handle_random_icmp() */
1000 int in_cksum(u_short * p, int n)
1001 {
1002 register u_short answer;
1003 register long sum = 0;
1004 u_short odd_byte = 0;
1006 while(n > 1) {
1007 sum += *p++;
1008 n -= 2;
1009 } /* WHILE */
1011 /* mop up an odd byte, if necessary */
1012 if(n == 1) {
1013 *(u_char *) (&odd_byte) = *(u_char *) p;
1014 sum += odd_byte;
1015 } /* IF */
1017 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
1018 sum += (sum >> 16); /* add carry */
1019 answer = ~sum; /* ones-complement, truncate */
1021 return (answer);
1023 } /* in_cksum() */
1025 void add_name(char *name)
1026 {
1027 struct hostent *host_ent;
1028 int ipaddress;
1029 struct in_addr *ipa = (struct in_addr *)&ipaddress;
1030 struct in_addr *host_add;
1031 char *nm;
1032 int i = 0;
1034 if((ipaddress = inet_addr(name)) != -1) {
1035 /* input name is an IP addr, go with it */
1036 if(name_flag) {
1037 if(addr_flag)
1038 add_addr(name, na_cat(get_host_by_address(*ipa), *ipa), *ipa);
1039 else {
1040 nm = cpystr(get_host_by_address(*ipa));
1041 add_addr(name, nm, *ipa);
1043 } /* ELSE */
1044 } /* IF */
1045 else add_addr(name, name, *ipa);
1047 return;
1048 } /* IF */
1050 /* input name is not an IP addr, maybe it's a host name */
1051 host_ent = gethostbyname(name);
1052 if(host_ent == NULL) {
1053 if(h_errno == TRY_AGAIN) {
1054 u_sleep(DNS_TIMEOUT);
1055 host_ent = gethostbyname(name);
1056 } /* IF */
1058 if(host_ent == NULL) {
1059 printf(_("%s address not found\n"), name);
1060 num_noaddress++;
1061 return;
1062 } /* IF */
1063 } /* IF */
1065 host_add = (struct in_addr *)*(host_ent->h_addr_list);
1066 if(host_add == NULL) {
1067 printf(_("%s has no address data\n"), name);
1068 num_noaddress++;
1069 return;
1070 } /* IF */
1071 else {
1072 /* it is indeed a hostname with a real address */
1073 while(host_add) {
1074 if(name_flag && addr_flag)
1075 add_addr(name, na_cat(name, *host_add), *host_add);
1076 else if(addr_flag) {
1077 nm = cpystr(inet_ntoa(*host_add));
1078 add_addr(name, nm, *host_add);
1079 } /* ELSE IF */
1080 else {
1081 add_addr(name, name, *host_add);
1082 }
1084 if(!multif_flag) break;
1086 host_add = (struct in_addr *)(host_ent->h_addr_list[++i]);
1087 } /* WHILE */
1088 } /* ELSE */
1089 } /* add_name() */
1092 char *na_cat(char *name, struct in_addr ipaddr)
1093 {
1094 char *nm, *as;
1096 as = inet_ntoa(ipaddr);
1097 nm = (char *)malloc(strlen(name) + strlen(as) + 4);
1099 if(!nm)
1100 crash(_("Can't allocate some space for a string"));
1102 strcpy(nm, name);
1103 strcat(nm, " (");
1104 strcat(nm, as);
1105 strcat(nm, ")");
1107 return (nm);
1109 } /* na_cat() */
1112 void add_addr(char *name, char *host, struct in_addr ipaddr)
1113 {
1114 HOST_ENTRY *p;
1115 unsigned int n;
1116 int *i;
1118 if(!(p = (HOST_ENTRY *) malloc(sizeof(HOST_ENTRY)))) {
1119 crash(_("Can't allocate HOST_ENTRY"));
1120 }
1122 memset((char *)p, 0, sizeof(HOST_ENTRY));
1124 p->name = name;
1125 p->host = host;
1126 p->saddr.sin_family = AF_INET;
1127 p->saddr.sin_addr = ipaddr;
1128 p->running = 1;
1130 /* array for response time results */
1131 if(!(i = (int *)malloc(trials * sizeof(int)))) {
1132 crash(_("Can't allocate resp_times array"));
1133 }
1135 for(n = 1; n < trials; n++)
1136 i[n] = RESP_UNUSED;
1138 p->resp_times = i;
1140 if(!rrlist) {
1141 rrlist = p;
1142 p->next = p;
1143 p->prev = p;
1144 } /* IF */
1145 else {
1146 p->next = rrlist;
1147 p->prev = rrlist->prev;
1148 p->prev->next = p;
1149 p->next->prev = p;
1150 } /* ELSE */
1152 num_hosts++;
1153 } /* add_addr() */
1156 void remove_job(HOST_ENTRY * h)
1157 {
1158 h->running = 0;
1159 h->waiting = 0;
1160 num_jobs--;
1163 if(num_jobs) {
1164 /* remove us from list of active jobs */
1165 h->prev->next = h->next;
1166 h->next->prev = h->prev;
1167 if(h == cursor) cursor = h->next;
1168 } /* IF */
1169 else {
1170 cursor = NULL;
1171 rrlist = NULL;
1172 } /* ELSE */
1174 } /* remove_job() */
1177 char *get_host_by_address(struct in_addr in)
1178 {
1179 struct hostent *h;
1180 h = gethostbyaddr((char *)&in, sizeof(struct in_addr), AF_INET);
1182 if(h == NULL || h->h_name == NULL)
1183 return inet_ntoa(in);
1184 else
1185 return (char *)h->h_name;
1187 } /* get_host_by_address() */
1190 char *cpystr(char *string)
1191 {
1192 char *dst;
1194 if(string) {
1195 dst = (char *)malloc(1 + strlen(string));
1196 if(!dst) crash(_("malloc() failed!"));
1198 strcpy(dst, string);
1199 return dst;
1201 } /* IF */
1202 else return NULL;
1204 } /* cpystr() */
1207 void crash(char *msg)
1208 {
1209 if(errno || h_errno) {
1210 if(errno)
1211 printf("%s: %s : %s\n", prog, msg, strerror(errno));
1212 if(h_errno)
1213 printf(_("%s: %s : A network error occurred\n"), prog, msg);
1214 }
1215 else printf("%s: %s\n", prog, msg);
1217 exit(STATE_UNKNOWN);
1218 } /* crash() */
1221 long timeval_diff(struct timeval *a, struct timeval *b)
1222 {
1223 double temp;
1225 temp = (((a->tv_sec * 1000000) + a->tv_usec) -
1226 ((b->tv_sec * 1000000) + b->tv_usec)) / 10;
1228 return (long)temp;
1230 } /* timeval_diff() */
1233 char *sprint_tm(int t)
1234 {
1235 static char buf[10];
1237 /* <= 0.99 ms */
1238 if(t < 100) {
1239 sprintf(buf, "0.%02d", t);
1240 return (buf);
1241 } /* IF */
1243 /* 1.00 - 9.99 ms */
1244 if(t < 1000) {
1245 sprintf(buf, "%d.%02d", t / 100, t % 100);
1246 return (buf);
1247 } /* IF */
1249 /* 10.0 - 99.9 ms */
1250 if(t < 10000) {
1251 sprintf(buf, "%d.%d", t / 100, (t % 100) / 10);
1252 return (buf);
1253 } /* IF */
1255 /* >= 100 ms */
1256 sprintf(buf, "%d", t / 100);
1257 return (buf);
1258 } /* sprint_tm() */
1261 /*
1262 * select() is posix, so we expect it to be around
1263 */
1264 void u_sleep(int u_sec)
1265 {
1266 int nfound;
1267 struct timeval to;
1268 fd_set readset, writeset;
1270 to.tv_sec = u_sec / 1000000;
1271 to.tv_usec = u_sec - (to.tv_sec * 1000000);
1272 /* printf("u_sleep :: to.tv_sec: %d, to_tv_usec: %d\n",
1273 (int)to.tv_sec, (int)to.tv_usec);
1274 */
1275 FD_ZERO(&writeset);
1276 FD_ZERO(&readset);
1277 nfound = select(0, &readset, &writeset, NULL, &to);
1278 if(nfound < 0)
1279 crash(_("select() in u_sleep:"));
1281 return;
1282 } /* u_sleep() */
1285 /************************************************************
1286 * Description:
1287 *
1288 * receive with timeout
1289 * returns length of data read or -1 if timeout
1290 * crash on any other errrors
1291 ************************************************************/
1292 /* TODO: add MSG_DONTWAIT to recvfrom flags (currently 0) */
1293 int recvfrom_wto(int lsock, char *buf, int len, struct sockaddr *saddr, int timo)
1294 {
1295 int nfound = 0, slen, n;
1296 struct timeval to;
1297 fd_set readset, writeset;
1299 to.tv_sec = timo / 1000000;
1300 to.tv_usec = (timo - (to.tv_sec * 1000000)) * 10;
1302 /* printf("to.tv_sec: %d, to.tv_usec: %d\n", (int)to.tv_sec, (int)to.tv_usec);
1303 */
1305 FD_ZERO(&readset);
1306 FD_ZERO(&writeset);
1307 FD_SET(lsock, &readset);
1308 nfound = select(lsock + 1, &readset, &writeset, NULL, &to);
1309 if(nfound < 0) crash(_("select() in recvfrom_wto"));
1311 if(nfound == 0) return -1; /* timeout */
1313 if(nfound) {
1314 slen = sizeof(struct sockaddr);
1315 n = recvfrom(sock, buf, len, 0, saddr, &slen);
1316 if(n < 0) crash(_("recvfrom"));
1317 return(n);
1318 }
1320 return(0); /* 0 bytes read, so return it */
1321 } /* recvfrom_wto() */
1324 /*
1325 * u = micro
1326 * m = milli
1327 * s = seconds
1328 */
1329 int get_threshold(char *str, threshold *th)
1330 {
1331 unsigned int i, factor = 0;
1332 char *p = NULL;
1334 if(!str || !strlen(str) || !th) return -1;
1336 for(i=0; i<strlen(str); i++) {
1337 /* we happily accept decimal points in round trip time thresholds,
1338 * but we ignore them quite blandly. The new way of specifying higher
1339 * precision is to specify 'u' (for microseconds),
1340 * 'm' (for millisecs - default) or 's' for seconds. */
1341 if(!p && !factor) {
1342 if(str[i] == 's') factor = 1000000; /* seconds */
1343 else if(str[i] == 'm') factor = 1000; /* milliseconds */
1344 else if(str[i] == 'u') factor = 1; /* microseconds */
1345 }
1347 if(str[i] == '%') str[i] = '\0';
1348 else if(str[i] == ',' && !p && i != (strlen(str) - 1)) {
1349 p = &str[i+1];
1350 str[i] = '\0';
1351 }
1352 }
1354 /* default to milliseconds */
1355 if(!factor) factor = 1000;
1357 if(!p || !strlen(p)) return -1;
1358 th->rta = (unsigned int)strtoul(str, NULL, 0) * factor;
1359 th->pl = (unsigned int)strtoul(p, NULL, 0);
1360 return 0;
1361 }
1363 void
1364 print_help (void)
1365 {
1366 print_revision (progname, revision);
1368 printf ("Copyright (c) 2004 Andreas Ericsson <ae@op5.se>\n");
1369 printf (COPYRIGHT, copyright, email);
1371 printf (_("This plugin will check hosts sending icmp pings\n\n"));
1373 print_usage ();
1375 printf (_(UT_HELP_VRSN));
1377 printf (_("\
1378 -H, \n\
1379 Host name argument for servers\n\
1380 -b \n\
1381 ping packet size in bytes (default %d)\n\
1382 -n \n\
1383 number of pings to send to each target (default %d)\n\
1384 -r \n\
1385 number of retries (default %d)\n\
1386 -t \n\
1387 timeout value (in msec) (default %d)\n\
1388 -i \n\
1389 packet interval (in msec) (default %d)\n\
1390 -w \n\
1391 warning threshold pair, given as RTA[ums],PL[%%]\n\
1392 -c \n\
1393 critical threshold pair, given as RTA[ums],PL[%%]\n\
1394 -D \n\
1395 increase debug output level\n\n"),ping_data_size,count,retry,(timeout / 100),DEFAULT_INTERVAL);
1397 printf (_(UT_WARN_CRIT));
1399 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
1401 printf (_(UT_VERBOSE));
1403 // printf (_("This plugin will check hosts sending icmp pings\n"));
1405 printf (_(UT_SUPPORT));
1406 }
1408 void
1409 print_usage (void)
1410 {
1411 printf ("\
1412 Usage: %s -H <vhost> | [-b <ping packet size in bytes>] [-n <number of pings>]\n\
1413 [-r <number of retries>] [-t <timeout>] [-i packet interval]\n\
1414 [-w <warning threshold>] [-c <critical threshold>]\n\
1415 [-D <debug>] \n", progname);
1416 }