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