script/ospfview

filter: Statementstmt 30.6% 46 / 150 Branchbran 1.4% 1 / 70 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 46.8% 15 / 32 Podpod n/a totaltotal 20.0% 62 / 309 CC 50 SCAR 66.1
CC
50
Cov
18%
main
1490.8
__ANON__
72.0
__ANON__
6.0
CRAP 744.1
↑ Summary blib/lib/OSPF/LSDB/View6.pm »
1 #!/usr/bin/perl
2  
3 ##########################################################################
4 # Copyright (c) 2012-2022 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 IPC::Open2;
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=40 SCAR=73.1
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{TERM} = 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 $term = 1;
113 0 };
114 $SIG{INT} = sub {
115 0 ▶ local $!;
Subroutine __ANON__: not called CC=2 SCAR=17.9
116 0 ▶ kill SIGTERM, $pid if $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pid 0 0 0
117 0 $SIG{'INT'} = 'DEFAULT';
118 0 $term = 1;
119 0 kill SIGINT, $$;
120 0 };
121 $SIG{CHLD} = sub {
122 0 ▶ if ($pid) {
Subroutine __ANON__: not called CC=8 SCAR=42.8
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
123 0 local ($!, $?);
124 0 ▶ if (waitpid($pid, POSIX::WNOHANG) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid($pid, 1) > 0) 0 0 0
125 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
126 ((WIFEXITED($?) && WEXITSTATUS($?) != 0) ||
127 (WIFSIGNALED($?) && WTERMSIG($?) != SIGTERM));
128 0 undef $pid;
129 0 $gone = 1;
130 }
131 }
132 0 };
133  
134 0 ▶ my $class = $ipv6 ? 'OSPF::LSDB::View6' : 'OSPF::LSDB::View';
Branch: 1 branch
BranchTrueFalseTotal
$ipv6 ? : 0 0 0
135 0 ▶ if ($legend) {
Branch: 1 branch
BranchTrueFalseTotal
if ($legend) 0 0 0
136 0 my $dot = $class->legend();
137 0 $gone = 0;
138 0 ▶ $pid = open2(undef, $fh, @cmd)
Branch: 1 branch
BranchTrueFalseTotal
$pid = open2(undef, $fh, @cmd) or die "Open pipe to '@{cmd}' failed: $!" 0 0 0
139 or die "Open pipe to '@cmd' failed: $!";
140 0 print $fh $dot, "\n";
141 0 ▶ close($fh)
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Close pipe to '@{cmd}' failed: $!" 0 0 0
142 or die "Close pipe to '@cmd' failed: $!";
143 0 pause();
144 0 exit 0;
145 }
146  
147 0 my($oldtime, $oldyaml);
148 0 ▶ until ($term) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($term) 0 0 0
149 0 my $time = time();
150 0 ▶ if ($oldtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oldtime) 0 0 0
151 0 my $sleeptime = $interval - ($time - $oldtime);
152 0 ▶ if ($sleeptime > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sleeptime > 0) 0 0 0
153 0 select(undef, undef, undef, $sleeptime);
154 }
155 }
156 0 $oldtime = time();
157  
158 0 ▶ my $ospfclass = $ipv6 ? 'OSPF::LSDB::ospf6d' : 'OSPF::LSDB::ospfd';
Branch: 1 branch
BranchTrueFalseTotal
$ipv6 ? : 0 0 0
159 0 my $ospf = $ospfclass->new(ssh => $ssh);
160 0 eval { $ospf->parse(); };
161 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
162 0 warn $@;
163 0 ▶ kill SIGTERM, $pid if $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pid 0 0 0
164 0 next;
165 }
166  
167 0 my $yamlospf = OSPF::LSDB::YAML->new($ospf);
168 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
169 0 die "Address family does not match -4 and -6 options.\n";
170 }
171 0 my $yaml = $yamlospf->Dump();
172 0 $yaml =~ s/^\s+(age|sequence): .*$//mg;
173 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
174 0 ▶ if ($dump) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dump) 0 0 0
175 0 print $yaml;
176 }
177 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
178 0 my %args = (
179 SUFFIX => ".yaml",
180 TEMPLATE => "ospfview-XXXXXXXXXX",
181 TMPDIR => 1,
182 UNLINK => 1
183 );
184 0 my $old = File::Temp->new(%args);
185 0 print $old $oldyaml;
186 0 my $new = File::Temp->new(%args);
187 0 print $new $yaml;
188 0 system('diff', '-up', $old->filename, $new->filename);
189 }
190 0 $oldyaml = $yaml;
191  
192 0 my $view = $class->new($ospf);
193 0 my $dot = $view->graph(%todo);
194 0 ▶ if ($todo{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($todo{"verbose"}) 0 0 0
195 0 my @errors = $view->get_errors;
196 0 ▶ print map { "$_\n" } @errors, "" if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 0 0
197 }
198  
199 0 my $chldsigset = POSIX::SigSet->new( &POSIX::SIGCHLD );
200 0 my $oldsigset = POSIX::SigSet->new();
201 0 ▶ sigprocmask(POSIX::SIG_BLOCK, $chldsigset, $oldsigset)
Branch: 1 branch
BranchTrueFalseTotal
sigprocmask(0, $chldsigset, $oldsigset) or die "Block sigprocmask failed: $!" 0 0 0
202 or die "Block sigprocmask failed: $!";
203 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
204 0 kill SIGTERM, $pid;
205 0 ▶ if (waitpid($pid, 0) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid($pid, 0) > 0) 0 0 0
206 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
207 ((WIFEXITED($?) && WEXITSTATUS($?) != 0) ||
208 (WIFSIGNALED($?) && WTERMSIG($?) != SIGTERM));
209 0 undef $pid;
210 0 $gone = 1;
211 }
212 }
213 0 ▶ sigprocmask(POSIX::SIG_SETMASK, $oldsigset, undef)
Branch: 1 branch
BranchTrueFalseTotal
sigprocmask(2, $oldsigset, undef) or die "Setmask sigprocmask failed: $!" 0 0 0
214 or die "Setmask sigprocmask failed: $!";
215  
216 0 $gone = 0;
217 0 ▶ $pid = open2(undef, $fh, @cmd)
Branch: 1 branch
BranchTrueFalseTotal
$pid = open2(undef, $fh, @cmd) or die "Open pipe to '@{cmd}' failed: $!" 0 0 0
218 or die "Open pipe to '@cmd' failed: $!";
219 0 print $fh $dot, "\n";
220 0 ▶ close($fh)
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Close pipe to '@{cmd}' failed: $!" 0 0 0
221 or die "Close pipe to '@cmd' failed: $!";
222 }
223 }
224  
225 main();
↑ Summary blib/lib/OSPF/LSDB/View6.pm »