[Nagios-Plugin] Fix a bug where default file used is not shown

Nagios Plugin Development nagios-plugins at users.sourceforge.net
Fri Jun 29 05:01:26 CEST 2012


 Module: Nagios-Plugin
 Branch: master
 Commit: e70d62aaf96830a6480b147e6c90c30aeef31a54
 Author: Thomas Guyot-Sionnest <dermoth at aei.ca>
   Date: Thu Jun 28 22:54:38 2012 -0400
    URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/Nagios-Plugin;a=commit;h=e70d62a

Fix a bug where default file used is not shown

When a default file is used but no section is found (ex. below using
"bad_section"), the plugin dies with:

  Invalid section 'bad_section' in config file ''

This patch add a function to Nagios::Plugin::Config that returns the last
used file, and use it to return a file name when we have none.

---

 lib/Nagios/Plugin/Config.pm |    7 ++++++-
 lib/Nagios/Plugin/Getopt.pm |    1 +
 2 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/lib/Nagios/Plugin/Config.pm b/lib/Nagios/Plugin/Config.pm
index 11128dd..dd270e9 100644
--- a/lib/Nagios/Plugin/Config.pm
+++ b/lib/Nagios/Plugin/Config.pm
@@ -7,6 +7,7 @@ use base qw(Config::Tiny);
 
 my $FILENAME1 = 'plugins.ini';
 my $FILENAME2 = 'nagios-plugins.ini';
+my $CURRENT_FILE = undef;
 
 # Config paths ending in nagios (search for $FILENAME1)
 my @NAGIOS_CONFIG_PATH = qw(/etc/nagios /usr/local/nagios/etc /usr/local/etc/nagios /etc/opt/nagios);
@@ -42,6 +43,7 @@ sub read
                 die "Cannot find '$FILENAME1' or '$FILENAME2' in any standard location.\n" unless $_[0];
         }
 
+        $CURRENT_FILE = $_[0];
         $class->SUPER::read( @_ );
 }
 
@@ -73,7 +75,7 @@ sub read_string
 
                 # Handle properties
                 if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) {
-			push @{$self->{$ns}->{$1}}, $2;
+                        push @{$self->{$ns}->{$1}}, $2;
                         next;
                 }
 
@@ -85,6 +87,9 @@ sub read_string
 
 sub write { croak "Write access not permitted" }
 
+# Return last file used by read();
+sub np_getfile { return $CURRENT_FILE; }
+
 1;
 
 =head1 NAME
diff --git a/lib/Nagios/Plugin/Getopt.pm b/lib/Nagios/Plugin/Getopt.pm
index 6432ee0..f910c4b 100644
--- a/lib/Nagios/Plugin/Getopt.pm
+++ b/lib/Nagios/Plugin/Getopt.pm
@@ -249,6 +249,7 @@ sub _load_config_section
   # TODO: is this check sane? Does --extra-opts=foo require a [foo] section?
   ## Nevertheless, if we die as UNKNOWN here we should do the same on default
   ## file *added eval/_die above*.
+  $file ||= $Config->np_getfile();
   $self->_die("Invalid section '$section' in config file '$file'")
     unless exists $Config->{$section};
 





More information about the Commits mailing list