diff options
author | Nathan Vonnahme <n8v@users.sourceforge.net> | 2006-11-10 01:26:16 +0000 |
---|---|---|
committer | Nathan Vonnahme <n8v@users.sourceforge.net> | 2006-11-10 01:26:16 +0000 |
commit | d84da4e2ec8569b8ff15356b6881df307c7333ca (patch) | |
tree | 16dd035b8a5aaaec75f7571eb78e21069f200049 /lib/Nagios | |
parent | 2d5e416592ff2fab507bd4ebfacab3a66a3b8e46 (diff) | |
download | monitoring-plugin-perl-d84da4e2ec8569b8ff15356b6881df307c7333ca.tar.gz |
* exposed Getopt and Threshold functionality from top level Nagios::Plugin
* exchanged Class::Struct for Class::Accessor
* POD is not updated yet.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1536 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib/Nagios')
-rw-r--r-- | lib/Nagios/Plugin.pm | 128 | ||||
-rw-r--r-- | lib/Nagios/Plugin/ExitResult.pm | 2 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Functions.pm | 2 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Getopt.pm | 4 |
4 files changed, 106 insertions, 30 deletions
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index 71d12ed..1fcd28a 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm | |||
@@ -1,30 +1,67 @@ | |||
1 | # This is only because Class::Struct doesn't allow subclasses | ||
2 | # Trick stolen from Class::DBI | ||
3 | ###package Nagios::__::Plugin; | ||
4 | |||
5 | use Class::Struct; | ||
6 | struct "Nagios::__::Plugin" => { | ||
7 | perfdata => '@', | ||
8 | shortname => '$', | ||
9 | messages => '%', | ||
10 | }; | ||
11 | 1 | ||
12 | package Nagios::Plugin; | 2 | package Nagios::Plugin; |
13 | 3 | ||
14 | use Nagios::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES); | 4 | use Nagios::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES); |
5 | use Nagios::Plugin::Getopt; | ||
6 | use Nagios::Plugin::Threshold; | ||
7 | use Params::Validate qw(:all); | ||
15 | 8 | ||
16 | use strict; | 9 | use strict; |
17 | use warnings; | 10 | use warnings; |
18 | 11 | ||
19 | use Carp; | 12 | use Carp; |
13 | use base qw(Class::Accessor::Fast); | ||
14 | |||
15 | # do we need all of these to be accessible? | ||
16 | Nagios::Plugin->mk_accessors(qw( | ||
17 | perfdata | ||
18 | messages | ||
19 | opts | ||
20 | threshold | ||
21 | )); | ||
20 | 22 | ||
21 | use Exporter; | 23 | use Exporter; |
22 | our @ISA = qw(Exporter Nagios::__::Plugin); | 24 | our @ISA = qw(Exporter); |
23 | our @EXPORT = (@STATUS_CODES); | 25 | our @EXPORT = (@STATUS_CODES); |
24 | our @EXPORT_OK = qw(%ERRORS); | 26 | our @EXPORT_OK = qw(%ERRORS); |
25 | 27 | ||
26 | # Remember to update Nagios::Plugin::Functions as well! | 28 | # Remember to update Nagios::Plugin::Functions as well! |
27 | our $VERSION = "0.14"; | 29 | our $VERSION = "0.15"; |
30 | |||
31 | sub new { | ||
32 | my $class = shift; | ||
33 | # my %args = @_; | ||
34 | |||
35 | my %args = validate( @_, { | ||
36 | shortname => 0, | ||
37 | usage => 1, | ||
38 | version => 0, | ||
39 | url => 0, | ||
40 | plugin => 0, | ||
41 | blurb => 0, | ||
42 | extra => 0, | ||
43 | license => 0, | ||
44 | timeout => 0 }, | ||
45 | ); | ||
46 | my $shortname = undef; | ||
47 | if (exists $args{shortname}) { | ||
48 | $shortname = $args{shortname}; | ||
49 | delete $args{shortname}; | ||
50 | } | ||
51 | my $self = { | ||
52 | shortname => $shortname, | ||
53 | perfdata => [], # to be added later | ||
54 | messages => { | ||
55 | warning => [], | ||
56 | critical => [], | ||
57 | ok => [] | ||
58 | }, | ||
59 | opts => new Nagios::Plugin::Getopt(%args), | ||
60 | threshold => undef, # defined later | ||
61 | }; | ||
62 | bless $self, $class; | ||
63 | return $self; | ||
64 | } | ||
28 | 65 | ||
29 | sub add_perfdata { | 66 | sub add_perfdata { |
30 | my ($self, %args) = @_; | 67 | my ($self, %args) = @_; |
@@ -38,9 +75,9 @@ sub all_perfoutput { | |||
38 | } | 75 | } |
39 | 76 | ||
40 | sub set_thresholds { | 77 | sub set_thresholds { |
41 | shift; | 78 | my $self = shift; |
42 | require Nagios::Plugin::Threshold; | 79 | require Nagios::Plugin::Threshold; |
43 | Nagios::Plugin::Threshold->set_thresholds(@_); | 80 | return $self->threshold( Nagios::Plugin::Threshold->set_thresholds(@_)); |
44 | } | 81 | } |
45 | 82 | ||
46 | # NP::Functions wrappers | 83 | # NP::Functions wrappers |
@@ -56,14 +93,54 @@ sub die { | |||
56 | my $self = shift; | 93 | my $self = shift; |
57 | Nagios::Plugin::Functions::nagios_die(@_, { plugin => $self }); | 94 | Nagios::Plugin::Functions::nagios_die(@_, { plugin => $self }); |
58 | } | 95 | } |
96 | |||
59 | # Override default shortname accessor to add default | 97 | # Override default shortname accessor to add default |
60 | sub shortname { | 98 | sub shortname { |
61 | my $self = shift; | 99 | my $self = shift; |
62 | $self->{'Nagios::__::Plugin::shortname'} = shift if @_; | 100 | $self->{shortname} = shift if @_; |
63 | return $self->{'Nagios::__::Plugin::shortname'} || | 101 | return $self->{shortname} || |
64 | Nagios::Plugin::Functions::get_shortname(); | 102 | Nagios::Plugin::Functions::get_shortname(); |
65 | } | 103 | } |
66 | 104 | ||
105 | # top level interface to Nagios::Plugin::Threshold | ||
106 | sub check_threshold { | ||
107 | my $self = shift; | ||
108 | |||
109 | my %args; | ||
110 | |||
111 | if ( $#_ == 0 && ! ref $_[0]) { # one positional param | ||
112 | %args = (check => shift); | ||
113 | } | ||
114 | else { | ||
115 | %args = validate ( @_, { # named params | ||
116 | check => 1, | ||
117 | warning => 0, | ||
118 | critical => 0, | ||
119 | } ); | ||
120 | } | ||
121 | |||
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 | |||
129 | |||
130 | return $self->threshold->get_status($args{check}); | ||
131 | } | ||
132 | |||
133 | # top level interface to my Nagios::Plugin::Getopt object | ||
134 | sub arg { | ||
135 | my $self = shift; | ||
136 | $self->opts->arg(@_); | ||
137 | } | ||
138 | sub getopts { | ||
139 | my $self = shift; | ||
140 | $self->opts->getopts(@_); | ||
141 | } | ||
142 | |||
143 | |||
67 | # ------------------------------------------------------------------------- | 144 | # ------------------------------------------------------------------------- |
68 | # NP::Functions::check_messages helpers and wrappers | 145 | # NP::Functions::check_messages helpers and wrappers |
69 | 146 | ||
@@ -80,8 +157,8 @@ sub add_message { | |||
80 | croak "Error code '$code' not supported by add_message" | 157 | croak "Error code '$code' not supported by add_message" |
81 | if $code eq 'unknown' || $code eq 'dependent'; | 158 | if $code eq 'unknown' || $code eq 'dependent'; |
82 | 159 | ||
83 | $self->messages($code, []) unless $self->messages($code); | 160 | $self->messages($code, []) unless $self->messages->{$code}; |
84 | push @{$self->messages($code)}, @messages; | 161 | push @{$self->messages->{$code}}, @messages; |
85 | } | 162 | } |
86 | 163 | ||
87 | sub check_messages { | 164 | sub check_messages { |
@@ -90,7 +167,7 @@ sub check_messages { | |||
90 | 167 | ||
91 | # Add object messages to any passed in as args | 168 | # Add object messages to any passed in as args |
92 | for my $code (qw(critical warning ok)) { | 169 | for my $code (qw(critical warning ok)) { |
93 | my $messages = $self->messages($code) || []; | 170 | my $messages = $self->messages->{$code} || []; |
94 | if ($args{$code}) { | 171 | if ($args{$code}) { |
95 | unless (ref $args{$code} eq 'ARRAY') { | 172 | unless (ref $args{$code} eq 'ARRAY') { |
96 | if ($code eq 'ok') { | 173 | if ($code eq 'ok') { |
@@ -123,9 +200,10 @@ __END__ | |||
123 | Nagios::Plugin - a family of perl modules to streamline writing Nagios | 200 | Nagios::Plugin - a family of perl modules to streamline writing Nagios |
124 | plugins | 201 | plugins |
125 | 202 | ||
126 | |||
127 | =head1 SYNOPSIS | 203 | =head1 SYNOPSIS |
128 | 204 | ||
205 | # TODO NJV -- make this more like check_stuff. | ||
206 | |||
129 | # Constants OK, WARNING, CRITICAL, and UNKNOWN are exported by default | 207 | # Constants OK, WARNING, CRITICAL, and UNKNOWN are exported by default |
130 | # See also Nagios::Plugin::Functions for a functional interface | 208 | # See also Nagios::Plugin::Functions for a functional interface |
131 | use Nagios::Plugin; | 209 | use Nagios::Plugin; |
@@ -188,9 +266,7 @@ plugins | |||
188 | # | size=36kB;10:25;25: time=... | 266 | # | size=36kB;10:25;25: time=... |
189 | 267 | ||
190 | # Option handling methods (NOT YET IMPLEMENTED - use | 268 | # Option handling methods (NOT YET IMPLEMENTED - use |
191 | # Nagios::Plugin::Getopt for now) | 269 | # Nagios::Plugin::Getopt for |
192 | |||
193 | |||
194 | 270 | ||
195 | =head1 DESCRIPTION | 271 | =head1 DESCRIPTION |
196 | 272 | ||
@@ -280,6 +356,7 @@ NOT YET IMPLEMENTED - use Nagios::Plugin::Threshold directly for now. | |||
280 | 356 | ||
281 | =over 4 | 357 | =over 4 |
282 | 358 | ||
359 | =item check_threshold( $value ) | ||
283 | =item check_threshold( check => $value, warning => $warn, critical => $crit ) | 360 | =item check_threshold( check => $value, warning => $warn, critical => $crit ) |
284 | 361 | ||
285 | =back | 362 | =back |
@@ -382,6 +459,8 @@ section of the Nagios Plugin guidelines | |||
382 | 459 | ||
383 | =head2 OPTION HANDLING METHODS | 460 | =head2 OPTION HANDLING METHODS |
384 | 461 | ||
462 | TODO | ||
463 | |||
385 | NOT YET IMPLEMENTED - use Nagios::Plugin::Getopt directly for now. | 464 | NOT YET IMPLEMENTED - use Nagios::Plugin::Getopt directly for now. |
386 | 465 | ||
387 | 466 | ||
@@ -426,9 +505,6 @@ http://nagiosplug.sourceforge.net. | |||
426 | 505 | ||
427 | Originally by Ton Voon, E<lt>ton.voon@altinity.comE<gt>. | 506 | Originally by Ton Voon, E<lt>ton.voon@altinity.comE<gt>. |
428 | 507 | ||
429 | Nathan Vonnahme added extra tests and subsequent fixes. | ||
430 | |||
431 | |||
432 | =head1 COPYRIGHT AND LICENSE | 508 | =head1 COPYRIGHT AND LICENSE |
433 | 509 | ||
434 | Copyright (C) 2006 by Nagios Plugin Development Team | 510 | Copyright (C) 2006 by Nagios Plugin Development Team |
diff --git a/lib/Nagios/Plugin/ExitResult.pm b/lib/Nagios/Plugin/ExitResult.pm index 191c92a..b161e9e 100644 --- a/lib/Nagios/Plugin/ExitResult.pm +++ b/lib/Nagios/Plugin/ExitResult.pm | |||
@@ -39,7 +39,7 @@ return codes when testing. | |||
39 | $e = nagios_exit( CRITICAL, 'aiiii ...' ); | 39 | $e = nagios_exit( CRITICAL, 'aiiii ...' ); |
40 | print $e->message; | 40 | print $e->message; |
41 | print $e->return_code; | 41 | print $e->return_code; |
42 | 42 | ||
43 | # NP::ExitResult also stringifies to the message output | 43 | # NP::ExitResult also stringifies to the message output |
44 | like(nagios_exit( WARNING, 'foobar'), qr/^foo/, 'matches!'); | 44 | like(nagios_exit( WARNING, 'foobar'), qr/^foo/, 'matches!'); |
45 | 45 | ||
diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm index 5772c97..41ec27a 100644 --- a/lib/Nagios/Plugin/Functions.pm +++ b/lib/Nagios/Plugin/Functions.pm | |||
@@ -11,7 +11,7 @@ use File::Basename; | |||
11 | use Params::Validate qw(validate :types); | 11 | use Params::Validate qw(validate :types); |
12 | 12 | ||
13 | # Remember to update Nagios::Plugins as well | 13 | # Remember to update Nagios::Plugins as well |
14 | our $VERSION = "0.14"; | 14 | our $VERSION = "0.15"; |
15 | 15 | ||
16 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); | 16 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); |
17 | 17 | ||
diff --git a/lib/Nagios/Plugin/Getopt.pm b/lib/Nagios/Plugin/Getopt.pm index 8a9fc1c..bbf1fc9 100644 --- a/lib/Nagios/Plugin/Getopt.pm +++ b/lib/Nagios/Plugin/Getopt.pm | |||
@@ -306,7 +306,7 @@ This documentation applies to version 0.01 of Nagios::Plugin::Getopt. | |||
306 | 306 | ||
307 | use Nagios::Plugin::Getopt; | 307 | use Nagios::Plugin::Getopt; |
308 | 308 | ||
309 | # Instantiate object (usage and version are mandatory) | 309 | # Instantiate object (usage is mandatory) |
310 | $ng = Nagios::Plugin::Getopt->new( | 310 | $ng = Nagios::Plugin::Getopt->new( |
311 | usage => "Usage: %s -H <host> -w <warning_threshold> | 311 | usage => "Usage: %s -H <host> -w <warning_threshold> |
312 | -c <critical threshold>", | 312 | -c <critical threshold>", |
@@ -352,7 +352,7 @@ additional arguments to be easily defined. | |||
352 | 352 | ||
353 | =head2 CONSTRUCTOR | 353 | =head2 CONSTRUCTOR |
354 | 354 | ||
355 | # Instantiate object (usage and version are mandatory) | 355 | # Instantiate object (usage is mandatory) |
356 | $ng = Nagios::Plugin::Getopt->new( | 356 | $ng = Nagios::Plugin::Getopt->new( |
357 | usage => 'Usage: %s --hello', | 357 | usage => 'Usage: %s --hello', |
358 | version => '0.01', | 358 | version => '0.01', |