How do I use the Nagios::Plugin perl module?

Posted by tonvoon on 9 October 2007 - 8:22am

The Nagios::Plugin perl module can be obtained from two main locations:

If you install from the nagiosplug tarball, the perl module will be installed in $prefix/perl.

If you install from CPAN, the perl module will be installed by default into your perl's system directories.

To write your plugin, you should start it with:

use FindBin;
use lib "$FindBin::Bin/../perl/lib";
use Nagios::Plugin;

This bit of code tells perl to look for the Nagios::Plugin module in a directory relative to where the plugin is executed - this is a hard dependency. If Nagios::Plugin is not found there, perl's system directories will be searched.

This approach allows a system administrator to decide whether they want Nagios::Plugin installed via system directories or within the $prefix area of the plugins.

Comment viewing options
Select your preferred way to display the comments and click "Save settings" to activate your changes.
Hello Plug-in Developers, so

Hello Plug-in Developers,

so far I haven't been using your Nagios::Plugin module for my own custom Perl plug-ins.
This wasn't necessary because my plug-ins were never intended for distribution.

However, I appreciate your efforts in providing us with a CPAN module to make it easier
for us non-developers to cling to the plug-in developer's guidelines.

Nevertheless, I find it a bit awkward that you advise the Perl plug-in authors to first pull in that FindBin module (which I have never used so far) just to grope around in the vicinity of the plug-in's location for possible non-standard Perl module locations.

Having configured my CPAN/Config.pm and being root on my hosts
I naturally prefer the perl -MCPAN 'install Nagios::Plugin way of installing Perl modules, no matter what various Linux distro's or Unices' package managers deem appropriate as installation target for various Perl modules.

Thus I would suggest to first try to load Nagios::Plugin and only if this fails
doing the poking with FindBin.
Of course one then cannot use the modules as this is already happening at compile time.

(sorry, preview shows that indentations get lost despite html tagging)

e.g.


#!/usr/bin/perl

use strict;
use warnings;

eval { require Nagios::Plugin && Nagios::Plugin->import };
if ($@) {
# assume a relatively recent Perl where FindBin is in core
no warnings;
require FindBin;
unshift @INC, map "$FindBin::Bin/$_",
qw(perl lib perl/lib ../perl ../lib ../perl/lib);
local $" = "\n\t";
eval {
require Nagios::Plugin && Nagios::Plugin->import
}
or die "Couldn't locate Nagios::Plugin in \n\t@INC\n";
}

Posted by ragro on 19 October 2007 - 12:23pm
Hi ragro, The idea is that

Hi ragro,

The idea is that N::P could be in ../perl/lib, but if it isn't then the system directories would be used.

So just continue to install N::P on your systems in the standard perl directories and make sure ../perl/lib doesn't contain N::P and there should be no difference.

I fail to see how your suggestion is nicer: there is still a dependency on FindBin.

Ton

Posted by tonvoon on 25 October 2007 - 8:01pm
Hi, and thanks for your

Hi, and thanks for your feedback. I haven't really looked closely when they decided how to include N::P in Nagios-Plugins, but my understanding (or at least how I think it should be) is that we want to first load any N::P module that came with the plugin installation.

While distributions will likely include a system-wide N::P, by default manual installation from source should install and use a local N::P module so:
1. We won't clash with any previously installed module
2. If there's any bug we can be pretty confident it's not caused by an outdated version of the module.
There will also be an option to install it system-wide if you don't want to use CPAN.

OTOH I strongly recommend any external plugin (Nagiosexchange, etc) include N::P directly. The FindBin method (or any other way we may use when we actually implement it) should in most cases be specific to Nagios-Plugins.

I'll talk with Ton to see if he's willing to clarify this post.

Posted by dermoth on 24 July 2009 - 11:20pm