#!/usr/local/bin/perl 
##
#
#  pm3t1e1mon - perl monitor for t1/e1 line status on pm3 terminal servers
#
#   - original code by Vikas Aggarwal, vikas@navya.com
#
#   - PM3 modifications by Steve Milton (milton@isomedia.com)
#
#
#####################
#
# pm3t1e1mon  reads a list of pm3 terminal servers to monitor (from
# a config file) and thresholds. It then logs into the pm3's and
# checks whether or not the PRI is up. 
#
# in the pm3t1e1-confg file, the threshold values are ignored, only 
# the up/down status is returned.
#
# Supplemental Packages Required:
#
# snmpget
#
# Files used:
#
# Nocol event elements used:
#   sender                     "pm3t1e1mon"
#   severity                   as read from the config file
#   site
#    name.ifnum                the pm3 name .dot. the t1/e1 interface number
#    addr                      pm3 IP address
#   var                       
#    name                      "Status"
#    value                     1 means at Info level
#    threshold                 as read from the config file
#    units                     always "Up/Down"
#
## 
##
#
############################
## Variables customization #  overrides values in the nocollib.pl library
############################
$prog =  "/usr/bin/snmpget" ;	# location of 'snmpget'

############################
$debug = 0;				# set to 1 for debugging output
$libdebug = 0;				# set to 1 for debugging output
$prognm = $0;				# save program name

$varname="Status";
$varunits="Up/Down" ;			# the var.units field in EVENT struct
$sleepint=60*5;       			# Seconds to sleep between tries.

require  "/nocol/bin/nocollib.pl" ;

-x $prog || die("Could not find executable $prog, exiting");

##
# Read the config file. Use '\t' as a separator for 'item'
sub readconf {
    local ($nets, $interface, $zone) ;

    open(CONFIG,"<$cfile")||die("Couldn't find $cfile, exiting");
    while(<CONFIG>)
    {
	chop;
	if(/^\s*#/) {next;}   # skip comments
	if(/^\s*$/) {next;}   # skip blank lines
	if (/\s*(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s*$/)
	{
	    $item="$1\t$2" ;	 # the name and address
	    $wlevel{$item} = $3; # Warning level
	    $elevel{$item} = $4; # Error level
	    $clevel{$item} = $5; # Critical level
	    push(@items,$item);
	}
	else {print "Ignoring illegal line: $_\n";}

    }	# end while(CONFIG)

    close(CONFIG);
    if(0>$#items){die("Nothing to monitor in $cfile, exiting")};
    if ($debug) {
	print "Items are:\n"; foreach (@items) { print "\t$_\n" } ;
    }
}				# end: readconf


# 
## Check state of each terminal server
##
#
sub dotest {
    local ($host, $router) = @_ ;
    local ($acount, $tcount, $isok) = (0, 0, 0);

    if ($debug) { print "Checking $router\n"; }

    -x $prog || die("Could not find executable $prog, exiting");

    if ($debug) { 
	print "Active cmd= $walkactive\nType cmd= $walktype\n\n" ;
    }
#
# Output of PMWHO command is:
#
    $mibvar = ".1.3.6.1.4.1.307.3.2.2.1.1.4";
    ($myhost,$ifnum) = $host =~ /^(\S+)\.(\d+)$/;
    $active = "";
    while(!($active =~ /^enterprises/)) {
	$active = `$prog -v 1 $router public $mibvar.$ifnum`;
	if (!($active =~ /^enterprises/)) { 
	    if ($debug) { print $active; }
	}
    }
#    chop($active);
    $tcount = 1;
    if ($active =~ /up\(1\)/)
    {
	$acount++;
    }
    if ($debug) { print "$active\n"; }

    if ($debug) { 
	print "(debug)Active/Total terminals: $acount/$tcount\n"; 
    }

    $isok = $acount;
    if ($debug) {print "(debug): Status $isok, MaxSev= $maxseverity\n";}
    return ($isok, $acount);
}	# end &dotest()


###
### main
###

## Since our dotest() and readconf() is pretty standard, we can call
## the nocollib.pl routine nocol_main() to do all the work for us...

&nocol_main;

#

