script/ospfview

filter: Statementstmt 30.4% 46 / 151 Branchbran 1.5% 1 / 66 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 48.3% 15 / 31 Podpod n/a totaltotal 19.6% 62 / 315 CC 50 SCAR 67.0
CC
50
Cov
18%
main
1567.3
__ANON__
72.0
__ANON__
6.0
CRAP 811.1
↑ Summary blib/lib/OSPF/LSDB/View6.pm »
1 #!/usr/bin/perl
2  
3 ##########################################################################
4 # Copyright (c) 2012-2021 Alexander Bluhm <alexander.bluhm@gmx.net>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 ##########################################################################
18  
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Getopt::Long qw(:config posix_default bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Time::HiRes qw(time sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use OSPF::LSDB::ospfd;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use OSPF::LSDB::ospf6d;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use OSPF::LSDB::View;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use OSPF::LSDB::View6;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use OSPF::LSDB::YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 sub usage(@) {
33 1 ▶ print STDERR "Error: @_\n" if @_;
Subroutine usage: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
34 1 print STDERR <<EOF;
35 Periodically poll OSPF database from routing daemon and display it on X11.
36  
37 Usage: $0 [-46bBcdDeEhlpPsSwWv] [-H user\@host] [-I interval]
38 -4 disable IPv6
39 -6 enable IPv6
40 -b generate other area AS boundary router summary
41 -B aggregate other area AS boundary router summary
42 -c cluster identical networks
43 -d show OSPF database diff between updates
44 -D dump OSPF database after updates as YAML to stdout
45 -e generate AS external networks
46 -E aggregate AS external networks
47 -h help, print usage
48 -H user\@host use ssh to login into user\@host to run ospfctl there
49 -I interval query interval in seconds, default 5
50 -l generate legend
51 -p generate link and intra-area-prefix
52 -P generate intra-area-prefix
53 -s generate other area network summary
54 -S aggregate other area network summary
55 -w show most serious warning in dot graph
56 -W show all warnings and areas in dot graph
57 -v be verbose, print warnings to stdout
58 EOF
59 1 exit(2);
60 }
61  
62 sub main() {
63 1 ▶ my $diff;
Subroutine main: called CC=41 SCAR=73.6
64 my $dump;
65 1 my $interval = 5;
66 1 my $ipv6;
67 my $legend;
68 1 my $ssh;
69 1 my %todo;
70 GetOptions(
71 0 ▶ '4' => sub { $ipv6 = 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 ▶ '6' => sub { $ipv6 = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 ▶ 'b' => sub { $todo{boundary}{generate} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 ▶ 'B' => sub { $todo{boundary}{aggregate} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 ▶ 'c' => sub { $todo{cluster} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 'd' => \$diff,
77 'D' => \$dump,
78 0 ▶ 'e' => sub { $todo{external}{generate} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 0 ▶ 'E' => sub { $todo{external}{aggregate} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 1 ▶ 'h' => sub { usage() },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 'H=s' => \$ssh,
82 'I=i' => \$interval,
83 'l' => \$legend,
84 0 ▶ 'p' => sub { $todo{prefix}{generate} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 0 ▶ 'P' => sub { $todo{prefix}{aggregate} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 0 ▶ 's' => sub { $todo{summary}{generate} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 ▶ 'S' => sub { $todo{summary}{aggregate} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 ▶ 'w' => sub { $todo{warning}{single} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 ▶ 'W' => sub { $todo{warning}{all} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 ▶ 'v' => sub { $todo{verbose} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 1 ▶ ) or usage("Bad option");
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(4, sub { $ipv6 = 0; } , 6, sub { $ipv6 = 1; } , "b", sub { $todo{"boundary"}{"generate"} = 1; } , "B", sub { $todo{"boundary"}{"aggregate"} = 1; } , "c", sub { $todo{"cluster"} = 1; } , "d", \$diff, "D", \$dump, "e", sub { $todo{"external"}{"generate"} = 1; } , "E", sub { $todo{"external"}{"aggregate"} = 1; } , "h", sub { &usage(); } , "H=s", \$ssh, "I=i", \$interval, "l", \$legend, "p", sub { $todo{"prefix"}{"generate"} = 1; } , "P", sub { $todo{"prefix"}{"aggregate"} = 1; } , "s", sub { $todo{"summary"}{"generate"} = 1; } , "S", sub { $todo{"summary"}{"aggregate"} = 1; } , "w", sub { $todo{"warning"}{"single"} = 1; } , "W", sub { $todo{"warning"}{"all"} = 1; } , "v", sub { $todo{"verbose"} = 1; } ) or &usage("Bad option") 0 0 0
92 0 ▶ usage("No arguments allowed") if @ARGV > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV > 0 0 0 0
93  
94 0 foreach my $option (qw(boundary external prefix summary warning)) {
95 0 ▶ if (keys %{$todo{$option} || {}} > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %{$todo{$option} or {};} > 1) 0 0 0
$todo{$option} or {} 0 0 0
96 0 my $opt = substr($option, 0, 1);
97 0 usage("Options -$opt and -".uc($opt)." used together");
98 }
99 }
100  
101 0 ▶ if ($todo{prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($todo{"prefix"}) 0 0 0
102 0 $todo{intra}{generate} = 1;
103 0 ▶ $todo{link}{generate} = 1 if $todo{prefix}{generate};
Branch: 1 branch
BranchTrueFalseTotal
if $todo{"prefix"}{"generate"} 0 0 0
104 }
105  
106 0 my @cmd = qw(dot -Txlib);
107 0 my($pid, $fh, $gone, $term);
108 0 $term = 0;
109 $SIG{INT} = sub {
110 0 ▶ local $!;
Subroutine __ANON__: not called CC=2 SCAR=17.9
111 0 ▶ kill SIGTERM, $pid if $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pid 0 0 0
112 0 $SIG{'INT'} = 'DEFAULT';
113 0 $term = 1;
114 0 kill SIGINT, $$;
115 0 };
116 $SIG{CHLD} = sub {
117 0 ▶ return unless $pid;
Subroutine __ANON__: not called CC=8 SCAR=42.8
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
118 0 local ($!, $?);
119 0 $? = 0;
120 0 waitpid($pid, POSIX::WNOHANG);
121 0 ▶ die "'@cmd' failed: $?" if $? &&
Branch: 1 branch
BranchTrueFalseTotal
if $? and WIFEXITED($?) && WEXITSTATUS($?) != 0 || WIFSIGNALED($?) && WTERMSIG($?) != 15 0 0 0
Condition0%0/12
$? and WIFEXITED($?) && WEXITSTATUS($?) != 0 || WIFSIGNALED($?) && WTERMSIG($?) != 15
!ll&&!rl&&r
000
WIFEXITED($?) and WEXITSTATUS($?) != 0 or WIFSIGNALED($?) and WTERMSIG($?) != 15
l!l&&r!l&&!r
000
WIFEXITED($?) and WEXITSTATUS($?) != 0
!ll&&!rl&&r
000
WIFSIGNALED($?) and WTERMSIG($?) != 15
!ll&&!rl&&r
000
MC/DC0%0/5
$? and WIFEXITED($?) && WEXITSTATUS($?) != 0 || WIFSIGNALED($?) && WTERMSIG($?) != 15
$? WIFEXITED($?) WEXITSTATUS($?) != 0 WIFSIGNALED($?) WTERMSIG($?) != 15
Truth table0%0/8
A and B and C or D and E
A= $?B= WIFEXITED($?)C= WEXITSTATUS($?) != 0D= WIFSIGNALED($?)E= WTERMSIG($?) != 15
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
122 ((WIFEXITED($?) && WEXITSTATUS($?) != 0) ||
123 (WIFSIGNALED($?) && WTERMSIG($?) != SIGTERM));
124 0 ▶ $gone = 1 if kill(0, $pid) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if kill(0, $pid) == 0 0 0 0
125 0 };
126  
127 0 ▶ my $class = $ipv6 ? 'OSPF::LSDB::View6' : 'OSPF::LSDB::View';
Branch: 1 branch
BranchTrueFalseTotal
$ipv6 ? : 0 0 0
128 0 ▶ if ($legend) {
Branch: 1 branch
BranchTrueFalseTotal
if ($legend) 0 0 0
129 0 my $dot = $class->legend();
130 0 $gone = 0;
131 0 ▶ $pid = open($fh, '|-', @cmd)
Branch: 1 branch
BranchTrueFalseTotal
$pid = open($fh, "|-", @cmd) or die "Open pipe to '@{cmd}' failed: $!" 0 0 0
132 or die "Open pipe to '@cmd' failed: $!";
133 0 print $fh $dot, "\n";
134 # work around the non interruptable wait in CORE::close.
135 0 IO::Handle::flush($fh);
136 0 POSIX::close(fileno($fh));
137 0 pause();
138 0 ▶ unless (close($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $fh) 0 0 0
139 0 ▶ die "'@cmd' failed: $?" if WIFSIGNALED($?) &&
Branch: 1 branch
BranchTrueFalseTotal
if WIFSIGNALED($?) and WTERMSIG($?) != 15 0 0 0
Condition0%0/3
WIFSIGNALED($?) and WTERMSIG($?) != 15
!ll&&!rl&&r
000
MC/DC0%0/2
WIFSIGNALED($?) and WTERMSIG($?) != 15
WIFSIGNALED($?) WTERMSIG($?) != 15
Truth table0%0/3
A and B
A= WIFSIGNALED($?)B= WTERMSIG($?) != 15
ABresult
0X0
100
111
140 WTERMSIG($?) != SIGTERM;
141 }
142 0 undef $pid;
143 0 exit 0;
144 }
145  
146 0 my($oldtime, $oldyaml);
147 0 for (;;) {
148 0 my $time = time();
149 0 ▶ if ($oldtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oldtime) 0 0 0
150 0 my $sleeptime = $interval - ($time - $oldtime);
151 0 ▶ if ($sleeptime > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sleeptime > 0) 0 0 0
152 0 select(undef, undef, undef, $sleeptime);
153 }
154 }
155 0 $oldtime = time();
156  
157 0 ▶ my $ospfclass = $ipv6 ? 'OSPF::LSDB::ospf6d' : 'OSPF::LSDB::ospfd';
Branch: 1 branch
BranchTrueFalseTotal
$ipv6 ? : 0 0 0
158 0 my $ospf = $ospfclass->new(ssh => $ssh);
159 0 eval { $ospf->parse(); };
160 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
161 0 warn $@;
162 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
163 0 kill SIGTERM, $pid;
164 0 ▶ unless (close($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $fh) 0 0 0
165 0 ▶ die "'@cmd' failed: $?" if WIFSIGNALED($?) &&
Branch: 1 branch
BranchTrueFalseTotal
if WIFSIGNALED($?) and WTERMSIG($?) != 15 0 0 0
Condition0%0/3
WIFSIGNALED($?) and WTERMSIG($?) != 15
!ll&&!rl&&r
000
MC/DC0%0/2
WIFSIGNALED($?) and WTERMSIG($?) != 15
WIFSIGNALED($?) WTERMSIG($?) != 15
Truth table0%0/3
A and B
A= WIFSIGNALED($?)B= WTERMSIG($?) != 15
ABresult
0X0
100
111
166 WTERMSIG($?) != SIGTERM;
167 }
168 0 undef $pid;
169 }
170 0 next;
171 }
172  
173 0 my $yamlospf = OSPF::LSDB::YAML->new($ospf);
174 0 ▶ if (defined $ipv6 && $ipv6 != $yamlospf->ipv6()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ipv6 and $ipv6 != $yamlospf->ipv6) 0 0 0
Condition0%0/3
defined $ipv6 and $ipv6 != $yamlospf->ipv6
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ipv6 and $ipv6 != $yamlospf->ipv6
defined $ipv6 $ipv6 != $yamlospf->ipv6
Truth table0%0/3
A and B
A= defined $ipv6B= $ipv6 != $yamlospf->ipv6
ABresult
0X0
100
111
175 0 die "Address family does not match -4 and -6 options.\n";
176 }
177 0 my $yaml = $yamlospf->Dump();
178 0 $yaml =~ s/^\s+(age|sequence): .*$//mg;
179 0 ▶ next if ($oldyaml && $oldyaml eq $yaml && ! $gone) || $term;
Branch: 1 branch
BranchTrueFalseTotal
if $oldyaml and $oldyaml eq $yaml and not $gone or $term 0 0 0
Condition0%0/9
$oldyaml and $oldyaml eq $yaml and not $gone or $term
l!l&&r!l&&!r
000
$oldyaml and $oldyaml eq $yaml and not $gone
!ll&&!rl&&r
000
$oldyaml and $oldyaml eq $yaml
!ll&&!rl&&r
000
MC/DC0%0/4
$oldyaml and $oldyaml eq $yaml and not $gone or $term
$oldyaml $oldyaml eq $yaml not $gone $term
Truth table0%0/7
A and B and not(C) or D
A= $oldyamlB= $oldyaml eq $yamlC= not $goneD= $term
ABCDresult
111X1
0XX11
10X11
11011
0XX00
10X00
11000
180 0 ▶ if ($dump) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dump) 0 0 0
181 0 print $yaml;
182 }
183 0 ▶ if ($diff && $oldyaml) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff and $oldyaml) 0 0 0
Condition0%0/3
$diff and $oldyaml
!ll&&!rl&&r
000
MC/DC0%0/2
$diff and $oldyaml
$diff $oldyaml
Truth table0%0/3
A and B
A= $diffB= $oldyaml
ABresult
0X0
100
111
184 0 my %args = (
185 SUFFIX => ".yaml",
186 TEMPLATE => "ospfview-XXXXXXXXXX",
187 TMPDIR => 1,
188 UNLINK => 1
189 );
190 0 my $old = File::Temp->new(%args);
191 0 print $old $oldyaml;
192 0 my $new = File::Temp->new(%args);
193 0 print $new $yaml;
194 0 system('diff', '-up', $old->filename, $new->filename);
195 }
196 0 $oldyaml = $yaml;
197  
198 0 my $view = $class->new($ospf);
199 0 my $dot = $view->graph(%todo);
200 0 ▶ if ($todo{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($todo{"verbose"}) 0 0 0
201 0 my @errors = $view->get_errors;
202 0 ▶ print map { "$_\n" } @errors, "" if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 0 0
203 }
204  
205 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
206 0 kill SIGTERM, $pid;
207 0 $? = 0;
208 0 ▶ unless (close($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $fh) 0 0 0
209 0 ▶ die "'@cmd' failed: $?" if $? &&
Branch: 1 branch
BranchTrueFalseTotal
if $? and WIFEXITED($?) && WEXITSTATUS($?) != 0 || WIFSIGNALED($?) && WTERMSIG($?) != 15 0 0 0
Condition0%0/12
$? and WIFEXITED($?) && WEXITSTATUS($?) != 0 || WIFSIGNALED($?) && WTERMSIG($?) != 15
!ll&&!rl&&r
000
WIFEXITED($?) and WEXITSTATUS($?) != 0 or WIFSIGNALED($?) and WTERMSIG($?) != 15
l!l&&r!l&&!r
000
WIFEXITED($?) and WEXITSTATUS($?) != 0
!ll&&!rl&&r
000
WIFSIGNALED($?) and WTERMSIG($?) != 15
!ll&&!rl&&r
000
MC/DC0%0/5
$? and WIFEXITED($?) && WEXITSTATUS($?) != 0 || WIFSIGNALED($?) && WTERMSIG($?) != 15
$? WIFEXITED($?) WEXITSTATUS($?) != 0 WIFSIGNALED($?) WTERMSIG($?) != 15
Truth table0%0/8
A and B and C or D and E
A= $?B= WIFEXITED($?)C= WEXITSTATUS($?) != 0D= WIFSIGNALED($?)E= WTERMSIG($?) != 15
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
210 ((WIFEXITED($?) && WEXITSTATUS($?) != 0) ||
211 (WIFSIGNALED($?) && WTERMSIG($?) != SIGTERM));
212 }
213 0 undef $pid;
214 }
215 0 $gone = 0;
216 0 ▶ $pid = open($fh, '|-', @cmd)
Branch: 1 branch
BranchTrueFalseTotal
$pid = open($fh, "|-", @cmd) or die "Open pipe to '@{cmd}' failed: $!" 0 0 0
217 or die "Open pipe to '@cmd' failed: $!";
218 0 print $fh $dot, "\n";
219 # work around the wait in CORE::close.
220 0 IO::Handle::flush($fh);
221 0 POSIX::close(fileno($fh));
222 }
223 }
224  
225 main();
↑ Summary blib/lib/OSPF/LSDB/View6.pm »