#!/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 EOF exit; }