| 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
| |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | if (ref($leaf->{$k})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$tree and $dst_dir
MC/DC0%0/2
$tree and $dst_dir
$tree
$dst_dir
Truth table0%0/3
A and B
A= $treeB= $dst_dir
| |||||||||||||||||||||||||||||
| 52 | unless $tree && $dst_dir; | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $opts = {} unless $opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | my $verbose = 0; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | unless (defined($tree->{'config'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $Ym::BRANCHES{$def}[0] and defined $Ym::BRANCHES{$def}[1]
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]
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my $block = ProcessNode($tree->{$def}, $def); | |||||||||||||||||||||||||||
| 96 | 0 | print OUT "$block"; | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | ($verbose) and print "$block\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $Ym::BRANCHES{$def}[0] and defined $Ym::BRANCHES{$def}[1]
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]
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | my $cfg_out = "$dst_dir/$Ym::BRANCHES{$def}[1]"; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | rename("$cfg_out.$pid", $cfg_out) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | or die "Can not rename cfg file : $!\n"; | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | 1; | |||||||||||||||||||||||||||