#!/usr/bin/perl
# polls each configured netbotz device with network address $IP[0]..$IP[n]
# collects the set points on the device but uses the setpoints defined
# here for alarms.
# does not collect information on door alarms, etc.
##
# NOTE if you have multiple netbotz devices you must create an
# array for each parameter shown below with a [0]..[1]..[n]
##
# pt	4/24/2002	install
# pt	4/29/2002	modify to detect telnet problems
# pt	6/10/2002	modify to allow multiple netbotz devices
# pt    6/11/2002       Bug fixes: improper color reporting, bad date format
#			add warn() so telnet errors show up in BBOUT
# pt    8/13/2002       Adapt to work with new version of Net::Telnet
# pt    7/30/2003       By popular demand, move red's to higher in the page
sub variables {
	$title="Netbotz environmental monitoring";
# maximum alarm level allowed on telnet failure; yellow or red,
	$maxalarm="yellow";
# hostname(s) and location(s) of netbotz, each must be a member of the
# $IP array
        $IP[0]="172.16.0.79";
        $location[0]="Data Center Example";
        $IP[1]="172.16.0.89";
        $location[1]="Local Communications Room Example";
        $IP[2]="172.17.1.77";
        $location[2]="Remote Communications Room 1 Example";
        $IP[3]="172.18.1.17";
        $location[3]="Remote Communications Room 2 Example";
# login for netbotz's configured above [0]..[n]
	$login[0]="console";
	$passwd[0]="pass";
	$login[1]="console";
	$passwd[1]="pass";
	$login[2]="console";
	$passwd[2]="pass";
	$login[3]="console";
	$passwd[3]="pass";
# temperature high (red) set point & yellow set point array
# this script does not make use of the set points defined
# in the netbotz.  make an array [0] .. [n] for each
# netbotz configured above.
# no yellow alarm configured for humidity at this time
	$tempHSP[0]=85;
	$tempYSP[0]=80;
	$humdHSP[0]=99;
	$tempHSP[1]=86;
	$tempYSP[1]=80;
	$humdHSP[1]=99;
	$tempHSP[2]=86;
	$tempYSP[2]=84;
	$humdHSP[2]=99;
	$tempHSP[3]=86;
	$tempYSP[3]=80;
	$humdHSP[3]=99;
# telnet prompt array
	$prompt[0]='/NetBotz\>/';
	$prompt[1]='/NetBotz\>/';
	$prompt[2]='/NetBotz\>/';
	$prompt[3]='/NetBotz\>/';
# extra debugging enabled? 1=yes
	$debug=0;
	$inputlog='/dev/null';
	if ($debug==1 ) {
		#linux "stdout" in proc filesystem
		#could not get STDOUT to work as input_log device any more
		$inputlog='/proc/self/fd/1';
		}
# output of telnet commands when in nodebug mode
# telnet login timeout modify if excess failures over slow link
	$loginTO="20";
# telnet command timeout; same as above
	$cmdTO="20";
##
# Settings below are not often changed
##
# command to get netbotz status
	$statuscmd="status";
# what we are looking for
	$statstring1="Temp\:";
	$statstring2="Humidity\:";
# telnet error mode
	$errmode="return";
# zero out some nonlocal variables
	$ip="";
	$I=0;
	$msgindex=0;
	$bblog="";
	$mycolor="green";
	$message="";
	$color="";
# Big Brother specific items here for running as ext script
# path to the big brother network client
        $bb=$ENV{BB};
# the actual machine name we want this to appear under
        $host="netbotz";
# bb brother display host
        $bbs=$ENV{BBDISP};
# what page to put this under
	$page="msgs";
# message type
	$msgtyp="status";
}
sub getStatus {
#
# ask the netbotz for its status using the $statuscmd and place the output
# in the @STAT array
#
	@STAT=	$t->cmd(String=>$statuscmd,
                        Prompt=>$prompt[$I],
                        Timeout=>$cmdTO);
	if ($debug==1) { print("@STAT\n"); }
}
sub closeTelnet {
#
# close out when we're done
#
	my $i="";
	$i=$t->cmd(	String=>"exit",
			Prompt=>"$prompt[$I]",
			Timeout=>$cmdTO);
	if ($debug==1) { print("$i\n"); } 
	}
