index a059f266629f3ae339f96ea9a3e53f278c929866..1dde4478936afc8df47549d1da12da28b2eb40ca 100644 (file)
* Copyright (c) 2005-2008 Nagios Plugins Development Team
* Original Author : Andreas Ericsson <ae@op5.se>
*
* Copyright (c) 2005-2008 Nagios Plugins Development Team
* Original Author : Andreas Ericsson <ae@op5.se>
*
-* Last Modified: $Date$
-*
* Description:
*
* This file contains the check_icmp plugin
* Description:
*
* This file contains the check_icmp plugin
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
-* $Id$
*
*****************************************************************************/
/* progname may change */
/* char *progname = "check_icmp"; */
char *progname;
*
*****************************************************************************/
/* progname may change */
/* char *progname = "check_icmp"; */
char *progname;
-const char *revision = "$Revision$";
const char *copyright = "2005-2008";
const char *email = "nagiosplug-devel@lists.sourceforge.net";
const char *copyright = "2005-2008";
const char *email = "nagiosplug-devel@lists.sourceforge.net";
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <signal.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <signal.h>
+#include <float.h>
/** sometimes undefined system macros (quite a few, actually) **/
/** sometimes undefined system macros (quite a few, actually) **/
# define ICMP_UNREACH_PRECEDENCE_CUTOFF 15
#endif
# define ICMP_UNREACH_PRECEDENCE_CUTOFF 15
#endif
+#ifndef DBL_MAX
+# define DBL_MAX 9.9999999999e999
+#endif
typedef unsigned short range_t; /* type for get_range() -- unimplemented */
typedef unsigned short range_t; /* type for get_range() -- unimplemented */
unsigned char icmp_type, icmp_code; /* type and code from errors */
unsigned short flags; /* control/status flags */
double rta; /* measured RTA */
unsigned char icmp_type, icmp_code; /* type and code from errors */
unsigned short flags; /* control/status flags */
double rta; /* measured RTA */
+ double rtmax; /* max rtt */
+ double rtmin; /* min rtt */
unsigned char pl; /* measured packet loss */
struct rta_host *next; /* linked list */
} rta_host;
unsigned char pl; /* measured packet loss */
struct rta_host *next; /* linked list */
} rta_host;
static u_int get_timevaldiff(struct timeval *, struct timeval *);
static in_addr_t get_ip_address(const char *);
static int wait_for_reply(int, u_int);
static u_int get_timevaldiff(struct timeval *, struct timeval *);
static in_addr_t get_ip_address(const char *);
static int wait_for_reply(int, u_int);
-static int recvfrom_wto(int, char *, unsigned int, struct sockaddr *, u_int *);
+static int recvfrom_wto(int, void *, unsigned int, struct sockaddr *, u_int *);
static int send_icmp_ping(int, struct rta_host *);
static int get_threshold(char *str, threshold *th);
static void run_checks(void);
static void set_source_ip(char *);
static int add_target(char *);
static int add_target_ip(char *, struct in_addr *);
static int send_icmp_ping(int, struct rta_host *);
static int get_threshold(char *str, threshold *th);
static void run_checks(void);
static void set_source_ip(char *);
static int add_target(char *);
static int add_target_ip(char *, struct in_addr *);
-static int handle_random_icmp(char *, struct sockaddr_in *);
+static int handle_random_icmp(unsigned char *, struct sockaddr_in *);
static unsigned short icmp_checksum(unsigned short *, int);
static void finish(int);
static void crash(const char *, ...);
static unsigned short icmp_checksum(unsigned short *, int);
static void finish(int);
static void crash(const char *, ...);
static struct rta_host **table, *cursor, *list;
static threshold crit = {80, 500000}, warn = {40, 200000};
static int mode, protocols, sockets, debug = 0, timeout = 10;
static struct rta_host **table, *cursor, *list;
static threshold crit = {80, 500000}, warn = {40, 200000};
static int mode, protocols, sockets, debug = 0, timeout = 10;
-static unsigned short icmp_pkt_size, icmp_data_size = DEFAULT_PING_DATA_SIZE;
+static unsigned short icmp_data_size = DEFAULT_PING_DATA_SIZE;
+static unsigned short icmp_pkt_size = DEFAULT_PING_DATA_SIZE + ICMP_MINLEN;
+
static unsigned int icmp_sent = 0, icmp_recv = 0, icmp_lost = 0;
#define icmp_pkts_en_route (icmp_sent - (icmp_recv + icmp_lost))
static unsigned short targets_down = 0, targets = 0, packets = 0;
static unsigned int icmp_sent = 0, icmp_recv = 0, icmp_lost = 0;
#define icmp_pkts_en_route (icmp_sent - (icmp_recv + icmp_lost))
static unsigned short targets_down = 0, targets = 0, packets = 0;
}
static int
}
static int
-handle_random_icmp(char *packet, struct sockaddr_in *addr)
+handle_random_icmp(unsigned char *packet, struct sockaddr_in *addr)
{
struct icmp p, sent_icmp;
struct rta_host *host = NULL;
{
struct icmp p, sent_icmp;
struct rta_host *host = NULL;
* to RFC 792). If it isn't, just ignore it */
memcpy(&sent_icmp, packet + 28, sizeof(sent_icmp));
if(sent_icmp.icmp_type != ICMP_ECHO || sent_icmp.icmp_id != pid ||
* to RFC 792). If it isn't, just ignore it */
memcpy(&sent_icmp, packet + 28, sizeof(sent_icmp));
if(sent_icmp.icmp_type != ICMP_ECHO || sent_icmp.icmp_id != pid ||
- sent_icmp.icmp_seq >= targets)
+ sent_icmp.icmp_seq >= targets*packets)
{
if(debug) printf("Packet is no response to a packet we sent\n");
return 0;
}
/* it is indeed a response for us */
{
if(debug) printf("Packet is no response to a packet we sent\n");
return 0;
}
/* it is indeed a response for us */
- host = table[sent_icmp.icmp_seq];
+ host = table[sent_icmp.icmp_seq/packets];
if(debug) {
printf("Received \"%s\" from %s for ICMP ECHO sent to %s.\n",
get_icmp_error_msg(p.icmp_type, p.icmp_code),
if(debug) {
printf("Received \"%s\" from %s for ICMP ECHO sent to %s.\n",
get_icmp_error_msg(p.icmp_type, p.icmp_code),
packets = 5;
}
packets = 5;
}
+ /* Parse extra opts if any */
+ argv=np_extra_opts(&argc, argv, progname);
+
/* parse the arguments */
for(i = 1; i < argc; i++) {
while((arg = getopt(argc, argv, "vhVw:c:n:p:t:H:s:i:b:I:l:m:")) != EOF) {
/* parse the arguments */
for(i = 1; i < argc; i++) {
while((arg = getopt(argc, argv, "vhVw:c:n:p:t:H:s:i:b:I:l:m:")) != EOF) {
+ long size;
switch(arg) {
case 'v':
debug++;
break;
case 'b':
switch(arg) {
case 'v':
debug++;
break;
case 'b':
- /* silently ignored for now */
+ size = strtol(optarg,NULL,0);
+ if (size >= (sizeof(struct icmp) + sizeof(struct icmp_ping_data)) &&
+ size < MAX_PING_DATA) {
+ icmp_data_size = size;
+ icmp_pkt_size = size + ICMP_MINLEN;
+ } else
+ usage_va("ICMP data length must be between: %d and %d",
+ sizeof(struct icmp) + sizeof(struct icmp_ping_data),
+ MAX_PING_DATA - 1);
+
break;
case 'i':
pkt_interval = get_timevar(optarg);
break;
case 'i':
pkt_interval = get_timevar(optarg);
set_source_ip(optarg);
break;
case 'V': /* version */
set_source_ip(optarg);
break;
case 'V': /* version */
- /*print_revision (progname, revision);*/ /* FIXME: Why? */
+ print_revision (progname, NP_VERSION);
exit (STATE_OK);
case 'h': /* help */
print_help ();
exit (STATE_OK);
case 'h': /* help */
print_help ();
}
}
}
}
- icmp_pkt_size = icmp_data_size + ICMP_MINLEN;
- if(debug > 2) printf("icmp_pkt_size = %u\n", icmp_pkt_size);
- if(icmp_pkt_size < sizeof(struct icmp) + sizeof(struct icmp_ping_data)) {
- icmp_pkt_size = sizeof(struct icmp) + sizeof(struct icmp_ping_data);
- }
- if(debug > 2) printf("icmp_pkt_size = %u\n", icmp_pkt_size);
-
if(debug) {
printf("crit = {%u, %u%%}, warn = {%u, %u%%}\n",
crit.rta, crit.pl, warn.rta, warn.pl);
if(debug) {
printf("crit = {%u, %u%%}, warn = {%u, %u%%}\n",
crit.rta, crit.pl, warn.rta, warn.pl);
table = malloc(sizeof(struct rta_host **) * (argc - 1));
i = 0;
while(host) {
table = malloc(sizeof(struct rta_host **) * (argc - 1));
i = 0;
while(host) {
- host->id = i;
+ host->id = i*packets;
table[i] = host;
host = host->next;
i++;
table[i] = host;
host = host->next;
i++;
wait_for_reply(int sock, u_int t)
{
int n, hlen;
wait_for_reply(int sock, u_int t)
{
int n, hlen;
- static char buf[4096];
+ static unsigned char buf[4096];
struct sockaddr_in resp_addr;
struct ip *ip;
struct icmp icp;
struct sockaddr_in resp_addr;
struct ip *ip;
struct icmp icp;
/* check the response */
memcpy(&icp, buf + hlen, sizeof(icp));
/* check the response */
memcpy(&icp, buf + hlen, sizeof(icp));
- if(icp.icmp_id != pid) {
- handle_random_icmp(buf + hlen, &resp_addr);
- continue;
- }
-
- if(icp.icmp_type != ICMP_ECHOREPLY || icp.icmp_seq >= targets) {
+ if(icp.icmp_id != pid || icp.icmp_type != ICMP_ECHOREPLY || icp.icmp_seq >= targets*packets) {
if(debug > 2) printf("not a proper ICMP_ECHOREPLY\n");
handle_random_icmp(buf + hlen, &resp_addr);
continue;
if(debug > 2) printf("not a proper ICMP_ECHOREPLY\n");
handle_random_icmp(buf + hlen, &resp_addr);
continue;
/* this is indeed a valid response */
memcpy(&data, icp.icmp_data, sizeof(data));
/* this is indeed a valid response */
memcpy(&data, icp.icmp_data, sizeof(data));
+ if (debug > 2)
+ printf("ICMP echo-reply of len %u, id %u, seq %u, cksum 0x%X\n",
+ sizeof(data), icp.icmp_id, icp.icmp_seq, icp.icmp_cksum);
- host = table[icp.icmp_seq];
+ host = table[icp.icmp_seq/packets];
gettimeofday(&now, &tz);
tdiff = get_timevaldiff(&data.stime, &now);
host->time_waited += tdiff;
host->icmp_recv++;
icmp_recv++;
gettimeofday(&now, &tz);
tdiff = get_timevaldiff(&data.stime, &now);
host->time_waited += tdiff;
host->icmp_recv++;
icmp_recv++;
+ if (tdiff > host->rtmax)
+ host->rtmax = tdiff;
+ if (tdiff < host->rtmin)
+ host->rtmin = tdiff;
if(debug) {
if(debug) {
- printf("%0.3f ms rtt from %s, outgoing ttl: %u, incoming ttl: %u\n",
+ printf("%0.3f ms rtt from %s, outgoing ttl: %u, incoming ttl: %u, max: %0.3f, min: %0.3f\n",
(float)tdiff / 1000, inet_ntoa(resp_addr.sin_addr),
(float)tdiff / 1000, inet_ntoa(resp_addr.sin_addr),
- ttl, ip->ip_ttl);
+ ttl, ip->ip_ttl, (float)host->rtmax / 1000, (float)host->rtmin / 1000);
}
/* if we're in hostcheck mode, exit with limited printouts */
}
/* if we're in hostcheck mode, exit with limited printouts */
send_icmp_ping(int sock, struct rta_host *host)
{
static union {
send_icmp_ping(int sock, struct rta_host *host)
{
static union {
- char *buf; /* re-use so we prevent leaks */
+ void *buf; /* re-use so we prevent leaks */
struct icmp *icp;
u_short *cksum_in;
} packet = { NULL };
struct icmp *icp;
u_short *cksum_in;
} packet = { NULL };
packet.icp->icmp_code = 0;
packet.icp->icmp_cksum = 0;
packet.icp->icmp_id = pid;
packet.icp->icmp_code = 0;
packet.icp->icmp_cksum = 0;
packet.icp->icmp_id = pid;
- packet.icp->icmp_seq = host->id;
+ packet.icp->icmp_seq = host->id++;
packet.icp->icmp_cksum = icmp_checksum(packet.cksum_in, icmp_pkt_size);
packet.icp->icmp_cksum = icmp_checksum(packet.cksum_in, icmp_pkt_size);
+ if (debug > 2)
+ printf("Sending ICMP echo-request of len %u, id %u, seq %u, cksum 0x%X to host %s\n",
+ sizeof(data), packet.icp->icmp_id, packet.icp->icmp_seq, packet.icp->icmp_cksum, host->name);
+
len = sendto(sock, packet.buf, icmp_pkt_size, 0, (struct sockaddr *)addr,
sizeof(struct sockaddr));
len = sendto(sock, packet.buf, icmp_pkt_size, 0, (struct sockaddr *)addr,
sizeof(struct sockaddr));
}
static int
}
static int
-recvfrom_wto(int sock, char *buf, unsigned int len, struct sockaddr *saddr,
+recvfrom_wto(int sock, void *buf, unsigned int len, struct sockaddr *saddr,
u_int *timo)
{
u_int slen;
u_int *timo)
{
u_int slen;
host = list;
while(host) {
if(debug) puts("");
host = list;
while(host) {
if(debug) puts("");
- printf("%srta=%0.3fms;%0.3f;%0.3f;0; %spl=%u%%;%u;%u;; ",
+ printf("%srta=%0.3fms;%0.3f;%0.3f;0; %spl=%u%%;%u;%u;; %srtmax=%0.3fms;;;; %srtmin=%0.3fms;;;; ",
(targets > 1) ? host->name : "",
host->rta / 1000, (float)warn.rta / 1000, (float)crit.rta / 1000,
(targets > 1) ? host->name : "",
host->rta / 1000, (float)warn.rta / 1000, (float)crit.rta / 1000,
- (targets > 1) ? host->name : "",
- host->pl, warn.pl, crit.pl);
+ (targets > 1) ? host->name : "", host->pl, warn.pl, crit.pl,
+ (targets > 1) ? host->name : "", (float)host->rtmax / 1000,
+ (targets > 1) ? host->name : "", (host->rtmin < DBL_MAX) ? (float)host->rtmin / 1000 : (float)0);
host = host->next;
}
host = host->next;
}
if(!early) early = &prog_start;
/* if early > later we return 0 so as to indicate a timeout */
if(!early) early = &prog_start;
/* if early > later we return 0 so as to indicate a timeout */
- if(early->tv_sec > early->tv_sec ||
+ if(early->tv_sec > later->tv_sec ||
(early->tv_sec == later->tv_sec && early->tv_usec > later->tv_usec))
{
return 0;
(early->tv_sec == later->tv_sec && early->tv_usec > later->tv_usec))
{
return 0;
host->saddr_in.sin_family = AF_INET;
host->saddr_in.sin_addr.s_addr = in->s_addr;
host->saddr_in.sin_family = AF_INET;
host->saddr_in.sin_addr.s_addr = in->s_addr;
+ host->rtmin = DBL_MAX;
+
if(!list) list = cursor = host;
else cursor->next = host;
if(!list) list = cursor = host;
else cursor->next = host;
print_help(void)
{
print_help(void)
{
- /*print_revision (progname, revision);*/ /* FIXME: Why? */
+ /*print_revision (progname);*/ /* FIXME: Why? */
printf ("Copyright (c) 2005 Andreas Ericsson <ae@op5.se>\n");
printf (COPYRIGHT, copyright, email);
printf ("Copyright (c) 2005 Andreas Ericsson <ae@op5.se>\n");
printf (COPYRIGHT, copyright, email);
print_usage ();
printf (_(UT_HELP_VRSN));
print_usage ();
printf (_(UT_HELP_VRSN));
+ printf (_(UT_EXTRA_OPTS));
printf (" %s\n", "-H");
printf (" %s\n", _("specify a target"));
printf (" %s\n", "-w");
printf (" %s", _("warning threshold (currently "));
printf (" %s\n", "-H");
printf (" %s\n", _("specify a target"));
printf (" %s\n", "-w");
printf (" %s", _("warning threshold (currently "));
- printf ("%0.3fms,%u%%)\n", (float)warn.rta / 1000 , warn.pl / 1000);
+ printf ("%0.3fms,%u%%)\n", (float)warn.rta / 1000, warn.pl);
printf (" %s\n", "-c");
printf (" %s", _("critical threshold (currently "));
printf (" %s\n", "-c");
printf (" %s", _("critical threshold (currently "));
- printf ("%0.3fms,%u%%)\n", (float)crit.rta, crit.pl);
+ printf ("%0.3fms,%u%%)\n", (float)crit.rta / 1000, crit.pl);
printf (" %s\n", "-s");
printf (" %s\n", _("specify a source IP address or device name"));
printf (" %s\n", "-n");
printf (" %s\n", "-s");
printf (" %s\n", _("specify a source IP address or device name"));
printf (" %s\n", "-n");
printf ("\n");
printf (" %s\n", "-l");
printf (" %s", _("TTL on outgoing packets (currently "));
printf ("\n");
printf (" %s\n", "-l");
printf (" %s", _("TTL on outgoing packets (currently "));
- printf ("%u)", ttl);
+ printf ("%u)\n", ttl);
printf (" %s\n", "-t");
printf (" %s",_("timeout value (seconds, currently "));
printf ("%u)\n", timeout);
printf (" %s\n", "-b");
printf (" %s\n", "-t");
printf (" %s",_("timeout value (seconds, currently "));
printf ("%u)\n", timeout);
printf (" %s\n", "-b");
- printf (" %s\n", _("icmp packet size (currenly ignored)"));
+ printf (" %s\n", _("Number of icmp data bytes to send"));
+ printf (" %s %u + %d)\n", _("Packet size will be data bytes + icmp header (currently"),icmp_data_size, ICMP_MINLEN);
printf (" %s\n", "-v");
printf (" %s\n", _("verbose"));
printf (" %s\n", "-v");
printf (" %s\n", _("verbose"));
printf ("%s\n\n", _("NOTE: Some systems decrease TTL when forming ICMP_ECHOREPLY, others do not."));*/
printf ("\n");
printf (" %s\n", _("The -v switch can be specified several times for increased verbosity."));
printf ("%s\n\n", _("NOTE: Some systems decrease TTL when forming ICMP_ECHOREPLY, others do not."));*/
printf ("\n");
printf (" %s\n", _("The -v switch can be specified several times for increased verbosity."));
-
/* printf ("%s\n", _("Long options are currently unsupported."));
printf ("%s\n", _("Options marked with * require an argument"));
*/
/* printf ("%s\n", _("Long options are currently unsupported."));
printf ("%s\n", _("Options marked with * require an argument"));
*/
+#ifdef NP_EXTRA_OPTS
+ printf ("\n");
+ printf (_(UT_EXTRA_OPTS_NOTES));
+#endif
printf (_(UT_SUPPORT));
}
printf (_(UT_SUPPORT));
}