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