[Nagios-Plugin] shortname enhancement

"Thomas Guyot-Sionnest" <[email protected]>
Newsgroups gmane.network.nagios.plugins.cvs
Message-ID <[email protected]>
 Module: Nagios-Plugin
 Branch: master
 Commit: c128d293b015291c5e45637d9e3d5b1e1fb36c12
 Author: Thomas Guyot-Sionnest <[email protected]>
   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" );
 


------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.