line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Monitoring::TT::Input::Nagios; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
2128
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
55
|
|
4
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
47
|
|
5
|
2
|
|
|
2
|
|
8
|
use utf8; |
|
2
|
|
|
|
|
1
|
|
|
2
|
|
|
|
|
7
|
|
6
|
2
|
|
|
2
|
|
39
|
use Monitoring::TT::Log qw/error warn info debug trace/; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
91
|
|
7
|
2
|
|
|
2
|
|
6
|
use Monitoring::TT::Utils; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
1274
|
|
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
|
9
|
my($class, %options) = @_; |
33
|
|
|
|
|
|
|
my $self = { |
34
|
|
|
|
|
|
|
'types' => [ 'hosts', 'contacts' ], |
35
|
2
|
|
|
|
|
6
|
'montt' => $options{'montt'}, |
36
|
|
|
|
|
|
|
}; |
37
|
2
|
|
|
|
|
2
|
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
|
|
|
|
|
19
|
my $types = []; |
55
|
1
|
|
|
|
|
1
|
for my $dir (@{$folders}) { |
|
1
|
|
|
|
|
2
|
|
56
|
1
|
|
|
|
|
1
|
for my $type (sort @{$self->{'types'}}) { |
|
1
|
|
|
|
|
4
|
|
57
|
2
|
|
|
|
|
4
|
my $pattern = $dir.'/'.$type.'*.cfg'; |
58
|
2
|
|
|
|
|
7
|
trace('looking for nagios file: '.$pattern); |
59
|
2
|
|
|
|
|
113
|
my @files = glob($pattern); |
60
|
2
|
|
|
|
|
4
|
for my $f (@files) { |
61
|
2
|
|
|
|
|
6
|
debug('found nagios file: '.$f); |
62
|
2
|
|
|
|
|
2
|
push @{$types}, $type; |
|
2
|
|
|
|
|
4
|
|
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
1
|
|
|
|
|
3
|
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
|
469
|
my($self, $dir, $type) = @_; |
78
|
2
|
|
|
|
|
22
|
my $data = []; |
79
|
2
|
|
|
|
|
5
|
my $pattern = $dir.'/'.$type.'*.cfg'; |
80
|
2
|
|
|
|
|
145
|
my @files = glob($pattern); |
81
|
2
|
|
|
|
|
6
|
my $current = { 'conf' => {}}; |
82
|
2
|
|
|
|
|
2
|
my $in_obj = 0; |
83
|
2
|
|
|
|
|
3
|
for my $file (@files) { |
84
|
3
|
|
|
|
|
16
|
info("reading $type from $file"); |
85
|
|
|
|
|
|
|
|
86
|
3
|
|
|
|
|
3
|
my $output = ""; |
87
|
3
|
|
|
|
|
13
|
my $template = $self->{'montt'}->_process_template($self->{'montt'}->_read_replaced_template($file)); |
88
|
3
|
50
|
|
|
|
16
|
$self->{'montt'}->tt->process(\$template, {}, \$output) or $self->{'montt'}->_template_process_die($file, $data); |
89
|
|
|
|
|
|
|
|
90
|
3
|
|
|
|
|
1922
|
for my $line (split(/\n/mx, $output)) { |
91
|
40
|
50
|
|
|
|
71
|
next if substr($line, 0, 1) eq '#'; |
92
|
40
|
50
|
|
|
|
82
|
next if $line =~ m/^\s*$/gmx; |
93
|
40
|
|
|
|
|
31
|
chomp($line); |
94
|
40
|
100
|
|
|
|
79
|
if($line =~ m/^\s*define\s+(\w+)($|\s|{)/mx) { |
|
|
50
|
|
|
|
|
|
95
|
5
|
|
|
|
|
11
|
my $in_type = $1; |
96
|
5
|
50
|
|
|
|
13
|
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
|
|
|
|
49
|
if($line =~ m/^\s*}/mx) { |
103
|
5
|
|
|
|
|
6
|
$in_obj = 0; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# bring tags in shape |
106
|
5
|
|
|
|
|
18
|
$current->{'tags'} = Monitoring::TT::Utils::parse_tags(delete $current->{'conf'}->{'_tags'}); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# bring groups in shape |
109
|
5
|
|
|
|
|
13
|
$current->{'groups'} = Monitoring::TT::Utils::parse_groups(delete $current->{'conf'}->{'_groups'}); |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# bring apps in shape |
112
|
5
|
50
|
|
|
|
18
|
$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
|
|
|
|
|
7
|
$current->{'type'} = delete $current->{'conf'}->{'_type'}; |
116
|
5
|
|
50
|
|
|
33
|
$current->{'alias'} = $current->{'conf'}->{'alias'} || ''; |
117
|
|
|
|
|
|
|
|
118
|
5
|
50
|
|
|
|
9
|
if($type eq 'hosts') { |
119
|
5
|
|
50
|
|
|
13
|
$current->{'name'} = $current->{'conf'}->{'host_name'} || ''; |
120
|
5
|
|
50
|
|
|
14
|
$current->{'address'} = $current->{'conf'}->{'address'} || ''; |
121
|
5
|
|
50
|
|
|
23
|
$current->{'groups'} = $current->{'conf'}->{'host_groups'} || $current->{'conf'}->{'hostgroups'} || []; |
122
|
|
|
|
|
|
|
} |
123
|
5
|
50
|
|
|
|
11
|
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
|
|
|
|
|
8
|
$current->{'file'} = delete $current->{'conf'}->{'_src'}; |
130
|
5
|
|
|
|
|
18
|
$current->{'file'} =~ s/:(\d+)$//gmx; |
131
|
5
|
|
|
|
|
10
|
$current->{'line'} = $1; |
132
|
|
|
|
|
|
|
|
133
|
5
|
|
|
|
|
6
|
push @{$data}, $current; |
|
5
|
|
|
|
|
9
|
|
134
|
5
|
|
|
|
|
12
|
$current = { 'conf' => {}}; |
135
|
|
|
|
|
|
|
} else { |
136
|
30
|
|
|
|
|
60
|
$line =~ s/^\s*//gmx; |
137
|
30
|
|
|
|
|
235
|
$line =~ s/\s*$//gmx; |
138
|
30
|
|
|
|
|
66
|
my($key,$val) = split/\s+/mx, $line, 2; |
139
|
30
|
|
|
|
|
34
|
$key = lc $key; |
140
|
30
|
|
|
|
|
51
|
$current->{'conf'}->{$key} = $val; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
3
|
|
|
|
|
6
|
debug("read ".(scalar @{$data})." $type from $file"); |
|
3
|
|
|
|
|
16
|
|
145
|
|
|
|
|
|
|
} |
146
|
2
|
|
|
|
|
9
|
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; |