diff options
author | Gavin Carr <gonzai@users.sourceforge.net> | 2007-03-16 11:25:15 +0000 |
---|---|---|
committer | Gavin Carr <gonzai@users.sourceforge.net> | 2007-03-16 11:25:15 +0000 |
commit | c6cbf050974c8f6642fa1d7bde309710b66cbfa0 (patch) | |
tree | 0eb0ea62814b0623fb619c2af3287a585de48c2c /lib/Nagios/Plugin/Threshold.pm | |
parent | e5109f99c9657a1a8e9fb32b19254a417e2ccd65 (diff) | |
download | monitoring-plugin-perl-c6cbf050974c8f6642fa1d7bde309710b66cbfa0.tar.gz |
Cleanups, mostly to N::P::Range/Threshold/Performance.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1641 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib/Nagios/Plugin/Threshold.pm')
-rw-r--r-- | lib/Nagios/Plugin/Threshold.pm | 90 |
1 files changed, 55 insertions, 35 deletions
diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm index 0c4805a..ad58134 100644 --- a/lib/Nagios/Plugin/Threshold.pm +++ b/lib/Nagios/Plugin/Threshold.pm | |||
@@ -5,55 +5,75 @@ use 5.006; | |||
5 | use strict; | 5 | use strict; |
6 | use warnings; | 6 | use warnings; |
7 | 7 | ||
8 | use base qw(Class::Accessor::Fast); | ||
9 | __PACKAGE__->mk_accessors(qw(warning critical)); | ||
10 | |||
8 | use Nagios::Plugin::Range; | 11 | use Nagios::Plugin::Range; |
9 | use Nagios::Plugin::Functions qw(:codes nagios_die); | 12 | use Nagios::Plugin::Functions qw(:codes nagios_die); |
10 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; | 13 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; |
11 | 14 | ||
12 | use Class::Struct; | 15 | sub get_status |
13 | struct "Nagios::Plugin::Threshold" => { | 16 | { |
14 | warning => 'Nagios::Plugin::Range', | ||
15 | critical => 'Nagios::Plugin::Range', | ||
16 | }; | ||
17 | |||
18 | sub set_thresholds { | ||
19 | my ($class, %args) = @_; | ||
20 | my $t = $class->new( warning => Nagios::Plugin::Range->new, critical => Nagios::Plugin::Range->new ); | ||
21 | if (defined $args{warning}) { | ||
22 | my $r = Nagios::Plugin::Range->parse_range_string($args{warning}); | ||
23 | if (defined $r) { | ||
24 | $t->warning($r); | ||
25 | } else { | ||
26 | nagios_die( "Warning range incorrect: '$args{warning}'" ); | ||
27 | } | ||
28 | } | ||
29 | if (defined $args{critical}) { | ||
30 | my $r = Nagios::Plugin::Range->parse_range_string($args{critical}); | ||
31 | if (defined $r) { | ||
32 | $t->critical($r); | ||
33 | } else { | ||
34 | nagios_die( "Critical range incorrect: '$args{critical}'" ); | ||
35 | } | ||
36 | } | ||
37 | return $t; | ||
38 | } | ||
39 | |||
40 | sub get_status { | ||
41 | my ($self, $value) = @_; | 17 | my ($self, $value) = @_; |
42 | 18 | ||
43 | if ($self->critical->is_set) { | 19 | if ($self->critical->is_set) { |
44 | if ($self->critical->check_range($value) == 1) { | 20 | return CRITICAL if $self->critical->check_range($value); |
45 | return CRITICAL; | ||
46 | } | ||
47 | } | 21 | } |
48 | if ($self->warning->is_set) { | 22 | if ($self->warning->is_set) { |
49 | if ($self->warning->check_range($value) == 1) { | 23 | return WARNING if $self->warning->check_range($value); |
50 | return WARNING; | ||
51 | } | ||
52 | } | 24 | } |
53 | return OK; | 25 | return OK; |
54 | } | 26 | } |
27 | |||
28 | sub _inflate | ||
29 | { | ||
30 | my ($self, $value, $key) = @_; | ||
31 | |||
32 | # Return an undefined range if $value is undef | ||
33 | return Nagios::Plugin::Range->new if ! defined $value; | ||
34 | |||
35 | # For refs, check isa N::P::Range | ||
36 | if (ref $value) { | ||
37 | nagios_die("Invalid $key object: type " . ref $value) | ||
38 | unless $value->isa("Nagios::Plugin::Range"); | ||
39 | return $value; | ||
40 | } | ||
41 | |||
42 | # Otherwise parse $value | ||
43 | my $range = Nagios::Plugin::Range->parse_range_string($value) | ||
44 | or nagios_die("Cannot parse $key range: '$value'"); | ||
45 | return $range; | ||
46 | } | ||
47 | |||
48 | sub set_thresholds | ||
49 | { | ||
50 | my ($self, %arg) = @_; | ||
51 | |||
52 | # Equals new() as a class method | ||
53 | return $self->new(%arg) unless ref $self; | ||
54 | |||
55 | # On an object, just acts as special mutator | ||
56 | $self->set($_, $arg{$_}) foreach qw(warning critical); | ||
57 | } | ||
58 | |||
59 | sub set | ||
60 | { | ||
61 | my $self = shift; | ||
62 | my ($key, $value) = @_; | ||
63 | $self->SUPER::set($key, $self->_inflate($value, $key)); | ||
64 | } | ||
55 | 65 | ||
66 | # Constructor - inflate scalars to N::P::Range objects | ||
67 | sub new | ||
68 | { | ||
69 | my ($self, %arg) = @_; | ||
70 | $self->SUPER::new({ | ||
71 | map { $_ => $self->_inflate($arg{$_}, $_) } qw(warning critical) | ||
72 | }); | ||
73 | } | ||
74 | |||
56 | 1; | 75 | 1; |
76 | |||
57 | __END__ | 77 | __END__ |
58 | 78 | ||
59 | =head1 NAME | 79 | =head1 NAME |