TT.pm

filter: Statementstmt 20.9% 96 / 458 Branchbran 11.0% 23 / 208 Conditioncond 6.0% 2 / 33 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 45.7% 16 / 35 Podpod 100% 3 / 3 totaltotal 18.4% 140 / 758 CC 153 SCAR 53.6
CC
153
Cov
17%
_build_template
812.0
_build_dynamic_object_config
420.0
run
156.0
CRAP 213.6
↑ Summary TT/Object.pm »
1 package Monitoring::TT;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Monitoring::TT::Identifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Monitoring::TT::Log qw/error warn info debug trace log/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Monitoring::TT::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Monitoring::TT::Render;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Monitoring::TT::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 our $VERSION = '1.0.1';
16  
17 #####################################################################
18  
19 =head1 NAME
20  
21 Monitoring::TT - Generic Monitoring Config based on Template Toolkit Templates
22  
23 =head1 DESCRIPTION
24  
25 Generic Monitoring Config based on Template Toolkit Templates
26  
27 =cut
28  
29 #####################################################################
30  
31 =head1 CONSTRUCTOR
32  
33 =head2 new
34  
35 new(%options)
36  
37 =cut
38  
39 sub new {
40 3 ▶ my($class, %options) = @_;
Subroutine new: called CC=4 SCAR=14.0
Pod documented
41 3 my $self = {
42 tt_opts => {
43 TRIM => 1,
44 RELATIVE => 1,
45 STAT_TTL => 60,
46 STRICT => 1,
47 }
48 };
49 3 bless $self, $class;
50  
51 3 ▶ $self->{'tt_opts'}->{'STRICT'} = 1 if $ENV{'TEST_AUTHOR'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TEST_AUTHOR"} 0 3 3
52 3 ▶ $self->{'tt_opts'}->{'STRICT'} = 1 if -f '.author';
Branch: 1 branch
BranchTrueFalseTotal
if -f ".author" 0 3 3
53 3 for my $s (@{Monitoring::TT::Identifier::functions('Monitoring::TT::Render')}) {
54 63 $self->{'tt_opts'}->{'PRE_DEFINE'}->{$s} = \&{'Monitoring::TT::Render::'.$s};
55 }
56  
57 3 return $self;
58 }
59  
60 #####################################################################
61  
62 =head1 METHODS
63  
64 =head2 run
65  
66 run config generator and write it to the output folder
67  
68 =cut
69  
70 sub run {
71 0 ▶ my( $self ) = @_;
Subroutine run: not called CC=12 SCAR=50.5
Pod documented
72 0 ▶ return unless $self->_get_options();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_get_options 0 0 0
73 0 info('generating config from '.join(', ', @{$self->{'in'}}));
74 0 info('into '.$self->{'out'});
75 0 for my $in (@{$self->{'in'}}) {
76 0 ▶ if(! -d $in.'/.') {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $in . '/.') 0 0 0
77 0 error($in.': '.$!);
78 0 exit 1;
79 }
80 }
81 0 $self->_run_hook('pre', join(',', @{$self->{'in'}}));
82  
83 # die if output directory already exists
84 0 ▶ if(-e $self->{'out'} and !$self->{'opt'}->{'force'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->{"out"} and not $self->{"opt"}{"force"}) 0 0 0
Condition0%0/3
-e $self->{"out"} and not $self->{"opt"}{"force"}
!ll&&!rl&&r
000
MC/DC0%0/2
-e $self->{"out"} and not $self->{"opt"}{"force"}
-e $self->{"out"} not $self->{"opt"}{"force"}
Truth table0%0/3
A and not(B)
A= -e $self->{"out"}B= not $self->{"opt"}{"force"}
ABresult
0X0
100
111
85 0 my @files = glob($self->{'out'}.'/*');
86 0 ▶ if(scalar @files > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @files > 0) 0 0 0
87 0 error($self->{'out'}.' does already exist and is not empty. (use --force to overwrite contents)');
88 0 exit 1;
89 }
90 }
91 0 $self->_mkdir_r($self->{'out'});
92  
93 0 ▶ info('using template filter: '.$self->{'opt'}->{'templatefilter'}) if $self->{'opt'}->{'templatefilter'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt"}{"templatefilter"} 0 0 0
94 0 ▶ info('using contact filter: '.$self->{'opt'}->{'contactfilter'}) if $self->{'opt'}->{'contactfilter'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt"}{"contactfilter"} 0 0 0
95 0 ▶ info('using host filter: '.$self->{'opt'}->{'hostfilter'}) if $self->{'opt'}->{'hostfilter'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt"}{"hostfilter"} 0 0 0
96  
97 # reset counter
98 0 $self->{'possible_types'} = {};
99 0 $self->{'possible_tags'} = {};
100 0 $self->{'possible_apps'} = {};
101  
102 0 $self->_copy_static_files();
103 0 $self->_build_dynamic_config();
104 0 ▶ $self->_check_typos() unless $self->{'opt'}->{'templatefilter'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opt"}{"templatefilter"} 0 0 0
105 0 ▶ $self->_print_stats() if $Monitoring::TT::Log::Verbose >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $Monitoring::TT::Log::Verbose >= 2 0 0 0
106 0 $self->_run_hook('post', join(',', @{$self->{'in'}}));
107 0 info('done');
108 0 return 0;
109 }
110  
111 #####################################################################
112  
113 =head2 tt
114  
115 return template toolkit object
116  
117 =cut
118  
119 sub tt {
120 6 ▶ my($self) = @_;
Subroutine tt: called CC=2 SCAR=6.9
Pod documented
121  
122 6 ▶ return $self->{'_tt'} if $self->{'_tt'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_tt"} 4 2 6
123  
124 # make some globals available in TT stash
125 2 $self->{'tt_opts'}->{'PRE_DEFINE'}->{'src'} = $self->{'in'};
126  
127 2 $self->{'_tt'} = Template->new($self->{'tt_opts'});
128 2 $Template::Stash::PRIVATE = undef;
129  
130 2 return $self->{'_tt'};
131 }
132  
133 #####################################################################
134 # INTERNAL SUBS
135 #####################################################################
136 sub _get_options {
137 0 ▶ my($self) = @_;
Subroutine _get_options: not called CC=11 SCAR=48.8
138 0 Getopt::Long::Configure('no_ignore_case');
139 0 Getopt::Long::Configure('bundling');
140 0 $self->{'opt'} = {
141 files => [],
142 verbose => 1,
143 force => 0,
144 dryrun => 0,
145 };
146 GetOptions (
147 'h|help' => \$self->{'opt'}->{'help'},
148 0 ▶ 'v|verbose' => sub { $self->{'opt'}->{'verbose'}++ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
149 'q|quiet' => \$self->{'opt'}->{'quiet'},
150 'V|version' => \$self->{'opt'}->{'version'},
151 'f|force' => \$self->{'opt'}->{'force'},
152 'cf|contactfilter=s' => \$self->{'opt'}->{'contactfilter'},
153 'hf|hostfilter=s' => \$self->{'opt'}->{'hostfilter'},
154 'tf|templatefilter=s' => \$self->{'opt'}->{'templatefilter'},
155 'n|dry-run' => \$self->{'opt'}->{'dryrun'},
156 0 ▶ '<>' => sub { push @{$self->{'opt'}->{'files'}}, $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
157 0 ▶ ) or $self->_usage();
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("h|help", \$self->{"opt"}{"help"}, "v|verbose", sub { $self->{"opt"}{"verbose"}++; } , "q|quiet", \$self->{"opt"}{"quiet"}, "V|version", \$self->{"opt"}{"version"}, "f|force", \$self->{"opt"}{"force"}, "cf|contactfilter=s", \$self->{"opt"}{"contactfilter"}, "hf|hostfilter=s", \$self->{"opt"}{"hostfilter"}, "tf|templatefilter=s", \$self->{"opt"}{"templatefilter"}, "n|dry-run", \$self->{"opt"}{"dryrun"}, "<>", sub { push @{$$self{"opt"}{"files"};}, $_[0]; } ) or $self->_usage 0 0 0
158 0 ▶ if($self->{'opt'}->{'version'}) { print 'Version ', $VERSION,"\n"; exit 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt"}{"version"}) 0 0 0
159 0 ▶ pod2usage({ -verbose => 2, -exit => 3 } ) if $self->{'opt'}->{'help'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt"}{"help"} 0 0 0
160 0 ▶ $self->_usage('please specify at least one input and output folder!') if scalar @{$self->{'opt'}->{'files'}} <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"opt"}{"files"};} <= 1 0 0 0
161 0 for my $f (@{$self->{'opt'}->{'files'}}) { $f =~ s/\/*$//gmx; }
162 0 $self->{'out'} = pop @{$self->{'opt'}->{'files'}};
163 0 $self->{'in'} = $self->{'opt'}->{'files'};
164 0 ▶ $self->{'opt'}->{'verbose'} = 0 if $self->{'opt'}->{'quiet'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt"}{"quiet"} 0 0 0
165 0 ▶ $self->{'opt'}->{'dryrun'} = 1 if $self->{'opt'}->{'contactfilter'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt"}{"contactfilter"} 0 0 0
166 0 ▶ $self->{'opt'}->{'dryrun'} = 1 if $self->{'opt'}->{'hostfilter'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt"}{"hostfilter"} 0 0 0
167 0 ▶ $self->{'opt'}->{'dryrun'} = 1 if $self->{'opt'}->{'templatefilter'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt"}{"templatefilter"} 0 0 0
168 0 $Monitoring::TT::Log::Verbose = $self->{'opt'}->{'verbose'};
169 0 ▶ info('Dry Run, Hooks won\'t be executed') if $self->{'opt'}->{'dryrun'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt"}{"dryrun"} 0 0 0
170 0 return 1;
171 }
172  
173 #####################################################################
174 sub _usage {
175 0 ▶ my($self, $msg) = @_;
Subroutine _usage: not called CC=2 SCAR=17.9
176 0 ▶ print $msg, "\n\n" if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 0 0 0
177 0 print "usage: $0 [options] <input> [<input>...] <output>\ndetailed help available with --help\n";
178 0 exit 3;
179 }
180  
181 #####################################################################
182 sub _copy_static_files {
183 0 ▶ my($self) = @_;
Subroutine _copy_static_files: not called CC=3 SCAR=24.8
184 0 for my $in (@{$self->{'in'}}) {
185 0 ▶ if(-d $in.'/static/.') {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $in . '/static/.') 0 0 0
186 0 my $cmd = 'cp -LR '.$in.'/static/* '.$self->{'out'}.'/';
187 0 debug($cmd);
188 0 `$cmd`;
189 }
190 }
191 0 return;
192 }
193  
194 #####################################################################
195 sub _build_dynamic_config {
196 0 ▶ my($self) = @_;
Subroutine _build_dynamic_config: not called CC=8 SCAR=42.8
197 # main work block, dynamic object configuration
198 0 $self->_build_dynamic_object_config();
199  
200 # other files
201 0 for my $in (@{$self->{'in'}}) {
202 0 for my $file (sort glob($in.'/*.cfg')) {
203 0 ▶ next if defined $self->{'opt'}->{'templatefilter'} and $file !~ m/$self->{'opt'}->{'templatefilter'}/mx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"opt"}{"templatefilter"} and not $file =~ /$self->{"opt"}{"templatefilter"}/mx 0 0 0
Condition0%0/3
defined $self->{"opt"}{"templatefilter"} and not $file =~ /$self->{"opt"}{"templatefilter"}/mx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"opt"}{"templatefilter"} and not $file =~ /$self->{"opt"}{"templatefilter"}/mx
defined $self->{"opt"}{"templatefilter"} not $file =~ /$self->{"opt"}{"templatefilter"}/mx
Truth table0%0/3
A and not(B)
A= defined $self->{"opt"}{"templatefilter"}B= not $file =~ /$self->{"opt"}{"templatefilter"}/mx
ABresult
0X0
100
111
204 0 info('processing non object: '.$file);
205 0 my $outfile = $file;
206 0 $outfile =~ s/.*\///mx;
207 0 ▶ next if $outfile =~ m/^hosts.*\.cfg/gmx;
Branch: 1 branch
BranchTrueFalseTotal
if $outfile =~ /^hosts.*\.cfg/gmx 0 0 0
208 0 ▶ next if $outfile =~ m/^contacts.*\.cfg/gmx;
Branch: 1 branch
BranchTrueFalseTotal
if $outfile =~ /^contacts.*\.cfg/gmx 0 0 0
209 0 $outfile = $self->{'out'}.'/'.$outfile;
210 0 debug('writing: '.$outfile);
211 0 ▶ open(my $fh, '>', $outfile) or die('cannot write '.$outfile.': '.$!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $outfile or die 'cannot write ' . $outfile . ': ' . $! 0 0 0
212 0 print $fh $self->_process_template($self->_read_replaced_template($file), {});
213 0 print $fh "\n";
214 0 close($fh);
215 }
216 }
217  
218 0 return;
219 }
220  
221 #####################################################################
222 # do the main work, this block is essential for maximum performance
223 sub _build_dynamic_object_config {
224 0 ▶ my($self) = @_;
Subroutine _build_dynamic_object_config: not called CC=20 SCAR=60.4
225  
226 # detect input type
227 0 my $input_types = $self->_get_input_types($self->{'in'});
228  
229 # no dynamic config at all?
230 0 ▶ return unless scalar keys %{$input_types} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$input_types;} > 0 0 0 0
231  
232 # build templates
233 0 my $templates = {
234 contacts => $self->_build_template('conf.d', 'contacts'),
235 hosts => $self->_build_template('conf.d', 'hosts', [ 'conf.d/apps', 'conf.d/apps.cfg' ]),
236 };
237  
238 0 mkdir($self->{'out'}.'/conf.d');
239  
240 0 my $data = { hosts => [], contacts => []};
241 0 for my $type (keys %{$input_types}) {
242 0 my $typefilter = $self->{'opt'}->{substr($type,0,-1).'filter'};
243 0 my $obj_list = [];
244 0 ▶ trace('fetching data for '.$type) if $Monitoring::TT::Log::Verbose >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $Monitoring::TT::Log::Verbose >= 4 0 0 0
245 0 for my $cls (@{$input_types->{$type}}) {
246 0 for my $in (@{$self->{'in'}}) {
247 0 my $data = $cls->read($in, $type);
248 0 for my $d (@{$data}) {
249 0 $d->{'montt'} = $self;
250 0 my $o = Monitoring::TT::Object->new($type, $d);
251 0 ▶ die('got no object') unless defined $o;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o 0 0 0
252 0 ▶ next if defined $typefilter and join(',', values %{$o}) !~ m/$typefilter/mx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $typefilter and not join(",", values %{$o;}) =~ /$typefilter/mx 0 0 0
Condition0%0/3
defined $typefilter and not join(",", values %{$o;}) =~ /$typefilter/mx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $typefilter and not join(",", values %{$o;}) =~ /$typefilter/mx
defined $typefilter not join(",", values %{$o;}) =~ /$typefilter/mx
Truth table0%0/3
A and not(B)
A= defined $typefilterB= not join(",", values %{$o;}) =~ /$typefilter/mx
ABresult
0X0
100
111
253 0 ▶ trace($o) if $Monitoring::TT::Log::Verbose >= 5;
Branch: 1 branch
BranchTrueFalseTotal
if $Monitoring::TT::Log::Verbose >= 5 0 0 0
254 0 push @{$obj_list}, $o;
255 }
256 }
257 }
258 # sort objects by name
259 0 @{$obj_list} = sort {$a->{'name'} cmp $b->{'name'}} @{$obj_list};
260 0 $data->{$type} = $obj_list;
261  
262 0 my $outfile = $self->{'out'}.'/conf.d/'.$type.'.cfg';
263 0 info('writing: '.$outfile);
264 0 ▶ open(my $fh, '>', $outfile) or die('cannot write '.$outfile.': '.$!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $outfile or die 'cannot write ' . $outfile . ': ' . $! 0 0 0
265 0 print $fh $self->_process_template($templates->{$type}, { type => $type, data => $obj_list });
266 0 print $fh "\n";
267 0 close($fh);
268 }
269  
270 0 for my $in (@{$self->{'in'}}) {
271 0 for my $file (reverse sort @{$self->_get_files($in.'/conf.d', '\.cfg')}) {
272 0 ▶ next if defined $self->{'opt'}->{'templatefilter'} and $file !~ m/$self->{'opt'}->{'templatefilter'}/mx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"opt"}{"templatefilter"} and not $file =~ /$self->{"opt"}{"templatefilter"}/mx 0 0 0
Condition0%0/3
defined $self->{"opt"}{"templatefilter"} and not $file =~ /$self->{"opt"}{"templatefilter"}/mx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"opt"}{"templatefilter"} and not $file =~ /$self->{"opt"}{"templatefilter"}/mx
defined $self->{"opt"}{"templatefilter"} not $file =~ /$self->{"opt"}{"templatefilter"}/mx
Truth table0%0/3
A and not(B)
A= defined $self->{"opt"}{"templatefilter"}B= not $file =~ /$self->{"opt"}{"templatefilter"}/mx
ABresult
0X0
100
111
273 0 ▶ next if $file =~ m/^$in\/conf\.d\/apps/mx;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ m[^$in/conf\.d/apps]mx 0 0 0
274 0 ▶ next if $file =~ m/^$in\/conf\.d\/contacts/mx;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ m[^$in/conf\.d/contacts]mx 0 0 0
275 0 ▶ next if $file =~ m/^$in\/conf\.d\/hosts/mx;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ m[^$in/conf\.d/hosts]mx 0 0 0
276 0 info('processing object file: '.$file);
277 0 my $outfile = $file;
278 0 $outfile =~ s/.*\///mx;
279 0 $outfile = $self->{'out'}.'/conf.d/'.$outfile;
280 0 debug('writing: '.$outfile);
281 0 ▶ open(my $fh, '>', $outfile) or die('cannot write '.$outfile.': '.$!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $outfile or die 'cannot write ' . $outfile . ': ' . $! 0 0 0
282 0 print $fh $self->_process_template($self->_read_replaced_template($file), $data);
283 0 print $fh "\n";
284 0 close($fh);
285 }
286 }
287  
288 0 $self->{'data'} = $data;
289  
290 0 return;
291 }
292  
293 #####################################################################
294 sub _print_stats {
295 0 ▶ my($self) = @_;
Subroutine _print_stats: not called CC=3 SCAR=24.8
296 0 my $out = $self->{'out'};
297 0 info('written:');
298 0 for my $type (qw/host hostgroup hostdependency hostextinfo hostescalation
299 service servicegroup servicedependency serviceextinfo serviceescalation
300 contact contactgroup command timeperiod
301 /) {
302 0 my $num = $self->_grep_count($out, '^\s*define\s*'.$type.'\( \|{\)');
303 0 ▶ next if $num == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num == 0 0 0 0
304 0 info(sprintf('# %-15s %6s', $type, $num));
305 }
306 0 return;
307 }
308  
309 #####################################################################
310 sub _grep_count {
311 0 ▶ my($self, $dir, $pattern) = @_;
Subroutine _grep_count: not called CC=3 SCAR=24.8
312 0 my $txt = `grep -r -c '$pattern' $dir 2>&1`;
313 0 my $total = 0;
314 0 for my $line (split/\n/mx, $txt) {
315 0 ▶ if($line =~ m/:(\d+)$/mx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /:(\d+)$/mx) 0 0 0
316 0 $total += $1;
317 }
318 }
319 0 return $total;
320 }
321  
322 #####################################################################
323 sub _build_template {
324 0 ▶ my($self, $dir, $type, $appdirs) = @_;
Subroutine _build_template: not called CC=28 SCAR=67.0
325 0 my $shorttype = substr($type, 0, -1);
326 0 my $template = "[% FOREACH d = data %][% ".$shorttype." = d %]\n";
327 0 my $found = 0;
328 0 for my $in (@{$self->{'in'}}) {
329 0 for my $path (glob($in.'/'.$dir.'/'.$type.'/ '.
330 $in.'/'.$dir.'/'.$type.'*.cfg')
331 ) {
332 0 ▶ trace('looking for '.$type.' templates in '.$path) if $Monitoring::TT::Log::Verbose >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $Monitoring::TT::Log::Verbose >= 4 0 0 0
333 0 ▶ if(-e $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $path) 0 0 0
334 0 my $templates = $self->_get_files($path, '\.cfg');
335 0 for my $t (reverse sort @{$templates}) {
336 0 ▶ next if defined $self->{'opt'}->{'templatefilter'} and $t !~ m|$self->{'opt'}->{'templatefilter'}|mx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"opt"}{"templatefilter"} and not $t =~ /$self->{"opt"}{"templatefilter"}/mx 0 0 0
Condition0%0/3
defined $self->{"opt"}{"templatefilter"} and not $t =~ /$self->{"opt"}{"templatefilter"}/mx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"opt"}{"templatefilter"} and not $t =~ /$self->{"opt"}{"templatefilter"}/mx
defined $self->{"opt"}{"templatefilter"} not $t =~ /$self->{"opt"}{"templatefilter"}/mx
Truth table0%0/3
A and not(B)
A= defined $self->{"opt"}{"templatefilter"}B= not $t =~ /$self->{"opt"}{"templatefilter"}/mx
ABresult
0X0
100
111
337 0 my $tags = $self->_get_tags_for_path($t, $path);
338 0 my $required_type = shift @{$tags};
339 0 ▶ info('adding '.$type.' template: '.$t.($required_type ? ' for type '.$required_type : '').(scalar @{$tags} > 0 ? ' with tags: '.join(' & ', @{$tags}) : ''));
Branch: 2 branches
BranchTrueFalseTotal
$required_type ? : 0 0 0
scalar @{$tags;} > 0 ? : 0 0 0
340 0 ▶ if($required_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required_type) 0 0 0
341 0 $self->{$type.'possible_types'}->{$required_type} = 1;
342 0 $template .= "[% IF d.type == '$required_type' %]";
343 }
344 0 for my $tag (@{$tags}) {
345 0 $self->{$type.'possible_tags'}->{$tag} = 1;
346 0 $template .= "[% IF d.has_tag('$tag') %]";
347 }
348 0 $template .= $self->_read_replaced_template($t);
349 0 for my $tag (@{$tags}) {
350 0 $template .= "[% END %]";
351 }
352 0 ▶ $template .= "[% END %]" if $required_type;
Branch: 1 branch
BranchTrueFalseTotal
if $required_type 0 0 0
353 0 $found++;
354 0 $template .= "\n";
355 }
356 }
357 }
358 }
359  
360 # add apps for hosts
361 0 ▶ if(defined $appdirs and scalar @{$appdirs} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $appdirs and scalar @{$appdirs;} > 0) 0 0 0
Condition0%0/3
defined $appdirs and scalar @{$appdirs;} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $appdirs and scalar @{$appdirs;} > 0
defined $appdirs scalar @{$appdirs;} > 0
Truth table0%0/3
A and B
A= defined $appdirsB= scalar @{$appdirs;} > 0
ABresult
0X0
100
111
362 0 for my $in (@{$self->{'in'}}) {
363 0 for my $path (@{$appdirs}) {
364 0 $path = $in.'/'.$path;
365 0 ▶ trace('looking for '.$type.' apps in '.$path) if $Monitoring::TT::Log::Verbose >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $Monitoring::TT::Log::Verbose >= 4 0 0 0
366 0 ▶ if(-e $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $path) 0 0 0
367 0 my $templates = $self->_get_files($path, '\.cfg');
368 0 for my $t (reverse sort @{$templates}) {
369 0 ▶ next if defined $self->{'opt'}->{'templatefilter'} and $t !~ m|$self->{'opt'}->{'templatefilter'}|mx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"opt"}{"templatefilter"} and not $t =~ /$self->{"opt"}{"templatefilter"}/mx 0 0 0
Condition0%0/3
defined $self->{"opt"}{"templatefilter"} and not $t =~ /$self->{"opt"}{"templatefilter"}/mx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"opt"}{"templatefilter"} and not $t =~ /$self->{"opt"}{"templatefilter"}/mx
defined $self->{"opt"}{"templatefilter"} not $t =~ /$self->{"opt"}{"templatefilter"}/mx
Truth table0%0/3
A and not(B)
A= defined $self->{"opt"}{"templatefilter"}B= not $t =~ /$self->{"opt"}{"templatefilter"}/mx
ABresult
0X0
100
111
370 0 my $apps = $self->_get_tags_for_path($t, $path);
371 0 ▶ info('adding apps template: '.$t.(scalar @{$apps} > 0 ? ' for apps: '.join(' & ', @{$apps}) : ''));
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$apps;} > 0 ? : 0 0 0
372 0 for my $app (@{$apps}) {
373 0 $self->{'possible_apps'}->{$app} = 1;
374 0 $template .= "[% IF d.has_app('$app') %]";
375 }
376 0 $template .= $self->_read_replaced_template($t);
377 0 for my $app (@{$apps}) {
378 0 $template .= "[% END %]";
379 }
380 0 $found++;
381 0 $template .= "\n";
382 }
383 }
384 }
385 }
386 }
387  
388 0 ▶ if($found == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found == 0) 0 0 0
389 0 debug('no templates for type '.$type.' found');
390 0 return '';
391 }
392 0 $template .= "[% END %]\n";
393 0 ▶ if($Monitoring::TT::Log::Verbose >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Monitoring::TT::Log::Verbose >= 4) 0 0 0
394 0 trace('created template:');
395 0 trace($template);
396 }
397 0 return $template;
398 }
399  
400 #####################################################################
401 sub _get_files {
402 0 ▶ my($self, $dir, $pattern) = @_;
Subroutine _get_files: not called CC=9 SCAR=45.0
403 0 ▶ if(!-d $dir and $dir =~ m/$pattern/mx) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -d $dir and $dir =~ /$pattern/mx) 0 0 0
Condition0%0/3
not -d $dir and $dir =~ /$pattern/mx
!ll&&!rl&&r
000
MC/DC0%0/2
not -d $dir and $dir =~ /$pattern/mx
not -d $dir $dir =~ /$pattern/mx
Truth table0%0/3
not(A) and B
A= not -d $dirB= $dir =~ /$pattern/mx
ABresult
0X0
100
111
404 0 return([$dir]);
405 }
406 0 my @files;
407 0 ▶ return \@files unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
408 0 ▶ opendir(my $d, $dir) or die("cannot read directory $dir: $!");
Branch: 1 branch
BranchTrueFalseTotal
opendir my $d, $dir or die "cannot read directory ${dir}: $!" 0 0 0
409 0 ▶ while(my $file = readdir($d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readdir $d)) 0 0 0
410 0 ▶ next if substr($file,0,1) eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if substr($file, 0, 1) eq "." 0 0 0
411 0 ▶ if(-d $dir.'/'.$file.'/.') {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir . '/' . $file . '/.') { } 0 0 0
412 0 push @files, @{$self->_get_files($dir.'/'.$file, $pattern)};
413 } else {
414 0 ▶ next if $file !~ m/$pattern/mx;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /$pattern/mx 0 0 0
415 0 push @files, $dir."/".$file;
416 }
417 }
418 0 return \@files;
419 }
420  
421 #####################################################################
422 sub _process_template {
423 3 ▶ my($self, $template, $data) = @_;
Subroutine _process_template: called CC=6 SCAR=21.8
424  
425 3 ▶ if(!defined $self->{'_config_template'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_config_template"}) 2 1 3
426 2 $self->{'_config_template'} = "";
427 2 for my $in (@{$self->{'in'}}) {
428 0 debug('looking for a '.$in.'/config.cfg');
429 0 ▶ if(-e $in.'/config.cfg') {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $in . '/config.cfg') 0 0 0
430 0 debug('added config template '.$in.'/config.cfg');
431 0 $self->{'_config_template'} .= $self->_read_replaced_template($in.'/config.cfg')."\n";
432 }
433 }
434 }
435 3 $template = $self->{'_config_template'}.$template;
436  
437 3 ▶ if($Monitoring::TT::Log::Verbose >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Monitoring::TT::Log::Verbose >= 4) 0 3 3
438 0 trace('template:');
439 0 trace('==========================');
440 0 trace($template);
441 0 trace('==========================');
442 }
443  
444 3 my $output;
445 3 ▶ $self->tt->process(\$template, $data, \$output) or $self->_template_process_die($template, $data);
Branch: 1 branch
BranchTrueFalseTotal
$self->tt->process(\$template, $data, \$output) or $self->_template_process_die($template, $data) 0 3 3
446  
447 # clean up result
448 3 $output =~ s/^\s*$//sgmxo;
449 3 $output =~ s/^\n//gmxo;
450  
451 3 return $output;
452 }
453  
454 #####################################################################
455 sub _get_input_classes {
456 1 ▶ my($self, $folders) = @_;
Subroutine _get_input_classes: called CC=10 SCAR=25.5
457 1 my $types = [];
458  
459 1 for my $dir (@{$folders}) {
460 0 ▶ next unless -d $dir.'/lib/.';
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir . '/lib/.' 0 0 0
461 0 unshift @INC, "$dir/lib";
462 0 ▶ trace('added '.$dir.'/lib to @INC') if $Monitoring::TT::Log::Verbose >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $Monitoring::TT::Log::Verbose >= 4 0 0 0
463 }
464  
465 1 ▶ if($Monitoring::TT::Log::Verbose >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Monitoring::TT::Log::Verbose >= 4) 0 1 1
466 0 trace('@INC:');
467 0 trace(\@INC);
468 }
469  
470 1 my $uniq_types = {};
471 1 my $uniq_libs = {};
472 1 for my $inc (@INC) {
473 9 ▶ next if defined $uniq_libs->{$inc};
Branch: 1 branch
BranchTrueFalseTotal
if defined $uniq_libs->{$inc} 1 8 9
474 8 $uniq_libs->{$inc} = 1;
475 8 my @files = glob($inc.'/Monitoring/TT/Input/*.pm');
476 8 for my $file (glob($inc.'/Monitoring/TT/Input/*.pm')) {
477 4 ▶ trace('found input class: '.$file) if $Monitoring::TT::Log::Verbose >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $Monitoring::TT::Log::Verbose >= 4 0 4 4
478 4 $file =~ s|^$inc/Monitoring/TT/Input/||mx;
479 4 $file =~ s|\.pm$||mx;
480 4 ▶ push @{$types}, $file unless defined $uniq_types->{$types}->{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uniq_types->{$types}{$file} 2 2 4
481 4 $uniq_types->{$types}->{$file} = 1;
482 }
483 }
484 1 return $types;
485 }
486  
487 #####################################################################
488 sub _get_input_types {
489 0 ▶ my($self, $folders) = @_;
Subroutine _get_input_types: not called CC=6 SCAR=37.4
490 0 my $input_types = {};
491 0 my $input_classes = $self->_get_input_classes($folders);
492 0 for my $t (@{$input_classes}) {
493 0 debug('requesting input files from: '.$t);
494 0 my $objclass = 'Monitoring::TT::Input::'.$t;
495 ## no critic
496 0 eval "require $objclass;";
497 ## use critic
498 0 ▶ error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
499 0 my $obj = \&{$objclass."::new"};
500 0 my $it = &$obj($objclass, montt => $self);
501 0 my $types = $it->get_types($folders);
502 0 ▶ trace('input \''.$t.'\' supports: '.join(', ', @{$types})) if $Monitoring::TT::Log::Verbose >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $Monitoring::TT::Log::Verbose >= 4 0 0 0
503 0 for my $type (@{$types}) {
504 0 ▶ $input_types->{$type} = [] unless defined $input_types->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input_types->{$type} 0 0 0
505 0 push @{$input_types->{$type}}, $it;
506 }
507 }
508 0 return $input_types;
509 }
510  
511 #####################################################################
512 sub _run_hook {
513 0 ▶ my($self, $name, $args) = @_;
Subroutine _run_hook: not called CC=8 SCAR=42.8
514 0 ▶ return if $self->{'opt'}->{'dryrun'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt"}{"dryrun"} 0 0 0
515 0 for my $in (@{$self->{'in'}}) {
516 0 my $hook = $in.'/hooks/'.$name;
517 0 ▶ trace("hook: looking for ".$hook) if $Monitoring::TT::Log::Verbose >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $Monitoring::TT::Log::Verbose >= 4 0 0 0
518 0 ▶ if(-x $hook) {
Branch: 1 branch
BranchTrueFalseTotal
if (-x $hook) 0 0 0
519 0 my $cmd = $hook;
520 0 ▶ $cmd = $cmd." ".$args if defined $args;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args 0 0 0
521 0 debug($cmd);
522 0 open(my $ph, '-|', $cmd);
523 0 ▶ while(my $line = <$ph>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $ph)) 0 0 0
524 0 log($line);
525 }
526 0 close($ph);
527 0 my $rc = $?>>8;
528 0 debug('hook returned: '.$rc);
529 0 ▶ if($rc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 0 0 0
530 0 debug(' -> exiting');
531 0 exit $rc;
532 }
533 }
534 }
535 0 return;
536 }
537  
538 #####################################################################
539 sub _read_replaced_template {
540 3 ▶ my($self, $template) = @_;
Subroutine _read_replaced_template: called CC=9 SCAR=23.5
541 3 $template =~ s|//|/|gmxo;
542 3 my $text = '[%# SRC '.$template.':1 #%]';
543 3 ▶ open(my $fh, '<', $template) or die("cannot read: ".$template.': '.$!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $template or die 'cannot read: ' . $template . ': ' . $! 0 3 3
544 3 ▶ while(my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 25 3 28
545 # remove utf8 file bom
546 25 ▶ if($. == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($. == 1) 3 22 25
547 3 my $bom = pack("CCC", 0xef, 0xbb, 0xbf);
548 3 ▶ if(substr($line,0,3) eq $bom) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($line, 0, 3) eq $bom) 0 3 3
549 0 $line = substr($line, 3);
550 }
551 }
552 25 $text .= $line;
553 25 ▶ if($line =~ m/^define\s+(\w+)/mxo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^define\s+(\w+)/mox) 3 22 25
554 3 ▶ if($1 eq 'service' or $1 eq 'host' or $1 eq 'contact') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'service' or $1 eq 'host' or $1 eq 'contact') { } 3 0 3
Condition33%2/6
$1 eq "service" or $1 eq "host" or $1 eq "contact"
l!l&&r!l&&!r
300
$1 eq "service" or $1 eq "host"
l!l&&r!l&&!r
030
MC/DC0%0/3
$1 eq "service" or $1 eq "host" or $1 eq "contact"
$1 eq "service" $1 eq "host" $1 eq "contact"
Truth table25%1/4
A or B or C
A= $1 eq "service"B= $1 eq "host"C= $1 eq "contact"
ABCresult
1XX1
01X1
0011
0000
555 3 $text .= ' _SRC '.$template.':'.$.."\n";
556 } else {
557 0 $text .= '# SRC '.$template.':'.$.."\n";
558 }
559 }
560 }
561 3 close($fh);
562 3 return $text;
563 }
564  
565 #####################################################################
566 sub _get_tags_for_path {
567 0 ▶ my($self, $path, $basepath) = @_;
Subroutine _get_tags_for_path: not called CC=1 SCAR=6.9
568 0 my $tmppath = lc $path;
569 0 $tmppath =~ s|^$basepath||mx;
570 0 $tmppath =~ s|\.cfg$||mx;
571 0 $tmppath =~ s|^/||mx;
572 0 my @tags = split(/\//mx, $tmppath);
573 0 return \@tags;
574 }
575  
576 #####################################################################
577 sub _template_process_die {
578 0 ▶ my($self, $template, $data) = @_;
Subroutine _template_process_die: not called CC=9 SCAR=45.0
579 0 my $tterror = "".$self->tt->error();
580 0 my $already_printed = 0;
581  
582 # try to find file / line
583 0 ▶ if($tterror =~ m/input\s+text\s+line\s+(\d+)/mx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tterror =~ /input\s+text\s+line\s+(\d+)/mx) 0 0 0
584 0 my $linenr = $1;
585 0 my @lines = split/\n/mx, $template;
586 0 my($realfile, $realline) = $self->_get_file_and_line_for_error(\@lines, $linenr);
587 0 ▶ if($realfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($realfile) 0 0 0
588 0 my $newloc = $realfile.' line '.$realline;
589 0 $tterror =~ s|input\s+text\s+line\s+\d+|$newloc|gmx;
590 }
591 }
592  
593 # var.undef error - undefined variable: host.tag('contact_groups')
594 0 ▶ if($tterror =~ m/var\.undef\ error\ -\ undefined\ variable:\s+(.*)$/mx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tterror =~ /var\.undef\ error\ -\ undefined\ variable:\s+(.*)$/mx) 0 0 0
595 0 my $err = $1;
596 0 my $linenr = 0;
597 0 error($tterror);
598 0 $already_printed = 1;
599 0 my @lines = split/\n/mx, $template;
600 0 for my $line (@lines) {
601 0 $linenr++;
602 0 ▶ if($line =~ m/\Q$err\E/mx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\Q$err\E/mx) 0 0 0
603 0 my($realfile, $realline) = $self->_get_file_and_line_for_error(\@lines, $linenr);
604 0 ▶ if($realfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($realfile) 0 0 0
605 0 error('occurs in: '.$realfile.':'.$realline);
606 }
607 }
608 }
609 }
610  
611 0 ▶ error($tterror) unless $already_printed;
Branch: 1 branch
BranchTrueFalseTotal
unless $already_printed 0 0 0
612 0 debug('in template:');
613 0 debug($template);
614 0 ▶ trace($data) if $Monitoring::TT::Log::Verbose >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $Monitoring::TT::Log::Verbose >= 4 0 0 0
615 0 exit 1;
616 }
617  
618 #####################################################################
619 sub _get_file_and_line_for_error {
620 0 ▶ my($self, $lines, $linenr) = @_;
Subroutine _get_file_and_line_for_error: not called CC=4 SCAR=30.0
621 0 ▶ for(my $x = $linenr; $x >= 0; $x--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= 0) 0 0 0
622 0 ▶ if(defined $lines->[$x] and $lines->[$x] =~ m/SRC\s+(.*):(\d+)/mx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lines->[$x] and $lines->[$x] =~ /SRC\s+(.*):(\d+)/mx) 0 0 0
Condition0%0/3
defined $lines->[$x] and $lines->[$x] =~ /SRC\s+(.*):(\d+)/mx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $lines->[$x] and $lines->[$x] =~ /SRC\s+(.*):(\d+)/mx
defined $lines->[$x] $lines->[$x] =~ /SRC\s+(.*):(\d+)/mx
Truth table0%0/3
A and B
A= defined $lines->[$x]B= $lines->[$x] =~ /SRC\s+(.*):(\d+)/mx
ABresult
0X0
100
111
623 0 my $diff = $x - $2 + 1;
624 0 return($1, ($linenr - $diff))
625 }
626 }
627 0 return(undef, undef);
628 }
629  
630 #####################################################################
631 sub _mkdir_r {
632 0 ▶ my($self, $dir) = @_;
Subroutine _mkdir_r: not called CC=4 SCAR=30.0
633 0 my $path = '';
634 0 for my $part (split/(\/)/mx, $dir) {
635 0 $path .= $part;
636 0 ▶ next if $path eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" 0 0 0
637 0 ▶ mkdir($path) unless -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 0 0 0
638 }
639 0 return;
640 }
641  
642 #####################################################################
643 sub _check_typos {
644 0 ▶ my($self) = @_;
Subroutine _check_typos: not called CC=12 SCAR=50.5
645 0 for my $type (qw/hosts contacts/) {
646 0 for my $o (@{$self->{'data'}->{$type}}) {
647 0 ▶ if($o->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"type"}) 0 0 0
648 0 ▶ warn('unused type \''.$o->{'type'}.'\' defined in '.$o->{'file'}.':'.$o->{'line'}) unless defined $self->{$type.'possible_types'}->{$o->{'type'}};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self{$type . 'possible_types'}{$o->{"type"}} 0 0 0
649 }
650 0 ▶ if($o->{'tags'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"tags"}) 0 0 0
651 0 for my $t (keys %{$o->{'tags'}}) {
652 0 ▶ next if substr($t,0,1) eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if substr($t, 0, 1) eq "_" 0 0 0
653 0 ▶ warn('unused tag \''.$t.'\' defined in '.$o->{'file'}.':'.$o->{'line'}) unless defined $self->{$type.'possible_tags'}->{$t};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self{$type . 'possible_tags'}{$t} 0 0 0
654 }
655 }
656 0 ▶ if($o->{'apps'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"apps"}) 0 0 0
657 0 for my $a (keys %{$o->{'apps'}}) {
658 0 ▶ warn('unused app \''.$a.'\' defined in '.$o->{'file'}.':'.$o->{'line'}) unless defined $self->{$type.'possible_apps'}->{$a};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self{$type . 'possible_apps'}{$a} 0 0 0
659 }
660 }
661 }
662 }
663 0 return;
664 }
665  
666 =head1 AUTHOR
667  
668 Sven Nierlein, 2013, <sven.nierlein@consol.de>
669  
670 =cut
671  
672 4 1;
↑ Summary TT/Object.pm »