summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Vonnahme <n8v@users.sourceforge.net>2006-11-15 02:11:10 +0000
committerNathan Vonnahme <n8v@users.sourceforge.net>2006-11-15 02:11:10 +0000
commit22509ac75b3ae04f35b22c87bbd85c643bb1db2b (patch)
tree8423e25fd60ca9c275aa890817b96ae9d50d168f
parent79b36b4d71afeb016a3b4764b6b484754323b011 (diff)
downloadmonitoring-plugin-perl-22509ac75b3ae04f35b22c87bbd85c643bb1db2b.tar.gz
made 'usage' unmandatory in N::P::new().
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1538 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r--lib/Nagios/Plugin.pm90
-rw-r--r--t/Nagios-Plugin-01.t6
-rw-r--r--t/Nagios-Plugin-02.t4
-rw-r--r--t/Nagios-Plugin-03.t26
4 files changed, 78 insertions, 48 deletions
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm
index 1fcd28a..f1b1807 100644
--- a/lib/Nagios/Plugin.pm
+++ b/lib/Nagios/Plugin.pm
@@ -2,8 +2,6 @@
2package Nagios::Plugin; 2package Nagios::Plugin;
3 3
4use Nagios::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES); 4use Nagios::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES);
5use Nagios::Plugin::Getopt;
6use Nagios::Plugin::Threshold;
7use Params::Validate qw(:all); 5use Params::Validate qw(:all);
8 6
9use strict; 7use strict;
@@ -12,7 +10,6 @@ use warnings;
12use Carp; 10use Carp;
13use base qw(Class::Accessor::Fast); 11use base qw(Class::Accessor::Fast);
14 12
15# do we need all of these to be accessible?
16Nagios::Plugin->mk_accessors(qw( 13Nagios::Plugin->mk_accessors(qw(
17 perfdata 14 perfdata
18 messages 15 messages
@@ -32,34 +29,41 @@ sub new {
32 my $class = shift; 29 my $class = shift;
33# my %args = @_; 30# my %args = @_;
34 31
35 my %args = validate( @_, { 32 my %args = validate( @_,
36 shortname => 0, 33 {
37 usage => 1, 34 shortname => 0,
38 version => 0, 35 usage => 0,
39 url => 0, 36 version => 0,
40 plugin => 0, 37 url => 0,
41 blurb => 0, 38 plugin => 0,
42 extra => 0, 39 blurb => 0,
43 license => 0, 40 extra => 0,
44 timeout => 0 }, 41 license => 0,
42 timeout => 0
43 },
45 ); 44 );
45
46 my $shortname = undef; 46 my $shortname = undef;
47 if (exists $args{shortname}) { 47 if (exists $args{shortname}) {
48 $shortname = $args{shortname}; 48 $shortname = $args{shortname};
49 delete $args{shortname}; 49 delete $args{shortname};
50 } 50 }
51 my $self = { 51 my $self = {
52 shortname => $shortname, 52 shortname => $shortname,
53 perfdata => [], # to be added later 53 perfdata => [], # to be added later
54 messages => { 54 messages => {
55 warning => [], 55 warning => [],
56 critical => [], 56 critical => [],
57 ok => [] 57 ok => []
58 }, 58 },
59 opts => new Nagios::Plugin::Getopt(%args), 59 opts => undef, # see below
60 threshold => undef, # defined later 60 threshold => undef, # defined later
61 }; 61 };
62 bless $self, $class; 62 bless $self, $class;
63 if (exists $args{usage}) {
64 require Nagios::Plugin::Getopt;
65 $self->opts( new Nagios::Plugin::Getopt(%args) );
66 }
63 return $self; 67 return $self;
64} 68}
65 69
@@ -119,28 +123,54 @@ sub check_threshold {
119 } ); 123 } );
120 } 124 }
121 125
122 if (! $self->threshold || exists $args{warning} || exists $args{critical}) {
123 $self->set_thresholds(
124 warning => $args{warning} || $self->opts->warning ,
125 critical => $args{critical} || $self->opts->critical ,
126 );
127 }
128 126
127 # in order of preference, get warning and critical from
128 # 1. explicit arguments to check_threshold
129 # 2. previously explicitly set threshold object
130 # 3. implicit options from Getopts object
129 131
132 if ( exists $args{warning} || exists $args{critical} ) {
133 $self->set_thresholds(
134 warning => $args{warning},
135 critical => $args{critical},
136 );
137 }
138 elsif ( defined $self->threshold ) {
139 # noop
140 }
141 elsif ( defined $self->opts ) {
142 $self->set_thresholds(
143 warning => $self->opts->warning,
144 critical => $self->opts->critical,
145 );
146 }
147 else {
148 return UNKNOWN;
149 }
150
130 return $self->threshold->get_status($args{check}); 151 return $self->threshold->get_status($args{check});
131} 152}
132 153
133# top level interface to my Nagios::Plugin::Getopt object 154# top level interface to my Nagios::Plugin::Getopt object
134sub arg { 155sub arg {
135 my $self = shift; 156 my $self = shift;
136 $self->opts->arg(@_); 157 $self->opts->arg(@_) if $self->_check_for_opts;
137} 158}
138sub getopts { 159sub getopts {
139 my $self = shift; 160 my $self = shift;
140 $self->opts->getopts(@_); 161 $self->opts->getopts(@_) if $self->_check_for_opts;
162}
163
164sub _check_for_opts {
165 my $self = shift;
166 croak
167 "You have to supply a 'usage' param to Nagios::Plugin::new() if you want to use Getopts from your Nagios::Plugin object."
168 unless ref $self->opts() eq 'Nagios::Plugin::Getopt';
169 return $self;
141} 170}
142 171
143 172
173
144# ------------------------------------------------------------------------- 174# -------------------------------------------------------------------------
145# NP::Functions::check_messages helpers and wrappers 175# NP::Functions::check_messages helpers and wrappers
146 176
diff --git a/t/Nagios-Plugin-01.t b/t/Nagios-Plugin-01.t
index a73fce4..db12c35 100644
--- a/t/Nagios-Plugin-01.t
+++ b/t/Nagios-Plugin-01.t
@@ -11,16 +11,16 @@ Nagios::Plugin::Functions::_fake_exit(1);
11diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n" 11diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n"
12 if $ENV{TEST_VERBOSE}; 12 if $ENV{TEST_VERBOSE};
13 13
14my $p = Nagios::Plugin->new (usage => "dummy usage"); 14my $p = Nagios::Plugin->new();
15isa_ok( $p, "Nagios::Plugin"); 15isa_ok( $p, "Nagios::Plugin");
16 16
17$p->shortname("PAGESIZE"); 17$p->shortname("PAGESIZE");
18is($p->shortname, "PAGESIZE", "shortname explicitly set correctly"); 18is($p->shortname, "PAGESIZE", "shortname explicitly set correctly");
19 19
20$p = Nagios::Plugin->new (usage => "dummy usage"); 20$p = Nagios::Plugin->new();
21is($p->shortname, "NAGIOS-PLUGIN-01", "shortname should default on new"); 21is($p->shortname, "NAGIOS-PLUGIN-01", "shortname should default on new");
22 22
23$p = Nagios::Plugin->new( shortname => "SIZE", usage => "dummy usage" ); 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
26diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE}; 26diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE};
diff --git a/t/Nagios-Plugin-02.t b/t/Nagios-Plugin-02.t
index 15ae3d6..f64fb6f 100644
--- a/t/Nagios-Plugin-02.t
+++ b/t/Nagios-Plugin-02.t
@@ -16,7 +16,7 @@ is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}");
16is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}"); 16is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}");
17 17
18my $plugin = 'TEST_PLUGIN'; 18my $plugin = 'TEST_PLUGIN';
19my $np = Nagios::Plugin->new( shortname => $plugin, usage => "dummy usage" ); 19my $np = Nagios::Plugin->new( shortname => $plugin );
20is($np->shortname, $plugin, "shortname() is $plugin"); 20is($np->shortname, $plugin, "shortname() is $plugin");
21 21
22# Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg ) 22# Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg )
@@ -151,7 +151,7 @@ for (@ok) {
151# shortname testing 151# shortname testing
152SKIP: { 152SKIP: {
153 skip "requires File::Basename", 2 unless eval { require File::Basename }; 153 skip "requires File::Basename", 2 unless eval { require File::Basename };
154 $np = Nagios::Plugin->new (usage => "dummy usage", version => "1"); 154 $np = Nagios::Plugin->new( version => "1");
155 $plugin = uc File::Basename::basename($0); 155 $plugin = uc File::Basename::basename($0);
156 $plugin =~ s/\..*$//; 156 $plugin =~ s/\..*$//;
157 is($np->shortname, $plugin, "shortname() is '$plugin'"); 157 is($np->shortname, $plugin, "shortname() is '$plugin'");
diff --git a/t/Nagios-Plugin-03.t b/t/Nagios-Plugin-03.t
index 0b7b8af..bc4f5e3 100644
--- a/t/Nagios-Plugin-03.t
+++ b/t/Nagios-Plugin-03.t
@@ -10,7 +10,7 @@ BEGIN {
10Nagios::Plugin::Functions::_fake_exit(1); 10Nagios::Plugin::Functions::_fake_exit(1);
11 11
12my $plugin = 'NP_CHECK_MESSAGES_03'; 12my $plugin = 'NP_CHECK_MESSAGES_03';
13my $np = Nagios::Plugin->new( shortname => $plugin, usage => "dummy usage" ); 13my $np = Nagios::Plugin->new( shortname => $plugin, () );
14is($np->shortname, $plugin, "shortname() is $plugin"); 14is($np->shortname, $plugin, "shortname() is $plugin");
15 15
16my ($code, $message); 16my ($code, $message);
@@ -172,33 +172,33 @@ is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $mess
172# add_messages 172# add_messages
173 173
174# Constant codes 174# Constant codes
175$np = Nagios::Plugin->new (usage => "dummy usage"); 175$np = Nagios::Plugin->new();
176$np->add_message( CRITICAL, "A B C" ); 176$np->add_message( CRITICAL, "A B C" );
177$np->add_message( WARNING, "D E F" ); 177$np->add_message( WARNING, "D E F" );
178($code, $message) = $np->check_messages(); 178($code, $message) = $np->check_messages();
179is($code, CRITICAL, "(CRITICAL, WARNING) code is $STATUS_TEXT{$code}"); 179is($code, CRITICAL, "(CRITICAL, WARNING) code is $STATUS_TEXT{$code}");
180is($message, $messages{critical}, "(CRITICAL, WARNING) message is $message"); 180is($message, $messages{critical}, "(CRITICAL, WARNING) message is $message");
181 181
182$np = Nagios::Plugin->new (usage => "dummy usage"); 182$np = Nagios::Plugin->new();
183$np->add_message( CRITICAL, "A B C" ); 183$np->add_message( CRITICAL, "A B C" );
184($code, $message) = $np->check_messages(); 184($code, $message) = $np->check_messages();
185is($code, CRITICAL, "(CRITICAL) code is $STATUS_TEXT{$code}"); 185is($code, CRITICAL, "(CRITICAL) code is $STATUS_TEXT{$code}");
186is($message, $messages{critical}, "(CRITICAL) message is $message"); 186is($message, $messages{critical}, "(CRITICAL) message is $message");
187 187
188$np = Nagios::Plugin->new (usage => "dummy usage"); 188$np = Nagios::Plugin->new();
189$np->add_message( WARNING, "D E F" ); 189$np->add_message( WARNING, "D E F" );
190($code, $message) = $np->check_messages(); 190($code, $message) = $np->check_messages();
191is($code, WARNING, "(WARNING) code is $STATUS_TEXT{$code}"); 191is($code, WARNING, "(WARNING) code is $STATUS_TEXT{$code}");
192is($message, $messages{warning}, "(WARNING) message is $message"); 192is($message, $messages{warning}, "(WARNING) message is $message");
193 193
194$np = Nagios::Plugin->new (usage => "dummy usage"); 194$np = Nagios::Plugin->new();
195$np->add_message( WARNING, "D E F" ); 195$np->add_message( WARNING, "D E F" );
196$np->add_message( OK, "G H I" ); 196$np->add_message( OK, "G H I" );
197($code, $message) = $np->check_messages(); 197($code, $message) = $np->check_messages();
198is($code, WARNING, "(WARNING, OK) code is $STATUS_TEXT{$code}"); 198is($code, WARNING, "(WARNING, OK) code is $STATUS_TEXT{$code}");
199is($message, $messages{warning}, "(WARNING, OK) message is $message"); 199is($message, $messages{warning}, "(WARNING, OK) message is $message");
200 200
201$np = Nagios::Plugin->new (usage => "dummy usage"); 201$np = Nagios::Plugin->new();
202$np->add_message( OK, "G H I" ); 202$np->add_message( OK, "G H I" );
203($code, $message) = $np->check_messages(); 203($code, $message) = $np->check_messages();
204is($code, OK, "(OK) code is $STATUS_TEXT{$code}"); 204is($code, OK, "(OK) code is $STATUS_TEXT{$code}");
@@ -206,33 +206,33 @@ is($message, $messages{ok}, "(OK) message is $message");
206 206
207 207
208# String codes 208# String codes
209$np = Nagios::Plugin->new (usage => "dummy usage"); 209$np = Nagios::Plugin->new();
210$np->add_message( critical => "A B C" ); 210$np->add_message( critical => "A B C" );
211$np->add_message( warning => "D E F" ); 211$np->add_message( warning => "D E F" );
212($code, $message) = $np->check_messages(); 212($code, $message) = $np->check_messages();
213is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); 213is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}");
214is($message, $messages{critical}, "(critical, warning) message is $message"); 214is($message, $messages{critical}, "(critical, warning) message is $message");
215 215
216$np = Nagios::Plugin->new (usage => "dummy usage"); 216$np = Nagios::Plugin->new();
217$np->add_message( critical => "A B C" ); 217$np->add_message( critical => "A B C" );
218($code, $message) = $np->check_messages(); 218($code, $message) = $np->check_messages();
219is($code, CRITICAL, "(critical) code is $STATUS_TEXT{$code}"); 219is($code, CRITICAL, "(critical) code is $STATUS_TEXT{$code}");
220is($message, $messages{critical}, "(critical) message is $message"); 220is($message, $messages{critical}, "(critical) message is $message");
221 221
222$np = Nagios::Plugin->new (usage => "dummy usage"); 222$np = Nagios::Plugin->new();
223$np->add_message( warning => "D E F" ); 223$np->add_message( warning => "D E F" );
224($code, $message) = $np->check_messages(); 224($code, $message) = $np->check_messages();
225is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); 225is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}");
226is($message, $messages{warning}, "(warning) message is $message"); 226is($message, $messages{warning}, "(warning) message is $message");
227 227
228$np = Nagios::Plugin->new (usage => "dummy usage"); 228$np = Nagios::Plugin->new();
229$np->add_message( warning => "D E F" ); 229$np->add_message( warning => "D E F" );
230$np->add_message( ok => "G H I" ); 230$np->add_message( ok => "G H I" );
231($code, $message) = $np->check_messages(); 231($code, $message) = $np->check_messages();
232is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); 232is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}");
233is($message, $messages{warning}, "(warning, ok) message is $message"); 233is($message, $messages{warning}, "(warning, ok) message is $message");
234 234
235$np = Nagios::Plugin->new (usage => "dummy usage"); 235$np = Nagios::Plugin->new();
236$np->add_message( ok => "G H I" ); 236$np->add_message( ok => "G H I" );
237($code, $message) = $np->check_messages(); 237($code, $message) = $np->check_messages();
238is($code, OK, "(ok) code is $STATUS_TEXT{$code}"); 238is($code, OK, "(ok) code is $STATUS_TEXT{$code}");
@@ -240,7 +240,7 @@ is($message, $messages{ok}, "(ok) message is $message");
240 240
241 241
242# No add_message 242# No add_message
243$np = Nagios::Plugin->new (usage => "dummy usage"); 243$np = Nagios::Plugin->new();
244($code, $message) = $np->check_messages(); 244($code, $message) = $np->check_messages();
245is($code, OK, "() code is $STATUS_TEXT{$code}"); 245is($code, OK, "() code is $STATUS_TEXT{$code}");
246is($message, '', "() message is ''"); 246is($message, '', "() message is ''");
@@ -250,7 +250,7 @@ is($message, '', "() message is ''");
250# Error conditions 250# Error conditions
251 251
252# add_message errors 252# add_message errors
253$np = Nagios::Plugin->new (usage => "dummy usage"); 253$np = Nagios::Plugin->new();
254ok(! defined eval { $np->add_message( foobar => 'hi mum' ) }, 254ok(! defined eval { $np->add_message( foobar => 'hi mum' ) },
255 'add_message dies on invalid code'); 255 'add_message dies on invalid code');
256ok(! defined eval { $np->add_message( OKAY => 'hi mum' ) }, 256ok(! defined eval { $np->add_message( OKAY => 'hi mum' ) },