Nagios website regex pattern check

Here's a simple perl script that uses curl to search for a regex pattern on a website.

It returns status values that are Nagios compatible. This means you can write a command definition for Nagios that looks like this:

# regex check
define command{
        command_name    check_regex
        command_line    /usr/lib/nagios/plugins/check_regex $ARG1$ $ARG2$
}

And write a couple of service definitions like this:

define service {
        host_name                       foo
        service_description           foo regex
        check_command              check_regex!http://www.google.com.au!"pages from Australia"
        use                                 generic-service
        contact_groups                admins
}

define service {
        host_name                       foo
        service_description           another foo regex
        check_command              check_regex!http://www.mig5.net!Drupal
        use                                 generic-service
        contact_groups                admins
}

And then you'll have Nagios reporting OK or CRITICAL results based on whether these patterns are found on the appropriate site.

The output is like this:

toot:~# /usr/lib/nagios/plugins/check_regex http://www.mig5.net Drupal
OK - looked for Drupal on http://www.mig5.net

toot:~# /usr/lib/nagios/plugins/check_regex http://www.mig5.net Drupaal
CRITICAL - looked for Drupaal on http://www.mig5.net

Here is check_regex

#!/usr/bin/perl
# Check a site and if regex not found, do $stuff
#
use strict;
use warnings;
use WWW::Curl::Easy;
 
# Get host and regex from stdin
my $url = $ARGV[0];
my $regex = $ARGV[1];
 
my $status = "";
my $help = "";
 
my %nagios_returnvalue = (
    'OK'       => 0,
    'WARNING'  => 1,
    'CRITICAL' => 2,
    'UNKNOWN'  => 3,
);
 
&help if $help;
&help unless $url;
&help unless $regex;
&help if $ARGV[2];
 
# Init the curl session
my $curl= WWW::Curl::Easy->new() or die "curl init failed!\n";
 
# Give curl the URL to use
$curl->setopt(CURLOPT_URL, $url);
 
# a subroutine which is called for each 'chunk' as the file is received.
sub body_callback {
    my ($chunk,$context)=@_;
    # add the chunk we received to the end of the array we've been given
    push @{$context}, $chunk;
    return length($chunk); # OK
}
 
# configure which subroutine to call when some data comes in
$curl->setopt(CURLOPT_WRITEFUNCTION, \&body_callback);
 
my @body;
 
# tell the subroutine which array to put the data into
$curl->setopt(CURLOPT_FILE, \@body);
if ($curl->perform() != 0) {
    print "Failed ::".$curl->errbuf."\n";
};
 
my $output = join("",@body);
if ($output =~ m/$regex/) {
    $status = "OK";
}
else {
  $status = "CRITICAL";
}
 
# create output for Nagios
my $msg = sprintf "%s - looked for %s on %s", $status, $regex, $url;
print "$msg\n";
 
exit $nagios_returnvalue{$status};
 
sub help {
    print <<"EOF";
Usage:
 
./check_regex <url> <regex>
EOF
    exit;
}

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <perl>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options

CAPTCHA

This question is for testing whether you are a human visitor and to prevent automated spam submissions.