Greetings Monks! I am trying to write a script which will monitor available memory on a cisco router. The scrip works without issues when ran on a single node. However, when i try to add additional switches or routers the script fails frequently. If a script fails to log into any of the routers or switches it fails all together. I am using eval to check my ssh connection before running any commands and it helped a little. However, if i add more than 3-4 switches, the script fails pretty consistently. It could have something do do with Net::SSH2 module as this is my first time using it, but i am not 100% sure. Any help would be greatly appreciate it!
#! /usr/bin/perl
use Net::SSH2;
use warnings;
use strict;
use Data::Dumper;
use Net::Syslog;
my $user = "user";
my $password = 'password';
my $secret = 'secret'; ### enable password
my @hosts=qw/switch1 switch2 switch3 switch4 switch5 switch6/;
my (%deviceHash, $total, $total_unicast,$total_ipv4nonvrf,$total_ipv4v
+rf, $total_multicast, $total_mpls, $host, $connect, $s, $ssh);
my $array_count= scalar @hosts;
foreach (@hosts){
&connect;
}
my $hash_count = keys %deviceHash;
my $i=0;
print "The number of keys in the hash $hash_count\n";
if(($array_count != $hash_count) && ($i<=2)){
print "went into conditional statement\n";
print "the kyes $_\n" for keys %deviceHash;
foreach my $connection(@hosts){
if (exists $deviceHash{$connection}){
print "the $connection host exists\n";
}
else{
print "the $connection doesn't exist";
&connect($connection);
}
}
$i++
}
sub connect {
$host=$_;
$ssh = Net::SSH2->new();
my $retry_count = 0;
my $max_retry_count =5;
while($retry_count <= $max_retry_count){
my $rc = eval { $ssh->connect($host); };
last if defined $rc;
$retry_count++;
sleep 2;
}
if ($@) {
&mail2 ($host);
print "output on $host $@\n";
next;
}
if(!$ssh->auth_password($user,$password)){
print("Authentication Failed");
exit(1);
}
my $channel = $ssh->channel();
$channel->blocking(0);
$channel->shell();
$channel->write("enable\n");
$channel->write("$secret\n"); ### enable password from input strin
+g3
$channel->write("term len 0\n");
print $channel "sh mls cef sum\n";
while (<$channel>) {
unless($_ =~ /routes/){
next;
}
if ($_ =~ /Total routes/){
$total = (split(/:/, $_))[1];
$total =~ s/^\s+//;
$total =~ s/\s+$//;
$deviceHash{$host}{"Total"} = $total;
}
elsif($_ =~ /IPv4 unicast routes/){
$total_unicast = (split(/:/, $_))[1];
$total_unicast =~ s/^\s+//;
$total_unicast =~ s/\s+$//;
$deviceHash{$host}{"Total_Unicast"} = $total_unicast;
}
elsif($_ =~ /IPv4 non-vrf routes/){
$total_ipv4nonvrf = (split(/:/, $_))[1];
$total_ipv4nonvrf =~ s/^\s+//;
$total_ipv4nonvrf =~ s/\s+$//;
$deviceHash{$host}{"Total_IPV4_NONVRF"} = $total_ipv4nonvrf;
}
elsif($_ =~ /IPv4 vrf routes/){
$total_ipv4vrf = (split(/:/, $_))[1];
$total_ipv4vrf =~ s/^\s+//;
$total_ipv4vrf =~ s/\s+$//;
$deviceHash{$host}{"Total_IPV4_VRF"} = $total_ipv4vrf;
}
elsif($_ =~ /IPv4 Multicast routes/){
$total_multicast = (split(/:/, $_))[1];
$total_multicast =~ s/^\s+//;
$total_multicast =~ s/\s+$//;
$deviceHash{$host}{"Total_IPV4_MULTICAST"} = $total_multicast;
}
elsif($_ =~ /MPLS routes/){
$total_mpls = (split(/:/, $_))[1];
$total_mpls =~ s/^\s+//;
$total_mpls =~ s/\s+$//;
$deviceHash{$host}{"Total_IPV4_MULTICAST"} = $total_mpls;
}
}
# exit the routine
$channel->write('exit');
# Close the connection
$ssh->disconnect();
#sleep (1);
}
foreach my $key (keys %deviceHash){
if ($deviceHash{$key}{"Total"} >= 100000){
$s = new Net::Syslog();
my $memory = $deviceHash{$key}{"Total"};
$s->send("TCAM memory is being exhausted on $key, the current memory i
+s $memory",Name=>'tcam-test',Facility=>'local7',Priority=>'info',Sysl
+ogHost=>'192.168.0.1');
#print "the TCAM memory is being exhausted on $key, the current memory
+ is $memory \n";
}
}
sub mail2 {
my $node=shift;
my @to=qw/host.email.com/;
open(MAIL, "|/usr/sbin/sendmail -t");
## Mail Header
print MAIL "To: @to\n";
print MAIL "From: root\n";
print MAIL "Subject: The tcam monitoring script was not able to lo
+g into $node\n";
##Mail Body
print MAIL "The tcam monitoring script was not able to log into $
+node\n";
print MAIL "If the problem repeats itself please investigate as so
+on as possible\n";
close(MAIL);
}
The output of the command that is being assigned to the hash goes something like this:
Total routes: 94000
IPv4 unicast routes: 94000
IPv4 non-vrf routes: 130
IPv4 vrf routes: 95000
IPv4 Multicast routes: 65
MPLS routes: 560
IPv6 unicast routes: 3
IPv6 non-vrf routes: 2
IPv6 vrf routes: 0
IPv6 multicast routes: 5
EoM routes: 1