summaryrefslogtreecommitdiffstats
path: root/lib/Nagios/Plugin
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Nagios/Plugin')
-rw-r--r--lib/Nagios/Plugin/Functions.pm18
-rw-r--r--lib/Nagios/Plugin/Performance.pm2
-rw-r--r--lib/Nagios/Plugin/Range.pm24
-rw-r--r--lib/Nagios/Plugin/Threshold.pm90
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;
9use warnings; 9use warnings;
10use File::Basename; 10use File::Basename;
11use Params::Validate qw(validate :types); 11use Params::Validate qw(validate :types);
12use Math::Calc::Units;
12 13
13# Remember to update Nagios::Plugins as well 14# Remember to update Nagios::Plugins as well
14our $VERSION = "0.15"; 15our $VERSION = "0.16";
15 16
16our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); 17our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
17 18
18require Exporter; 19require Exporter;
19our @ISA = qw(Exporter); 20our @ISA = qw(Exporter);
20our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); 21our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages));
21our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state); 22our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state convert);
22our %EXPORT_TAGS = ( 23our %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
28use constant OK => 0; 29use 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
157sub 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
8use Carp; 8use Carp;
9use base qw(Class::Accessor::Fast); 9use base qw(Class::Accessor::Fast);
10Nagios::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
5use strict; 5use strict;
6use warnings; 6use warnings;
7
7use Carp; 8use Carp;
9use base qw(Class::Accessor::Fast);
10__PACKAGE__->mk_accessors(
11 qw(start end start_infinity end_infinity alert_on)
12);
8 13
9use Nagios::Plugin::Functions; 14use Nagios::Plugin::Functions;
10our ($VERSION) = $Nagios::Plugin::Functions::VERSION; 15our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
@@ -12,15 +17,7 @@ our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
12use overload 17use overload
13 '""' => sub { shift->_stringify }; 18 '""' => sub { shift->_stringify };
14 19
15use Class::Struct; 20# alert_on constants (undef == range not set)
16struct "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
24use constant OUTSIDE => 0; 21use constant OUTSIDE => 0;
25use constant INSIDE => 1; 22use 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
120sub new
121{
122 shift->SUPER::new({ @_ });
123}
124
1221; 1251;
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;
5use strict; 5use strict;
6use warnings; 6use warnings;
7 7
8use base qw(Class::Accessor::Fast);
9__PACKAGE__->mk_accessors(qw(warning critical));
10
8use Nagios::Plugin::Range; 11use Nagios::Plugin::Range;
9use Nagios::Plugin::Functions qw(:codes nagios_die); 12use Nagios::Plugin::Functions qw(:codes nagios_die);
10our ($VERSION) = $Nagios::Plugin::Functions::VERSION; 13our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
11 14
12use Class::Struct; 15sub get_status
13struct "Nagios::Plugin::Threshold" => { 16{
14 warning => 'Nagios::Plugin::Range',
15 critical => 'Nagios::Plugin::Range',
16 };
17
18sub 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
40sub 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
28sub _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
48sub 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
59sub 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
67sub new
68{
69 my ($self, %arg) = @_;
70 $self->SUPER::new({
71 map { $_ => $self->_inflate($arg{$_}, $_) } qw(warning critical)
72 });
73}
74
561; 751;
76
57__END__ 77__END__
58 78
59=head1 NAME 79=head1 NAME