bin/cisco-status

filter: Statementstmt 23.3% 39 / 167 Branchbran 1.7% 1 / 58 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 57.8% 11 / 19 Podpod n/a totaltotal 16.8% 51 / 302 CC 49 SCAR 52.4
CC
49
Cov
15%
process
506.0
run
72.0
parse_etherchannels
42.0
CRAP 188.0
« blib/lib/RackMan/Device/Server/HP_ProLiant.pm ↑ Summary blib/lib/RackMan/Tasks.pm »
1 #!/usr/bin/env perl
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use List::Util qw< max >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::Telnet::Cisco;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use RackMan;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use RackMan::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use RackMan::Device::Switch::Cisco_Catalyst;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Term::ANSIColor qw< :constants >;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 1 $::PROGRAM = "Cisco switch status";
16 1 $::VERSION = "1.02";
17 1 $::COMMAND = basename($0);
18  
19  
20 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 CONFIG_SECTION => RackMan::Device::Switch::Cisco_Catalyst->CONFIG_SECTION,
22 1 };
23  
24  
25 #
26 # main
27 #
28 MAIN: {
29 1 ▶ if (not caller()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller()) 0 1 1
30 0 run();
31 0 exit RackMan->status;
32 }
33 }
34  
35  
36 #
37 # run()
38 # ---
39 0 ▶sub run {
Subroutine run: not called CC=8 SCAR=42.8
40 # detect if stdout is connected to a terminal
41 0 ▶ (-t STDOUT ? $Term::ANSIColor::AUTORESET : $ENV{ANSI_COLORS_DISABLED}) = 1;
Branch: 1 branch
BranchTrueFalseTotal
-t STDOUT ? : 0 0 0
42 0 $|++;
43  
44 # default options
45 0 my %options = (
46 config => "/usr/local/etc/rack.conf",
47 );
48  
49 # parse options
50 0 Getopt::Long::Configure(qw< no_auto_abbrev no_ignore_case >);
51 0 ▶ GetOptions(\%options, qw{
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%options, ("help|usage|h!", "man!", "version|V!", "verbose|v!", "config|c=s", "device_login|device-login=s", "device_password|device-password=s")) or pod2usage(0) 0 0 0
52 help|usage|h! man! version|V!
53 verbose|v! config|c=s
54 device_login|device-login=s device_password|device-password=s
55 }) or pod2usage(0);
56  
57 # handle --version, --help and --man
58 0 ▶ $options{man} and pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
$options{"man"} and pod2usage(2) 0 0 0
59 0 ▶ $options{help} and pod2usage(1);
Branch: 1 branch
BranchTrueFalseTotal
$options{"help"} and pod2usage(1) 0 0 0
60 0 ▶ $options{version} and print "$::PROGRAM v$::VERSION\n" and exit;
Branch: 1 branch
BranchTrueFalseTotal
$options{"version"} and print "$main::PROGRAM v$main::VERSION\n" and exit 0 0 0
Condition0%0/3
$options{"version"} and print "$main::PROGRAM v$main::VERSION\n"
!ll&&!rl&&r
000
MC/DC0%0/2
$options{"version"} and print "$main::PROGRAM v$main::VERSION\n"
$options{"version"} print "$main::PROGRAM v$main::VERSION\n"
Truth table0%0/3
A and B
A= $options{"version"}B= print "$main::PROGRAM v$main::VERSION\n"
ABresult
0X0
100
111
61  
62 # if there's no argument, print the usage
63 0 ▶ pod2usage(1) if @ARGV == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV == 0 0 0 0
64  
65 # read configuration file
66 0 my $config = RackMan::Config->new(-file => $options{config});
67  
68 # instanciate the backend object
69 0 my $rackman = RackMan->new({ options => \%options, config => $config });
70  
71 # do the actual work
72 0 process($rackman, @ARGV);
73 }
74  
75  
76 #
77 # pod2usage()
78 # ---------
79 0 ▶sub pod2usage {
Subroutine pod2usage: not called CC=1 SCAR=6.9
80 0 my ($n) = @_;
81 0 require Pod::Usage;
82 0 Pod::Usage::pod2usage({ -exitval => 0, -verbose => $n, -noperldoc => 1 });
83 }
84  
85  
86 #
87 # process()
88 # -------
89 0 ▶sub process {
Subroutine process: not called CC=22 SCAR=62.3
90 0 my ($rackman, @args) = @_;
91  
92 0 my $device_name = shift @args;
93 0 my $racktables = $rackman->racktables;
94  
95 # fetch the corresponding RackObject
96 0 my $rackobj = $rackman->device($device_name);
97  
98 # check that the device is actually a Cisco switch
99 0 ▶ print STDERR "error: '$device_name' is not a Cisco device\n" and return
Branch: 1 branch
BranchTrueFalseTotal
unless $rackobj->DOES("RackMan::Device::Switch::Cisco_Catalyst") 0 0 0
Condition0%0/5
$rackobj->DOES('RackMan::Device::Switch::Cisco_Catalyst') || print(STDERR "error: '${device_name}' is not a Cisco device\n") && (return)
l!l&&r!l&&!r
000
print STDERR "error: '${device_name}' is not a Cisco device\n" and return
!ll
00
MC/DC0%0/2
$rackobj->DOES('RackMan::Device::Switch::Cisco_Catalyst') || print(STDERR "error: '${device_name}' is not a Cisco device\n") && (return)
$rackobj->DOES('RackMan::Device::Switch::Cisco_Catalyst') print STDERR "error: '${device_name}' is not a Cisco device\n"
Truth table0%0/3
A || B
A= $rackobj->DOES('RackMan::Device::Switch::Cisco_Catalyst')B= print STDERR "error: '${device_name}' is not a Cisco device\n"
ABresult
1X1
011
000
100 unless $rackobj->DOES("RackMan::Device::Switch::Cisco_Catalyst");
101  
102 # fetch the access password
103 0 $rackman->config->set_current_rackobject($rackobj);
104 my $ios_password = $rackman->options->{device_password}
105 0 ▶ || $rackman->config->val(CONFIG_SECTION, "ios_password")
Branch: 1 branch
BranchTrueFalseTotal
my $ios_password = $rackman->options->{"device_password"} || $rackman->config->val("device:switch:cisco_catalyst", "ios_password") or "RackMan"->error("missing IOS access password") 0 0 0
Condition0%0/3
$rackman->options->{'device_password'} || $rackman->config->val('device:switch:cisco_catalyst', 'ios_password')
l!l&&r!l&&!r
000
MC/DC0%0/2
$rackman->options->{'device_password'} || $rackman->config->val('device:switch:cisco_catalyst', 'ios_password')
$rackman->options->{'device_password'} $rackman->config->val('device:switch:cisco_catalyst', 'ios_password')
Truth table0%0/3
A || B
A= $rackman->options->{'device_password'}B= $rackman->config->val('device:switch:cisco_catalyst', 'ios_password')
ABresult
1X1
011
000
106 or RackMan->error("missing IOS access password");
107  
108 # connect to the device
109 0 ▶ my $host = $rackobj->attributes->{FQDN} || $rackobj->object_name;
Condition0%0/3
$rackobj->attributes->{'FQDN'} || $rackobj->object_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$rackobj->attributes->{'FQDN'} || $rackobj->object_name
$rackobj->attributes->{'FQDN'} $rackobj->object_name
Truth table0%0/3
A || B
A= $rackobj->attributes->{'FQDN'}B= $rackobj->object_name
ABresult
1X1
011
000
110 0 print $rackobj->object_name, " ($host, ";
111 0 my $session = Net::Telnet::Cisco->new(Host => $host);
112 0 $session->login(Password => $ios_password);
113 0 $session->cmd("terminal length 0");
114  
115 # fetch and parse information from the device
116 0 my @out = $session->cmd("show version");
117 0 my %version = parse_version(@out);
118 0 print "$version{model}, IOS $version{ios})\n";
119  
120 0 @out = $session->cmd("show etherchannel summary");
121 0 my %group = parse_etherchannels(@out);
122  
123 0 @out = $session->cmd("show mac address-table dynamic");
124 0 my %addr = parse_mac_address_table(@out);
125  
126 0 @out = $session->cmd("show interfaces status");
127 0 my %interface = parse_interfaces_status(@out);
128  
129 # associate the MAC addresses of a channel with the physical
130 # interfaces that compose the channel
131 0 for my $channel (keys %group) {
132 0 for my $port (keys %{ $group{$channel}{ports} }) {
133 $addr{$port} = $addr{$channel}
134 0 ▶ if not $addr{$port} or not @{ $addr{$port} };
Branch: 1 branch
BranchTrueFalseTotal
unless $addr{$port} and @{$addr{$port};} 0 0 0
Condition0%0/3
$addr{$port} and @{$addr{$port};}
!ll&&!rl&&r
000
MC/DC0%0/2
$addr{$port} and @{$addr{$port};}
$addr{$port} @{$addr{$port};}
Truth table0%0/3
A and B
A= $addr{$port}B= @{$addr{$port};}
ABresult
0X0
100
111
135 }
136 }
137  
138 0 my $m = max map length, keys %interface;
139 0 my %port_status = (
140 connected => GREEN ("[+]"),
141 disabled => RED ("[X]"),
142 notconnect => YELLOW("[ ]"),
143 );
144  
145 0 for my $name (sort by_interface keys %interface) {
146 0 ▶ next if $group{$name}; # skip port channels
Branch: 1 branch
BranchTrueFalseTotal
if $group{$name} 0 0 0
147 0 ▶ my $n = ref $addr{$name} ? @{ $addr{$name} } : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $addr{$name} ? : 0 0 0
148 0 ▶ my $hilight = $interface{$name}{status} eq "connected" ? BOLD : "";
Branch: 1 branch
BranchTrueFalseTotal
$interface{$name}{'status'} eq 'connected' ? : 0 0 0
149  
150 printf "- %-${m}s %s%s %s%-7s %6s/%-6s%s ",
151 $name,
152 $hilight,
153 $port_status{ $interface{$name}{status} },
154 $hilight,
155 $interface{$name}{vlan} eq "trunk" ? "trunk"
156 : "vlan:$interface{$name}{vlan}",
157 $interface{$name}{duplex},
158 $interface{$name}{speed},
159 0 ▶ $interface{$name}{status} eq "connected" ? RESET : "",
Branch: 2 branches
BranchTrueFalseTotal
$interface{$name}{'vlan'} eq 'trunk' ? : 0 0 0
$interface{$name}{'status'} eq 'connected' ? : 0 0 0
160 ;
161  
162 0 ▶ if (0 < $n and $n <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $n and $n <= 2) 0 0 0
Condition0%0/3
0 < $n and $n <= 2
!ll&&!rl&&r
000
MC/DC0%0/2
0 < $n and $n <= 2
0 < $n $n <= 2
Truth table0%0/3
A and B
A= 0 < $nB= $n <= 2
ABresult
0X0
100
111
163 # fetch the MAC address associated to the interface
164 0 my $peer_mac = lc shift @{ $addr{$name} };
165 0 my ($peer_name, $peer_iface) = ("", "");
166  
167 0 ▶ if (index($peer_mac, "00005e0001") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($peer_mac, '00005e0001') == 0) { } 0 0 0
168 # detect VRRP addresses
169 0 $peer_name = "(VRRP)";
170 }
171 else {
172 # find the corresponding device
173 0 my $peer_obj = $racktables->resultset("RackObject")->search(
174 { l2address => $peer_mac },
175 { join => "ports" },
176 )->first;
177 0 $peer_name = eval { $peer_obj->name };
178  
179 0 ▶ if ($peer_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($peer_name) 0 0 0
180 0 my $peer = $rackman->device($peer_name);
181 my ($peer_port)
182 0 ▶ = grep { lc($_->{l2address} || "") eq $peer_mac }
Condition0%0/2
$_->{'l2address'} || ''
l!l
00
MC/DC0%0/1
$_->{'l2address'} || ''
$_->{'l2address'}
Truth table0%0/2
A
A= $_->{'l2address'}
Aresult
11
00
183 0 @{ $peer->ports };
184 0 $peer_iface = "[$peer_port->{name}]";
185 }
186 }
187  
188 # format the MAC address in the usual way
189 0 my $peer_addr = $peer_mac;
190 0 $peer_addr =~ s/(\w\w)/$1:/g;
191 0 $peer_addr =~ s/:$//;
192  
193 0 ▶ $peer_name ||= YELLOW("unknown");
Condition0%0/3
$peer_name ||= YELLOW("unknown")
l!l&&r!l&&!r
000
MC/DC0%0/2
$peer_name ||= YELLOW("unknown")
$peer_name YELLOW("unknown")
Truth table0%0/3
A ||= B
A= $peer_nameB= YELLOW("unknown")
ABresult
1X1
011
000
194 0 print "$peer_addr = $peer_name $peer_iface ";
195 }
196  
197 0 print $/;
198 }
199 }
200  
201  
202 #
203 # by_interface()
204 # ------------
205 0 ▶sub by_interface {
Subroutine by_interface: not called CC=3 SCAR=24.8
206 0 my @a = split "/", $a;
207 0 my @b = split "/", $b;
208 0 ▶ $a[0] cmp $b[0] || $a[1] <=> $b[1] || $a[2] <=> $b[2]
Branch: 1 branch
BranchTrueFalseTotal
$a[0] cmp $b[0] or $a[1] <=> $b[1] or $a[2] <=> $b[2] 0 0 0
Condition0%0/3
$a[0] cmp $b[0] or $a[1] <=> $b[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$a[0] cmp $b[0] or $a[1] <=> $b[1]
$a[0] cmp $b[0] $a[1] <=> $b[1]
Truth table0%0/3
A or B
A= $a[0] cmp $b[0]B= $a[1] <=> $b[1]
ABresult
1X1
011
000
209 }
210  
211  
212 #
213 # parse_version()
214 # -------------
215 0 ▶sub parse_version {
Subroutine parse_version: not called CC=4 SCAR=30.0
216 0 my (@out) = @_;
217  
218 0 chomp @out;
219 0 my %version = ( ios => "unkown", model => "unkown" );
220  
221 0 for (@out) {
222 0 ▶ /Cisco IOS Software.*Version (\S+),/ and $version{ios} = $1;
Branch: 1 branch
BranchTrueFalseTotal
/Cisco IOS Software.*Version (\S+),/ and $version{"ios"} = $1 0 0 0
223 0 ▶ /Model number\s*:\s*(\S+)/ and $version{model} = $1;
Branch: 1 branch
BranchTrueFalseTotal
/Model number\s*:\s*(\S+)/ and $version{"model"} = $1 0 0 0
224 }
225  
226 0 return %version
227 }
228  
229  
230 #
231 # parse_etherchannels()
232 # -------------------
233 0 ▶sub parse_etherchannels {
Subroutine parse_etherchannels: not called CC=6 SCAR=37.4
234 0 my (@out) = @_;
235  
236 0 chomp @out;
237 0 my %channel;
238  
239 0 for (@out) {
240 0 s/^ *//; # remove leading blanks
241 0 ▶ next if /^Flags/ .. /^----/; # skip the header
Branch: 1 branch
BranchTrueFalseTotal
if /^Flags/ .. /^----/ 0 0 0
242 0 my ($num, $name, $proto, @ports) = split / +/;
243 0 ▶ next unless $num and $num =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $num and $num =~ /^\d+$/ 0 0 0
Condition0%0/3
$num and $num =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$num and $num =~ /^\d+$/
$num $num =~ /^\d+$/
Truth table0%0/3
A and B
A= $numB= $num =~ /^\d+$/
ABresult
0X0
100
111
244  
245 # extract the group name and flags
246 0 $name =~ s/\((\w+)\)$//;
247 0 my %flags = map { $_ => 1 } split //, $1;
248  
249 0 my %ports;
250  
251 # extract each port name and flags
252 0 for my $port (@ports) {
253 0 (my $name = $port) =~ s/\((\w+)\)$//;
254 0 my %flags = map { $_ => 1 } split //, $1;
255 0 $ports{$name} = { flags => \%flags };
256 }
257  
258 0 $channel{$name} = {
259 flags => \%flags,
260 proto => $proto,
261 ports => \%ports,
262 };
263 }
264  
265 0 return %channel
266 }
267  
268  
269 #
270 # parse_mac_address_table()
271 # -----------------------
272 0 ▶sub parse_mac_address_table {
Subroutine parse_mac_address_table: not called CC=6 SCAR=37.4
273 0 my (@out) = @_;
274  
275 0 chomp @out;
276 0 my %table;
277  
278 0 for (@out) {
279 0 s/^ *//; # remove leading blanks
280 0 ▶ next if /^Mac/ .. /^---- /; # skip the header
Branch: 1 branch
BranchTrueFalseTotal
if /^Mac/ .. /^---- / 0 0 0
281 0 my ($num, $addr, $type, $port) = split / +/;
282 0 ▶ next unless $num and $num =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $num and $num =~ /^\d+$/ 0 0 0
Condition0%0/3
$num and $num =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$num and $num =~ /^\d+$/
$num $num =~ /^\d+$/
Truth table0%0/3
A and B
A= $numB= $num =~ /^\d+$/
ABresult
0X0
100
111
283  
284 0 $addr =~ s/\W//g; # normalize MAC address
285 0 ▶ push @{ $table{$port} ||= [] }, $addr;
Condition0%0/2
$table{$port} ||= []
l!l
00
MC/DC0%0/1
$table{$port} ||= []
$table{$port}
Truth table0%0/2
A
A= $table{$port}
Aresult
11
00
286 }
287  
288 0 return %table
289 }
290  
291  
292 #
293 # parse_interfaces_status()
294 # -----------------------
295 0 ▶sub parse_interfaces_status {
Subroutine parse_interfaces_status: not called CC=6 SCAR=37.4
296 0 my (@out) = @_;
297  
298 0 chomp @out;
299 0 my %interface;
300  
301 0 for my $line (@out) {
302 0 $line =~ s/^ *//; # remove leading blanks
303 0 ▶ next if $line =~ /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$/ 0 0 0
304  
305 # extract the port name
306 0 $line =~ s/^(\S+)\s+//;
307 0 ▶ my $name = $1 or next;
Branch: 1 branch
BranchTrueFalseTotal
my $name = $1 or next 0 0 0
308  
309 # extract the fixed fields
310 0 ▶ $line =~ s/\s*((?:connected|disabled|notconnect).*)$// or next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ s/\s*((?:connected|disabled|notconnect).*)$// or next 0 0 0
311 0 my $rest = $1;
312 0 my ($status, $vlan, $duplex, $speed, $type) = split / +/, $rest, 5;
313  
314 # what's left should be the port description, if any
315 0 ▶ my $desc = length $line ? $line : "";
Branch: 1 branch
BranchTrueFalseTotal
length $line ? : 0 0 0
316 0 $line =~ s/^(\S+)\s+//;
317  
318 0 $interface{$name} = {
319 desc => $desc, status => $status, vlan => $vlan,
320 duplex => $duplex, speed => $speed, type => $type,
321 };
322 }
323  
324 0 return %interface
325 }
326  
327  
328 1 1
329  
330 __END__
« blib/lib/RackMan/Device/Server/HP_ProLiant.pm ↑ Summary blib/lib/RackMan/Tasks.pm »