sub parseStatus {
#
# create a TEMP array and parse out the temperature and
# humidity reported within using the $statstringx to
# figure where we are in the output
#
	my @TEMP="";
	foreach $_(@STAT) {
		if ($_ =~ /$statstring1/m ) {
		@TEMP=split(" ",$_);
		$temperature=$TEMP[1];
		$realTHSP=$TEMP[2];
		}
		elsif ($_ =~ /$statstring2/m ) {
		@TEMP=split(" ",$_);
		$humidity=$TEMP[1];
		$realHHSP=$TEMP[2];
		$realHLSP=$TEMP[3];
		}
	}
}
sub compilelog {
#
# compare the color this check is reporting and upgrade the overall color
# if it is higher
#
        if (defined($color[$msgindex])==1) {
        if ($color[$msgindex] eq "red" ) {
                $mycolor="red";
                }
        elsif ($color[$msgindex] eq "yellow" && $mycolor ne "red" ) {
                $mycolor="yellow";
                }
        }
        $bblog=$msgtyp." ".$host.".".$page." ".$mycolor." ".strftime("%c",localtime())." ".$title."\n";
#
# message index creates a unique line for each check
#
        $msgindex++;
}
sub TelnetError {
	# 
	# Delete the ip addr if an error is detected so the rest of the checks
	# don't run
	#
	$color[$msgindex]=$maxalarm;
	$message[$msgindex]="&red Problem connecting to $location[$I] $ip via telnet\n";
	warn($message[$msgindex]);
	compilelog;
	$ip="";
	}
sub chkTemp {
#
# actual temperature and humidity comparisons are done here for each server properly defined in variables
#
	if (int($temperature) >= $tempHSP[$I] ) {
		$color[$msgindex]="red";
		$message[$msgindex]="	&red $location[$I] temperature $temperature degrees exceeds high set point $tempHSP[$I] degrees!\n";
		compilelog;
		}
	elsif (int($temperature) >= $tempYSP[$I] ) {
		$color[$msgindex]="yellow";
		$message[$msgindex]="	&yellow $location[$I] temperature is getting warm: $temperature degrees.\n";
		compilelog;
		}
	else {
		$message[$msgindex]="	&green $location[$I] temperature is $temperature degrees.\n";
		compilelog;
		}
	if (int($humidity) >= $humdHSP[$I] ) {
		$color[$msgindex]="red";
		$message[$msgindex]="	&red $location[$I] humidity is excessive: $humidity.\n";
		compilelog;
		}
	else {
		$message[$msgindex]="	&green $location[$I] humidity is $humidity.\n";
		compilelog;
		}
}
sub sendlog {
#
# create a status update by combining the initial message with the various
# updates from the individual tests, then send it out.
#
        my $msg="";
        foreach $_(@message) {
		if ($_ =~ /\&red/m ) {
                	$msg=$msg.$_."\r\n";
			}
		}
        foreach $_(@message) {
                $msg=$msg.$_;
                }
        $bblog=$bblog.$msg."\n";
        if ($debug==1) { warn("$bblog\n"); }
        system("$bb $bbs \"$bblog\"");
}
sub openTelnet {
#
# open a telnet to the $ip
#
use Net::Telnet();
$|++;
$t = new Net::Telnet (Timeout => $loginTO,
                Input_log =>$inputlog,
                Errmode=>$errmode);
$t->open($ip) or warn("$!");
sleep 1;
$t->login($login[$I],$passwd[$I]) or TelnetError;
sleep 2;
#
# if $ip = "" then there was a telnet error
#
if ($ip eq  "") { return; }
$message[$msgindex]="&green $location[$I] $ip telnet OK.\n";
compilelog;
}
#######
# MAIN
#######
use POSIX qw(strftime);
variables;
foreach $ip(@IP) {
	my @STAT="";
	my $temperature=0;
	my $humidity=0;
	my $realTHSP=0;
	my $realHHSP=0;
	my $realHLSP=0;
	openTelnet;
	if ($ip ne "" ) {
		getStatus;
		closeTelnet;
		parseStatus;
		chkTemp;
		}
	$I++;
}
sendlog;
exit;
