#! /usr/bin/perl -wT
#
# check_atalk_ping plugin for nagios
#
# usage:
#    check_atalk_ping atalkaddress
#
# Checks if an atalkhost responds to an atalk echo
# using "aecho"
#
# initial version: 23 October 2002 by Stefan Beck, IT Software Solutions
# current status: $Revision: 1.1 $
#
# Copyright Notice: GPL
#

delete $ENV{'LANG'};

require 5.004;

use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw($helpparameterlist $versionparameterlist $hostnameparameter
			 $warningparameter $criticalparameter $timeoutparameter
			 $verboseparameter);
use vars qw($opt_p $opt_c $opt_w $opt_H $opt_h $xml $opt_v $opt_t $PROGNAME
	    $warning_avg $warning_loss $critical_avg $critical_loss);
use utils qw(%ERRORS &usage);

$warningparameter->type("THRESHOLD");
$warningparameter->checker(sub { my ($opt, $parameter, $plugin) = @_;
				 &Plugin::Parameter::_check_threshold($opt, $parameter, $plugin);
				 $$opt =~ m/^(\d+)[,:](\d+)%$/;
				 $warning_avg = $1;
				 $warning_loss = $2; });
$warningparameter->description("warning threshold pair, THRESHOLD is <rta>,<pl>% where <rta> is the round trip average travel time (ms) and <pl> is the percentage of packet loss to trigger an alarm state");
$criticalparameter->type("THRESHOLD");
$criticalparameter->checker(sub { my ($opt, $parameter, $plugin) = @_;
				 &Plugin::Parameter::_check_threshold($opt, $parameter, $plugin);
				 $$opt =~ m/^(\d+)[,:](\d+)%$/;
				 $critical_avg = $1;
				 $critical_loss = $2; });
$criticalparameter->description("critical threshold pair, THRESHOLD is <rta>,<pl>% where <rta> is the round trip average travel time (ms) and <pl> is the percentage of packet loss to trigger an alarm state");
my $packetsparameter = new Plugin::Parameter(-name => "packets", -flags => [ 'p', 'packets' ],
					     -optional => 1, -valueoptional => 0, -type => "INTEGER", -default => 5,
					     -checker => \&Plugin::Parameter::_check_integer,
					     -description => "number of ICMP ECHO packets to send");
my @commandparameterlist = ( $hostnameparameter,
			     $warningparameter,
			     $criticalparameter,
			     $packetsparameter,
			     $timeoutparameter,
			     $verboseparameter );

my $plugin = new Plugin(-revision => '$Revision: 1.1 $',
			-copyright => "2002 Stefan Beck",
			-shortcomment => "Check if an atalkhost responds to an atalk echo using 'aecho -c <packets> <atalkhost>'",
			-parameterlists => [ \@commandparameterlist, $helpparameterlist, $versionparameterlist ] );

$ENV{'PATH'}='';
$ENV{'IFS'}='';
$ENV{'CDPATH'}='';
$ENV{'ENV'}='';
$ENV{'BASH_ENV'}='';

$plugin->init();

$plugin->start_timeout($opt_t, "Plugin timeout");

my $cmd = "/usr/bin/aecho -c $opt_p $opt_H 2>&1 |";
print "$cmd\n" if ($opt_v);
open CMD, $cmd;

my $avg= undef;
my $loss = undef;
my $line = undef;

while (<CMD>) {
  print $_ if ($opt_v);
  $line = $_;

  # 5 packets sent, 5 packets received, 0% packet loss
  # round-trip (ms)  min/avg/max = 0/0/0

  if (/received, ([0-9]+)% packet loss/) {
    $loss = $1;
  }
  if (/min\/avg\/max = [0-9]+\/([0-9]+)\/[0-9]+/) {
    $avg = $1;
  }
}
close CMD;

$plugin->stop_timeout();

my $state  = "OK";
my $answer = undef;

if ( defined $loss && defined $avg ) {
  if ( $loss >= $critical_loss || $avg >= $critical_avg ) { $state = "CRITICAL"; }
  elsif ( $loss >= $warning_loss || $avg >= $warning_avg ) { $state = "WARNING"; }
  else { $state = "OK"; }
  $answer = "$state - Packet loss = $loss%, RTA = $avg ms";
} else {
  $state  = "UNKNOWN";
  $answer = "UNKNOWN: ";
  $answer .= chomp($line) if ($line);
}

print "$PROGNAME $answer\n";

exit $ERRORS{$state};
