[Nagios-Plugin] shortname enhancement

Thomas Guyot-Sionnest dermoth at users.sourceforge.net
Thu Mar 18 02:02:30 CET 2010


 Module: Nagios-Plugin
 Branch: master
 Commit: c128d293b015291c5e45637d9e3d5b1e1fb36c12
 Author: Thomas Guyot-Sionnest <dermoth at aei.ca>
   Date: Wed Mar 10 01:54:06 2010 -0500
    URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/Nagios-Plugin;a=commit;h=c128d29

shortname enhancement

This patch makes shortname use the defined plugin's name if set,
otherwise the normal method should prevail. To do so I had to
generate shortname during np initialization instead of at use time.

---

 lib/Nagios/Plugin.pm           |   16 +++-------------
 lib/Nagios/Plugin/Functions.pm |   14 +++++++++-----
 t/Nagios-Plugin-01.t           |    8 +++++++-
 3 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm
index 697005a..82bbfcb 100644
--- a/lib/Nagios/Plugin.pm
+++ b/lib/Nagios/Plugin.pm
@@ -11,6 +11,7 @@ use Carp;
 use base qw(Class::Accessor::Fast);
 
 Nagios::Plugin->mk_accessors(qw(
+								shortname
 								perfdata 
 								messages 
 								opts
@@ -45,11 +46,8 @@ sub new {
 		},
 	);
 
-	my $shortname = undef;
-	if (exists $args{shortname}) {
-		$shortname = $args{shortname};
-		delete $args{shortname};
-	}
+	my $shortname = Nagios::Plugin::Functions::get_shortname(\%args);
+	delete $args{shortname} if (exists $args{shortname});
 	my $self = {
 		shortname => $shortname,
 		perfdata  => [],           # to be added later
@@ -106,14 +104,6 @@ sub max_state_alt {
     Nagios::Plugin::Functions::max_state_alt(@_);
 }
 
-# Override default shortname accessor to add default
-sub shortname {
-    my $self = shift;
-    $self->{shortname} = shift if @_;
-    return $self->{shortname} || 
-           Nagios::Plugin::Functions::get_shortname();
-}
-
 # top level interface to Nagios::Plugin::Threshold
 sub check_threshold {
 	my $self = shift;
diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm
index 4ff6118..65200ec 100644
--- a/lib/Nagios/Plugin/Functions.pm
+++ b/lib/Nagios/Plugin/Functions.pm
@@ -54,12 +54,15 @@ my $_use_die = 0;
 sub _use_die { @_ ? $_use_die = shift : $_use_die };
 
 sub get_shortname {
-    my %arg = @_;
+    my $arg = shift;
 
-    return $arg{plugin}->shortname if $arg{plugin};
+    my $shortname = undef;
 
-    my $shortname = uc basename($ENV{NAGIOS_PLUGIN} || $0);
-    $shortname =~ s/^CHECK_//;     # Remove any leading CHECK_
+    return $arg->{shortname} if (defined($arg->{shortname}));
+    $shortname = $arg->{plugin} if (defined( $arg->{plugin}));
+
+    $shortname = uc basename($shortname || $ENV{NAGIOS_PLUGIN} || $0);
+    $shortname =~ s/^CHECK_(?:BY_)?//;     # Remove any leading CHECK_[BY_]
     $shortname =~ s/\..*$//;       # Remove any trailing suffix
     return $shortname;
 }
@@ -116,7 +119,8 @@ sub nagios_exit {
     # Setup output
     my $output = "$STATUS_TEXT{$code}";
     $output .= " - $message" if defined $message && $message ne '';
-    my $shortname = get_shortname(plugin => $arg->{plugin});
+    my $shortname = ($arg->{plugin} ? $arg->{plugin}->shortname : undef);
+    $shortname ||= get_shortname(); # Should happen only if funnctions are called directly
     $output = "$shortname $output" if $shortname;
     if ($arg->{plugin}) {
         my $plugin = $arg->{plugin};
diff --git a/t/Nagios-Plugin-01.t b/t/Nagios-Plugin-01.t
index 3ada472..947a704 100644
--- a/t/Nagios-Plugin-01.t
+++ b/t/Nagios-Plugin-01.t
@@ -1,7 +1,7 @@
 # Nagios::Plugin original test cases
 
 use strict;
-use Test::More tests => 13;
+use Test::More tests => 15;
 
 BEGIN { use_ok('Nagios::Plugin') };
 
@@ -23,6 +23,12 @@ is($p->shortname, "NAGIOS-PLUGIN-01", "shortname should default on new");
 $p = Nagios::Plugin->new( shortname => "SIZE", () );
 is($p->shortname, "SIZE", "shortname set correctly on new");
 
+$p = Nagios::Plugin->new( plugin => "check_stuff", () );
+is($p->shortname, "STUFF", "shortname uses plugin name as default");
+
+$p = Nagios::Plugin->new(  shortname => "SIZE", plugin => "check_stuff", () );
+is($p->shortname, "SIZE", "shortname is not overriden by default");
+
 diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE};
 my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" );
 





More information about the Commits mailing list