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