1
  7   ˆl–ßi~”d
_)Ú€Æœ¸TÅ£�þ^dm,ãNˆ/8ðÈF�üÿÂ„e—@ÁÀ¿ ?÷     #!/usr/bin/perl

use strict 'refs';
require Net::Ping;
require Term::ReadKey;

#defaults
$mibident=".2";
$miberrflag=".100";
$miberrmsg=".101";
$mibfix=".102";
$mibheadall=".1.3.6.1.4.1.2021";
$mibclearcache="$mibheadall.100.VERCLEARCACHE";
$mibrestartagent="$mibheadall.100.VERRESTARTAGENT";
$mibupdateconfig="$mibheadall.100.VERUPDATECONFIG";
%miblist=(    '.2.1' => 'processes',
	      '.8.1' => 'scripts',
	      '.4' => 'swap space',
	      '.9.1' => 'disks',
	      '.10.1' => 'load-average',
	      '.101' => 'snmp-agent-errors');
@fixitlist=('.2.1','.8.1');
%mibchecklist = ('.2.1' => 1,
		 '.8.1' => 1,
		 '.4' => 1,
		 '.9.1' => 1,
		 '.10.1' => 1,
		 '.101' => 1);
$errlog="/net/tyfon/1/OV/log/ece-log";
$default_get_args = "-v 1 -c private %s";
$default_set_args = "-v 1 -c private %s";
$andlog=0;
$snmppath="/usr/bin";
$eraseline="                                                                           \r";
$fixit=0;  # this should be 0 not -1, but is necessary till getc(STDIN) works
$rescanWhen = 300;
$display = $ENV{'DISPLAY'};
$hidden = 0;
$pinghost = 0;
$loglevel = 1;
$logwindowatstart = 0;
$numloglevels = 5;
$dontstart = 0;
$raiseonnew = 1;

#
#  Mib Package:  Each mib has a mib number attached and can check/fix itself;
#

package Mib;

# @ISA = qw( Host );

sub new {
    my $tmp = shift;
    my $self = {};
    $self->{'HostId'} = shift;
    $self->{'Host'} = $self->{'HostId'}->{'Name'};
    $_ = shift;
    $self->{'Mib'} = $_;
    print "test: $_\n";
    ($self->{'MibSuffix'}) = /(\.[0-9]+)$/;
    if (!defined($mibchecklist{$self->{'MibSuffix'}})) {
	($self->{'MibSuffix'}) = /(\.[0-9]+\.1)$/;
    }
    print "suff: $self->{'MibSuffix'}\n";
    $self->{'MibDesc'} = shift;
    $self->{'Frame'} = shift;
    bless $self;
}

sub getmibnum {
    my $self = shift;
    return ($self->{'Mib'});
}

sub snmp_walk {
    my $self = shift;
    $self->{'Frame'}->toplevel->Busy() if ($::display);
    my $mib = shift;
    my $cmd = "$::snmppath/snmpwalk " . sprintf($::default_get_args, $self->{'Host'}) . " $mib|";
    ::addToLog("running:  $cmd",5);
    open(OUT,"$cmd");
    my $outcount = 0;
    my @result = [];
    while (<OUT>) {
	$result[$outcount] = $_;
	chop;
	::addToLog("snmpwalk:  $_",5);
	if ($::display) {
	    $self->{'Frame'}->toplevel->update;
	}
	$outcount++;
    }
    close(OUT);
    for($i=0; $i <= $#result; $i++) {
	$result[$i] =~ s/ Hex:.*$//g;
	$result[$i] =~ s/\"//g;
    }
    $self->{'Frame'}->toplevel->Unbusy() if ($::display);
    if ($result[0] =~ /No Response/) {
	$self->{'HostId'}->hostdown;
	splice(@result,0);
    }
    return @result;
}

sub check {
    my $self = shift;
    my $tmp = [];
    if ($::mibchecklist{$self->{'MibSuffix'}} != 1) {
	return @{$tmp};
    }
    if (! $::display) {
	printf "%sChecking %s: %s\r", $::eraseline,$self->{'Host'},
	$self->{'MibDesc'};
    }
    my @walkout = $self->snmp_walk("$self->{'Mib'}$::miberrflag");
    while ($#walkout > -1) {
	$_ = shift @walkout;
	($result) = /= ([0-9]+)/;
	if (defined($result) && $result > 0)
	{
	    ($mibloc) = /\.([0-9]+) /;
	    push(@{$tmp},FixProblem::new("",$self->{'HostId'}, $self->{'Mib'},
					 $self->{'MibDesc'},
					 $mibloc,$self->{'Frame'}));
	    if (! $::display) {
		printf("%s%-8.8s  %-12.12s  %2d -- %-37.37s",$::eraseline,
		       $self->{'Host'},$tmp->[0]->{'ErrName'},
		       $result,$tmp->[0]->{'ErrMsg'});
		if ($tmp->[0]->canfix() && $::fixit == 0) {
		    printf(" / Fix? ");
		    $ans = Term::ReadKey::ReadKey(0);
		    if ("$ans" eq "y" || "$ans" eq "Y") {
			printf("\b\b\b\b\b\b\b\b");
			$tmp->[0]->fix($mibloc);     # fix now if curses
		    } else {
			print $ans;
			printf("\nChecking %s: %s\r",$self->{'Host'}, 
			       $self->{'MibDesc'});
		    }
		} elsif ($::fixit > 0) {
		    $tmp->[0]->fix($mibloc);     # fix now if curses
		}
		shift @{$tmp};
	    }
	}
    }
    return(@{$tmp});
}

#
#  Problem Package: A problem comes into existence when found.  It may
#  or may not know how to fix itself (Problem/FixProblem).
#

package Problem