#!/usr/bin/perl 
# -*- perl -*-

#===============================================================================
# Name
#   check_pim
#
# Description
#   Checks for a PIM neighbor 
#   Returns text to STDOUT and a return code to be used by the netsaint process
#
# TODO
#
# Author
#  Original author Matthew Davy (mpd@iu.edu)
#
# Copyright
#  Copyright (c) 2001 The Trustees of Indiana University.
#  All Rights Reserved.
#===============================================================================

#===============================================================================
#                               Configuration        
#===============================================================================

my $lastScriptModification = '2001-08-10';

#===============================================================================
#                              Initialization
#===============================================================================

#-------------------------------------------------------------------------------
#    Libraries
#-------------------------------------------------------------------------------

use Getopt::Std;


#-------------------------------------------------------------------------------
#    Global var declarations
#-------------------------------------------------------------------------------
my (
   # Working vars 
   $usage,		# String containing usage help
   $hostname,		# Hostname of machine being queried via SNMP
   $community,		# SNMP community string to use for query
   $oid			# PIM Neighbor UpTime OID + Peer IP Address
   );

#-------------------------------------------------------------------------------
#    Global var initializations
#-------------------------------------------------------------------------------

$usage = <<"EOF";
usage:  $0 [-h] -r <hostname> -c <community> -p <peer>

[-h]            :       Print this message
[-r] <router>   :       IP Address or Hostname of the router
[-c] <community>:       SNMP Community String  (default = "public")
[-p] <peer>     :       IP address of the PIM peer
                               
$lastScriptModification
 
EOF

#===============================================================================
#                              Input Phase
#===============================================================================

# Check the usage
die $usage if (!getopts('hr:c:p:') || $opt_h);
die $usage if (!$opt_r || !$opt_p || $opt_h);

# Build the args for the check_snmp command 
$hostname = $opt_r;
$community = $opt_c || "public";
$oid = ".1.3.6.1.3.61.1.1.3.1.3.$opt_p";

# Call the check_snmp command with the appropriate args
$_ = `/usr/lib/nagios/plugins/check_snmp -H $hostname -C $community -o $oid`;


#===============================================================================
#                              Output Phase
#===============================================================================

# Print a line to STDOUT and generate an exit code based on what the 
# check_snmp command returns.

if (s/^.*\)//) {
	chop;
        print "PIM Neighbor UP (Uptime = $_)\n";
        exit(0);
} else {
        print "PIM Neighbor DOWN or Not Configured\n";
        exit(2);
}

