bin/storage-merge-dots

filter: Statementstmt 21.5% 49 / 227 Branchbran 3.8% 4 / 104 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 59.0% 13 / 22 Podpod n/a totaltotal 18.0% 66 / 366 CC 63 SCAR 51.6
CC
63
Cov
16%
handle_dotfile
462.0
collect_from_remote
183.8
match_links
110.0
CRAP 173.7
↑ Summary bin/storage2dot »
1 #!/usr/bin/env perl
2 #
3 # This file is part of StorageDisplay
4 #
5 # This software is copyright (c) 2014-2023 by Vincent Danjean.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10  
11 # PODNAME: storage-merge-dots
12 # ABSTRACT: merge dot files created by storage2dot, adding inter links if possible
13  
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 2 our $VERSION = '2.06'; # VERSION
19  
20  
21 2 ▶use StorageDisplay;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use StorageDisplay::Collect;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 0 ▶sub collect_from_remote {
Subroutine collect_from_remote: not called CC=16 SCAR=52.1
25 0 my $remote = shift;
26 0 my $content='';
27 2 ▶ use Net::OpenSSH;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶ use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶ END {
Subroutine END: called CC=1 SCAR=0.0
30 2 ReadMode('normal');
31 }
32 0 my $ssh = Net::OpenSSH->new($remote);
33 0 ▶ $ssh->error and
Branch: 1 branch
BranchTrueFalseTotal
$ssh->error and die q[Couldn't establish SSH connection: ] . $ssh->error 0 0 0
34 die "Couldn't establish SSH connection: ". $ssh->error;
35  
36 0 my ($in, $out, $pid) = $ssh->open2(
37 #'cat',
38 'perl', '--', '-',
39 );
40  
41 0 my $fdperlmod;
42 open($fdperlmod, '<', $INC{'StorageDisplay/Collect.pm'})
43 0 ▶ or die "Cannot open ".INC{'StorageDisplay/Collect.pm'}.": $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fdperlmod, "<", $INC{"StorageDisplay/Collect.pm"} or die 'Cannot open ' . do { "StorageDisplay/Collect.pm" }->INC . ": $!\n" 0 0 0
44 #use Sys::Syscall;
45 #Sys::Syscall::sendfile($in, $fdperlmod);
46 {
47 0 ▶ while(defined(my $line=<$fdperlmod>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fdperlmod)) 0 0 0
48 0 ▶ last if $line =~ m/^__END__\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^__END__\s*$/ 0 0 0
49 0 print $in $line;
50 }
51 0 close $fdperlmod;
52 }
53 #print $in "StorageDisplay::Collect::dump_collect;\n";
54 0 my @args = (@_, 'LocalBySSH');
55 0 my $cmd = "StorageDisplay::Collect::dump_collect('".join("','", @args)."');\n";
56 0 print STDERR 'Running through SSH: ',$cmd;
57 0 print $in $cmd;
58 0 print $in "__END__\n";
59 0 flush $in;
60  
61 2 ▶ use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶ use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
63 0 my $sel = IO::Select->new(\*STDIN, $out);
64 0 my $timeout = 1;
65 0 ReadMode('noecho');
66 0 my ($in_closed,$out_closed) = (0,0);
67 0 while(1) {
68 0 $!=0;
69 0 my @ready = $sel->can_read($timeout);
70 0 ▶ if ($!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 0 0 0
71 0 die "Error with select: $!\n";
72 }
73 0 ▶ if (scalar(@ready)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ready) { } 0 0 0
74 0 foreach my $fd (@ready) {
75 0 ▶ if ($fd == $out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fd == $out) { } 0 0 0
76 0 my $line=<$out>;
77 0 ▶ if (defined($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) { } 0 0 0
78 0 $content .= $line;
79 } else {
80 0 $sel->remove($out);
81 0 close $out;
82 0 $out_closed=1;
83 }
84 } else {
85 0 my $line=<STDIN>;
86 0 ▶ if (print $in $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (print $in $line) { } 0 0 0
87 0 flush $in;
88 } else {
89 0 $sel->remove(\*STDIN);
90 0 close $in;
91 0 $in_closed=1;
92 }
93 }
94 }
95 } else {
96 0 my $res = waitpid($pid, WNOHANG);
97 0 ▶ if ($res==-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res == -1) 0 0 0
98 0 die "Some error occurred ".($? >> 8).": $!\n";
99 }
100 0 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
101 0 ▶ if (!$in_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_closed) 0 0 0
102 0 $sel->remove(\*STDIN);
103 0 close $in;
104 }
105 0 ReadMode('normal');
106 0 last;
107 }
108 #print STDERR "timeout for $pid\n";
109 }
110 }
111 0 ▶ if (!$out_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out_closed) 0 0 0
112 0 ▶ while (defined(my $line=<$out>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $out)) 0 0 0
113 0 $content .= $out;
114 }
115 0 $sel->remove($out);
116 0 close $out;
117 }
118 0 return $content;
119 }
120  
121 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 2 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 2 $Data::Dumper::Sortkeys = 1;
125 2 $Data::Dumper::Purity = 1;
126  
127 2 my $help;
128 my $man;
129 2 my $verbose;
130 2 my $vmnames={};
131  
132 2 ▶GetOptions ("v|verbose+" => \$verbose, # flag
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("v|verbose+", \$verbose, "h|help", \$help, "man", \$man, "vm=s", $vmnames) or pod2usage(2) 2 0 2
133 "h|help" => \$help, # flag
134 "man" => \$man, # flag
135 "vm=s" => $vmnames,
136 ) or pod2usage(2);
137  
138 2 my $dotfiles;
139  
140 2 my $print_level=0;
141  
142 0 ▶sub print_info {
Subroutine print_info: not called CC=3 SCAR=24.8
143 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
144 0 foreach my $l (@_) {
145 0 print STDERR "I: ".(' 'x$print_level).$l."\n";
146 }
147 }
148 }
149  
150 0 ▶sub print_warn {
Subroutine print_warn: not called CC=2 SCAR=17.9
151 0 foreach my $l (@_) {
152 0 print STDERR "W: ".(' 'x$print_level).$l."\n";
153 }
154 }
155  
156 2 my $slinks={};
157 2 my $tlinks={};
158  
159 0 ▶sub add_link {
Subroutine add_link: not called CC=4 SCAR=30.0
160 0 my $self = shift;
161 0 my $name = shift;
162 0 my $link = shift;
163 0 ▶ if ($link !~ m,([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s*$,) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($link =~ /([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s*$/) 0 0 0
164  
165 0 return;
166 }
167 0 my ($vm, $size, $disk, $sl) = ($1, $2, $3, $4);
168 0 my $realdisk = 1;
169 0 ▶ if ($disk =~ m,^[(](.*)[)]$,) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disk =~ /^[(](.*)[)]$/) 0 0 0
170 0 $disk = $1;
171 0 $realdisk = 0;
172 }
173 0 ▶ if (exists($self->{$vm}->{$disk})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$vm}{$disk}) { } 0 0 0
174 0 print_warn "Ignoring duplicate $name link: $link";
175 } else {
176 0 $self->{$vm}->{$disk} = {
177 'link' => $sl,
178 'size' => $size,
179 'realdisk' => $realdisk,
180 'name' => $disk,
181 };
182 }
183 }
184  
185 0 ▶sub add_slink {
Subroutine add_slink: not called CC=1 SCAR=6.9
186 0 my $link = shift;
187 0 add_link($slinks, "source", $link);
188 }
189  
190 0 ▶sub add_tlink {
Subroutine add_tlink: not called CC=1 SCAR=6.9
191 0 my $link = shift;
192 0 add_link($tlinks, "target", $link);
193 }
194  
195 0 ▶sub check_disks {
Subroutine check_disks: not called CC=5 SCAR=34.0
196 0 my $hostdisk = shift;
197 0 my $vmdisk = shift;
198  
199 0 ▶ return 0 if not exists($vmdisk->{name});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $vmdisk->{"name"} 0 0 0
200 0 ▶ return 0 if $hostdisk->{matched};
Branch: 1 branch
BranchTrueFalseTotal
if $hostdisk->{"matched"} 0 0 0
201 0 ▶ return 0 if $vmdisk->{matched};
Branch: 1 branch
BranchTrueFalseTotal
if $vmdisk->{"matched"} 0 0 0
202  
203 0 ▶ if ($hostdisk->{size} != $vmdisk->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hostdisk->{"size"} != $vmdisk->{"size"}) 0 0 0
204 0 print_info "Rejecting $hostdisk->{name} to $vmdisk->{name}: different size";
205 0 return 0;
206 }
207 0 return 1;
208 }
209  
210 0 ▶sub match_links {
Subroutine match_links: not called CC=10 SCAR=47.0
211 0 my $host = shift;
212 0 my $vm = shift;
213  
214 0 my @matching;
215 0 foreach my $hostdname (sort keys %$host) {
216 0 my $hostdisk = $host->{$hostdname};
217 0 ▶ next if $hostdisk->{matched};
Branch: 1 branch
BranchTrueFalseTotal
if $hostdisk->{"matched"} 0 0 0
218 0 my $vmdname = $hostdname;
219 0 ▶ my $vmdisk = $vm->{$vmdname} // {};
Condition0%0/2
$vm->{$vmdname} // {}
l!l
00
MC/DC0%0/1
$vm->{$vmdname} // {}
$vm->{$vmdname}
Truth table0%0/2
A
A= $vm->{$vmdname}
Aresult
11
00
220 0 ▶ if ($hostdisk->{realdisk}
Branch: 1 branch
BranchTrueFalseTotal
if ($hostdisk->{"realdisk"} and check_disks($hostdisk, $vmdisk)) 0 0 0
Condition0%0/3
$hostdisk->{"realdisk"} and check_disks($hostdisk, $vmdisk)
!ll&&!rl&&r
000
MC/DC0%0/2
$hostdisk->{"realdisk"} and check_disks($hostdisk, $vmdisk)
$hostdisk->{"realdisk"} check_disks($hostdisk, $vmdisk)
Truth table0%0/3
A and B
A= $hostdisk->{"realdisk"}B= check_disks($hostdisk, $vmdisk)
ABresult
0X0
100
111
221 && check_disks($hostdisk, $vmdisk)) {
222 # fast path with real diskname (qemu agent was available)
223 0 push @matching, [$hostdisk, $vmdisk];
224 0 $hostdisk->{matched} = 1;
225 0 $vmdisk->{matched} = 1;
226 0 next;
227 }
228 # slow path: qemu-agent not available or device renamed after reboot
229 # trying to check with disksize
230 0 $vmdisk = undef;
231 0 foreach my $vmdisk_try (values %$vm) {
232 0 ▶ if (check_disks($hostdisk, $vmdisk_try)) {
Branch: 1 branch
BranchTrueFalseTotal
if (check_disks($hostdisk, $vmdisk_try)) 0 0 0
233 0 ▶ if (defined($vmdisk)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vmdisk) 0 0 0
234 0 print_warn "Multiple disks matching, ignoring it";
235 0 $vmdisk = undef;
236 0 last;
237 }
238 0 $vmdisk = $vmdisk_try;
239 }
240 }
241 0 ▶ if (defined($vmdisk)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vmdisk) 0 0 0
242 0 push @matching, [$hostdisk, $vmdisk];
243 0 $hostdisk->{matched} = 1;
244 0 $vmdisk->{matched} = 1;
245 }
246 }
247 0 return @matching;
248 }
249  
250 2 my $one_handled;
251 0 ▶sub handle_dotfile {
Subroutine handle_dotfile: not called CC=21 SCAR=61.4
252 0 my $filename = shift;
253  
254 0 my $state = 0;
255 0 ▶ open(my $h, '<', $filename) or die "Cannot read $filename\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $h, "<", $filename or die "Cannot read $filename\n" 0 0 0
256 0 ▶ my $print = $one_handled ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$one_handled ? : 0 0 0
257 0 my $header_size = 0;
258 0 my $footer_size = 0;
259
260 0 ▶ while(defined(my $line = <$h>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $h)) 0 0 0
261 0 chomp($line);
262 0 ▶ if ($state == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 0) 0 0 0
263 0 $state = 1;
264 }
265 0 ▶ if ($line =~ m,//\s*HEADER: MACHINE\s*$,) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ m[//\s*HEADER: MACHINE\s*$]) { } 0 0 0
elsif ($line =~ m[//\s*FOOTER: MACHINE\s*$]) { } 0 0 0
266 0 ▶ if ($state == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 1) { } 0 0 0
267 0 ▶ print $line, "\n" if not $one_handled;
Branch: 1 branch
BranchTrueFalseTotal
unless $one_handled 0 0 0
268 0 $header_size++;
269 } else {
270 0 print_warn "Ignoring header line after data: $line";
271 }
272 } elsif ($line =~ m,//\s*FOOTER: MACHINE\s*$,) {
273 0 ▶ if ($state < 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state < 2) { } 0 0 0
elsif ($state == 2) { } 0 0 0
274 0 print_warn "Ignoring footer line before data: $line";
275 } elsif ($state == 2) {
276 0 $state = 3;
277 }
278 0 ▶ if ($state == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 3) 0 0 0
279 0 ▶ if ($line !~ m,^\s*}\s*//\s*FOOTER: MACHINE\s*$,) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ m[^\s*}\s*//\s*FOOTER: MACHINE\s*$]) 0 0 0
280 0 print_warn "Strange footer line $line";
281 }
282 0 $footer_size++;
283 }
284 } else {
285 0 ▶ if ($state < 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state < 1) { } 0 0 0
elsif ($state == 1) { } 0 0 0
286 0 print_warn "Data before header";
287 } elsif ($state == 1) {
288 0 $state = 2;
289 0 $one_handled++;
290 }
291 0 ▶ if ($state == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state == 2) { } 0 0 0
elsif ($state == 3) { } 0 0 0
292 0 print $line, "\n";
293 0 ▶ if ($line =~ m,//\s*SOURCE\s+LINK\s*:\s*(.*)$,) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[//\s*SOURCE\s+LINK\s*:\s*(.*)$]) 0 0 0
294 0 my $link = $1;
295 0 add_slink($link);
296 }
297 0 ▶ if ($line =~ m,//\s*TARGET\s+LINK\s*:\s*(.*)$,) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[//\s*TARGET\s+LINK\s*:\s*(.*)$]) 0 0 0
298 0 my $link = $1;
299 0 add_tlink($link);
300 }
301 } elsif ($state == 3) {
302 0 print_warn "Ignoring data after footer line: $line";
303 }
304 }
305
306 }
307 0 ▶ if ($header_size != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_size != 2) 0 0 0
308 0 print_warn "Strange header with $header_size lines";
309 }
310 0 ▶ if ($footer_size != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($footer_size != 1) 0 0 0
311 0 print_warn "Strange footer with $header_size lines";
312 }
313 }
314  
315 2 ▶sub main() {
Subroutine main: called CC=9 SCAR=41.2
316 2 ▶ pod2usage(-exitval => 0, -verbose => 1) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 1 1 2
317 1 ▶ pod2usage(-exitval => 0, -verbose => 2) if $man;
Branch: 1 branch
BranchTrueFalseTotal
if $man 1 0 1
318  
319 0 foreach my $dotfilename (@ARGV) {
320 0 print_info "handling $dotfilename";
321 0 $print_level++;
322 0 handle_dotfile($dotfilename);
323 0 $print_level--;
324 }
325 #use Data::Dumper;
326 #print STDERR Dumper($slinks), "\n";
327 #print STDERR Dumper($tlinks), "\n";
328 0 print_info "handling interlinks";
329 0 $print_level++;
330 0 foreach my $vm (sort keys %{$slinks}) {
331 0 ▶ my $hostname = $vmnames->{$vm} // $vm;
Condition0%0/3
$vmnames->{$vm} // $vm
l!l&&r!l&&!r
000
MC/DC0%0/2
$vmnames->{$vm} // $vm
$vmnames->{$vm} $vm
Truth table0%0/3
A // B
A= $vmnames->{$vm}B= $vm
ABresult
1X1
011
000
332 0 print_info "Looking for $vm ($hostname) VM";
333 0 ▶ if (exists($tlinks->{$hostname})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tlinks->{$hostname}) 0 0 0
334 0 $print_level++;
335 0 print_info "Found target";
336 0 my @matches = match_links($slinks->{$vm}, $tlinks->{$hostname});
337 0 foreach (@matches) {
338 0 my ($hd, $vmd) = @{$_};
339 0 print_info "Linking $hd->{name}\@$vm to $vmd->{name}\@$hostname";
340 0 print $hd->{link}, " -> ", $vmd->{link}, "\n";
341 }
342 0 $print_level--;
343 }
344 }
345 0 $print_level--;
346  
347 0 ▶ if ($one_handled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($one_handled) 0 0 0
348 0 print "} // FOOTER: MACHINE\n";
349 }
350 }
351  
352 2 main
353  
354 __END__
↑ Summary bin/storage2dot »