From c128d293b015291c5e45637d9e3d5b1e1fb36c12 Mon Sep 17 00:00:00 2001 From: Thomas Guyot-Sionnest Date: Wed, 10 Mar 2010 01:54:06 -0500 Subject: 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 +++++++++----- 2 files changed, 12 insertions(+), 18 deletions(-) (limited to 'lib') 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}; -- cgit v1.2.3-74-g34f1