TT/Input/Nagios.pm

filter: Statementstmt 89.3% 84 / 94 Branchbran 50.0% 17 / 34 Conditioncond 21.4% 6 / 28 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 65.5% 118 / 180 CC 35 SCAR 43.2
CC
35
Cov
69%
read
98.3
get_types
4.0
BEGIN
1.0
CRAP 75.5
« TT.pm ↑ Summary TT/Object.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 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
32 2 my($class, %options) = @_;
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 1 ▶sub get_types {
Subroutine get_types: called CC=4 SCAR=13.9
Pod documented
53 1 my($self, $folders) = @_;
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 2 ▶sub read {
Subroutine read: called CC=32 SCAR=45.9
Pod documented
77 2 my($self, $dir, $type) = @_;
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 ▶ if($self->{'montt'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'montt'}) { } 3 0 3
88 3 my $template = $self->{'montt'}->_process_template($self->{'montt'}->_read_replaced_template($file));
89 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) 3 0 3
90 } else {
91 0 ▶ open(my $fh, '<', $file) or die("cannot read: ".$file.': '.$!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die 'cannot read: ' . $file . ': ' . $! 0 0 0
92 0 ▶ while(my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
93 0 $output .= $line;
94 }
95 0 close($fh);
96 }
97  
98 3 my $in_type;
99 3 for my $line (split(/\n/mx, $output)) {
100 40 ▶ next if substr($line, 0, 1) eq '#';
Branch: 1 branch
BranchTrueFalseTotal
if substr($line, 0, 1) eq "#" 0 40 40
101 40 ▶ next if $line =~ m/^\s*$/gmx;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/gmx 0 40 40
102 40 chomp($line);
103 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
104 5 $in_type = $1;
105 5 ▶ if($type && $in_type.'s' ne $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and $in_type . 's' ne $type) 0 5 5
Condition33%1/3
$type and $in_type . 's' ne $type
!ll&&!rl&&r
050
MC/DC0%0/2
$type and $in_type . 's' ne $type
$type $in_type . 's' ne $type
Truth table33%1/3
A and B
A= $typeB= $in_type . 's' ne $type
ABresult
0X0
100
111
106 0 warn("unexpected input type '".$in_type."' in ".$file.':'.$.);
107 0 next;
108 }
109 5 $in_obj = 1;
110 } elsif($in_obj) {
111 35 ▶ if($line =~ m/^\s*}/mx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*}/mx) { } 5 30 35
112 5 $in_obj = 0;
113  
114 # bring tags in shape
115 5 $current->{'tags'} = Monitoring::TT::Utils::parse_tags(delete $current->{'conf'}->{'_tags'});
116  
117 # bring groups in shape
118 5 $current->{'groups'} = Monitoring::TT::Utils::parse_groups(delete $current->{'conf'}->{'_groups'});
119  
120 # bring apps in shape
121 5 ▶ $current->{'apps'} = Monitoring::TT::Utils::parse_tags(delete $current->{'conf'}->{'_apps'}) if $in_type eq 'host';
Branch: 1 branch
BranchTrueFalseTotal
if $in_type eq "host" 5 0 5
122  
123 # transfer type and some other attributes
124 5 $current->{'type'} = delete $current->{'conf'}->{'_type'};
125 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
126  
127 5 ▶ if($in_type eq 'host') {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_type eq "host") 5 0 5
128 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
129 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
130 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
131 }
132 5 ▶ if($in_type eq 'contact') {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_type eq "contact") 0 5 5
133 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
134 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
135 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
136 }
137 5 ▶ if($in_type eq 'service') {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_type eq "service") 0 5 5
138 0 ▶ $current->{'groups'} = $current->{'conf'}->{'service_groups'} || $current->{'conf'}->{'servicegroups'} || [];
Condition0%0/5
$current->{'conf'}{'service_groups'} || $current->{'conf'}{'servicegroups'} || []
l!l
00
$current->{"conf"}{"service_groups"} or $current->{"conf"}{"servicegroups"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$current->{'conf'}{'service_groups'} || $current->{'conf'}{'servicegroups'} || []
$current->{"conf"}{"service_groups"} $current->{"conf"}{"servicegroups"}
Truth table0%0/3
A or B
A= $current->{"conf"}{"service_groups"}B= $current->{"conf"}{"servicegroups"}
ABresult
1X1
011
000
139 }
140  
141 5 ▶ $current->{'type'} = $in_type unless $current->{'type'};
Branch: 1 branch
BranchTrueFalseTotal
unless $current->{"type"} 0 5 5
142  
143 5 $current->{'file'} = delete $current->{'conf'}->{'_src'};
144 5 ▶ $current->{'file'} = $file unless $current->{'file'};
Branch: 1 branch
BranchTrueFalseTotal
unless $current->{"file"} 0 5 5
145 5 $current->{'file'} =~ s/:(\d+)$//gmx;
146 5 ▶ $current->{'line'} = defined $1 ? $1 : $.;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 5 0 5
147  
148 5 push @{$data}, $current;
149 5 $current = { 'conf' => {}};
150 } else {
151 30 $line =~ s/^\s*//gmx;
152 30 $line =~ s/\s*$//gmx;
153 30 my($key,$val) = split/\s+/mx, $line, 2;
154 30 $key = lc $key;
155 30 $current->{'conf'}->{$key} = $val;
156 }
157 }
158 }
159 3 debug("read ".(scalar @{$data})." $type from $file");
160 }
161 2 return $data;
162 }
163  
164 #####################################################################
165 # internal subs
166 #####################################################################
167  
168 #####################################################################
169  
170 =head1 AUTHOR
171  
172 Sven Nierlein, 2013, <sven.nierlein@consol.de>
173  
174 =cut
175  
176 2 1;
« TT.pm ↑ Summary TT/Object.pm »