summaryrefslogtreecommitdiffstats
path: root/lib/Monitoring/Plugin/Threshold.pm
diff options
context:
space:
mode:
authorSven Nierlein <sven@nierlein.de>2014-01-19 23:54:34 (GMT)
committerSven Nierlein <sven@nierlein.de>2014-01-19 23:54:34 (GMT)
commitb418181dfe80dd75169b6e8a619ac1932155dea2 (patch)
treecad9c0ae0eae8e800cfff60555ead06ad33c6856 /lib/Monitoring/Plugin/Threshold.pm
parent1cd8d1c52cbd47121f344c4074aec84653f412ce (diff)
downloadmonitoring-plugin-perl-b418181dfe80dd75169b6e8a619ac1932155dea2.tar.gz
renamed module into Monitoring::Plugin
since the complete monitoring team has been renamed, we also rename this module. Signed-off-by: Sven Nierlein <sven@nierlein.de>
Diffstat (limited to 'lib/Monitoring/Plugin/Threshold.pm')
-rw-r--r--lib/Monitoring/Plugin/Threshold.pm134
1 files changed, 134 insertions, 0 deletions
diff --git a/lib/Monitoring/Plugin/Threshold.pm b/lib/Monitoring/Plugin/Threshold.pm
new file mode 100644
index 0000000..e71e21b
--- /dev/null
+++ b/lib/Monitoring/Plugin/Threshold.pm
@@ -0,0 +1,134 @@
1package Monitoring::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 Monitoring::Plugin::Range;
12use Monitoring::Plugin::Functions qw(:codes plugin_die);
13our ($VERSION) = $Monitoring::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 Monitoring::Plugin::Range->new if ! defined $value;
39
40 # For refs, check isa N::P::Range
41 if (ref $value) {
42 plugin_die("Invalid $key object: type " . ref $value)
43 unless $value->isa("Monitoring::Plugin::Range");
44 return $value;
45 }
46
47 # Another quick exit if $value is an empty string
48 return Monitoring::Plugin::Range->new if $value eq "";
49
50 # Otherwise parse $value
51 my $range = Monitoring::Plugin::Range->parse_range_string($value);
52 plugin_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
89Monitoring::Plugin::Threshold - class for handling Monitoring::Plugin thresholds.
90
91=head1 SYNOPSIS
92
93 # NB: This is an internal Monitoring::Plugin class.
94 # See Monitoring::Plugin itself for public interfaces.
95
96 # Constructor
97 $t = Monitoring::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 Monitoring::Plugin class for handling threshold data. See
114Monitoring::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 Monitoring Plugin Development Team: see
125https://monitoring-plugins.org
126
127=head1 COPYRIGHT AND LICENSE
128
129Copyright (C) 2006-2014 Monitoring 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