Fix a bug where default file used is not shown
[Nagios-Plugin.git] / lib / Nagios / Plugin / Config.pm
blobdd270e93978d9856c38f26ab116c36a1ac55fc5b
1 package Nagios::Plugin::Config;
3 use strict;
4 use Carp;
5 use File::Spec;
6 use base qw(Config::Tiny);
8 my $FILENAME1 = 'plugins.ini';
9 my $FILENAME2 = 'nagios-plugins.ini';
10 my $CURRENT_FILE = undef;
12 # Config paths ending in nagios (search for $FILENAME1)
13 my @NAGIOS_CONFIG_PATH = qw(/etc/nagios /usr/local/nagios/etc /usr/local/etc/nagios /etc/opt/nagios);
14 # Config paths not ending in nagios (search for $FILENAME2)
15 my @CONFIG_PATH = qw(/etc /usr/local/etc /etc/opt);
17 # Override Config::Tiny::read to default the filename, if not given
18 sub read
20 my $class = shift;
22 unless ($_[0]) {
23 SEARCH: {
24 if ($ENV{NAGIOS_CONFIG_PATH}) {
25 for (split /:/, $ENV{NAGIOS_CONFIG_PATH}) {
26 my $file = File::Spec->catfile($_, $FILENAME1);
27 unshift(@_, $file), last SEARCH if -f $file;
28 $file = File::Spec->catfile($_, $FILENAME2);
29 unshift(@_, $file), last SEARCH if -f $file;
32 for (@NAGIOS_CONFIG_PATH) {
33 my $file = File::Spec->catfile($_, $FILENAME1);
34 unshift(@_, $file), last SEARCH if -f $file;
36 for (@CONFIG_PATH) {
37 my $file = File::Spec->catfile($_, $FILENAME2);
38 unshift(@_, $file), last SEARCH if -f $file;
42 # Use die instead of croak, so we can pass a clean message downstream
43 die "Cannot find '$FILENAME1' or '$FILENAME2' in any standard location.\n" unless $_[0];
46 $CURRENT_FILE = $_[0];
47 $class->SUPER::read( @_ );
50 # Straight from Config::Tiny - only changes are repeated property key support
51 # Would be nice if we could just override the per-line handling ...
52 sub read_string
54 my $class = ref $_[0] ? ref shift : shift;
55 my $self = bless {}, $class;
56 return undef unless defined $_[0];
58 # Parse the file
59 my $ns = '_';
60 my $counter = 0;
61 foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) {
62 $counter++;
64 # Skip comments and empty lines
65 next if /^\s*(?:\#|\;|$)/;
67 # Handle section headers
68 if ( /^\s*\[\s*(.+?)\s*\]\s*$/ ) {
69 # Create the sub-hash if it doesn't exist.
70 # Without this sections without keys will not
71 # appear at all in the completed struct.
72 $self->{$ns = $1} ||= {};
73 next;
76 # Handle properties
77 if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) {
78 push @{$self->{$ns}->{$1}}, $2;
79 next;
82 return $self->_error( "Syntax error at line $counter: '$_'" );
85 $self;
88 sub write { croak "Write access not permitted" }
90 # Return last file used by read();
91 sub np_getfile { return $CURRENT_FILE; }
95 =head1 NAME
97 Nagios::Plugin::Config - read nagios plugin .ini style config files
99 =head1 SYNOPSIS
101 # Read given nagios plugin config file
102 $Config = Nagios::Plugin::Config->read( '/etc/nagios/plugins.ini' );
104 # Search for and read default nagios plugin config file
105 $Config = Nagios::Plugin::Config->read();
107 # Access sections and properties (returns scalars or arrayrefs)
108 $rootproperty = $Config->{_}->{rootproperty};
109 $one = $Config->{section}->{one};
110 $Foo = $Config->{section}->{Foo};
112 =head1 DESCRIPTION
114 Nagios::Plugin::Config is a subclass of the excellent Config::Tiny,
115 with the following changes:
117 =over 4
119 =item
121 Repeated keys are allowed within sections, returning lists instead of scalars
123 =item
125 Write functionality has been removed i.e. access is read only
127 =item
129 Nagios::Plugin::Config searches for a default nagios plugins file if no explicit
130 filename is given to C<read()>. The current standard locations checked are:
132 =over 4
134 =item /etc/nagios/plugins.ini
136 =item /usr/local/nagios/etc/plugins.ini
138 =item /usr/local/etc/nagios /etc/opt/nagios/plugins.ini
140 =item /etc/nagios-plugins.ini
142 =item /usr/local/etc/nagios-plugins.ini
144 =item /etc/opt/nagios-plugins.ini
146 =back
148 To use a custom location, set a C<NAGIOS_CONFIG_PATH> environment variable
149 to the set of directories that should be checked. The first C<plugins.ini> or
150 C<nagios-plugins.ini> file found will be used.
152 =back
155 =head1 SEE ALSO
157 L<Config::Tiny>, L<Nagios::Plugin>
160 =head1 AUTHORS
162 This code is maintained by the Nagios Plugin Development Team:
163 L<http://nagiosplug.sourceforge.net>.
166 =head1 COPYRIGHT and LICENCE
168 Copyright (C) 2006-2007 by Nagios Plugin Development Team
170 This library is free software; you can redistribute it and/or modify
171 it under the same terms as Perl itself.
173 =cut