Monitoring Services with Nagios::Plugin

There are a lot of people who say, “if it isn’t monitored, then it isn’t a service.” The problem is that I don’t think enough people outside of the systems world believe that or even understand why its said. I think the primary offenders here are developers. It isn’t because they don’t know better, but typically developers just want to get the application up and running and then move on to developing the next thing. I also think there is some fault on the side of the administrators and the managers not insisting that part of the completed version of a project includes monitoring. But I don’t want to harp on this as much as I would like to show just how easy it is to compensate here by taking advantage of Nagios::Plugin.

Nagios::Plugin is a Perl module that can be used to shortcut how easy it is to add a custom check for your application to your Nagios monitoring infrastructure. It is so easy to write a Nagios plugin that even with comments and performance data, I have written one to check the alive-ness in under 50 lines.

use Nagios::Plugin;
use Time::HiRes qw( gettimeofday tv_interval );

my $np = Nagios::Plugin->new(
    shortname => 'template',
);

 # We need a URL to test
 my $url = "http://www.myapp.com/";

 # Create the UserAgent
 my $ua = LWP::UserAgent->new (
    agent       =>  'Nagios Application Check',
    from        =>  'webmaster@myapp.com',
 );

 # Change the timeout to 10 seconds instead of 3 min (180 seconds)
 $ua->timeout( 10 );

 my $t0 = [gettimeofday];
 my $resp = $ua->get($url);
 my $total = tv_interval($t0, [gettimeofday]);

 my $content;
 if (!$resp->is_success) {
    $np->nagios_exit( CRITICAL, $resp->status_line);
 }

 # Time it
 $np->add_perfdata(
    label => "time",
    value => "${total}",
    uom => "s",
 );

 $np->nagios_exit(OK, "Test Suggestions Found");

exit 0;

By reading the Pod documentation, you’ll also find that you can get a lot more specific. You can add command line switches, lots of performance data, and my favorite, some pretty extensive success, warning or critical messages that can be displayed on the dashboard.