diff options
author | Gavin Carr <gonzai@users.sourceforge.net> | 2007-03-16 11:25:15 +0000 |
---|---|---|
committer | Gavin Carr <gonzai@users.sourceforge.net> | 2007-03-16 11:25:15 +0000 |
commit | c6cbf050974c8f6642fa1d7bde309710b66cbfa0 (patch) | |
tree | 0eb0ea62814b0623fb619c2af3287a585de48c2c /lib/Nagios/Plugin | |
parent | e5109f99c9657a1a8e9fb32b19254a417e2ccd65 (diff) | |
download | monitoring-plugin-perl-c6cbf050974c8f6642fa1d7bde309710b66cbfa0.tar.gz |
Cleanups, mostly to N::P::Range/Threshold/Performance.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1641 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib/Nagios/Plugin')
-rw-r--r-- | lib/Nagios/Plugin/Functions.pm | 18 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Performance.pm | 2 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Range.pm | 24 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Threshold.pm | 90 |
4 files changed, 85 insertions, 49 deletions
diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm index 513501b..526dbed 100644 --- a/lib/Nagios/Plugin/Functions.pm +++ b/lib/Nagios/Plugin/Functions.pm | |||
@@ -9,20 +9,21 @@ use strict; | |||
9 | use warnings; | 9 | use warnings; |
10 | use File::Basename; | 10 | use File::Basename; |
11 | use Params::Validate qw(validate :types); | 11 | use Params::Validate qw(validate :types); |
12 | use Math::Calc::Units; | ||
12 | 13 | ||
13 | # Remember to update Nagios::Plugins as well | 14 | # Remember to update Nagios::Plugins as well |
14 | our $VERSION = "0.15"; | 15 | our $VERSION = "0.16"; |
15 | 16 | ||
16 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); | 17 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); |
17 | 18 | ||
18 | require Exporter; | 19 | require Exporter; |
19 | our @ISA = qw(Exporter); | 20 | our @ISA = qw(Exporter); |
20 | our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); | 21 | our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); |
21 | our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state); | 22 | our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state convert); |
22 | our %EXPORT_TAGS = ( | 23 | our %EXPORT_TAGS = ( |
23 | all => [ @EXPORT, @EXPORT_OK ], | 24 | all => [ @EXPORT, @EXPORT_OK ], |
24 | codes => [ @STATUS_CODES ], | 25 | codes => [ @STATUS_CODES ], |
25 | functions => [ qw(nagios_exit nagios_die check_messages max_state) ], | 26 | functions => [ qw(nagios_exit nagios_die check_messages max_state convert) ], |
26 | ); | 27 | ); |
27 | 28 | ||
28 | use constant OK => 0; | 29 | use constant OK => 0; |
@@ -150,6 +151,17 @@ sub die { nagios_die(@_); } | |||
150 | 151 | ||
151 | 152 | ||
152 | # ------------------------------------------------------------------------ | 153 | # ------------------------------------------------------------------------ |
154 | # Utility functions | ||
155 | |||
156 | # Simple wrapper around Math::Calc::Units::convert | ||
157 | sub convert | ||
158 | { | ||
159 | my ($value, $from, $to) = @_; | ||
160 | my ($newval) = Math::Calc::Units::convert("$value $from", $to, 'exact'); | ||
161 | return $newval; | ||
162 | } | ||
163 | |||
164 | # ------------------------------------------------------------------------ | ||
153 | # check_messages - return a status and/or message based on a set of | 165 | # check_messages - return a status and/or message based on a set of |
154 | # message arrays. | 166 | # message arrays. |
155 | # Returns a nagios status code in scalar context. | 167 | # Returns a nagios status code in scalar context. |
diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm index 1f036f2..63727c0 100644 --- a/lib/Nagios/Plugin/Performance.pm +++ b/lib/Nagios/Plugin/Performance.pm | |||
@@ -7,7 +7,7 @@ use warnings; | |||
7 | 7 | ||
8 | use Carp; | 8 | use Carp; |
9 | use base qw(Class::Accessor::Fast); | 9 | use base qw(Class::Accessor::Fast); |
10 | Nagios::Plugin::Performance->mk_ro_accessors( | 10 | __PACKAGE__->mk_ro_accessors( |
11 | qw(label value uom warning critical min max) | 11 | qw(label value uom warning critical min max) |
12 | ); | 12 | ); |
13 | 13 | ||
diff --git a/lib/Nagios/Plugin/Range.pm b/lib/Nagios/Plugin/Range.pm index dbb637c..3828d1a 100644 --- a/lib/Nagios/Plugin/Range.pm +++ b/lib/Nagios/Plugin/Range.pm | |||
@@ -4,7 +4,12 @@ use 5.006; | |||
4 | 4 | ||
5 | use strict; | 5 | use strict; |
6 | use warnings; | 6 | use warnings; |
7 | |||
7 | use Carp; | 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 | ); | ||
8 | 13 | ||
9 | use Nagios::Plugin::Functions; | 14 | use Nagios::Plugin::Functions; |
10 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; | 15 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; |
@@ -12,15 +17,7 @@ our ($VERSION) = $Nagios::Plugin::Functions::VERSION; | |||
12 | use overload | 17 | use overload |
13 | '""' => sub { shift->_stringify }; | 18 | '""' => sub { shift->_stringify }; |
14 | 19 | ||
15 | use Class::Struct; | 20 | # alert_on constants (undef == range not set) |
16 | struct "Nagios::Plugin::Range" => { | ||
17 | start => '$', | ||
18 | end => '$', | ||
19 | start_infinity => '$', # TRUE / FALSE | ||
20 | end_infinity => '$', # TRUE / FALSE | ||
21 | alert_on => '$', # OUTSIDE 0, INSIDE 1, not defined == range not set | ||
22 | }; | ||
23 | |||
24 | use constant OUTSIDE => 0; | 21 | use constant OUTSIDE => 0; |
25 | use constant INSIDE => 1; | 22 | use constant INSIDE => 1; |
26 | 23 | ||
@@ -119,7 +116,14 @@ sub check_range { | |||
119 | } | 116 | } |
120 | } | 117 | } |
121 | 118 | ||
119 | # Constructor - map args to hashref for SUPER | ||
120 | sub new | ||
121 | { | ||
122 | shift->SUPER::new({ @_ }); | ||
123 | } | ||
124 | |||
122 | 1; | 125 | 1; |
126 | |||
123 | __END__ | 127 | __END__ |
124 | 128 | ||
125 | =head1 NAME | 129 | =head1 NAME |
@@ -135,7 +139,7 @@ Nagios::Plugin::Range - class for handling Nagios::Plugin range data. | |||
135 | $r = Nagios::Plugin::Range->new; | 139 | $r = Nagios::Plugin::Range->new; |
136 | 140 | ||
137 | # Instantiate by parsing a standard nagios range string | 141 | # Instantiate by parsing a standard nagios range string |
138 | $r = Nagios::Plugin::Range->parse_range_string; | 142 | $r = Nagios::Plugin::Range->parse_range_string( $range_str ); |
139 | 143 | ||
140 | # Returns true if the range is defined/non-empty | 144 | # Returns true if the range is defined/non-empty |
141 | $r->is_set; | 145 | $r->is_set; |
diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm index 0c4805a..ad58134 100644 --- a/lib/Nagios/Plugin/Threshold.pm +++ b/lib/Nagios/Plugin/Threshold.pm | |||
@@ -5,55 +5,75 @@ use 5.006; | |||
5 | use strict; | 5 | use strict; |
6 | use warnings; | 6 | use warnings; |
7 | 7 | ||
8 | use base qw(Class::Accessor::Fast); | ||
9 | __PACKAGE__->mk_accessors(qw(warning critical)); | ||
10 | |||
8 | use Nagios::Plugin::Range; | 11 | use Nagios::Plugin::Range; |
9 | use Nagios::Plugin::Functions qw(:codes nagios_die); | 12 | use Nagios::Plugin::Functions qw(:codes nagios_die); |
10 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; | 13 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; |
11 | 14 | ||
12 | use Class::Struct; | 15 | sub get_status |
13 | struct "Nagios::Plugin::Threshold" => { | 16 | { |
14 | warning => 'Nagios::Plugin::Range', | ||
15 | critical => 'Nagios::Plugin::Range', | ||
16 | }; | ||
17 | |||
18 | sub set_thresholds { | ||
19 | my ($class, %args) = @_; | ||
20 | my $t = $class->new( warning => Nagios::Plugin::Range->new, critical => Nagios::Plugin::Range->new ); | ||
21 | if (defined $args{warning}) { | ||
22 | my $r = Nagios::Plugin::Range->parse_range_string($args{warning}); | ||
23 | if (defined $r) { | ||
24 | $t->warning($r); | ||
25 | } else { | ||
26 | nagios_die( "Warning range incorrect: '$args{warning}'" ); | ||
27 | } | ||
28 | } | ||
29 | if (defined $args{critical}) { | ||
30 | my $r = Nagios::Plugin::Range->parse_range_string($args{critical}); | ||
31 | if (defined $r) { | ||
32 | $t->critical($r); | ||
33 | } else { | ||
34 | nagios_die( "Critical range incorrect: '$args{critical}'" ); | ||
35 | } | ||
36 | } | ||
37 | return $t; | ||
38 | } | ||
39 | |||
40 | sub get_status { | ||
41 | my ($self, $value) = @_; | 17 | my ($self, $value) = @_; |
42 | 18 | ||
43 | if ($self->critical->is_set) { | 19 | if ($self->critical->is_set) { |
44 | if ($self->critical->check_range($value) == 1) { | 20 | return CRITICAL if $self->critical->check_range($value); |
45 | return CRITICAL; | ||
46 | } | ||
47 | } | 21 | } |
48 | if ($self->warning->is_set) { | 22 | if ($self->warning->is_set) { |
49 | if ($self->warning->check_range($value) == 1) { | 23 | return WARNING if $self->warning->check_range($value); |
50 | return WARNING; | ||
51 | } | ||
52 | } | 24 | } |
53 | return OK; | 25 | return OK; |
54 | } | 26 | } |
27 | |||
28 | sub _inflate | ||
29 | { | ||
30 | my ($self, $value, $key) = @_; | ||
31 | |||
32 | # Return an undefined range if $value is undef | ||
33 | return Nagios::Plugin::Range->new if ! defined $value; | ||
34 | |||
35 | # For refs, check isa N::P::Range | ||
36 | if (ref $value) { | ||
37 | nagios_die("Invalid $key object: type " . ref $value) | ||
38 | unless $value->isa("Nagios::Plugin::Range"); | ||
39 | return $value; | ||
40 | } | ||
41 | |||
42 | # Otherwise parse $value | ||
43 | my $range = Nagios::Plugin::Range->parse_range_string($value) | ||
44 | or nagios_die("Cannot parse $key range: '$value'"); | ||
45 | return $range; | ||
46 | } | ||
47 | |||
48 | sub set_thresholds | ||
49 | { | ||
50 | my ($self, %arg) = @_; | ||
51 | |||
52 | # Equals new() as a class method | ||
53 | return $self->new(%arg) unless ref $self; | ||
54 | |||
55 | # On an object, just acts as special mutator | ||
56 | $self->set($_, $arg{$_}) foreach qw(warning critical); | ||
57 | } | ||
58 | |||
59 | sub set | ||
60 | { | ||
61 | my $self = shift; | ||
62 | my ($key, $value) = @_; | ||
63 | $self->SUPER::set($key, $self->_inflate($value, $key)); | ||
64 | } | ||
55 | 65 | ||
66 | # Constructor - inflate scalars to N::P::Range objects | ||
67 | sub new | ||
68 | { | ||
69 | my ($self, %arg) = @_; | ||
70 | $self->SUPER::new({ | ||
71 | map { $_ => $self->_inflate($arg{$_}, $_) } qw(warning critical) | ||
72 | }); | ||
73 | } | ||
74 | |||
56 | 1; | 75 | 1; |
76 | |||
57 | __END__ | 77 | __END__ |
58 | 78 | ||
59 | =head1 NAME | 79 | =head1 NAME |