line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Monitoring::TT::Input::Nagios; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
2869
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
54
|
|
4
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
56
|
|
5
|
2
|
|
|
2
|
|
9
|
use utf8; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
10
|
|
6
|
2
|
|
|
2
|
|
63
|
use Monitoring::TT::Log qw/error warn info debug trace/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
126
|
|
7
|
2
|
|
|
2
|
|
10
|
use Monitoring::TT::Utils; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1850
|
|
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
|
|
|
2
|
1
|
13
|
my($class, %options) = @_; |
33
|
|
|
|
|
|
|
my $self = { |
34
|
|
|
|
|
|
|
'types' => [ 'hosts', 'contacts' ], |
35
|
2
|
|
|
|
|
6
|
'montt' => $options{'montt'}, |
36
|
|
|
|
|
|
|
}; |
37
|
2
|
|
|
|
|
5
|
bless $self, $class; |
38
|
2
|
|
|
|
|
4
|
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
|
|
|
1
|
1
|
17
|
my($self, $folders) = @_; |
54
|
1
|
|
|
|
|
2
|
my $types = []; |
55
|
1
|
|
|
|
|
1
|
for my $dir (@{$folders}) { |
|
1
|
|
|
|
|
2
|
|
56
|
1
|
|
|
|
|
2
|
for my $type (sort @{$self->{'types'}}) { |
|
1
|
|
|
|
|
4
|
|
57
|
2
|
|
|
|
|
4
|
my $pattern = $dir.'/'.$type.'*.cfg'; |
58
|
2
|
|
|
|
|
9
|
trace('looking for nagios file: '.$pattern); |
59
|
2
|
|
|
|
|
132
|
my @files = glob($pattern); |
60
|
2
|
|
|
|
|
6
|
for my $f (@files) { |
61
|
2
|
|
|
|
|
8
|
debug('found nagios file: '.$f); |
62
|
2
|
|
|
|
|
3
|
push @{$types}, $type; |
|
2
|
|
|
|
|
4
|
|
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
1
|
|
|
|
|
4
|
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
|
|
|
2
|
1
|
582
|
my($self, $dir, $type) = @_; |
78
|
2
|
|
|
|
|
4
|
my $data = []; |
79
|
2
|
|
|
|
|
5
|
my $pattern = $dir.'/'.$type.'*.cfg'; |
80
|
2
|
|
|
|
|
158
|
my @files = glob($pattern); |
81
|
2
|
|
|
|
|
8
|
my $current = { 'conf' => {}}; |
82
|
2
|
|
|
|
|
4
|
my $in_obj = 0; |
83
|
2
|
|
|
|
|
3
|
for my $file (@files) { |
84
|
3
|
|
|
|
|
18
|
info("reading $type from $file"); |
85
|
|
|
|
|
|
|
|
86
|
3
|
|
|
|
|
4
|
my $output = ""; |
87
|
3
|
50
|
|
|
|
14
|
if($self->{'montt'}) { |
88
|
3
|
|
|
|
|
14
|
my $template = $self->{'montt'}->_process_template($self->{'montt'}->_read_replaced_template($file)); |
89
|
3
|
50
|
|
|
|
16
|
$self->{'montt'}->tt->process(\$template, {}, \$output) or $self->{'montt'}->_template_process_die($file, $data); |
90
|
|
|
|
|
|
|
} else { |
91
|
0
|
0
|
|
|
|
0
|
open(my $fh, '<', $file) or die("cannot read: ".$file.': '.$!); |
92
|
0
|
|
|
|
|
0
|
while(my $line = <$fh>) { |
93
|
0
|
|
|
|
|
0
|
$output .= $line; |
94
|
|
|
|
|
|
|
} |
95
|
0
|
|
|
|
|
0
|
close($fh); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
3
|
|
|
|
|
2753
|
my $in_type; |
99
|
3
|
|
|
|
|
21
|
for my $line (split(/\n/mx, $output)) { |
100
|
40
|
50
|
|
|
|
74
|
next if substr($line, 0, 1) eq '#'; |
101
|
40
|
50
|
|
|
|
96
|
next if $line =~ m/^\s*$/gmx; |
102
|
40
|
|
|
|
|
54
|
chomp($line); |
103
|
40
|
100
|
|
|
|
94
|
if($line =~ m/^\s*define\s+(\w+)($|\s|{)/mx) { |
|
|
50
|
|
|
|
|
|
104
|
5
|
|
|
|
|
14
|
$in_type = $1; |
105
|
5
|
50
|
33
|
|
|
25
|
if($type && $in_type.'s' ne $type) { |
106
|
0
|
|
|
|
|
0
|
warn("unexpected input type '".$in_type."' in ".$file.':'.$.); |
107
|
0
|
|
|
|
|
0
|
next; |
108
|
|
|
|
|
|
|
} |
109
|
5
|
|
|
|
|
10
|
$in_obj = 1; |
110
|
|
|
|
|
|
|
} elsif($in_obj) { |
111
|
35
|
100
|
|
|
|
57
|
if($line =~ m/^\s*}/mx) { |
112
|
5
|
|
|
|
|
8
|
$in_obj = 0; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# bring tags in shape |
115
|
5
|
|
|
|
|
34
|
$current->{'tags'} = Monitoring::TT::Utils::parse_tags(delete $current->{'conf'}->{'_tags'}); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# bring groups in shape |
118
|
5
|
|
|
|
|
17
|
$current->{'groups'} = Monitoring::TT::Utils::parse_groups(delete $current->{'conf'}->{'_groups'}); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# bring apps in shape |
121
|
5
|
50
|
|
|
|
17
|
$current->{'apps'} = Monitoring::TT::Utils::parse_tags(delete $current->{'conf'}->{'_apps'}) if $in_type eq 'host'; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# transfer type and some other attributes |
124
|
5
|
|
|
|
|
11
|
$current->{'type'} = delete $current->{'conf'}->{'_type'}; |
125
|
5
|
|
50
|
|
|
20
|
$current->{'alias'} = $current->{'conf'}->{'alias'} || ''; |
126
|
|
|
|
|
|
|
|
127
|
5
|
50
|
|
|
|
11
|
if($in_type eq 'host') { |
128
|
5
|
|
50
|
|
|
14
|
$current->{'name'} = $current->{'conf'}->{'host_name'} || ''; |
129
|
5
|
|
50
|
|
|
23
|
$current->{'address'} = $current->{'conf'}->{'address'} || ''; |
130
|
5
|
|
50
|
|
|
26
|
$current->{'groups'} = $current->{'conf'}->{'host_groups'} || $current->{'conf'}->{'hostgroups'} || []; |
131
|
|
|
|
|
|
|
} |
132
|
5
|
50
|
|
|
|
11
|
if($in_type eq 'contact') { |
133
|
0
|
|
0
|
|
|
0
|
$current->{'name'} = $current->{'conf'}->{'contact_name'} || ''; |
134
|
0
|
|
0
|
|
|
0
|
$current->{'email'} = $current->{'conf'}->{'email'} || ''; |
135
|
0
|
|
0
|
|
|
0
|
$current->{'groups'} = $current->{'conf'}->{'contact_groups'} || $current->{'conf'}->{'contactgroups'} || []; |
136
|
|
|
|
|
|
|
} |
137
|
5
|
50
|
|
|
|
12
|
if($in_type eq 'service') { |
138
|
0
|
|
0
|
|
|
0
|
$current->{'groups'} = $current->{'conf'}->{'service_groups'} || $current->{'conf'}->{'servicegroups'} || []; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
5
|
50
|
|
|
|
14
|
$current->{'type'} = $in_type unless $current->{'type'}; |
142
|
|
|
|
|
|
|
|
143
|
5
|
|
|
|
|
9
|
$current->{'file'} = delete $current->{'conf'}->{'_src'}; |
144
|
5
|
50
|
|
|
|
13
|
$current->{'file'} = $file unless $current->{'file'}; |
145
|
5
|
|
|
|
|
21
|
$current->{'file'} =~ s/:(\d+)$//gmx; |
146
|
5
|
50
|
|
|
|
21
|
$current->{'line'} = defined $1 ? $1 : $.; |
147
|
|
|
|
|
|
|
|
148
|
5
|
|
|
|
|
7
|
push @{$data}, $current; |
|
5
|
|
|
|
|
9
|
|
149
|
5
|
|
|
|
|
52
|
$current = { 'conf' => {}}; |
150
|
|
|
|
|
|
|
} else { |
151
|
30
|
|
|
|
|
76
|
$line =~ s/^\s*//gmx; |
152
|
30
|
|
|
|
|
261
|
$line =~ s/\s*$//gmx; |
153
|
30
|
|
|
|
|
85
|
my($key,$val) = split/\s+/mx, $line, 2; |
154
|
30
|
|
|
|
|
46
|
$key = lc $key; |
155
|
30
|
|
|
|
|
65
|
$current->{'conf'}->{$key} = $val; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
3
|
|
|
|
|
13
|
debug("read ".(scalar @{$data})." $type from $file"); |
|
3
|
|
|
|
|
19
|
|
160
|
|
|
|
|
|
|
} |
161
|
2
|
|
|
|
|
11
|
return $data; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
##################################################################### |
165
|
|
|
|
|
|
|
# internal subs |
166
|
|
|
|
|
|
|
##################################################################### |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
##################################################################### |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 AUTHOR |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Sven Nierlein, 2013, |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=cut |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
1; |