#!/bin/sh

: << =cut

=head1 NAME

ping_ - Plugin to monitor ping times

=head1 CONFIGURATION

The following environment variables are used by this plugin

 ping_args           - Arguments to ping (default "-c 2")
 ping_args2          - Arguments after the host name (required for Solaris)
 ping                - Ping program to use
 host                - Host to ping. By default taken from command name.
 ping_warning        - Warning limit for nagios notification
 ping_critical       - Critical limit for nagios notification
 packetloss_warning  - Warning limit for nagios notification
 packetloss_critical - Critical limit for nagios notification
 enable_packetloss_graph - Display the graph of packetlosses (disabled by default)

=head2 CONFIGURATION EXAMPLES

The following configuration should be used for solaris hosts:

 [ping_*]
  env.ping_args      -s
  env.ping_args2     56 2

=head1 AUTHOR

Copyright (C) 2004 Jimmy Olsen

=head1 LICENSE

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 dated June, 1991.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

=head1 MAGIC MARKERS

 #%# family=manual

=cut

# shellcheck disable=SC1090
. "$MUNIN_LIBDIR/plugins/plugin.sh"

case $0 in
    *ping4_*)
        if command -v ping4 >/dev/null; then
            ping_func() { ping4 "$@"; }
        else
            ping_func() { ping -4 "$@"; }
        fi
        file_host=${0##*/ping_}
        V=IPv4
        ;;
    *ping6_*)
        # use "ping6" if available - otherwise fall back to "ping"
        if command -v ping6 >/dev/null; then
            ping_func() { ping6 "$@"; }
        else
            ping_func() { ping -6 "$@"; }
        fi
        file_host=${0##*/ping6_}
        V=IPv6
        ;;
    *ping_*)
        if command -v ping4 >/dev/null; then
            ping_func() { ping4 "$@"; }
        else
            # Some implementations (e.g. "inetutils-ping") do not support the "-4" argument.
            # This careful approach sadly prohibits the explicit selection of the IPv4 address
            # family.  Thus we are just hoping for the best (as this plugin always did).
            ping_func() { ping "$@"; }
        fi
        file_host=${0##*/ping_}
        V=IPv4
        ;;
esac

host=${host:-${file_host:-www.google.com}}
if [ -n "${ping:-}" ]; then
    ping_func() { "$ping" "$@"; }
fi

if [ "$1" = "config" ]; then
        echo "graph_title $V ping times to $host"
        echo 'graph_args --base 1000 -l 0'
        echo 'graph_vlabel roundtrip time (seconds)'
        echo 'graph_category network'
        echo 'graph_info This graph shows ping RTT statistics.'
        echo "ping.label $host"
        echo "ping.info Ping RTT statistics for $host."
        echo 'packetloss.label packet loss (%)'
        if [ "${enable_packetloss_graph:-0}" = "0" ]; then
                echo 'packetloss.graph no'
        fi
        print_warning ping
        print_warning packetloss
        print_critical ping
        print_critical packetloss
        exit 0
fi


# shellcheck disable=SC2086
ping_func ${ping_args:-'-c 2'} "${host}" ${ping_args2:-} \
	| perl -n -e 'print "ping.value ", $1 / 1000, "\n" if m@min/avg/max.*\s\d+(?:\.\d+)?/(\d+(?:\.\d+)?)/\d+(?:\.\d+)?@; print "packetloss.value $1\n" if /(\d+)% packet loss/;'
