| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Getopt::Long qw(:config no_ignore_case); #bundling | ||
| 6 | use Pod::Usage; | ||
| 7 | |||
| 8 | use Cisco::SNMP::Interface; | ||
| 9 | use Cisco::SNMP::IP; | ||
| 10 | |||
| 11 | my %opt; | ||
| 12 | my ($opt_help, $opt_man); | ||
| 13 | |||
| 14 | GetOptions( | ||
| 15 | 'community=s' => \$opt{community}, | ||
| 16 | 'Down!' => \$opt{down}, | ||
| 17 | 'interfaces=s' => \$opt{interfaces}, | ||
| 18 | 'm|metrics:s' => \$opt{metrics}, | ||
| 19 | 'repeat!' => \$opt{repeat}, | ||
| 20 | 'Up!' => \$opt{up}, | ||
| 21 | 'utilization:i' => \$opt{util}, | ||
| 22 | 'help!' => \$opt_help, | ||
| 23 | 'man!' => \$opt_man | ||
| 24 | ) or pod2usage(-verbose => 0); | ||
| 25 | |||
| 26 | pod2usage(-verbose => 1) if defined $opt_help; | ||
| 27 | pod2usage(-verbose => 2) if defined $opt_man; | ||
| 28 | |||
| 29 | # Make sure at least one arg was provided | ||
| 30 | if (!@ARGV) { | ||
| 31 | pod2usage(-verbose => 0, -message => "$0: host required\n") | ||
| 32 | } | ||
| 33 | |||
| 34 | $opt{community} = $opt{community} || 'private'; | ||
| 35 | |||
| 36 | my $oper; | ||
| 37 | if (defined $opt{up}) { | ||
| 38 | $oper = 'UP' | ||
| 39 | } elsif (defined $opt{down}) { | ||
| 40 | $oper = 'DOWN' | ||
| 41 | } | ||
| 42 | |||
| 43 | for (@ARGV) { | ||
| 44 | print "\n-- $_ --\n"; | ||
| 45 | |||
| 46 | my $cm; | ||
| 47 | if (!defined($cm = Cisco::SNMP::Interface->new( | ||
| 48 | hostname => $_, | ||
| 49 | community => $opt{community} | ||
| 50 | ))) { | ||
| 51 | printf "Error: %s\n", Cisco::SNMP::Interface->error; | ||
| 52 | next | ||
| 53 | } | ||
| 54 | |||
| 55 | my %params; | ||
| 56 | my $ERROR = 0; | ||
| 57 | # get interfaces | ||
| 58 | if (defined $opt{interfaces}) { | ||
| 59 | |||
| 60 | # by name | ||
| 61 | if ($opt{interfaces} =~ /^[A-Za-z]/) { | ||
| 62 | my @temp; | ||
| 63 | my @ifs = split /\s+/, $opt{interfaces}; | ||
| 64 | for (@ifs) { | ||
| 65 | if (defined(my $if = $cm->interface_getbyname(interface => $_, index => 1))) { | ||
| 66 | push @temp, $if | ||
| 67 | } else { | ||
| 68 | printf "Error: %s\n", Cisco::SNMP::Interface->error; | ||
| 69 | $ERROR = 1; | ||
| 70 | last | ||
| 71 | } | ||
| 72 | } | ||
| 73 | $params{interfaces} = join ',', @temp | ||
| 74 | } else { | ||
| 75 | $params{interfaces} = $opt{interfaces} | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | next if ($ERROR); | ||
| 80 | |||
| 81 | # UP/DOWN | ||
| 82 | if (defined $oper) { | ||
| 83 | $params{operation} = $oper; | ||
| 84 | if (defined(my $ifs = $cm->interface_updown(%params))) { | ||
| 85 | print "$_: Admin $oper interfaces = @{$ifs}\n" | ||
| 86 | } else { | ||
| 87 | printf "Error: %s\n", Cisco::SNMP::Interface->error | ||
| 88 | } | ||
| 89 | # metrics | ||
| 90 | } elsif (defined $opt{metrics} && !defined $opt{util}) { | ||
| 91 | if ($opt{metrics} ne '') { | ||
| 92 | my @temp; | ||
| 93 | push @temp, split /\s+/, $opt{metrics}; | ||
| 94 | $params{metrics} = \@temp | ||
| 95 | } | ||
| 96 | if (defined(my $ifs = $cm->interface_metrics(%params))) { | ||
| 97 | print "Index Multi(I/O) Broad(I/O) Octets(I/O)\n"; | ||
| 98 | print "-----------------------------------------\n"; | ||
| 99 | for my $int (sort {$a <=> $b} (keys(%{$ifs}))) { | ||
| 100 | printf "%5i %5s/%-5s %5s/%-5s %5s/%-5s\n", | ||
| 101 | $int, | ||
| 102 | defined($ifs->ifInMulticasts($int)) ? $ifs->ifInMulticasts($int) : '-', | ||
| 103 | defined($ifs->ifOutMulticasts($int)) ? $ifs->ifOutMulticasts($int) : '-', | ||
| 104 | defined($ifs->ifInBroadcasts($int)) ? $ifs->ifInBroadcasts($int) : '-', | ||
| 105 | defined($ifs->ifOutBroadcasts($int)) ? $ifs->ifOutBroadcasts($int) : '-', | ||
| 106 | defined($ifs->ifInOctets($int)) ? $ifs->ifInOctets($int) : '-', | ||
| 107 | defined($ifs->ifOutOctets($int)) ? $ifs->ifOutOctets($int) : '-' | ||
| 108 | } | ||
| 109 | } else { | ||
| 110 | printf "Error: %s\n", Cisco::SNMP::Interface->error | ||
| 111 | } | ||
| 112 | #utilization | ||
| 113 | } elsif (defined $opt{util}) { | ||
| 114 | if ($opt{util} > 0) { $params{polling} = $opt{util} } | ||
| 115 | if (defined $opt{metrics} && $opt{metrics} ne '') { | ||
| 116 | my @temp; | ||
| 117 | push @temp, split /\s+/, $opt{metrics}; | ||
| 118 | $params{metrics} = \@temp | ||
| 119 | } | ||
| 120 | |||
| 121 | print "Index Multi(I/O) Broad(I/O) Octets(I/O)\n"; | ||
| 122 | print " packets/s packets/s bits/s\n"; | ||
| 123 | |||
| 124 | my $stopRepeat = 0; | ||
| 125 | $SIG{INT} = sub { | ||
| 126 | print "SIGINT! - Stop\n"; | ||
| 127 | $stopRepeat = 1 | ||
| 128 | }; | ||
| 129 | |||
| 130 | my $recur; | ||
| 131 | while (1) { | ||
| 132 | if ($stopRepeat) { last } | ||
| 133 | my $ifs; | ||
| 134 | $params{recursive} = $recur; | ||
| 135 | ($ifs, $recur) = $cm->interface_utilization(%params); | ||
| 136 | if (defined $ifs) { | ||
| 137 | print "-----------------------------------------\n"; | ||
| 138 | for my $int (sort {$a <=> $b} (keys(%{$ifs}))) { | ||
| 139 | printf "%5i %5s/%-5s %5s/%-5s %5s/%-5s\n", | ||
| 140 | $int, | ||
| 141 | defined($ifs->ifInMulticasts($int)) ? $ifs->ifInMulticasts($int) : '-', | ||
| 142 | defined($ifs->ifOutMulticasts($int)) ? $ifs->ifOutMulticasts($int) : '-', | ||
| 143 | defined($ifs->ifInBroadcasts($int)) ? $ifs->ifInBroadcasts($int) : '-', | ||
| 144 | defined($ifs->ifOutBroadcasts($int)) ? $ifs->ifOutBroadcasts($int) : '-', | ||
| 145 | defined($ifs->ifInOctets($int)) ? $ifs->ifInOctets($int) : '-', | ||
| 146 | defined($ifs->ifOutOctets($int)) ? $ifs->ifOutOctets($int) : '-' | ||
| 147 | } | ||
| 148 | } else { | ||
| 149 | printf "Error: %s\n", Cisco::SNMP::Interface->error | ||
| 150 | } | ||
| 151 | if (!defined $opt{repeat}) { last } | ||
| 152 | } | ||
| 153 | # interface info | ||
| 154 | } else { | ||
| 155 | if (defined(my $ifs = $cm->interface_info(%params))) { | ||
| 156 | my ($cmip, $ips); | ||
| 157 | if (defined($cmip = Cisco::SNMP::IP->new( | ||
| 158 | hostname => $_, | ||
| 159 | community => $opt{community} | ||
| 160 | ))) { | ||
| 161 | $ips = $cmip->addr_info(); | ||
| 162 | } else { | ||
| 163 | printf "Error: %s\n", Cisco::SNMP::IP->error; | ||
| 164 | } | ||
| 165 | print "Index Description Speed/Duplex Admin/Oper IP(s)\n"; | ||
| 166 | print "---------------------------------------------------------------\n"; | ||
| 167 | for my $int (sort {$a <=> $b} (keys(%{$ifs}))) { | ||
| 168 | printf "%5i %-25s %7i/%-7s %4s/%-4s ", | ||
| 169 | $int, | ||
| 170 | $ifs->ifDescription($int), | ||
| 171 | ($ifs->ifSpeed($int) > 1000000) ? $ifs->ifSpeed($int)/1000000 : $ifs->ifSpeed($int), | ||
| 172 | $ifs->ifDuplex($int), | ||
| 173 | $ifs->ifAdminStatus($int), | ||
| 174 | $ifs->ifOperStatus($int); | ||
| 175 | if (exists($ips->{$int})) { | ||
| 176 | for (0..$#{$ips->{$int}}) { | ||
| 177 | printf " %s", $ips->addrAddr($int,$_) | ||
| 178 | } | ||
| 179 | } | ||
| 180 | print "\n" | ||
| 181 | } | ||
| 182 | } else { | ||
| 183 | printf "Error: %s\n", Cisco::SNMP::Interface->error | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | $cm->close() | ||
| 188 | } | ||
| 189 | |||
| 190 | __END__ |