TT/Input/Nagios.pm

filter: Statementstmt 94.0% 79 / 84 Branchbran 60.0% 12 / 20 Conditioncond 25.0% 5 / 20 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 74.3% 107 / 144 CC 25 SCAR 32.5
CC
25
Cov
77%
read
36.6
get_types
4.0
BEGIN
1.0
CRAP 25.8
« TT/Render.pm ↑ Summary TT/Log.pm »
1 package Monitoring::TT::Input::Nagios;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Monitoring::TT::Log qw/error warn info debug trace/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Monitoring::TT::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #####################################################################
10  
11 =head1 NAME
12  
13 Monitoring::TT::Input::Nagios - Input Nagios data
14  
15 =head1 DESCRIPTION
16  
17 Nagios Input for Hosts and Contacts
18  
19 =cut
20  
21 #####################################################################
22  
23 =head1 CONSTRUCTOR
24  
25 =head2 new
26  
27 new(%options)
28  
29 =cut
30  
31 sub new {
32 2 ▶ my($class, %options) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
33 my $self = {
34 'types' => [ 'hosts', 'contacts' ],
35 2 'montt' => $options{'montt'},
36 };
37 2 bless $self, $class;
38 2 return $self;
39 }
40  
41 #####################################################################
42  
43 =head1 METHODS
44  
45 =head2 get_types
46  
47 get_types($list_of_folders)
48  
49 return supported types for this input type
50  
51 =cut
52 sub get_types {
53 1 ▶ my($self, $folders) = @_;
Subroutine get_types: called CC=4 SCAR=13.9
Pod documented
54 1 my $types = [];
55 1 for my $dir (@{$folders}) {
56 1 for my $type (sort @{$self->{'types'}}) {
57 2 my $pattern = $dir.'/'.$type.'*.cfg';
58 2 trace('looking for nagios file: '.$pattern);
59 2 my @files = glob($pattern);
60 2 for my $f (@files) {
61 2 debug('found nagios file: '.$f);
62 2 push @{$types}, $type;
63 }
64 }
65 }
66 1 return Monitoring::TT::Utils::get_uniq_sorted($types);
67 }
68  
69 #####################################################################
70  
71 =head2 read
72  
73 read nagios file
74  
75 =cut
76 sub read {
77 2 ▶ my($self, $dir, $type) = @_;
Subroutine read: called CC=22 SCAR=36.0
Pod documented
78 2 my $data = [];
79 2 my $pattern = $dir.'/'.$type.'*.cfg';
80 2 my @files = glob($pattern);
81 2 my $current = { 'conf' => {}};
82 2 my $in_obj = 0;
83 2 for my $file (@files) {
84 3 info("reading $type from $file");
85  
86 3 my $output = "";
87 3 my $template = $self->{'montt'}->_process_template($self->{'montt'}->_read_replaced_template($file));
88 3 ▶ $self->{'montt'}->tt->process(\$template, {}, \$output) or $self->{'montt'}->_template_process_die($file, $data);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"montt"}->tt->process(\$template, {}, \$output) or $self->{"montt"}->_template_process_die($file, $data) 0 3 3
89  
90 3 for my $line (split(/\n/mx, $output)) {
91 40 ▶ next if substr($line, 0, 1) eq '#';
Branch: 1 branch
BranchTrueFalseTotal
if substr($line, 0, 1) eq "#" 0 40 40
92 40 ▶ next if $line =~ m/^\s*$/gmx;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/gmx 0 40 40
93 40 chomp($line);
94 40 ▶ if($line =~ m/^\s*define\s+(\w+)($|\s|{)/mx) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s*define\s+(\w+)($|\s|{)/mx) { } 5 35 40
elsif ($in_obj) { } 35 0 35
95 5 my $in_type = $1;
96 5 ▶ if($in_type.'s' ne $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_type . 's' ne $type) 0 5 5
97 0 warn("unexpected input type '".$in_type."' in ".$file.':'.$.);
98 0 next;
99 }
100 5 $in_obj = 1;
101 } elsif($in_obj) {
102 35 ▶ if($line =~ m/^\s*}/mx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*}/mx) { } 5 30 35
103 5 $in_obj = 0;
104  
105 # bring tags in shape
106 5 $current->{'tags'} = Monitoring::TT::Utils::parse_tags(delete $current->{'conf'}->{'_tags'});
107  
108 # bring groups in shape
109 5 $current->{'groups'} = Monitoring::TT::Utils::parse_groups(delete $current->{'conf'}->{'_groups'});
110  
111 # bring apps in shape
112 5 ▶ $current->{'apps'} = Monitoring::TT::Utils::parse_tags(delete $current->{'conf'}->{'_apps'}) if $type eq 'hosts';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hosts" 5 0 5
113  
114 # transfer type and some other attributes
115 5 $current->{'type'} = delete $current->{'conf'}->{'_type'};
116 5 ▶ $current->{'alias'} = $current->{'conf'}->{'alias'} || '';
Condition50%1/2
$current->{'conf'}{'alias'} || ''
l!l
05
MC/DC0%0/1
$current->{'conf'}{'alias'} || ''
$current->{'conf'}{'alias'}
Truth table50%1/2
A
A= $current->{'conf'}{'alias'}
Aresult
11
00
117  
118 5 ▶ if($type eq 'hosts') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "hosts") 5 0 5
119 5 ▶ $current->{'name'} = $current->{'conf'}->{'host_name'} || '';
Condition50%1/2
$current->{'conf'}{'host_name'} || ''
l!l
50
MC/DC0%0/1
$current->{'conf'}{'host_name'} || ''
$current->{'conf'}{'host_name'}
Truth table50%1/2
A
A= $current->{'conf'}{'host_name'}
Aresult
11
00
120 5 ▶ $current->{'address'} = $current->{'conf'}->{'address'} || '';
Condition50%1/2
$current->{'conf'}{'address'} || ''
l!l
50
MC/DC0%0/1
$current->{'conf'}{'address'} || ''
$current->{'conf'}{'address'}
Truth table50%1/2
A
A= $current->{'conf'}{'address'}
Aresult
11
00
121 5 ▶ $current->{'groups'} = $current->{'conf'}->{'host_groups'} || $current->{'conf'}->{'hostgroups'} || [];
Condition40%2/5
$current->{'conf'}{'host_groups'} || $current->{'conf'}{'hostgroups'} || []
l!l
05
$current->{"conf"}{"host_groups"} or $current->{"conf"}{"hostgroups"}
l!l&&r!l&&!r
005
MC/DC0%0/2
$current->{'conf'}{'host_groups'} || $current->{'conf'}{'hostgroups'} || []
$current->{"conf"}{"host_groups"} $current->{"conf"}{"hostgroups"}
Truth table33%1/3
A or B
A= $current->{"conf"}{"host_groups"}B= $current->{"conf"}{"hostgroups"}
ABresult
1X1
011
000
122 }
123 5 ▶ if($type eq 'contacts') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "contacts") 0 5 5
124 0 ▶ $current->{'name'} = $current->{'conf'}->{'contact_name'} || '';
Condition0%0/2
$current->{'conf'}{'contact_name'} || ''
l!l
00
MC/DC0%0/1
$current->{'conf'}{'contact_name'} || ''
$current->{'conf'}{'contact_name'}
Truth table0%0/2
A
A= $current->{'conf'}{'contact_name'}
Aresult
11
00
125 0 ▶ $current->{'email'} = $current->{'conf'}->{'email'} || '';
Condition0%0/2
$current->{'conf'}{'email'} || ''
l!l
00
MC/DC0%0/1
$current->{'conf'}{'email'} || ''
$current->{'conf'}{'email'}
Truth table0%0/2
A
A= $current->{'conf'}{'email'}
Aresult
11
00
126 0 ▶ $current->{'groups'} = $current->{'conf'}->{'contact_groups'} || $current->{'conf'}->{'contactgroups'} || [];
Condition0%0/5
$current->{'conf'}{'contact_groups'} || $current->{'conf'}{'contactgroups'} || []
l!l
00
$current->{"conf"}{"contact_groups"} or $current->{"conf"}{"contactgroups"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$current->{'conf'}{'contact_groups'} || $current->{'conf'}{'contactgroups'} || []
$current->{"conf"}{"contact_groups"} $current->{"conf"}{"contactgroups"}
Truth table0%0/3
A or B
A= $current->{"conf"}{"contact_groups"}B= $current->{"conf"}{"contactgroups"}
ABresult
1X1
011
000
127 }
128  
129 5 $current->{'file'} = delete $current->{'conf'}->{'_src'};
130 5 $current->{'file'} =~ s/:(\d+)$//gmx;
131 5 $current->{'line'} = $1;
132  
133 5 push @{$data}, $current;
134 5 $current = { 'conf' => {}};
135 } else {
136 30 $line =~ s/^\s*//gmx;
137 30 $line =~ s/\s*$//gmx;
138 30 my($key,$val) = split/\s+/mx, $line, 2;
139 30 $key = lc $key;
140 30 $current->{'conf'}->{$key} = $val;
141 }
142 }
143 }
144 3 debug("read ".(scalar @{$data})." $type from $file");
145 }
146 2 return $data;
147 }
148  
149 #####################################################################
150 # internal subs
151 #####################################################################
152  
153 #####################################################################
154  
155 =head1 AUTHOR
156  
157 Sven Nierlein, 2013, <sven.nierlein@consol.de>
158  
159 =cut
160  
161 2 1;
« TT/Render.pm ↑ Summary TT/Log.pm »