#!/usr/bin/perl -w
#
# Check Next Hop
#snmpget -v1 -c northpub gateway RFC1213-MIB::ipRouteNextHop.0.0.0.0

use strict;
use Getopt::Std;

my $snmp_get = "/usr/bin/snmpget";
my $oid = "RFC1213-MIB::ipRouteNextHop.0.0.0.0";
my $check_icmp = "/usr/local/nagios/libexec/check_icmp";

use vars qw($opt_v $opt_h $opt_H $opt_C $opt_w $opt_c);

my(%ERRORS) = ( OK=>0, WARNING=>1, CRITICAL=>2, UNKNOWN=>3, WARN=>1, CRIT=>2 );
my($message, $status);
my (@ignore);

sub print_usage () {
        print "Usage: \n\tcheck_next_hop [-v] [-h] -H host -C community -w warning -c critical\n\n";
}

sub print_help () {
        print <<HELP;

                Check Next Hop version 1.0
                Copyright 2010 Zachary McGibbon
                zachary.mcgibbon\@gmail.com

HELP

        print_usage();
}

# return true if parameter is not in ignore list
sub valid($) {
        my($v) = $_[0];
        $v = lc $v;
        foreach ( @ignore ) { return 0 if((lc $_) eq $v); }
        return 1;
}


sub check_snmp {
        my($C) = shift;
        my($H) = shift;
        my($w) = shift;
        my($c) = shift;

        my $snmp_cmd = "${snmp_get} -v1 -Ovq";
        my $community = $opt_C;
        my $host = $opt_H;
        my $warning = $opt_w;
        my $critical = $opt_c;

        my $command1 = `${snmp_cmd} -c ${community} ${host} ${oid}`;

        chomp($command1);

        my $next_hop = $command1;

        my $command2 = `$check_icmp -w $warning -c $critical -p 5 $next_hop`;

        chomp($command2);

        print $command2;
}

$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';

our($opt_H, $opt_C, $opt_w, $opt_c, $opt_v, $opt_h);

getopts("H:C:w:c:v:h");

my @vars = ($opt_H, $opt_C, $opt_w, $opt_c);

if ($opt_h) {print_help()};

foreach ( @vars ) {
        unless($_) {
                print_help();
                exit $ERRORS{WARNING};
        }
}

$status = $ERRORS{OK}; $message = '';

check_snmp($opt_C,$opt_H,$opt_w, $opt_c);
