| 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::Memory; | ||
| 9 | |||
| 10 | my %opt; | ||
| 11 | my ($opt_help, $opt_man); | ||
| 12 | |||
| 13 | GetOptions( | ||
| 14 | 'community=s' => \$opt{community}, | ||
| 15 | 'Kilobytes!' => \$opt{k}, | ||
| 16 | 'Megabytes!' => \$opt{m}, | ||
| 17 | 'help!' => \$opt_help, | ||
| 18 | 'man!' => \$opt_man | ||
| 19 | ) or pod2usage(-verbose => 0); | ||
| 20 | |||
| 21 | pod2usage(-verbose => 1) if defined $opt_help; | ||
| 22 | pod2usage(-verbose => 2) if defined $opt_man; | ||
| 23 | |||
| 24 | # Make sure at least one arg was provided | ||
| 25 | if (!@ARGV) { | ||
| 26 | pod2usage(-verbose => 0, -message => "$0: host required\n") | ||
| 27 | } | ||
| 28 | |||
| 29 | $opt{community} = $opt{community} || 'private'; | ||
| 30 | |||
| 31 | for (@ARGV) { | ||
| 32 | print "\n-- $_ --\n"; | ||
| 33 | |||
| 34 | my $cm; | ||
| 35 | if (!defined($cm = Cisco::SNMP::Memory->new( | ||
| 36 | hostname => $_, | ||
| 37 | community => $opt{community} | ||
| 38 | ))) { | ||
| 39 | printf "Error: %s\n", Cisco::SNMP::Memory->error; | ||
| 40 | next | ||
| 41 | } | ||
| 42 | |||
| 43 | if (defined(my $mem = $cm->memory_info())) { | ||
| 44 | my $params = 'B'; | ||
| 45 | if (defined($opt{k})) { $params = 'K' } | ||
| 46 | if (defined($opt{m})) { $params = 'M' } | ||
| 47 | |||
| 48 | printf "Memory Pool Name | Total(%s) | Used(%s) |Percent(%%)\n", $params, $params; | ||
| 49 | print "--------------------|---------------|---------------|----------\n"; | ||
| 50 | for (0..$#{$mem}) { | ||
| 51 | my ($Used, $Total); | ||
| 52 | if ($params eq 'K') { $Used = $mem->memUsed($_) /1000; | ||
| 53 | $Total = $mem->memTotal($_)/1000 | ||
| 54 | } elsif ($params eq 'M') { $Used = $mem->memUsed($_) /1000000; | ||
| 55 | $Total = $mem->memTotal($_)/1000000 | ||
| 56 | } else { $Used = $mem->memUsed($_); | ||
| 57 | $Total = $mem->memTotal($_) | ||
| 58 | } | ||
| 59 | printf "%-20s|%15.2f|%15.2f|%7.2f\n", | ||
| 60 | $mem->memName($_), | ||
| 61 | $Total, | ||
| 62 | $Used, | ||
| 63 | $mem->memUsed($_)/$mem->memTotal($_)*100 | ||
| 64 | } | ||
| 65 | print "\n" | ||
| 66 | } else { | ||
| 67 | printf "Error: %s\n", Cisco::SNMP::Memory->error | ||
| 68 | } | ||
| 69 | |||
| 70 | $cm->close() | ||
| 71 | } | ||
| 72 | |||
| 73 | __END__ |