#!/usr/bin/perl -wT
#
# $Id: check_wave.pl,v 1.4 2002/10/28 13:05:08 kdebisschop Exp $

require 5.004;
use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw(:DEFAULT :standard :thresholds);

use vars qw($PROGNAME $opt_t
	    $opt_w $opt_c $opt_H);

use utils qw(%ERRORS);

$w_opt->optional("yes");
$c_opt->optional("yes");
my $plugin = new Plugin(-revision => '$Revision: 1.4 $',
			-copyright => "2000 Jeffery Blank/Karl DeBisschop, 2004 Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment => "Plugin to test signal strength on Speedlan Wireless Equipment",
			-parameterlists => [ [ $H_opt, $w_opt, $c_opt, $t_opt ], $h_opts, $V_opts ]);

my (@test, $low1, $med1, $high1, $snr, $low2, $med2, $high2);
my ($low, $med, $high, $lowavg, $medavg, $highavg, $tot, $ss);

my $snmpget = "/usr/bin/snmpget";

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

$plugin->init();

my $host = $opt_H;

my $critical = $1 if (defined $opt_c && $opt_c =~ /([0-9]+)/);

my $warning = $1 if (defined $opt_w && $opt_w =~ /([0-9]+)/);

$plugin->start_timeout($opt_t, "Timed out while retrieving signal data");

$low1 = `$snmpget -v 1 -c public $host .1.3.6.1.4.1.74.2.21.1.2.1.8.1`;
@test = split(/ /,$low1);
$low1 = $test[2];

$med1 = `$snmpget -v 1 -c public $host .1.3.6.1.4.1.74.2.21.1.2.1.9.1`;
@test = split(/ /,$med1);
$med1 = $test[2];

$high1 = `$snmpget -v 1 -c public $host .1.3.6.1.4.1.74.2.21.1.2.1.10.1`;
@test = split(/ /,$high1);
$high1 = $test[2];

sleep(2);

$snr = `$snmpget -v 1 -c public $host .1.3.6.1.4.1.762.2.5.2.1.17.1`;
@test = split(/ /,$snr);
$snr = $test[2];
$snr = int($snr*25);

$low2 = `$snmpget -v 1 -c public $host .1.3.6.1.4.1.74.2.21.1.2.1.8.1`;
@test = split(/ /,$low2);
$low2 = $test[2];

$med2 = `$snmpget -v 1 -c public $host .1.3.6.1.4.1.74.2.21.1.2.1.9.1`;
@test = split(/ /,$med2);
$med2 = $test[2];

$high2 = `$snmpget -v 1 -c public $host .1.3.6.1.4.1.74.2.21.1.2.1.10.1`;
@test = split(/ /,$high2);
$high2 = $test[2];

$plugin->stop_timeout();

$low = $low2 - $low1;
$med = $med2 - $med1;
$high = $high2 - $high1;

$tot = $low + $med + $high;

if ($tot==0) {
	$ss = 0;
} else {
	$lowavg = $low / $tot;
	$medavg = $med / $tot;
	$highavg = $high / $tot;
	$ss = ($medavg*50) + ($highavg*100);
}

my $state = 'OK';

if ($ss<$critical) {
	$state = 'CRITICAL';
} elsif ($ss<$warning) {
	$state = 'WARNING';
}

printf("$PROGNAME $state: Signal Strength at: %3.0f%,  SNR at $snr%",$ss);

exit $ERRORS{$state};
