From c6cbf050974c8f6642fa1d7bde309710b66cbfa0 Mon Sep 17 00:00:00 2001 From: Gavin Carr Date: Fri, 16 Mar 2007 11:25:15 +0000 Subject: 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 --- lib/Nagios/Plugin.pm | 3 +- lib/Nagios/Plugin/Functions.pm | 18 ++++++-- lib/Nagios/Plugin/Performance.pm | 2 +- lib/Nagios/Plugin/Range.pm | 24 ++++++----- lib/Nagios/Plugin/Threshold.pm | 90 ++++++++++++++++++++++++---------------- 5 files changed, 86 insertions(+), 51 deletions(-) (limited to 'lib') diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index 576322b..b063e4c 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm @@ -22,8 +22,7 @@ our @ISA = qw(Exporter); our @EXPORT = (@STATUS_CODES); our @EXPORT_OK = qw(%ERRORS); -# Remember to update Nagios::Plugin::Functions as well! -our $VERSION = "0.15"; +our $VERSION = $Nagios::Plugin::Functions::VERSION; sub new { my $class = shift; 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; use warnings; use File::Basename; use Params::Validate qw(validate :types); +use Math::Calc::Units; # Remember to update Nagios::Plugins as well -our $VERSION = "0.15"; +our $VERSION = "0.16"; our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); require Exporter; our @ISA = qw(Exporter); our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); -our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state); +our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state convert); our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ], codes => [ @STATUS_CODES ], - functions => [ qw(nagios_exit nagios_die check_messages max_state) ], + functions => [ qw(nagios_exit nagios_die check_messages max_state convert) ], ); use constant OK => 0; @@ -149,6 +150,17 @@ sub nagios_die { sub die { nagios_die(@_); } +# ------------------------------------------------------------------------ +# Utility functions + +# Simple wrapper around Math::Calc::Units::convert +sub convert +{ + my ($value, $from, $to) = @_; + my ($newval) = Math::Calc::Units::convert("$value $from", $to, 'exact'); + return $newval; +} + # ------------------------------------------------------------------------ # check_messages - return a status and/or message based on a set of # message arrays. 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; use Carp; use base qw(Class::Accessor::Fast); -Nagios::Plugin::Performance->mk_ro_accessors( +__PACKAGE__->mk_ro_accessors( qw(label value uom warning critical min max) ); 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; use strict; use warnings; + use Carp; +use base qw(Class::Accessor::Fast); +__PACKAGE__->mk_accessors( + qw(start end start_infinity end_infinity alert_on) +); use Nagios::Plugin::Functions; our ($VERSION) = $Nagios::Plugin::Functions::VERSION; @@ -12,15 +17,7 @@ our ($VERSION) = $Nagios::Plugin::Functions::VERSION; use overload '""' => sub { shift->_stringify }; -use Class::Struct; -struct "Nagios::Plugin::Range" => { - start => '$', - end => '$', - start_infinity => '$', # TRUE / FALSE - end_infinity => '$', # TRUE / FALSE - alert_on => '$', # OUTSIDE 0, INSIDE 1, not defined == range not set - }; - +# alert_on constants (undef == range not set) use constant OUTSIDE => 0; use constant INSIDE => 1; @@ -119,7 +116,14 @@ sub check_range { } } +# Constructor - map args to hashref for SUPER +sub new +{ + shift->SUPER::new({ @_ }); +} + 1; + __END__ =head1 NAME @@ -135,7 +139,7 @@ Nagios::Plugin::Range - class for handling Nagios::Plugin range data. $r = Nagios::Plugin::Range->new; # Instantiate by parsing a standard nagios range string - $r = Nagios::Plugin::Range->parse_range_string; + $r = Nagios::Plugin::Range->parse_range_string( $range_str ); # Returns true if the range is defined/non-empty $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; use strict; use warnings; +use base qw(Class::Accessor::Fast); +__PACKAGE__->mk_accessors(qw(warning critical)); + use Nagios::Plugin::Range; use Nagios::Plugin::Functions qw(:codes nagios_die); our ($VERSION) = $Nagios::Plugin::Functions::VERSION; -use Class::Struct; -struct "Nagios::Plugin::Threshold" => { - warning => 'Nagios::Plugin::Range', - critical => 'Nagios::Plugin::Range', - }; - -sub set_thresholds { - my ($class, %args) = @_; - my $t = $class->new( warning => Nagios::Plugin::Range->new, critical => Nagios::Plugin::Range->new ); - if (defined $args{warning}) { - my $r = Nagios::Plugin::Range->parse_range_string($args{warning}); - if (defined $r) { - $t->warning($r); - } else { - nagios_die( "Warning range incorrect: '$args{warning}'" ); - } - } - if (defined $args{critical}) { - my $r = Nagios::Plugin::Range->parse_range_string($args{critical}); - if (defined $r) { - $t->critical($r); - } else { - nagios_die( "Critical range incorrect: '$args{critical}'" ); - } - } - return $t; -} - -sub get_status { +sub get_status +{ my ($self, $value) = @_; if ($self->critical->is_set) { - if ($self->critical->check_range($value) == 1) { - return CRITICAL; - } + return CRITICAL if $self->critical->check_range($value); } if ($self->warning->is_set) { - if ($self->warning->check_range($value) == 1) { - return WARNING; - } + return WARNING if $self->warning->check_range($value); } return OK; } + +sub _inflate +{ + my ($self, $value, $key) = @_; + + # Return an undefined range if $value is undef + return Nagios::Plugin::Range->new if ! defined $value; + + # For refs, check isa N::P::Range + if (ref $value) { + nagios_die("Invalid $key object: type " . ref $value) + unless $value->isa("Nagios::Plugin::Range"); + return $value; + } + + # Otherwise parse $value + my $range = Nagios::Plugin::Range->parse_range_string($value) + or nagios_die("Cannot parse $key range: '$value'"); + return $range; +} + +sub set_thresholds +{ + my ($self, %arg) = @_; + + # Equals new() as a class method + return $self->new(%arg) unless ref $self; + + # On an object, just acts as special mutator + $self->set($_, $arg{$_}) foreach qw(warning critical); +} + +sub set +{ + my $self = shift; + my ($key, $value) = @_; + $self->SUPER::set($key, $self->_inflate($value, $key)); +} +# Constructor - inflate scalars to N::P::Range objects +sub new +{ + my ($self, %arg) = @_; + $self->SUPER::new({ + map { $_ => $self->_inflate($arg{$_}, $_) } qw(warning critical) + }); +} + 1; + __END__ =head1 NAME -- cgit v1.2.3-74-g34f1