blib/lib/Ym/ymgen.pl

filter: Statementstmt 14.7% 10 / 68 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 50.0% 3 / 6 Podpod 0.0% 0 / 3 totaltotal 10.4% 13 / 124 CC 27 SCAR 58.7
CC
27
Cov
9%
GenerateCfg
506.0
GenDefs
30.0
ProcessNode
6.0
CRAP 353.0
« blib/lib/Ym/ymcommon.pl ↑ Summary blib/lib/Ym/ymstat.pl »
1 #!/usr/bin/perl -w
2  
3 package Ym;
4  
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Ym;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶sub GenDefs {
Subroutine GenDefs: not called CC=5 SCAR=34.0
Pod undocumented
11 0 my ($def_type, $leaf) = @_;
12 0 my %basket;
13 0 my $i = 0; # Count if there were true definitions not only references.
14 0 my $definition = "define $def_type {\n";
15  
16 0 ▶ while (my ($k, $v) = each %$leaf) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$leaf) 0 0 0
17 0 ▶ if (ref($leaf->{$k})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $leaf->{$k}) 0 0 0
18 0 $basket{$k} = $leaf->{$k};
19 0 next;
20 }
21 0 $definition .= "\t$k\t$v\n";
22 0 ++$i;
23 }
24 0 $definition .= "}\n\n";
25  
26 0 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) 0 0 0
27 0 $definition = "";
28 }
29 0 foreach my $el (keys %basket) {
30 0 $definition .= ProcessNode($basket{$el}, $el);
31 }
32  
33 0 return $definition;
34 }
35  
36 0 ▶sub ProcessNode {
Subroutine ProcessNode: not called CC=2 SCAR=17.9
Pod undocumented
37 0 my ($node, $nodename) = @_;
38 0 my $definition = '';
39  
40 0 foreach my $obj (sort keys %{$node}) {
41 0 my $block = GenDefs($Ym::BRANCHES{$nodename}[0], $node->{$obj});
42 0 $definition .= "$block";
43 }
44 0 return $definition;
45 }
46  
47 0 ▶sub GenerateCfg {
Subroutine GenerateCfg: not called CC=22 SCAR=62.3
Pod undocumented
48 0 my ($tree, $dst_dir, $opts) = @_;
49 0 my $pid = $$;
50  
51 0 ▶ die("GenerateCfg: at least 2 parameters expected")
Branch: 1 branch
BranchTrueFalseTotal
unless $tree and $dst_dir 0 0 0
Condition0%0/3
$tree and $dst_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$tree and $dst_dir
$tree $dst_dir
Truth table0%0/3
A and B
A= $treeB= $dst_dir
ABresult
0X0
100
111
52 unless $tree && $dst_dir;
53  
54 0 ▶ $opts = {} unless $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless $opts 0 0 0
55  
56 0 my $verbose = 0;
57  
58 0 ▶ unless (defined($tree->{'config'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tree->{"config"}) 0 0 0
59 0 die "nagios.cfg definitions are missing in \$hash{config}\n";
60 }
61 0 $tree->{'config'}->{'cfg_file'} = undef;
62  
63 0 foreach my $m (values %Ym::BRANCHES) {
64 0 ▶ next unless defined($m->[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m->[1] 0 0 0
65 0 push @{$tree->{'config'}{'cfg_file'}}, $m->[1];
66 }
67  
68 # Generate nagios.cfg
69 0 my $nagios_main = $Ym::NAGIOS_CFG_NAME;
70 0 ▶ open(NAGIOS_MAIN, ">$dst_dir/$nagios_main.$pid")
Branch: 1 branch
BranchTrueFalseTotal
open NAGIOS_MAIN, ">$dst_dir/$nagios_main.$pid" or die "Can't open $dst_dir/$nagios_main.$pid : $!\n" 0 0 0
71 or die "Can't open $dst_dir/$nagios_main.$pid : $!\n";
72  
73 0 foreach my $def (sort keys %{$tree->{'config'}}) {
74 0 ▶ if ("$def" eq "cfg_file") {
Branch: 1 branch
BranchTrueFalseTotal
if ("$def" eq 'cfg_file') { } 0 0 0
75 0 foreach my $c (@{$tree->{'config'}->{'cfg_file'}}) {
76 printf NAGIOS_MAIN "cfg_file=%s\n",
77 0 ▶ ($opts->{'target_base'} ? $opts->{'target_base'} : $dst_dir) . "/$c";
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'target_base'} ? : 0 0 0
78 }
79 }
80 else {
81 0 printf NAGIOS_MAIN "%s=%s\n", $def, $tree->{'config'}{$def};
82 }
83 }
84 0 close(NAGIOS_MAIN);
85  
86 # Generate object config files
87 0 foreach my $def (keys %Ym::BRANCHES) {
88  
89 next unless (defined($Ym::BRANCHES{$def}[0])
90 0 ▶ && defined($Ym::BRANCHES{$def}[1]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Ym::BRANCHES{$def}[0] and defined $Ym::BRANCHES{$def}[1] 0 0 0
Condition0%0/3
defined $Ym::BRANCHES{$def}[0] and defined $Ym::BRANCHES{$def}[1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Ym::BRANCHES{$def}[0] and defined $Ym::BRANCHES{$def}[1]
defined $Ym::BRANCHES{$def}[0] defined $Ym::BRANCHES{$def}[1]
Truth table0%0/3
A and B
A= defined $Ym::BRANCHES{$def}[0]B= defined $Ym::BRANCHES{$def}[1]
ABresult
0X0
100
111
91  
92 0 my $cfg_out = "$dst_dir/$Ym::BRANCHES{$def}[1]";
93 0 ▶ open(OUT, ">$cfg_out.$pid") or die "Can't open $cfg_out.$pid : $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">$cfg_out.$pid" or die "Can't open $cfg_out.$pid : $!\n" 0 0 0
94  
95 0 my $block = ProcessNode($tree->{$def}, $def);
96 0 print OUT "$block";
97 0 ▶ ($verbose) and print "$block\n";
Branch: 1 branch
BranchTrueFalseTotal
$verbose and print "$block\n" 0 0 0
98  
99 0 close(OUT);
100 }
101  
102 # Rename tmp files to regular. Save modification times.
103  
104 0 ▶ rename("$dst_dir/$nagios_main.$pid", "$dst_dir/$nagios_main")
Branch: 1 branch
BranchTrueFalseTotal
rename "$dst_dir/$nagios_main.$pid", "$dst_dir/$nagios_main" or die "Can not rename cfg file : $!\n" 0 0 0
105 or die "Can not rename cfg file : $!\n";
106  
107 0 foreach my $def (keys %Ym::BRANCHES) {
108 next unless (defined($Ym::BRANCHES{$def}[0])
109 0 ▶ && defined($Ym::BRANCHES{$def}[1]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Ym::BRANCHES{$def}[0] and defined $Ym::BRANCHES{$def}[1] 0 0 0
Condition0%0/3
defined $Ym::BRANCHES{$def}[0] and defined $Ym::BRANCHES{$def}[1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Ym::BRANCHES{$def}[0] and defined $Ym::BRANCHES{$def}[1]
defined $Ym::BRANCHES{$def}[0] defined $Ym::BRANCHES{$def}[1]
Truth table0%0/3
A and B
A= defined $Ym::BRANCHES{$def}[0]B= defined $Ym::BRANCHES{$def}[1]
ABresult
0X0
100
111
110  
111 0 my $cfg_out = "$dst_dir/$Ym::BRANCHES{$def}[1]";
112  
113 0 ▶ rename("$cfg_out.$pid", $cfg_out)
Branch: 1 branch
BranchTrueFalseTotal
rename "$cfg_out.$pid", $cfg_out or die "Can not rename cfg file : $!\n" 0 0 0
114 or die "Can not rename cfg file : $!\n";
115 }
116 }
117  
118 1 1;
« blib/lib/Ym/ymcommon.pl ↑ Summary blib/lib/Ym/ymstat.pl »