#!/usr/bin/perl
my
%opt
;
my
(
$opt_help
,
$opt_man
);
GetOptions(
'community=s'
=> \
$opt
{community},
'down!'
=> \
$opt
{down},
'interfaces=s'
=> \
$opt
{interfaces},
'metrics:s'
=> \
$opt
{metrics},
'repeat!'
=> \
$opt
{repeat},
'up!'
=> \
$opt
{up},
'Utilization:i'
=> \
$opt
{util},
'help!'
=> \
$opt_help
,
'man!'
=> \
$opt_man
) or pod2usage(
-verbose
=> 0);
pod2usage(
-verbose
=> 1)
if
defined
$opt_help
;
pod2usage(
-verbose
=> 2)
if
defined
$opt_man
;
if
(!
@ARGV
) {
pod2usage(
-verbose
=> 0,
-message
=>
"$0: host required\n"
)
}
$opt
{community} =
$opt
{community} ||
'private'
;
my
$oper
;
if
(
defined
(
$opt
{up})) {
$oper
=
'UP'
}
elsif
(
defined
(
$opt
{down})) {
$oper
=
'DOWN'
}
for
(
@ARGV
) {
print
"\n-- $_ --\n"
;
my
$cm
;
if
(!
defined
(
$cm
= Cisco::Management->new(
hostname
=>
$_
,
community
=>
$opt
{community}
))) {
printf
"Error: %s\n"
, Cisco::Management->error;
next
}
my
%params
;
my
$ERROR
= 0;
if
(
defined
(
$opt
{interfaces})) {
if
(
$opt
{interfaces} =~ /^[A-Za-z]/) {
my
@temp
;
my
@ifs
=
split
/\s+/,
$opt
{interfaces};
for
(
@ifs
) {
if
(
defined
(
my
$if
=
$cm
->interface_getbyname(
interface
=>
$_
,
index
=> 1))) {
push
@temp
,
$if
}
else
{
printf
"Error: %s\n"
, Cisco::Management->error;
$ERROR
= 1;
last
}
}
$params
{interfaces} =
join
','
,
@temp
}
else
{
$params
{interfaces} =
$opt
{interfaces}
}
}
next
if
(
$ERROR
);
if
(
defined
(
$oper
)) {
$params
{operation} =
$oper
;
if
(
defined
(
my
$ifs
=
$cm
->interface_updown(
%params
))) {
print
"$_: Admin $oper interfaces = @{$ifs}\n"
}
else
{
printf
"Error: %s\n"
, Cisco::Management->error
}
}
elsif
(
defined
(
$opt
{metrics}) && !
defined
(
$opt
{util})) {
if
(
$opt
{metrics} ne
''
) {
my
@temp
;
push
@temp
,
split
/\s+/,
$opt
{metrics};
$params
{metrics} = \
@temp
}
if
(
defined
(
my
$ifs
=
$cm
->interface_metrics(
%params
))) {
print
"Index Multi(I/O) Broad(I/O) Octets(I/O)\n"
;
print
"-----------------------------------------\n"
;
for
my
$int
(
sort
{
$a
<=>
$b
} (
keys
(%{
$ifs
}))) {
printf
"%5i %5s/%-5s %5s/%-5s %5s/%-5s\n"
,
$int
,
defined
(
$ifs
->{
$int
}->{InMulticasts}) ?
$ifs
->{
$int
}->{InMulticasts} :
'-'
,
defined
(
$ifs
->{
$int
}->{OutMulticasts}) ?
$ifs
->{
$int
}->{OutMulticasts} :
'-'
,
defined
(
$ifs
->{
$int
}->{InBroadcasts}) ?
$ifs
->{
$int
}->{InBroadcasts} :
'-'
,
defined
(
$ifs
->{
$int
}->{OutBroadcasts}) ?
$ifs
->{
$int
}->{OutBroadcasts} :
'-'
,
defined
(
$ifs
->{
$int
}->{InOctets}) ?
$ifs
->{
$int
}->{InOctets} :
'-'
,
defined
(
$ifs
->{
$int
}->{OutOctets}) ?
$ifs
->{
$int
}->{OutOctets} :
'-'
}
}
else
{
printf
"Error: %s\n"
, Cisco::Management->error
}
}
elsif
(
defined
(
$opt
{util})) {
if
(
$opt
{util} > 0) {
$params
{polling} =
$opt
{util} }
if
(
defined
(
$opt
{metrics}) &&
$opt
{metrics} ne
''
) {
my
@temp
;
push
@temp
,
split
/\s+/,
$opt
{metrics};
$params
{metrics} = \
@temp
}
print
"Index Multi(I/O) Broad(I/O) Octets(I/O)\n"
;
print
" packets/s packets/s bits/s\n"
;
my
$stopRepeat
= 0;
$SIG
{INT} =
sub
{
print
"SIGINT! - Stop\n"
;
$stopRepeat
= 1
};
my
$recur
;
my
$GO
= 1;
while
(
$GO
) {
my
$ifs
;
$params
{recursive} =
$recur
;
(
$ifs
,
$recur
) =
$cm
->interface_utilization(
%params
);
if
(
defined
(
$ifs
)) {
print
"-----------------------------------------\n"
;
for
my
$int
(
sort
{
$a
<=>
$b
} (
keys
(%{
$ifs
}))) {
printf
"%5i %5s/%-5s %5s/%-5s %5s/%-5s\n"
,
$int
,
defined
(
$ifs
->{
$int
}->{InMulticasts}) ?
$ifs
->{
$int
}->{InMulticasts} :
'-'
,
defined
(
$ifs
->{
$int
}->{OutMulticasts}) ?
$ifs
->{
$int
}->{OutMulticasts} :
'-'
,
defined
(
$ifs
->{
$int
}->{InBroadcasts}) ?
$ifs
->{
$int
}->{InBroadcasts} :
'-'
,
defined
(
$ifs
->{
$int
}->{OutBroadcasts}) ?
$ifs
->{
$int
}->{OutBroadcasts} :
'-'
,
defined
(
$ifs
->{
$int
}->{InOctets}) ?
$ifs
->{
$int
}->{InOctets} :
'-'
,
defined
(
$ifs
->{
$int
}->{OutOctets}) ?
$ifs
->{
$int
}->{OutOctets} :
'-'
}
}
else
{
printf
"Error: %s\n"
, Cisco::Management->error
}
if
(!
defined
(
$opt
{repeat}) ||
$stopRepeat
) {
last
}
}
}
else
{
if
(
defined
(
my
$ifs
=
$cm
->interface_info(
%params
))) {
my
$ips
=
$cm
->interface_ip();
print
"Index Description Speed/Duplex Admin/Oper IP(s)\n"
;
print
"--------------------------------------------------------------\n"
;
for
my
$int
(
sort
{
$a
<=>
$b
} (
keys
(%{
$ifs
}))) {
printf
"%5i %-25s %4i/%-7s %4s/%-4s "
,
$int
,
$ifs
->{
$int
}->{Description},
(
$ifs
->{
$int
}->{Speed} > 1000000) ?
$ifs
->{
$int
}->{Speed}/1000000 :
$ifs
->{
$int
}->{Speed},
$ifs
->{
$int
}->{Duplex},
$ifs
->{
$int
}->{AdminStatus},
$ifs
->{
$int
}->{OperStatus};
if
(
exists
(
$ips
->{
$int
})) {
for
(0..$
print
" $ips->{$int}->[$_]->{IPAddress}"
}
}
print
"\n"
}
}
else
{
printf
"Error: %s\n"
, Cisco::Management->error
}
}
$cm
->
close
()
}