#!/usr/local/bin/perl 
#
# $Header: /home/vikas/netmgt/nocol/src/perlnocol/RCS/bgpmon,v 1.3 1994/11/29 20:48:57 vikas Exp $
#
#        bgpmon - perl monitor for BGP processes on cisco's
#
# Runs the command 'show ip bgp summary' on cisco routers and parses
# the output to see if any peer is 'Idle/Active/Down' state (i.e. not
# connected). If so, it marks the event as down. (NOTE: cisco router's
# indicate the time that a BGP peer has been active, so its easy to
# tell if a peer is not connected).
#
# Part of the NOCOL monitoring package.
#
# Author: Vikas Aggarwal,  vikas@navya.com
#
# 	Copyright 1994 Vikas Aggarwal, vikas@navya.com
#
## 
##
#
#
############################
## Variables customization #  overrides values in the nocollib.pl library
############################
$rprog="rcisco";			# Path for rcisco.
$rpasswd="";				# if NULL, uses the default in rcisco
$rcommand="show ip bgp summary";
$varname="BGPpeerDown";
$varunits="State" ;			# the var.units field in EVENT struct
$sleepint=60*10;       			# Seconds to sleep between tries.
############################
$debug = 0;				# set to 1 for debugging output
$libdebug = 0;				# set to 1 for debugging output
$maxseverity = $E_CRITICAL ;
$prognm = $0;				# save program name

require  "nocollib.pl" ;

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

##
# Read the config file. Use '\t' as a separator for 'item'
sub readconf {
    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*$/)
	{
	    $item="$1\t$2" ;	 # the name and address
	    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 router
#
sub dotest {
    local ($host, $router) = @_ ;
    local ($peer, $state, $peersdown);
    local ($loginok) = 0;

    if ($debug) { print "Checking $router\n"; }
    $command="$rprog $router ".' "'."$rpasswd".'" '.'"'."$rcommand".'"';
    if ($debug) {print "(debug) dotest: running command $command\n" ;}

    open (ROUTER, "$command |") ;
	
    while(<ROUTER>) {
	tr/\r\n//d;
	if ( />/ ) {$loginok = 1 ;} # got the 'Router>' prompt
	if ( /^\s*(\d+\.\d+.\d+.\d+).*\s+(\S+)\s*$/ )
	{
#	    if ($debug) {print "(debug) processing line: $_\n" ;}
	    $peer = $1;
	    $state = $2;
	    if ($debug) { print "(debug): Peer= $peer, State= $state\n"; }
	    if ( $2 =~ /Active|Idle|Down/ ) { ++$peersdown ;}
	}
	elsif ($debug) {
#	    print "(debug) skipping line: $_\n" ;
	}
    }  # end while
    close (ROUTER);

    if ($loginok == 0) { 
	print "Login into remote host $router failed\n" ;
	return(-1, 0);	# (status, value)
    }
    if ($debug) {print "Peers Down= $peersdown\n" ;}

    if ($peersdown) {return (0, $peersdown) ;}	# not caring about timeouts
    else { return(1, $peersdown) ;}		# no peer is down, status=1	

} # end: dotest()



###
### main
###

&nocol_main ;



