[Nagiosplug-devel] Perl plugins that fail gracefully by reporting missing modules.
Stanley Hopcroft
Stanley.Hopcroft at IPAustralia.Gov.AU
Fri Mar 5 23:00:30 CET 2004
Dear Ladies and Gentlemen,
I am writing to suggest that Perl plugins can be made to fail gracefully
when the pre requisite modules are not found on the host.
This may help to prevent
1 the FAQ about especially /contrib plugins that
fail when Perl cannot find utils.pm in the standard Perl search paths.
2 distress when a plugin is put into production after testing in a
directory containing the prereqs and thereupon no longer has access to
those modules.
Here is an example from an unpublished plugin check_traceroute (someone
started this but it vanished; this is a prototype that is waiting for
some feedback from the Net::Traceroute author).
stan> perl check_traceroute
This plugin requires the 'Net::Traceroute' module from a CPAN near you.
stan> echo $?
3
stan>
Lets give it Net::Traceroute shall we
stan> perl check_traceroute
This plugin requires the 'Net::Traceroute' module from a CPAN near you.
stan> echo $?
3
stan> cat > Net/Traceroute.pm
1
^D
stan> perl check_traceroute
This plugin requires the 'utils.pm' module from the Nag plugin
distro. Please install it in the Perl @INC path or add a 'use lib
q(path_to_utils.pm)'.
stan>
#!/usr/bin/perl -w
use strict;
eval { require utils_1_4_0alpha1 } ;
if ($@) {
print "This plugin requires the 'utils.pm' module from the Nag
plugin distro. Please install it in the Perl \@INC path or add a 'use
lib q(path_to_utils.pm)'.\n" ;
exit 0 ;
};
use vars qw(%ERRORS $TIMEOUT) ;
import utils qw(%ERRORS $TIMEOUT &print_revision &support &usage) ;
eval { require Net::Traceroute } ;
if ($@) {
print "This plugin requires the 'Net::Traceroute' module from a
CPAN near you.\n" ;
exit $ERRORS{UNKNOWN} ;
} ;
The eval { } _is_ a performance hit, but it may be worth it. If the
performance hit is thought too severe, the checks could be enabled by an
option ....
Yours sincerely.
--
------------------------------------------------------------------------
Stanley Hopcroft
------------------------------------------------------------------------
'...No man is an island, entire of itself; every man is a piece of the
continent, a part of the main. If a clod be washed away by the sea,
Europe is the less, as well as if a promontory were, as well as if a
manor of thy friend's or of thine own were. Any man's death diminishes
me, because I am involved in mankind; and therefore never send to know
for whom the bell tolls; it tolls for thee...'
from Meditation 17, J Donne.
More information about the Devel
mailing list