line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Nagios::Config::File; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
828
|
use strict; |
|
7
|
|
|
|
|
17
|
|
|
7
|
|
|
|
|
330
|
|
4
|
7
|
|
|
7
|
|
39
|
use warnings; |
|
7
|
|
|
|
|
17
|
|
|
7
|
|
|
|
|
223
|
|
5
|
7
|
|
|
7
|
|
35
|
use Carp; |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
481
|
|
6
|
7
|
|
|
7
|
|
1005
|
use Symbol; |
|
7
|
|
|
|
|
1153
|
|
|
7
|
|
|
|
|
12734
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# NOTE: due to CPAN version checks this cannot currently be changed to a |
9
|
|
|
|
|
|
|
# standard version string, i.e. '0.21' |
10
|
|
|
|
|
|
|
our $VERSION = '35'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my %DUPLICATES_ALLOWED = ( |
13
|
|
|
|
|
|
|
cfg_file => 1, |
14
|
|
|
|
|
|
|
cfg_dir => 1, |
15
|
|
|
|
|
|
|
); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 NAME |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Nagios::Config::File - Base class for Nagios configuration files |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 SYNOPSIS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
use Nagios::Config ; |
24
|
|
|
|
|
|
|
my $nc = new Nagios::Config("/usr/local/nagios/etc/nagios.cfg") ; |
25
|
|
|
|
|
|
|
my $resource = $nc->get_resource_cfg() ; |
26
|
|
|
|
|
|
|
print $resource->get_attr('$USER1$') . "\n" ; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 DESCRIPTION |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
C is the base class for all Nagios configuration |
31
|
|
|
|
|
|
|
files. You should not need to create these yourself. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=cut |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=over 4 |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=item new ([FILE]) |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Creates a C. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=back |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=cut |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub new { |
48
|
5
|
|
|
5
|
1
|
896
|
my $class = shift; |
49
|
5
|
|
|
|
|
11
|
my $file = shift; |
50
|
|
|
|
|
|
|
|
51
|
5
|
50
|
|
|
|
19
|
croak "Missing argument: must specify a configuration file to parse." |
52
|
|
|
|
|
|
|
if ( !$file ); |
53
|
|
|
|
|
|
|
|
54
|
5
|
|
|
|
|
11
|
my $this = {}; |
55
|
5
|
|
|
|
|
15
|
bless( $this, $class ); |
56
|
|
|
|
|
|
|
|
57
|
5
|
|
|
|
|
9
|
my $fh = undef; |
58
|
5
|
50
|
|
|
|
17
|
if ( ref($file) ) { |
59
|
0
|
|
|
|
|
0
|
$fh = $file; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
else { |
62
|
5
|
|
|
|
|
24
|
$fh = gensym; |
63
|
5
|
50
|
|
|
|
369
|
open( $fh, "<$file" ) |
64
|
|
|
|
|
|
|
|| croak("Can't open $file for reading: $!"); |
65
|
5
|
|
|
|
|
33
|
$this->{filename} = $file; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
5
|
|
|
|
|
88
|
$this->{file_attributes} = {}; |
69
|
5
|
|
|
|
|
9
|
$this->{fh} = $fh; |
70
|
|
|
|
|
|
|
|
71
|
5
|
|
|
|
|
19
|
$this->parse(); |
72
|
5
|
|
|
|
|
115
|
close($fh); |
73
|
|
|
|
|
|
|
|
74
|
5
|
|
|
|
|
37
|
return $this; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub parse { |
78
|
5
|
|
|
5
|
0
|
9
|
my $this = shift; |
79
|
|
|
|
|
|
|
|
80
|
5
|
|
|
|
|
8
|
my $fh = $this->{fh}; |
81
|
|
|
|
|
|
|
|
82
|
5
|
|
|
|
|
103
|
while (<$fh>) { |
83
|
3649
|
|
|
|
|
5675
|
my $line = $this->strip($_); |
84
|
|
|
|
|
|
|
|
85
|
3649
|
100
|
|
|
|
5668
|
if ( $this->is_comment($line) ) { |
|
|
50
|
|
|
|
|
|
86
|
3277
|
|
|
|
|
7766
|
next; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
elsif ( my ( $name, $value ) = $this->is_attribute($line) ) { |
89
|
372
|
100
|
|
|
|
695
|
if ( $DUPLICATES_ALLOWED{$name} ) { |
90
|
32
|
|
|
|
|
25
|
push @{ $this->{file_attributes}->{$name} }, $value; |
|
32
|
|
|
|
|
136
|
|
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
else { |
93
|
340
|
|
|
|
|
2014
|
$this->{file_attributes}->{$name} = $value; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub strip { |
100
|
3649
|
|
|
3649
|
0
|
3580
|
my $this = shift; |
101
|
3649
|
|
|
|
|
4133
|
my $line = shift; |
102
|
|
|
|
|
|
|
|
103
|
3649
|
|
|
|
|
5930
|
$line =~ s/^\s+//; |
104
|
3649
|
|
|
|
|
9142
|
$line =~ s/\s+$//; |
105
|
|
|
|
|
|
|
|
106
|
3649
|
|
|
|
|
5999
|
return $line; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub is_comment { |
110
|
3649
|
|
|
3649
|
0
|
3549
|
my $this = shift; |
111
|
3649
|
|
|
|
|
3456
|
my $line = shift; |
112
|
|
|
|
|
|
|
|
113
|
3649
|
100
|
100
|
|
|
12471
|
if ( ( $line eq '' ) || ( $line =~ /^#/ ) ) { |
114
|
3277
|
|
|
|
|
6322
|
return 1; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
372
|
|
|
|
|
929
|
return 0; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub is_attribute { |
121
|
372
|
|
|
372
|
0
|
347
|
my $this = shift; |
122
|
372
|
|
|
|
|
407
|
my $line = shift; |
123
|
|
|
|
|
|
|
|
124
|
372
|
50
|
|
|
|
1364
|
if ( $line =~ /^([\w\$]+)\s*=\s*(.+)$/ ) { |
125
|
372
|
|
|
|
|
1502
|
return ( $1, $2 ); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
0
|
return (); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 METHODS |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=over 4 |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item get ([NAME], [SPLIT]) |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Returns the value of the attribute C for the current file. |
138
|
|
|
|
|
|
|
If C is true, returns a list of all the values split on |
139
|
|
|
|
|
|
|
/\s*,\s*/. This is useful for attributes that can have more that one value. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=cut |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub get { |
144
|
24
|
|
|
24
|
1
|
52
|
my ( $this, $name, $split ) = @_; |
145
|
24
|
|
|
|
|
65
|
my $val = $this->{file_attributes}->{$name}; |
146
|
24
|
50
|
|
|
|
147
|
return $split ? split( /\s*,\s*/, $val ) : $val; |
147
|
|
|
|
|
|
|
} |
148
|
4
|
|
|
4
|
0
|
12
|
sub get_attr { &get; } |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item filename() |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Returns the filename for the current object. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |
155
|
|
|
|
|
|
|
|
156
|
0
|
|
|
0
|
1
|
|
sub filename { $_[0]->{filename} } |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item dump () |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Returns a scalar with the full configuration text ready to parse again. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub dump { |
165
|
0
|
|
|
0
|
1
|
|
my $this = shift; |
166
|
0
|
|
|
|
|
|
my $outtxt = "# filename: $this->{filename}\n"; |
167
|
0
|
|
|
|
|
|
foreach my $attr ( keys( %{ $this->{file_attributes} } ) ) { |
|
0
|
|
|
|
|
|
|
168
|
0
|
0
|
|
|
|
|
if ( $DUPLICATES_ALLOWED{$attr} ) { |
169
|
0
|
|
|
|
|
|
foreach my $element ( @{ $this->{file_attributes}{$attr} } ) { |
|
0
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
$outtxt .= $attr . '=' . $element . "\n"; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
else { |
174
|
0
|
|
|
|
|
|
$outtxt .= $attr . '=' . $this->{file_attributes}{$attr} . "\n"; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
} |
177
|
0
|
|
|
|
|
|
return $outtxt; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
1; |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=back |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 AUTHOR |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Patrick LeBoutillier, patl@cpan.org |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Al Tobey, tobeya@cpan.org |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 SEE ALSO |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Nagios::Config, Nagios::Config::Object |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=cut |
195
|
|
|
|
|
|
|
|