#!/usr/local/bin/perl 

# $Header: /home/vikas/src/nocol/perlnocol/RCS/modemmon,v 1.3 1999/11/01 14:21:11 vikas Exp $
#
#        modemmon - perl monitor for modem line's usage on cisco's
#
#	Copyright 1994. Vikas Aggarwal,  vikas@navya.com
#
#	Portions copyright 1993 JvNCnet
#
# Modemmon  reads a list of cisco terminal servers to monitor (from
# a config file) and thresholds. It then logs into the cisco's and
# counts the number of lines in use. When the used lines exceeds the
# thresholds, the lines are displayed in NOCOL data format.
# Requires the 'rcisco' program for logging into the cisco specified.
# Identifies dialin lines from the description field of the lines.
#
# Nocol event elements used:
#   sender                     "modemmon"
#   severity                   as read from the config file
#   site
#    name                      the cisco name
#    addr                      cisco IP address
#   var                       
#    name                      "ModemLines"
#    value                     1 means at Info level
#    threshold                 as read from the config file
#    units                     always "Usage"
#
## 
##
#
#
############################
## Variables customization #  overrides values in the nocollib.pl library
############################
$rprog="rcisco";			# Path for rcisco.
$rpasswd="";				# if NULL, uses the default in rcisco
$rcommand="who";

#$rprog="/usr/bin/finger";		# if using 'finger' instead
#$rcommand="-s \@";

$varname="ModemLines";
$varunits="Usage" ;			# the var.units field in EVENT struct
$sleepint=60*5;       			# Seconds to sleep between tries.
############################
$debug = 0;				# set to 1 for debugging output
$libdebug = 0;				# set to 1 for debugging output

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 {
    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 router
#
sub dotest {
    local ($host, $router) = @_ ;
    local ($acount, $isok) = (0, 0);
    local ($loginok) = 0;

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

    open (ROUTER, "$command|") ;
	
    while(<ROUTER>) {
	tr/\r\n//d;
	if ($rprog =~ /rcisco/) {
	    if ( />/ ) {$loginok = 1 ;} # got the 'Router>' prompt
	    if ( /Dial-in/ )  {$acount += 1 ;}
	    elsif ($debug) {
#		print "(debug) skipping line: $_\n" ;
	    }
	} 
	else {		# using finger command
	  if ( /login|logged|user/i ) {$loginok = 1 ;}	# got a real response
	  if (substr($_, 32, 4) =~ /S\d+/ || substr($_, 5, 3) eq "tty") {
	    $acount += 1 ;
	  }
	  elsif ($debug) {
#		print "(debug) skipping line: $_\n" ;
	  }

	}
    }  # end while <ROUTER>
    close (ROUTER);
#    sleep 5 ;			# process needs to die off ??

    if ($loginok == 0) { 
	print "Login into remote host failed\n" ;
	$acount = -1 ;
    }
    if ($debug) {print "Number of users= $acount\n" ;}

    if ($acount < 0) { return (-1, 0); } # negative status to be ignored

    ($isok, $varthres{$item}, $maxseverity) = 
	&calc_status ($acount, $wlevel{$item}, $elevel{$item}, $clevel{$item});

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

}	# end &dotest()



###
### main
###

&nocol_main;

#

