diff options
author | Sven Nierlein <sven@nierlein.de> | 2014-01-20 00:54:34 +0100 |
---|---|---|
committer | Sven Nierlein <sven@nierlein.de> | 2014-01-20 00:54:34 +0100 |
commit | b418181dfe80dd75169b6e8a619ac1932155dea2 (patch) | |
tree | cad9c0ae0eae8e800cfff60555ead06ad33c6856 /lib/Monitoring/Plugin/Range.pm | |
parent | 1cd8d1c52cbd47121f344c4074aec84653f412ce (diff) | |
download | monitoring-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/Range.pm')
-rw-r--r-- | lib/Monitoring/Plugin/Range.pm | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/lib/Monitoring/Plugin/Range.pm b/lib/Monitoring/Plugin/Range.pm new file mode 100644 index 0000000..af19577 --- /dev/null +++ b/lib/Monitoring/Plugin/Range.pm | |||
@@ -0,0 +1,169 @@ | |||
1 | package Monitoring::Plugin::Range; | ||
2 | |||
3 | use 5.006; | ||
4 | |||
5 | use strict; | ||
6 | use warnings; | ||
7 | |||
8 | use Carp; | ||
9 | use base qw(Class::Accessor::Fast); | ||
10 | __PACKAGE__->mk_accessors( | ||
11 | qw(start end start_infinity end_infinity alert_on) | ||
12 | ); | ||
13 | |||
14 | use Monitoring::Plugin::Functions qw(:DEFAULT $value_re); | ||
15 | our ($VERSION) = $Monitoring::Plugin::Functions::VERSION; | ||
16 | |||
17 | use overload | ||
18 | 'eq' => sub { shift->_stringify }, | ||
19 | '""' => sub { shift->_stringify }; | ||
20 | |||
21 | # alert_on constants (undef == range not set) | ||
22 | use constant OUTSIDE => 0; | ||
23 | use constant INSIDE => 1; | ||
24 | |||
25 | sub _stringify { | ||
26 | my $self = shift; | ||
27 | return "" unless $self->is_set; | ||
28 | return (($self->alert_on) ? "@" : "") . | ||
29 | (($self->start_infinity == 1) ? "~:" : (($self->start == 0)?"":$self->start.":")) . | ||
30 | (($self->end_infinity == 1) ? "" : $self->end); | ||
31 | } | ||
32 | |||
33 | sub is_set { | ||
34 | my $self = shift; | ||
35 | (! defined $self->alert_on) ? 0 : 1; | ||
36 | } | ||
37 | |||
38 | sub _set_range_start { | ||
39 | my ($self, $value) = @_; | ||
40 | $self->start($value+0); # Force scalar into number | ||
41 | $self->start_infinity(0); | ||
42 | } | ||
43 | |||
44 | sub _set_range_end { | ||
45 | my ($self, $value) = @_; | ||
46 | $self->end($value+0); # Force scalar into number | ||
47 | $self->end_infinity(0); | ||
48 | } | ||
49 | |||
50 | # Returns a N::P::Range object if the string is a conforms to a Monitoring Plugin range string, otherwise null | ||
51 | sub parse_range_string { | ||
52 | my ($class, $string) = @_; | ||
53 | my $valid = 0; | ||
54 | my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => OUTSIDE); | ||
55 | |||
56 | $string =~ s/\s//g; # strip out any whitespace | ||
57 | # check for valid range definition | ||
58 | unless ( $string =~ /[\d~]/ && $string =~ m/^\@?($value_re|~)?(:($value_re)?)?$/ ) { | ||
59 | carp "invalid range definition '$string'"; | ||
60 | return undef; | ||
61 | } | ||
62 | |||
63 | if ($string =~ s/^\@//) { | ||
64 | $range->alert_on(INSIDE); | ||
65 | } | ||
66 | |||
67 | if ($string =~ s/^~//) { # '~:x' | ||
68 | $range->start_infinity(1); | ||
69 | } | ||
70 | if ( $string =~ m/^($value_re)?:/ ) { # '10:' | ||
71 | my $start = $1; | ||
72 | $range->_set_range_start($start) if defined $start; | ||
73 | $range->end_infinity(1); # overridden below if there's an end specified | ||
74 | $string =~ s/^($value_re)?://; | ||
75 | $valid++; | ||
76 | } | ||
77 | if ($string =~ /^($value_re)$/) { # 'x:10' or '10' | ||
78 | $range->_set_range_end($string); | ||
79 | $valid++; | ||
80 | } | ||
81 | |||
82 | if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) { | ||
83 | return $range; | ||
84 | } | ||
85 | return undef; | ||
86 | } | ||
87 | |||
88 | # Returns 1 if an alert should be raised, otherwise 0 | ||
89 | sub check_range { | ||
90 | my ($self, $value) = @_; | ||
91 | my $false = 0; | ||
92 | my $true = 1; | ||
93 | if ($self->alert_on == INSIDE) { | ||
94 | $false = 1; | ||
95 | $true = 0; | ||
96 | } | ||
97 | if ($self->end_infinity == 0 && $self->start_infinity == 0) { | ||
98 | if ($self->start <= $value && $value <= $self->end) { | ||
99 | return $false; | ||
100 | } else { | ||
101 | return $true; | ||
102 | } | ||
103 | } elsif ($self->start_infinity == 0 && $self->end_infinity == 1) { | ||
104 | if ( $value >= $self->start ) { | ||
105 | return $false; | ||
106 | } else { | ||
107 | return $true; | ||
108 | } | ||
109 | } elsif ($self->start_infinity == 1 && $self->end_infinity == 0) { | ||
110 | if ($value <= $self->end) { | ||
111 | return $false; | ||
112 | } else { | ||
113 | return $true; | ||
114 | } | ||
115 | } else { | ||
116 | return $false; | ||
117 | } | ||
118 | } | ||
119 | |||
120 | # Constructor - map args to hashref for SUPER | ||
121 | sub new | ||
122 | { | ||
123 | shift->SUPER::new({ @_ }); | ||
124 | } | ||
125 | |||
126 | 1; | ||
127 | |||
128 | __END__ | ||
129 | |||
130 | =head1 NAME | ||
131 | |||
132 | Monitoring::Plugin::Range - class for handling Monitoring::Plugin range data. | ||
133 | |||
134 | =head1 SYNOPSIS | ||
135 | |||
136 | # NB: This is an internal Monitoring::Plugin class. | ||
137 | # See Monitoring::Plugin itself for public interfaces. | ||
138 | |||
139 | # Instantiate an empty range object | ||
140 | $r = Monitoring::Plugin::Range->new; | ||
141 | |||
142 | # Instantiate by parsing a standard nagios range string | ||
143 | $r = Monitoring::Plugin::Range->parse_range_string( $range_str ); | ||
144 | |||
145 | # Returns true if the range is defined/non-empty | ||
146 | $r->is_set; | ||
147 | |||
148 | # Returns true if $value matches range, false otherwise | ||
149 | $r->check_range($value); | ||
150 | |||
151 | |||
152 | =head1 DESCRIPTION | ||
153 | |||
154 | Internal Monitoring::Plugin class for handling common range data. See | ||
155 | Monitoring::Plugin for public interfaces. | ||
156 | |||
157 | =head1 AUTHOR | ||
158 | |||
159 | This code is maintained by the Monitoring Plugin Development Team: see | ||
160 | https://monitoring-plugins.org | ||
161 | |||
162 | =head1 COPYRIGHT AND LICENSE | ||
163 | |||
164 | Copyright (C) 2006-2014 Monitoring Plugin Development Team | ||
165 | |||
166 | This library is free software; you can redistribute it and/or modify | ||
167 | it under the same terms as Perl itself. | ||
168 | |||
169 | =cut | ||