line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl -w |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Ym; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
6
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
31
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
5
|
use Ym; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1046
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub GenDefs { |
11
|
0
|
|
|
0
|
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) { |
17
|
0
|
0
|
|
|
|
|
if (ref($leaf->{$k})) { |
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
|
0
|
|
|
|
|
if ($i == 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
|
|
|
|
|
|
|
sub ProcessNode { |
37
|
0
|
|
|
0
|
0
|
|
my ($node, $nodename) = @_; |
38
|
0
|
|
|
|
|
|
my $definition = ''; |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
foreach my $obj (sort keys %{$node}) { |
|
0
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
my $block = GenDefs($Ym::BRANCHES{$nodename}[0], $node->{$obj}); |
42
|
0
|
|
|
|
|
|
$definition .= "$block"; |
43
|
|
|
|
|
|
|
} |
44
|
0
|
|
|
|
|
|
return $definition; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub GenerateCfg { |
48
|
0
|
|
|
0
|
0
|
|
my ($tree, $dst_dir, $opts) = @_; |
49
|
0
|
|
|
|
|
|
my $pid = $$; |
50
|
|
|
|
|
|
|
|
51
|
0
|
0
|
0
|
|
|
|
die("GenerateCfg: at least 2 parameters expected") |
52
|
|
|
|
|
|
|
unless $tree && $dst_dir; |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
|
|
|
|
$opts = {} unless $opts; |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
my $verbose = 0; |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
unless (defined($tree->{'config'})) { |
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
|
0
|
|
|
|
|
next unless defined($m->[1]); |
65
|
0
|
|
|
|
|
|
push @{$tree->{'config'}{'cfg_file'}}, $m->[1]; |
|
0
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Generate nagios.cfg |
69
|
0
|
|
|
|
|
|
my $nagios_main = $Ym::NAGIOS_CFG_NAME; |
70
|
0
|
0
|
|
|
|
|
open(NAGIOS_MAIN, ">$dst_dir/$nagios_main.$pid") |
71
|
|
|
|
|
|
|
or die "Can't open $dst_dir/$nagios_main.$pid : $!\n"; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
foreach my $def (sort keys %{$tree->{'config'}}) { |
|
0
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
|
if ("$def" eq "cfg_file") { |
75
|
0
|
|
|
|
|
|
foreach my $c (@{$tree->{'config'}->{'cfg_file'}}) { |
|
0
|
|
|
|
|
|
|
76
|
0
|
0
|
|
|
|
|
printf NAGIOS_MAIN "cfg_file=%s\n", |
77
|
|
|
|
|
|
|
($opts->{'target_base'} ? $opts->{'target_base'} : $dst_dir) . "/$c"; |
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
|
0
|
0
|
0
|
|
|
|
next unless (defined($Ym::BRANCHES{$def}[0]) |
90
|
|
|
|
|
|
|
&& defined($Ym::BRANCHES{$def}[1])); |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
my $cfg_out = "$dst_dir/$Ym::BRANCHES{$def}[1]"; |
93
|
0
|
0
|
|
|
|
|
open(OUT, ">$cfg_out.$pid") or die "Can't open $cfg_out.$pid : $!\n"; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
my $block = ProcessNode($tree->{$def}, $def); |
96
|
0
|
|
|
|
|
|
print OUT "$block"; |
97
|
0
|
0
|
|
|
|
|
($verbose) and print "$block\n"; |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
close(OUT); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Rename tmp files to regular. Save modification times. |
103
|
|
|
|
|
|
|
|
104
|
0
|
0
|
|
|
|
|
rename("$dst_dir/$nagios_main.$pid", "$dst_dir/$nagios_main") |
105
|
|
|
|
|
|
|
or die "Can not rename cfg file : $!\n"; |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
foreach my $def (keys %Ym::BRANCHES) { |
108
|
0
|
0
|
0
|
|
|
|
next unless (defined($Ym::BRANCHES{$def}[0]) |
109
|
|
|
|
|
|
|
&& defined($Ym::BRANCHES{$def}[1])); |
110
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
my $cfg_out = "$dst_dir/$Ym::BRANCHES{$def}[1]"; |
112
|
|
|
|
|
|
|
|
113
|
0
|
0
|
|
|
|
|
rename("$cfg_out.$pid", $cfg_out) |
114
|
|
|
|
|
|
|
or die "Can not rename cfg file : $!\n"; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |