diff options
author | Nathan Vonnahme <n8v@users.sourceforge.net> | 2006-11-15 02:11:10 +0000 |
---|---|---|
committer | Nathan Vonnahme <n8v@users.sourceforge.net> | 2006-11-15 02:11:10 +0000 |
commit | 22509ac75b3ae04f35b22c87bbd85c643bb1db2b (patch) | |
tree | 8423e25fd60ca9c275aa890817b96ae9d50d168f /lib | |
parent | 79b36b4d71afeb016a3b4764b6b484754323b011 (diff) | |
download | monitoring-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
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Nagios/Plugin.pm | 90 |
1 files changed, 60 insertions, 30 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 @@ | |||
2 | package Nagios::Plugin; | 2 | package Nagios::Plugin; |
3 | 3 | ||
4 | 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); | 5 | use Params::Validate qw(:all); |
8 | 6 | ||
9 | use strict; | 7 | use strict; |
@@ -12,7 +10,6 @@ use warnings; | |||
12 | use Carp; | 10 | use Carp; |
13 | use base qw(Class::Accessor::Fast); | 11 | use base qw(Class::Accessor::Fast); |
14 | 12 | ||
15 | # do we need all of these to be accessible? | ||
16 | Nagios::Plugin->mk_accessors(qw( | 13 | Nagios::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 |
134 | sub arg { | 155 | sub arg { |
135 | my $self = shift; | 156 | my $self = shift; |
136 | $self->opts->arg(@_); | 157 | $self->opts->arg(@_) if $self->_check_for_opts; |
137 | } | 158 | } |
138 | sub getopts { | 159 | sub getopts { |
139 | my $self = shift; | 160 | my $self = shift; |
140 | $self->opts->getopts(@_); | 161 | $self->opts->getopts(@_) if $self->_check_for_opts; |
162 | } | ||
163 | |||
164 | sub _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 | ||