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.pm134
1 files changed, 0 insertions, 134 deletions
diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm
deleted file mode 100644
index 95a089b..0000000
--- a/lib/Nagios/Plugin/Threshold.pm
+++ /dev/null
@@ -1,134 +0,0 @@
1package Nagios::Plugin::Threshold;
2
3use 5.006;
4
5use strict;
6use warnings;
7
8use base qw(Class::Accessor::Fast);
9__PACKAGE__->mk_accessors(qw(warning critical));
10
11use Nagios::Plugin::Range;
12use Nagios::Plugin::Functions qw(:codes nagios_die);
13our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
14
15sub get_status
16{
17 my ($self, $value) = @_;
18
19 $value = [ $value ] if (ref $value eq "");
20 foreach my $v (@$value) {
21 if ($self->critical->is_set) {
22 return CRITICAL if $self->critical->check_range($v);
23 }
24 }
25 foreach my $v (@$value) {
26 if ($self->warning->is_set) {
27 return WARNING if $self->warning->check_range($v);
28 }
29 }
30 return OK;
31}
32
33sub _inflate
34{
35 my ($self, $value, $key) = @_;
36
37 # Return an undefined range if $value is undef
38 return Nagios::Plugin::Range->new if ! defined $value;
39
40 # For refs, check isa N::P::Range
41 if (ref $value) {
42 nagios_die("Invalid $key object: type " . ref $value)
43 unless $value->isa("Nagios::Plugin::Range");
44 return $value;
45 }
46
47 # Another quick exit if $value is an empty string
48 return Nagios::Plugin::Range->new if $value eq "";
49
50 # Otherwise parse $value
51 my $range = Nagios::Plugin::Range->parse_range_string($value);
52 nagios_die("Cannot parse $key range: '$value'") unless(defined($range));
53 return $range;
54}
55
56sub set_thresholds
57{
58 my ($self, %arg) = @_;
59
60 # Equals new() as a class method
61 return $self->new(%arg) unless ref $self;
62
63 # On an object, just acts as special mutator
64 $self->set($_, $arg{$_}) foreach qw(warning critical);
65}
66
67sub set
68{
69 my $self = shift;
70 my ($key, $value) = @_;
71 $self->SUPER::set($key, $self->_inflate($value, $key));
72}
73
74# Constructor - inflate scalars to N::P::Range objects
75sub new
76{
77 my ($self, %arg) = @_;
78 $self->SUPER::new({
79 map { $_ => $self->_inflate($arg{$_}, $_) } qw(warning critical)
80 });
81}
82
831;
84
85__END__
86
87=head1 NAME
88
89Nagios::Plugin::Threshold - class for handling Nagios::Plugin thresholds.
90
91=head1 SYNOPSIS
92
93 # NB: This is an internal Nagios::Plugin class.
94 # See Nagios::Plugin itself for public interfaces.
95
96 # Constructor
97 $t = Nagios::Plugin::Threshold->set_thresholds(
98 warning => $warning_range_string,
99 critical => $critical_range_string,
100 );
101
102 # Value checking - returns CRITICAL if in the critical range,
103 # WARNING if in the warning range, and OK otherwise
104 $status = $t->get_status($value);
105
106 # Accessors - return the associated N::P::Range object
107 $warning_range = $t->warning;
108 $critical_range = $t->critical;
109
110
111=head1 DESCRIPTION
112
113Internal Nagios::Plugin class for handling threshold data. See
114Nagios::Plugin for public interfaces.
115
116A threshold object contains (typically) a pair of ranges, associated
117with a particular severity e.g.
118
119 warning => range1
120 critical => range2
121
122=head1 AUTHOR
123
124This code is maintained by the Nagios Plugin Development Team: see
125http://nagiosplug.sourceforge.net.
126
127=head1 COPYRIGHT AND LICENSE
128
129Copyright (C) 2006-2007 Nagios Plugin Development Team
130
131This library is free software; you can redistribute it and/or modify
132it under the same terms as Perl itself.
133
134=cut