diff options
author | Ton Voon <tonvoon@users.sourceforge.net> | 2006-06-08 12:27:44 +0000 |
---|---|---|
committer | Ton Voon <tonvoon@users.sourceforge.net> | 2006-06-08 12:27:44 +0000 |
commit | 2c6651034f76e2bccb549a867485f8fabbf07cb1 (patch) | |
tree | 8de53d7efe4564017e1ebce73353c7e3a3c8b548 /lib/Nagios/Plugin/Range.pm | |
parent | 83e29d795dddf34a652cda8665179b77372ddcfe (diff) | |
download | monitoring-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.pm | 126 |
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 @@ | |||
1 | package Nagios::Plugin::Range; | ||
2 | |||
3 | use 5.008004; | ||
4 | |||
5 | use strict; | ||
6 | use warnings; | ||
7 | |||
8 | use overload | ||
9 | '""' => sub { shift->stringify }; | ||
10 | |||
11 | use Class::Struct; | ||
12 | struct "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 | |||
20 | my $outside = 0; | ||
21 | my $inside = 1; | ||
22 | |||
23 | sub 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 | |||
30 | sub set_range_start { | ||
31 | my ($self, $value) = @_; | ||
32 | $self->start($value+0); # Force scalar into number | ||
33 | $self->start_infinity(0); | ||
34 | } | ||
35 | |||
36 | sub 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 | ||
43 | sub 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 | ||
71 | sub 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 | |||
102 | 1; | ||
103 | __END__ | ||
104 | |||
105 | =head1 NAME | ||
106 | |||
107 | Nagios::Plugin::Range - Common range functions for Nagios::Plugin | ||
108 | |||
109 | =head1 DESCRIPTION | ||
110 | |||
111 | Handles common Nagios Plugin range data. See Nagios::Plugin for creation interfaces. | ||
112 | |||
113 | =head1 AUTHOR | ||
114 | |||
115 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
116 | |||
117 | =head1 COPYRIGHT AND LICENSE | ||
118 | |||
119 | Copyright (C) 2006 by Altinity Limited | ||
120 | |||
121 | This library is free software; you can redistribute it and/or modify | ||
122 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
123 | at your option, any later version of Perl 5 you may have available. | ||
124 | |||
125 | |||
126 | =cut | ||