| 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::Management; | ||
| 9 | |||
| 10 | my %opt; | ||
| 11 | my ($opt_help, $opt_man); | ||
| 12 | |||
| 13 | GetOptions( | ||
| 14 | 'community=s' => \$opt{community}, | ||
| 15 | 'CPU!' => \$opt{cpu}, | ||
| 16 | 'Kilobytes!' => \$opt{k}, | ||
| 17 | 'memory!' => \$opt{mem}, | ||
| 18 | 'Megabytes!' => \$opt{m}, | ||
| 19 | 'system!' => \$opt{sys}, | ||
| 20 | 'V|inventory+' => \$opt{inventory}, | ||
| 21 | 'help!' => \$opt_help, | ||
| 22 | 'man!' => \$opt_man | ||
| 23 | ) or pod2usage(-verbose => 0); | ||
| 24 | |||
| 25 | pod2usage(-verbose => 1) if defined $opt_help; | ||
| 26 | pod2usage(-verbose => 2) if defined $opt_man; | ||
| 27 | |||
| 28 | # Make sure at least one arg was provided | ||
| 29 | if (!@ARGV) { | ||
| 30 | pod2usage(-verbose => 0, -message => "$0: host required\n") | ||
| 31 | } | ||
| 32 | |||
| 33 | $opt{community} = $opt{community} || 'private'; | ||
| 34 | if (!defined($opt{cpu}) && | ||
| 35 | !(defined($opt{mem}) || defined($opt{k}) || defined($opt{m})) && | ||
| 36 | !defined($opt{inventory}) && | ||
| 37 | !defined($opt{sys})) { | ||
| 38 | $opt{sys} = 1 | ||
| 39 | } | ||
| 40 | |||
| 41 | for (@ARGV) { | ||
| 42 | print "\n-- $_ --\n"; | ||
| 43 | |||
| 44 | my $cm; | ||
| 45 | if (!defined($cm = Cisco::Management->new( | ||
| 46 | hostname => $_, | ||
| 47 | community => $opt{community} | ||
| 48 | ))) { | ||
| 49 | printf "Error: %s\n", Cisco::Management->error; | ||
| 50 | next | ||
| 51 | } | ||
| 52 | |||
| 53 | if (defined($opt{cpu})) { | ||
| 54 | if (defined(my $cpu = $cm->cpu_info())) { | ||
| 55 | print "CPU Name | 5 second(%) | 1 minute(%) | 5 minute(%)\n"; | ||
| 56 | print "--------------------|-------------|-------------|------------\n"; | ||
| 57 | for (0..$#{$cpu}) { | ||
| 58 | printf "%-20s|%9.2f |%9.2f |%9.2f\n", | ||
| 59 | $cpu->[$_]->{Name}, | ||
| 60 | $cpu->[$_]->{'5sec'}, | ||
| 61 | $cpu->[$_]->{'1min'}, | ||
| 62 | $cpu->[$_]->{'5min'} | ||
| 63 | } | ||
| 64 | print "\n" | ||
| 65 | } else { | ||
| 66 | printf "Error: %s\n", Cisco::Management->error | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | if (defined($opt{mem}) || defined($opt{m}) || defined($opt{k})) { | ||
| 71 | if (defined(my $mem = $cm->memory_info())) { | ||
| 72 | my $params = 'B'; | ||
| 73 | if (defined($opt{k})) { $params = 'K' } | ||
| 74 | if (defined($opt{m})) { $params = 'M' } | ||
| 75 | |||
| 76 | printf "Memory Pool Name | Total(%s) | Used(%s) |Percent(%%)\n", $params, $params; | ||
| 77 | print "--------------------|---------------|---------------|----------\n"; | ||
| 78 | for (0..$#{$mem}) { | ||
| 79 | my ($Used, $Total); | ||
| 80 | if ($params eq 'K') { $Used = $mem->[$_]->{Used}/1000; | ||
| 81 | $Total = $mem->[$_]->{Total}/1000 | ||
| 82 | } elsif ($params eq 'M') { $Used = $mem->[$_]->{Used}/1000000; | ||
| 83 | $Total = $mem->[$_]->{Total}/1000000 | ||
| 84 | } else { $Used = $mem->[$_]->{Used}; | ||
| 85 | $Total = $mem->[$_]->{Total} | ||
| 86 | } | ||
| 87 | printf "%-20s|%15.2f|%15.2f|%7.2f\n", | ||
| 88 | $mem->[$_]->{Name}, | ||
| 89 | $Total, | ||
| 90 | $Used, | ||
| 91 | $mem->[$_]->{Used}/$mem->[$_]->{Total}*100 | ||
| 92 | } | ||
| 93 | print "\n" | ||
| 94 | } else { | ||
| 95 | printf "Error: %s\n", Cisco::Management->error | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | if (defined($opt{inventory})) { | ||
| 100 | if (defined(my $inventory = $cm->system_inventory())) { | ||
| 101 | print "NAME SN FIRMREV SOFTREV\n"; | ||
| 102 | print "----------------------------------------------------------------\n"; | ||
| 103 | for my $unit (@{$inventory}) { | ||
| 104 | next if (($unit->{SerialNum} eq '') && ($opt{inventory} <= 1)); | ||
| 105 | printf "%-30s %-12s %-12s %-18s\n", $unit->{Name}, | ||
| 106 | $unit->{SerialNum}, | ||
| 107 | $unit->{FirmwareRev}, | ||
| 108 | $unit->{SoftwareRev} | ||
| 109 | } | ||
| 110 | } else { | ||
| 111 | printf "Error: %s\n", Cisco::Management->error | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | if (defined($opt{sys})) { | ||
| 116 | if (defined(my $sysinfo = $cm->system_info())) { | ||
| 117 | printf "Description = %s\n", $sysinfo->system_info_description; | ||
| 118 | printf "ObjectID = %s\n", $sysinfo->system_info_objectID; | ||
| 119 | printf "Uptime = %s\n", $sysinfo->system_info_uptime; | ||
| 120 | printf "Conctact = %s\n", $sysinfo->system_info_contact; | ||
| 121 | printf "Name = %s\n", $sysinfo->system_info_name; | ||
| 122 | printf "Location = %s\n", $sysinfo->system_info_location; | ||
| 123 | print "Services = "; | ||
| 124 | print "$_ " for (@{$sysinfo->system_info_services}); | ||
| 125 | printf "\n\nOS Version = %s\n", $sysinfo->system_info_osversion; | ||
| 126 | print "\n" | ||
| 127 | } else { | ||
| 128 | printf "Error: %s\n", Cisco::Management->error | ||
| 129 | } | ||
| 130 | } | ||
| 131 | $cm->close() | ||
| 132 | } | ||
| 133 | |||
| 134 | __END__ |