summaryrefslogtreecommitdiffstats
path: root/lib/Nagios/Plugin/Threshold.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Nagios/Plugin/Threshold.pm')
-rw-r--r--lib/Nagios/Plugin/Threshold.pm90
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;
5use strict; 5use strict;
6use warnings; 6use warnings;
7 7
8use base qw(Class::Accessor::Fast);
9__PACKAGE__->mk_accessors(qw(warning critical));
10
8use Nagios::Plugin::Range; 11use Nagios::Plugin::Range;
9use Nagios::Plugin::Functions qw(:codes nagios_die); 12use Nagios::Plugin::Functions qw(:codes nagios_die);
10our ($VERSION) = $Nagios::Plugin::Functions::VERSION; 13our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
11 14
12use Class::Struct; 15sub get_status
13struct "Nagios::Plugin::Threshold" => { 16{
14 warning => 'Nagios::Plugin::Range',
15 critical => 'Nagios::Plugin::Range',
16 };
17
18sub 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
40sub 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
28sub _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
48sub 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
59sub 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
67sub new
68{
69 my ($self, %arg) = @_;
70 $self->SUPER::new({
71 map { $_ => $self->_inflate($arg{$_}, $_) } qw(warning critical)
72 });
73}
74
561; 751;
76
57__END__ 77__END__
58 78
59=head1 NAME 79=head1 NAME