summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@aei.ca>2012-06-29 02:54:38 (GMT)
committerThomas Guyot-Sionnest <dermoth@aei.ca>2012-06-29 02:54:38 (GMT)
commite70d62aaf96830a6480b147e6c90c30aeef31a54 (patch)
tree8fcfa608a5770ec364b2c272d738e22ff72cbc55
parentbacb16365718f55bbb09f9d990011e3829691f16 (diff)
downloadmonitoring-plugin-perl-e70d62aaf96830a6480b147e6c90c30aeef31a54.tar.gz
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.
-rw-r--r--lib/Nagios/Plugin/Config.pm7
-rw-r--r--lib/Nagios/Plugin/Getopt.pm1
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);
7 7
8my $FILENAME1 = 'plugins.ini'; 8my $FILENAME1 = 'plugins.ini';
9my $FILENAME2 = 'nagios-plugins.ini'; 9my $FILENAME2 = 'nagios-plugins.ini';
10my $CURRENT_FILE = undef;
10 11
11# Config paths ending in nagios (search for $FILENAME1) 12# Config paths ending in nagios (search for $FILENAME1)
12my @NAGIOS_CONFIG_PATH = qw(/etc/nagios /usr/local/nagios/etc /usr/local/etc/nagios /etc/opt/nagios); 13my @NAGIOS_CONFIG_PATH = qw(/etc/nagios /usr/local/nagios/etc /usr/local/etc/nagios /etc/opt/nagios);
@@ -42,6 +43,7 @@ sub read
42 die "Cannot find '$FILENAME1' or '$FILENAME2' in any standard location.\n" unless $_[0]; 43 die "Cannot find '$FILENAME1' or '$FILENAME2' in any standard location.\n" unless $_[0];
43 } 44 }
44 45
46 $CURRENT_FILE = $_[0];
45 $class->SUPER::read( @_ ); 47 $class->SUPER::read( @_ );
46} 48}
47 49
@@ -73,7 +75,7 @@ sub read_string
73 75
74 # Handle properties 76 # Handle properties
75 if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) { 77 if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) {
76 push @{$self->{$ns}->{$1}}, $2; 78 push @{$self->{$ns}->{$1}}, $2;
77 next; 79 next;
78 } 80 }
79 81
@@ -85,6 +87,9 @@ sub read_string
85 87
86sub write { croak "Write access not permitted" } 88sub write { croak "Write access not permitted" }
87 89
90# Return last file used by read();
91sub np_getfile { return $CURRENT_FILE; }
92
881; 931;
89 94
90=head1 NAME 95=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
249 # TODO: is this check sane? Does --extra-opts=foo require a [foo] section? 249 # TODO: is this check sane? Does --extra-opts=foo require a [foo] section?
250 ## Nevertheless, if we die as UNKNOWN here we should do the same on default 250 ## Nevertheless, if we die as UNKNOWN here we should do the same on default
251 ## file *added eval/_die above*. 251 ## file *added eval/_die above*.
252 $file ||= $Config->np_getfile();
252 $self->_die("Invalid section '$section' in config file '$file'") 253 $self->_die("Invalid section '$section' in config file '$file'")
253 unless exists $Config->{$section}; 254 unless exists $Config->{$section};
254 255