summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@aei.ca>2010-03-10 06:54:06 (GMT)
committerThomas Guyot-Sionnest <dermoth@aei.ca>2010-03-12 13:09:11 (GMT)
commitc128d293b015291c5e45637d9e3d5b1e1fb36c12 (patch)
tree4b406d48f9d21b59a2aa4600b6d52ac11b7da4fd
parent58a1764446e5a1a30db360ee7a69555d7f2e8f1a (diff)
downloadmonitoring-plugin-perl-c128d293b015291c5e45637d9e3d5b1e1fb36c12.tar.gz
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.
-rw-r--r--lib/Nagios/Plugin.pm16
-rw-r--r--lib/Nagios/Plugin/Functions.pm14
-rw-r--r--t/Nagios-Plugin-01.t8
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;
11use base qw(Class::Accessor::Fast); 11use base qw(Class::Accessor::Fast);
12 12
13Nagios::Plugin->mk_accessors(qw( 13Nagios::Plugin->mk_accessors(qw(
14 shortname
14 perfdata 15 perfdata
15 messages 16 messages
16 opts 17 opts
@@ -45,11 +46,8 @@ sub new {
45 }, 46 },
46 ); 47 );
47 48
48 my $shortname = undef; 49 my $shortname = Nagios::Plugin::Functions::get_shortname(\%args);
49 if (exists $args{shortname}) { 50 delete $args{shortname} if (exists $args{shortname});
50 $shortname = $args{shortname};
51 delete $args{shortname};
52 }
53 my $self = { 51 my $self = {
54 shortname => $shortname, 52 shortname => $shortname,
55 perfdata => [], # to be added later 53 perfdata => [], # to be added later
@@ -106,14 +104,6 @@ sub max_state_alt {
106 Nagios::Plugin::Functions::max_state_alt(@_); 104 Nagios::Plugin::Functions::max_state_alt(@_);
107} 105}
108 106
109# Override default shortname accessor to add default
110sub shortname {
111 my $self = shift;
112 $self->{shortname} = shift if @_;
113 return $self->{shortname} ||
114 Nagios::Plugin::Functions::get_shortname();
115}
116
117# top level interface to Nagios::Plugin::Threshold 107# top level interface to Nagios::Plugin::Threshold
118sub check_threshold { 108sub check_threshold {
119 my $self = shift; 109 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;
54sub _use_die { @_ ? $_use_die = shift : $_use_die }; 54sub _use_die { @_ ? $_use_die = shift : $_use_die };
55 55
56sub get_shortname { 56sub get_shortname {
57 my %arg = @_; 57 my $arg = shift;
58 58
59 return $arg{plugin}->shortname if $arg{plugin}; 59 my $shortname = undef;
60 60
61 my $shortname = uc basename($ENV{NAGIOS_PLUGIN} || $0); 61 return $arg->{shortname} if (defined($arg->{shortname}));
62 $shortname =~ s/^CHECK_//; # Remove any leading CHECK_ 62 $shortname = $arg->{plugin} if (defined( $arg->{plugin}));
63
64 $shortname = uc basename($shortname || $ENV{NAGIOS_PLUGIN} || $0);
65 $shortname =~ s/^CHECK_(?:BY_)?//; # Remove any leading CHECK_[BY_]
63 $shortname =~ s/\..*$//; # Remove any trailing suffix 66 $shortname =~ s/\..*$//; # Remove any trailing suffix
64 return $shortname; 67 return $shortname;
65} 68}
@@ -116,7 +119,8 @@ sub nagios_exit {
116 # Setup output 119 # Setup output
117 my $output = "$STATUS_TEXT{$code}"; 120 my $output = "$STATUS_TEXT{$code}";
118 $output .= " - $message" if defined $message && $message ne ''; 121 $output .= " - $message" if defined $message && $message ne '';
119 my $shortname = get_shortname(plugin => $arg->{plugin}); 122 my $shortname = ($arg->{plugin} ? $arg->{plugin}->shortname : undef);
123 $shortname ||= get_shortname(); # Should happen only if funnctions are called directly
120 $output = "$shortname $output" if $shortname; 124 $output = "$shortname $output" if $shortname;
121 if ($arg->{plugin}) { 125 if ($arg->{plugin}) {
122 my $plugin = $arg->{plugin}; 126 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 @@
1# Nagios::Plugin original test cases 1# Nagios::Plugin original test cases
2 2
3use strict; 3use strict;
4use Test::More tests => 13; 4use Test::More tests => 15;
5 5
6BEGIN { use_ok('Nagios::Plugin') }; 6BEGIN { use_ok('Nagios::Plugin') };
7 7
@@ -23,6 +23,12 @@ is($p->shortname, "NAGIOS-PLUGIN-01", "shortname should default on new");
23$p = Nagios::Plugin->new( shortname => "SIZE", () ); 23$p = Nagios::Plugin->new( shortname => "SIZE", () );
24is($p->shortname, "SIZE", "shortname set correctly on new"); 24is($p->shortname, "SIZE", "shortname set correctly on new");
25 25
26$p = Nagios::Plugin->new( plugin => "check_stuff", () );
27is($p->shortname, "STUFF", "shortname uses plugin name as default");
28
29$p = Nagios::Plugin->new( shortname => "SIZE", plugin => "check_stuff", () );
30is($p->shortname, "SIZE", "shortname is not overriden by default");
31
26diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE}; 32diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE};
27my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" ); 33my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" );
28 34