From 3bec374bb21c4d6226bb4de94bc9f704e3821ea8 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Sat, 19 Jan 2019 12:12:57 +0100 Subject: [PATCH] Add scripts to ping other hosts --- zabbix-agent-addons.spec | 1 + zabbix_conf/icmp.conf | 6 +++++ zabbix_scripts/check_icmp_sudo | 45 ++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 zabbix_conf/icmp.conf create mode 100644 zabbix_scripts/check_icmp_sudo diff --git a/zabbix-agent-addons.spec b/zabbix-agent-addons.spec index 922a460..b06b165 100644 --- a/zabbix-agent-addons.spec +++ b/zabbix-agent-addons.spec @@ -22,6 +22,7 @@ Requires: perl(POSIX) Requires: perl(MIME::Base64) Requires: perl(File::Which) Requires: perl(Config::Simple) +Requires: fping %if ! 0%{?_without_selinux} Requires: policycoreutils BuildRequires: selinux-policy-devel diff --git a/zabbix_conf/icmp.conf b/zabbix_conf/icmp.conf new file mode 100644 index 0000000..d5a652a --- /dev/null +++ b/zabbix_conf/icmp.conf @@ -0,0 +1,6 @@ +# net.icmp takes two args. The host to ping (either an IP or a host name), and one of +# * all: returns info in JSON format +# * latency: returns latency in seconds. Floating number +# * respond: returns 0 if no response where received, 1 otherwise +# * loss: returns % of packet loss. Floating number +UserParameter=net.icmp[*],/usr/bin/sudo /var/lib/zabbix/bin/check_icmp_sudo $1 --info=$2 diff --git a/zabbix_scripts/check_icmp_sudo b/zabbix_scripts/check_icmp_sudo new file mode 100644 index 0000000..7bcfc9e --- /dev/null +++ b/zabbix_scripts/check_icmp_sudo @@ -0,0 +1,45 @@ +#!/usr/bin/perl -w + +use warnings; +use strict; +use File::Which; +use Getopt::Long; +use JSON; + +my $fping = which('fping'); + +unless ($fping){ + die "ZBX_NOTSUPPOTED\n"; +} + +my $info = 'all'; +my $pretty = 0; +my @valid_info = qw(all respond latency loss); +my $host = $ARGV[0]; + +GetOptions( + 'info=s' => \$info, + 'pretty' => \$pretty +); + +unless (grep { $info eq $_ } @valid_info){ + die "Usage: $0 [--info=] host\n"; +} + +my $ping = qx($fping -c 5 -p 10 -q $host 2>&1); +# Output looks like 10.29.254.2 : xmt/rcv/%loss = 5/5/0%, min/avg/max = 1.42/1.65/1.90 +if ($ping =~ m|^$host : xmt/rcv/%loss = 5/(\d)/(\d+(?:\.\d+)?)%(?:, min/avg/max = (?:\d+(?:\.\d+)?)/(\d+(\.\d+))/(?:\d+(?:\.\d+)?))?$|){ + my $stat = { + respond => ($1 > 0) ? 1 : 0, + loss => $2 + 0, + latency => (defined $3) ? $3 / 1000 : 0 + }; + if ($info ne 'all'){ + print $stat->{$info} . "\n"; + } else { + print to_json($stat, { pretty => $pretty }) . "\n"; + } +} else { + die "ZBX_NOTSUPPOTED\n"; +} +exit 0;