diff options
Diffstat (limited to 'lib/Monitoring/Plugin/Threshold.pm')
| -rw-r--r-- | lib/Monitoring/Plugin/Threshold.pm | 134 |
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 @@ | |||
| 1 | package Monitoring::Plugin::Threshold; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use base qw(Class::Accessor::Fast); | ||
| 9 | __PACKAGE__->mk_accessors(qw(warning critical)); | ||
| 10 | |||
| 11 | use Monitoring::Plugin::Range; | ||
| 12 | use Monitoring::Plugin::Functions qw(:codes plugin_die); | ||
| 13 | our ($VERSION) = $Monitoring::Plugin::Functions::VERSION; | ||
| 14 | |||
| 15 | sub 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 | |||
| 33 | sub _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 | |||
| 56 | sub 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 | |||
| 67 | sub 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 | ||
| 75 | sub new | ||
| 76 | { | ||
| 77 | my ($self, %arg) = @_; | ||
| 78 | $self->SUPER::new({ | ||
| 79 | map { $_ => $self->_inflate($arg{$_}, $_) } qw(warning critical) | ||
| 80 | }); | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | __END__ | ||
| 86 | |||
| 87 | =head1 NAME | ||
| 88 | |||
| 89 | Monitoring::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 | |||
| 113 | Internal Monitoring::Plugin class for handling threshold data. See | ||
| 114 | Monitoring::Plugin for public interfaces. | ||
| 115 | |||
| 116 | A threshold object contains (typically) a pair of ranges, associated | ||
| 117 | with a particular severity e.g. | ||
| 118 | |||
| 119 | warning => range1 | ||
| 120 | critical => range2 | ||
| 121 | |||
| 122 | =head1 AUTHOR | ||
| 123 | |||
| 124 | This code is maintained by the Monitoring Plugin Development Team: see | ||
| 125 | https://monitoring-plugins.org | ||
| 126 | |||
| 127 | =head1 COPYRIGHT AND LICENSE | ||
| 128 | |||
| 129 | Copyright (C) 2006-2014 Monitoring Plugin Development Team | ||
| 130 | |||
| 131 | This library is free software; you can redistribute it and/or modify | ||
| 132 | it under the same terms as Perl itself. | ||
| 133 | |||
| 134 | =cut | ||
