From c6ee438b0525b73d36bbcf5e5a9e0d445f69129b Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Fri, 31 Aug 2007 14:34:18 +0000 Subject: Convenience function to set use_die on use of N::P::Performance git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1773 f882894a-f735-0410-b71e-b25c423dba1c --- Changes | 3 ++- lib/Nagios/Plugin/Performance.pm | 16 +++++++++++++++- t/Nagios-Plugin-Performance-02.t | 13 +++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 t/Nagios-Plugin-Performance-02.t diff --git a/Changes b/Changes index 2b67941..c1f17ab 100644 --- a/Changes +++ b/Changes @@ -1,9 +1,10 @@ Revision history for Perl module Nagios::Plugin. -0.18 ?? +0.18 31st August 2007 - Fix error when parsing performance data where warn or crit are 0 - Optional _use_die flag to force nagios_die to call die instead of exit, so exceptions can be caught with an eval + - Convenience function to set use_die so you can run 'use Nagios::Plugin::Performance use_die => 1' 0.17 23rd March 2007 - bump version number again due to cpan indexing stupidity (Gavin) diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm index 63727c0..7ce5fa1 100644 --- a/lib/Nagios/Plugin/Performance.pm +++ b/lib/Nagios/Plugin/Performance.pm @@ -16,6 +16,12 @@ use Nagios::Plugin::Threshold; use Nagios::Plugin::Range; our ($VERSION) = $Nagios::Plugin::Functions::VERSION; +sub import { + my ($class, %attr) = @_; + $_ = $attr{use_die} || 0; + Nagios::Plugin::Functions::_use_die($_); +} + sub _parse { my $class = shift; my $string = shift; @@ -112,7 +118,7 @@ performance data. =head1 SYNOPSIS - use Nagios::Plugin::Performance; + use Nagios::Plugin::Performance use_die => 1; # Constructor (also accepts a 'threshold' obj instead of warning/critical) $p = Nagios::Plugin::Performance->new( @@ -162,6 +168,14 @@ parse_perfstring), for turning nagios performance output strings into their components, and a composition interface (via new), for turning components into perfdata strings. +=head1 USE'ING THE MODULE + +If you are using this module for the purposes of parsing perf data, you +will probably want to set use_die => 1 at use time. This forces +&Nagios::Plugin::Functions::nagios_exit to call die() - rather than exit() - +when an error occurs. This is then trappable by an eval. If you don't set use_die, +then an error in these modules will cause your script to exit + =head1 CLASS METHODS =over 4 diff --git a/t/Nagios-Plugin-Performance-02.t b/t/Nagios-Plugin-Performance-02.t new file mode 100644 index 0000000..c0c5a71 --- /dev/null +++ b/t/Nagios-Plugin-Performance-02.t @@ -0,0 +1,13 @@ + +use strict; +use Test::More tests => 3; +use_ok("Nagios::Plugin::Performance", use_die => 1); + +eval { Nagios::Plugin::Functions::nagios_die("Testing") }; +is( $@, "NAGIOS-PLUGIN-PERFORMANCE-02 UNKNOWN - Testing\n", "use_die correctly set on import"); + + +use_ok("Nagios::Plugin::Performance"); +eval { Nagios::Plugin::Functions::nagios_die("Test OK exit", 0) }; + +fail("Should not get here if code works correctly because prior nagios_die should have exited"); -- cgit v1.2.3-74-g34f1