summaryrefslogtreecommitdiffstats
path: root/lib/Nagios/Plugin/Range.pm
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2006-06-08 12:27:44 +0000
committerTon Voon <tonvoon@users.sourceforge.net>2006-06-08 12:27:44 +0000
commit2c6651034f76e2bccb549a867485f8fabbf07cb1 (patch)
tree8de53d7efe4564017e1ebce73353c7e3a3c8b548 /lib/Nagios/Plugin/Range.pm
parent83e29d795dddf34a652cda8665179b77372ddcfe (diff)
downloadmonitoring-plugin-perl-2c6651034f76e2bccb549a867485f8fabbf07cb1.tar.gz
Initial revision
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1419 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib/Nagios/Plugin/Range.pm')
-rw-r--r--lib/Nagios/Plugin/Range.pm126
1 files changed, 126 insertions, 0 deletions
diff --git a/lib/Nagios/Plugin/Range.pm b/lib/Nagios/Plugin/Range.pm
new file mode 100644
index 0000000..3d6f613
--- /dev/null
+++ b/lib/Nagios/Plugin/Range.pm
@@ -0,0 +1,126 @@
1package Nagios::Plugin::Range;
2
3use 5.008004;
4
5use strict;
6use warnings;
7
8use overload
9 '""' => sub { shift->stringify };
10
11use Class::Struct;
12struct "Nagios::Plugin::Range" => {
13 start => '$',
14 end => '$',
15 start_infinity => '$', # TRUE / FALSE
16 end_infinity => '$', # TRUE / FALSE
17 alert_on => '$', # OUTSIDE 0, INSIDE 1
18 };
19
20my $outside = 0;
21my $inside = 1;
22
23sub stringify {
24 my $self = shift;
25 return (($self->alert_on) ? "@" : "") .
26 (($self->start_infinity == 1) ? "~:" : (($self->start == 0)?"":$self->start.":")) .
27 (($self->end_infinity == 1) ? "" : $self->end);
28}
29
30sub set_range_start {
31 my ($self, $value) = @_;
32 $self->start($value+0); # Force scalar into number
33 $self->start_infinity(0);
34}
35
36sub set_range_end {
37 my ($self, $value) = @_;
38 $self->end($value+0); # Force scalar into number
39 $self->end_infinity(0);
40}
41
42# Returns a N::P::Range object if the string is a conforms to a Nagios Plugin range string, otherwise null
43sub parse_range_string {
44 my ($class, $string) = @_;
45 my $valid = 0;
46 my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => $outside);
47
48 if ($string =~ s/^\@//) {
49 $range->alert_on($inside);
50 }
51 if ($string =~ s/^~//) {
52 $range->start_infinity(1);
53 }
54 if (($_) = $string =~ /^([-\d\.]+)?:/) {
55 $range->set_range_start($_) if defined $_;
56 $string =~ s/^([-\d\.]+)?://;
57 $valid++
58 }
59 if ($string =~ /^([-\d\.]+)$/) {
60 $range->set_range_end($string);
61 $valid++;
62 }
63
64 if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) {
65 return $range;
66 }
67 return undef;
68}
69
70# Returns 1 if an alert should be raised, otherwise 0
71sub check_range {
72 my ($self, $value) = @_;
73 my $false = 0;
74 my $true = 1;
75 if ($self->alert_on == $inside) {
76 $false = 1;
77 $true = 0;
78 }
79 if ($self->end_infinity == 0 && $self->start_infinity == 0) {
80 if ($self->start <= $value && $value <= $self->end) {
81 return $false;
82 } else {
83 return $true;
84 }
85 } elsif ($self->start_infinity == 0 && $self->end_infinity == 1) {
86 if ($self->start <= $value) {
87 return $false;
88 } else {
89 return $true;
90 }
91 } elsif ($self->start_infinity == 1 && $self->end_infinity == 0) {
92 if ($value <= $self->end) {
93 return $false;
94 } else {
95 return $true;
96 }
97 } else {
98 return $false;
99 }
100}
101
1021;
103__END__
104
105=head1 NAME
106
107Nagios::Plugin::Range - Common range functions for Nagios::Plugin
108
109=head1 DESCRIPTION
110
111Handles common Nagios Plugin range data. See Nagios::Plugin for creation interfaces.
112
113=head1 AUTHOR
114
115Ton Voon, E<lt>ton.voon@altinity.comE<gt>
116
117=head1 COPYRIGHT AND LICENSE
118
119Copyright (C) 2006 by Altinity Limited
120
121This library is free software; you can redistribute it and/or modify
122it under the same terms as Perl itself, either Perl version 5.8.4 or,
123at your option, any later version of Perl 5 you may have available.
124
125
126=cut