blib/lib/RackMan/Device/Switch/Cisco_Catalyst.pm

filter: Statementstmt 15.9% 19 / 119 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 42.8% 6 / 14 Podpod 100% 6 / 6 totaltotal 14.7% 31 / 210 CC 30 SCAR 43.8
CC
30
Cov
11%
fetch_from_device
182.0
store_to_device
72.0
fetch_from_database
56.0
CRAP 80.0
« blib/lib/RackMan/Format/Cacti.pm ↑ Summary bin/rack »
1 package RackMan::Device::Switch::Cisco_Catalyst;
2  
3 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use RackMan;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use RackMan::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use RackMan::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 CONFIG_SECTION => "device:switch:cisco_catalyst",
12 CONFIG_FILENAME => "config.cmd",
13 2 };
14  
15  
16 #
17 # write_config()
18 # ------------
19 0 ▶sub write_config {
Subroutine write_config: not called CC=2 SCAR=17.9
Pod documented
20 0 my ($self, $args) = @_;
21  
22 0 my $rackman = $args->{rackman};
23 0 my $path = $rackman->config->val(general => "path");
24  
25 # generate the configuration file
26 0 my $config = $self->fetch_from_database($args);
27  
28 # write the configuration file on disk, to its final path
29 0 $config->name(CONFIG_FILENAME);
30 0 $config->path($path);
31 0 ▶ print " + writing ", $config->fullpath, $/ if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
32 0 my $scm = $rackman->get_scm({ path => $path });
33 0 $scm->update;
34 0 $config->write;
35 0 $scm->add($config->name);
36 0 $scm->commit($config->name, "generated by ".__PACKAGE__
37 . " / $::PROGRAM v$::VERSION");
38 }
39  
40  
41 #
42 # diff_config()
43 # -----------
44 0 ▶sub diff_config {
Subroutine diff_config: not called CC=4 SCAR=30.0
Pod documented
45 0 my ($self, $args) = @_;
46  
47 0 my $rackman = $args->{rackman};
48 0 my $path = $rackman->config->val(general => "path");
49  
50 # fetch config from the device
51 0 my $current = $self->fetch_from_device($args);
52  
53 # generate the config from the database
54 0 my $expected = $self->fetch_from_database($args);
55  
56 # prepare the files
57 0 my @cfg_current = split $/, $current->content;
58 0 my @cfg_expected = split $/, $expected->content;
59  
60 # computes the differences between the two files
61 0 ▶ print " - computing differences..." if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
62 0 my @diff = diff_lines(\@cfg_current, \@cfg_expected);
63 0 ▶ print " done\n\n" if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
64 0 print @diff;
65  
66 0 ▶ RackMan->set_status(1) if @diff;
Branch: 1 branch
BranchTrueFalseTotal
if @diff 0 0 0
67 }
68  
69  
70 #
71 # push_config()
72 # -----------
73 0 ▶sub push_config {
Subroutine push_config: not called CC=1 SCAR=6.9
Pod documented
74 0 my ($self, $args) = @_;
75  
76 0 my $rackman = $args->{rackman};
77 0 my $path = $rackman->config->val(general => "path");
78 0 my $config = RackMan::File->new;
79  
80 # read the configuration file from disk
81 0 $config->name(CONFIG_FILENAME);
82 0 $config->path($path);
83 0 $config->read;
84  
85 # send it to the device
86 0 $self->store_to_device($args, $config);
87 }
88  
89  
90 #
91 # fetch_from_database()
92 # -------------------
93 0 ▶sub fetch_from_database {
Subroutine fetch_from_database: not called CC=7 SCAR=40.3
Pod documented
94 0 my ($self, $args) = @_;
95  
96 0 my $rackman = $args->{rackman};
97 0 my $config = RackMan::File->new;
98 0 my %interface;
99  
100 0 for my $port (@{ $self->ports }) {
101 0 ▶ next unless $port->{name} =~ /^gi/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $port->{"name"} =~ /^gi/i 0 0 0
102  
103 0 (my $name = $port->{name}) =~ s/^\D+/GigabitEthernet/;
104 0 (my $num = $port->{name}) =~ s/^\D+//;
105 my $description = defined $port->{peer_object_name}
106 0 ▶ ? " description $port->{peer_object_name} ($port->{peer_port_name})"
Branch: 1 branch
BranchTrueFalseTotal
defined $port->{'peer_object_name'} ? : 0 0 0
107 : " no description";
108  
109 0 $interface{$num} = "interface $name\n$description\n!\n";
110 }
111  
112 # we need to first store the generated command, then sort them
113 # by the interface number (the "0/2" part in "GigabitEthernet0/2")
114 0 $config->add_content("!\n");
115 $config->add_content($interface{$_})
116 0 for sort {
117 0 my @a = split "/", $a;
118 0 my @b = split "/", $b;
119 0 ▶ $a[0] <=> $b[0] || $a[1] <=> $b[1] || $a[2] <=> $b[2]
Condition0%0/6
$a[0] <=> $b[0] || $a[1] <=> $b[1] || $a[2] <=> $b[2]
l!l&&r!l&&!r
000
$a[0] <=> $b[0] or $a[1] <=> $b[1]
l!l&&r!l&&!r
000
MC/DC0%0/3
$a[0] <=> $b[0] || $a[1] <=> $b[1] || $a[2] <=> $b[2]
$a[0] <=> $b[0] $a[1] <=> $b[1] $a[2] <=> $b[2]
Truth table0%0/4
A or B || C
A= $a[0] <=> $b[0]B= $a[1] <=> $b[1]C= $a[2] <=> $b[2]
ABCresult
1XX1
01X1
0011
0000
120 } keys %interface;
121  
122 0 return $config
123 }
124  
125  
126 #
127 # fetch_from_device()
128 # -----------------
129 0 ▶sub fetch_from_device {
Subroutine fetch_from_device: not called CC=13 SCAR=52.0
Pod documented
130 0 my ($self, $args) = @_;
131  
132 0 my $rackman = $args->{rackman};
133 0 my $config = RackMan::File->new;
134  
135 # fetch the access password
136 my $ios_password = $rackman->options->{device_password}
137 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
138 or RackMan->error("missing IOS access password");
139  
140 # load the protocol module
141 0 ▶ { local $SIG{__WARN__} = sub {}; require Net::Telnet::Cisco; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
142  
143 # connect to the device
144 0 ▶ my $host = $self->attributes->{FQDN} || $self->object_name;
Condition0%0/3
$self->attributes->{'FQDN'} || $self->object_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->attributes->{'FQDN'} || $self->object_name
$self->attributes->{'FQDN'} $self->object_name
Truth table0%0/3
A || B
A= $self->attributes->{'FQDN'}B= $self->object_name
ABresult
1X1
011
000
145 0 my $session = Net::Telnet::Cisco->new(Host => $host);
146 0 $session->login(Password => $ios_password);
147 0 $session->cmd("terminal length 0");
148  
149 # fetch the list of interfaces with their current description
150 0 ▶ print " < fetching the list of interfaces... " if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
151 0 my @out = $session->cmd("show interface description");
152 0 $config->add_content("!\n");
153  
154 0 ▶ print "parsing... " if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
155 0 chomp(my $line = shift @out);
156 0 my @fields = split / +/, $line;
157  
158 0 for my $line (@out) {
159 0 $line =~ s/[\x01-\x1f]//g; # clean control chars
160 0 ▶ next unless defined $line and length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and length $line 0 0 0
Condition0%0/3
defined $line and length $line
!ll&&!rl&&r
000
MC/DC0%0/2
defined $line and length $line
defined $line length $line
Truth table0%0/3
A and B
A= defined $lineB= length $line
ABresult
0X0
100
111
161  
162 # parse the status
163 0 my %interface;
164 0 $line =~ s/admin down/admin-down/;
165 0 @interface{@fields} = split / +/, $line, ~~@fields;
166  
167 # interface name
168 0 ▶ next unless $interface{Interface} =~ /^gi/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $interface{"Interface"} =~ /^gi/i 0 0 0
169 0 $interface{Interface} =~ s/^\D+/GigabitEthernet/;
170 0 $config->add_content("interface $interface{Interface}\n");
171  
172 # interface description
173 0 ▶ $interface{Description} ||= "";
Condition0%0/2
$interface{"Description"} ||= ""
l!l
00
MC/DC0%0/1
$interface{"Description"} ||= ""
$interface{"Description"}
Truth table0%0/2
A
A= $interface{"Description"}
Aresult
11
00
174 my $description = length $interface{Description}
175 0 ▶ ? "description $interface{Description}" : "no description";
Branch: 1 branch
BranchTrueFalseTotal
length $interface{'Description'} ? : 0 0 0
176 0 $config->add_content(" $description\n");
177  
178 0 $config->add_content("!\n");
179 }
180  
181 0 ▶ print "done\n" if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
182 0 $session->close;
183  
184 0 return $config
185 }
186  
187  
188 #
189 # store_to_device()
190 # ---------------
191 0 ▶sub store_to_device {
Subroutine store_to_device: not called CC=8 SCAR=42.8
Pod documented
192 0 my ($self, $args, $config) = @_;
193  
194 0 my $rackman = $args->{rackman};
195  
196 # fetch the access passwords
197 my $ios_password = $rackman->options->{device_password}
198 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
199 or RackMan->error("missing IOS access password");
200 0 ▶ my $enable_password
Branch: 1 branch
BranchTrueFalseTotal
my $enable_password = $rackman->config->val("device:switch:cisco_catalyst", "enable_password") or "RackMan"->error("missing IOS admin password") 0 0 0
201 = $rackman->config->val(CONFIG_SECTION, "enable_password")
202 or RackMan->error("missing IOS admin password");
203  
204 # load the protocol module
205 0 ▶ { local $SIG{__WARN__} = sub {}; require Net::Telnet::Cisco; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
206  
207 # connect to the device
208 0 ▶ my $host = $self->attributes->{FQDN} || $self->object_name;
Condition0%0/3
$self->attributes->{'FQDN'} || $self->object_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->attributes->{'FQDN'} || $self->object_name
$self->attributes->{'FQDN'} $self->object_name
Truth table0%0/3
A || B
A= $self->attributes->{'FQDN'}B= $self->object_name
ABresult
1X1
011
000
209 0 my $session = Net::Telnet::Cisco->new(Host => $host);
210 0 $session->login(Password => $ios_password);
211 0 $session->enable(Password => $enable_password);
212  
213 # enter configure mode
214 0 $session->cmd("configure terminal");
215 0 ▶ print " > sending commands batch... " if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
216  
217 # send the commands batch to the device
218 0 for my $cmd (split $/, $config->content) {
219 0 $session->cmd($cmd);
220 }
221  
222 0 ▶ print "done\n" if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
223 0 $session->close;
224  
225 0 return $config
226 }
227  
228  
229 2 __PACKAGE__
230  
231 __END__
« blib/lib/RackMan/Format/Cacti.pm ↑ Summary bin/rack »