diff options
Diffstat (limited to 'lib/Nagios/Plugin/Config.pm')
-rw-r--r-- | lib/Nagios/Plugin/Config.pm | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/lib/Nagios/Plugin/Config.pm b/lib/Nagios/Plugin/Config.pm new file mode 100644 index 0000000..92193c2 --- /dev/null +++ b/lib/Nagios/Plugin/Config.pm | |||
@@ -0,0 +1,170 @@ | |||
1 | package Nagios::Plugin::Config; | ||
2 | |||
3 | use strict; | ||
4 | use Carp; | ||
5 | use File::Spec; | ||
6 | use base qw(Config::Tiny); | ||
7 | |||
8 | my $FILENAME1 = 'plugins.ini'; | ||
9 | my $FILENAME2 = 'nagios-plugins.ini'; | ||
10 | |||
11 | # Config paths ending in nagios (search for $FILENAME1) | ||
12 | my @NAGIOS_CONFIG_PATH = qw(/etc/nagios /usr/local/nagios/etc /usr/local/etc/nagios /etc/opt/nagios); | ||
13 | # Config paths not ending in nagios (search for $FILENAME2) | ||
14 | my @CONFIG_PATH = qw(/etc /usr/local/etc /etc/opt); | ||
15 | |||
16 | # Override Config::Tiny::read to default the filename, if not given | ||
17 | sub read | ||
18 | { | ||
19 | my $class = shift; | ||
20 | |||
21 | unless ($_[0]) { | ||
22 | SEARCH: { | ||
23 | if ($ENV{NAGIOS_CONFIG_PATH}) { | ||
24 | for (split /:/, $ENV{NAGIOS_CONFIG_PATH}) { | ||
25 | my $file = File::Spec->catfile($_, $FILENAME1); | ||
26 | unshift(@_, $file), last SEARCH if -f $file; | ||
27 | $file = File::Spec->catfile($_, $FILENAME2); | ||
28 | unshift(@_, $file), last SEARCH if -f $file; | ||
29 | } | ||
30 | } | ||
31 | for (@NAGIOS_CONFIG_PATH) { | ||
32 | my $file = File::Spec->catfile($_, $FILENAME1); | ||
33 | unshift(@_, $file), last SEARCH if -f $file; | ||
34 | } | ||
35 | for (@CONFIG_PATH) { | ||
36 | my $file = File::Spec->catfile($_, $FILENAME2); | ||
37 | unshift(@_, $file), last SEARCH if -f $file; | ||
38 | } | ||
39 | } | ||
40 | |||
41 | croak "Cannot find '$FILENAME1' or '$FILENAME2' in any standard location." unless $_[0]; | ||
42 | } | ||
43 | |||
44 | $class->SUPER::read( @_ ); | ||
45 | } | ||
46 | |||
47 | # Straight from Config::Tiny - only changes are repeated property key support | ||
48 | # Would be nice if we could just override the per-line handling ... | ||
49 | sub read_string | ||
50 | { | ||
51 | my $class = ref $_[0] ? ref shift : shift; | ||
52 | my $self = bless {}, $class; | ||
53 | return undef unless defined $_[0]; | ||
54 | |||
55 | # Parse the file | ||
56 | my $ns = '_'; | ||
57 | my $counter = 0; | ||
58 | foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) { | ||
59 | $counter++; | ||
60 | |||
61 | # Skip comments and empty lines | ||
62 | next if /^\s*(?:\#|\;|$)/; | ||
63 | |||
64 | # Handle section headers | ||
65 | if ( /^\s*\[\s*(.+?)\s*\]\s*$/ ) { | ||
66 | # Create the sub-hash if it doesn't exist. | ||
67 | # Without this sections without keys will not | ||
68 | # appear at all in the completed struct. | ||
69 | $self->{$ns = $1} ||= {}; | ||
70 | next; | ||
71 | } | ||
72 | |||
73 | # Handle properties | ||
74 | if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) { | ||
75 | $self->{$ns}->{$1} = defined $self->{$ns}->{$1} ? | ||
76 | [ $self->{$ns}->{$1}, $2 ] : | ||
77 | $2; | ||
78 | next; | ||
79 | } | ||
80 | |||
81 | return $self->_error( "Syntax error at line $counter: '$_'" ); | ||
82 | } | ||
83 | |||
84 | $self; | ||
85 | } | ||
86 | |||
87 | sub write { croak "Write access not permitted" } | ||
88 | |||
89 | 1; | ||
90 | |||
91 | =head1 NAME | ||
92 | |||
93 | Nagios::Plugin::Config - read nagios plugin .ini style config files | ||
94 | |||
95 | =head1 SYNOPSIS | ||
96 | |||
97 | # Read given nagios plugin config file | ||
98 | $Config = Nagios::Plugin::Config->read( '/etc/nagios/plugins.ini' ); | ||
99 | |||
100 | # Search for and read default nagios plugin config file | ||
101 | $Config = Nagios::Plugin::Config->read(); | ||
102 | |||
103 | # Access sections and properties (returns scalars or arrayrefs) | ||
104 | $rootproperty = $Config->{_}->{rootproperty}; | ||
105 | $one = $Config->{section}->{one}; | ||
106 | $Foo = $Config->{section}->{Foo}; | ||
107 | |||
108 | =head1 DESCRIPTION | ||
109 | |||
110 | Nagios::Plugin::Config is a subclass of the excellent Config::Tiny, | ||
111 | with the following changes: | ||
112 | |||
113 | =over 4 | ||
114 | |||
115 | =item | ||
116 | |||
117 | Repeated keys are allowed within sections, returning lists instead of scalars | ||
118 | |||
119 | =item | ||
120 | |||
121 | Write functionality has been removed i.e. access is read only | ||
122 | |||
123 | =item | ||
124 | |||
125 | Nagios::Plugin::Config searches for a default nagios plugins file if no explicit | ||
126 | filename is given to C<read()>. The current standard locations checked are: | ||
127 | |||
128 | =over 4 | ||
129 | |||
130 | =item /etc/nagios/plugins.ini | ||
131 | |||
132 | =item /usr/local/nagios/etc/plugins.ini | ||
133 | |||
134 | =item /usr/local/etc/nagios /etc/opt/nagios/plugins.ini | ||
135 | |||
136 | =item /etc/nagios-plugins.ini | ||
137 | |||
138 | =item /usr/local/etc/nagios-plugins.ini | ||
139 | |||
140 | =item /etc/opt/nagios-plugins.ini | ||
141 | |||
142 | =back | ||
143 | |||
144 | To use a custom location, set a C<NAGIOS_CONFIG_PATH> environment variable | ||
145 | to the set of directories that should be checked. The first C<plugins.ini> or | ||
146 | C<nagios-plugins.ini> file found will be used. | ||
147 | |||
148 | =back | ||
149 | |||
150 | |||
151 | =head1 SEE ALSO | ||
152 | |||
153 | L<Config::Tiny>, L<Nagios::Plugin> | ||
154 | |||
155 | |||
156 | =head1 AUTHORS | ||
157 | |||
158 | This code is maintained by the Nagios Plugin Development Team: | ||
159 | L<http://nagiosplug.sourceforge.net>. | ||
160 | |||
161 | |||
162 | =head1 COPYRIGHT and LICENCE | ||
163 | |||
164 | Copyright (C) 2006-2007 by Nagios Plugin Development Team | ||
165 | |||
166 | This library is free software; you can redistribute it and/or modify | ||
167 | it under the same terms as Perl itself. | ||
168 | |||
169 | =cut | ||
170 | |||