From: Jan Willamowius Date: Tue, 4 Oct 2005 09:45:22 +0000 (+0000) Subject: check content on web pages (new module by Adrian Chadd) X-Git-Tag: spong-2_8_0-beta3~10 X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d11b80ca18c4d6e4fb289bdda098e7dd90b9b019;p=spong.git check content on web pages (new module by Adrian Chadd) --- diff --git a/src/lib/Spong/Network/plugins/check_webcontents b/src/lib/Spong/Network/plugins/check_webcontents new file mode 100644 index 0000000..9bc9d77 --- /dev/null +++ b/src/lib/Spong/Network/plugins/check_webcontents @@ -0,0 +1,71 @@ +#!/usr/bin/perl -w + +use LWP::UserAgent; + +# +# Simple plugin to check whether a page has the given content +# on it. Its useful for checking whether various dynamically +# generated pages work. +# +# Adrian Chadd +# $Id: check_webcontents,v 1.1 2005/10/04 09:45:22 willamowius Exp $ +# + + +$PLUGINS{'webcontents'} = \&check_webcontents; + +# +# Configuration parameters: +# +# +# Under the HOST entry in spong.hosts, have: +# * checkurl => "url-to-check", +# * checkfor => "perl regexp to match entire HTTP response on" +# * checklist => [ ( checkurl => url, checkfor => string ), ... ] +# + +# +# If any of them are failures, RED is returned. + +sub check_webcontents { + my ($host) = @_; + my ($message, $colour, $summary); + + $message = ""; + $summary = ""; + $colour = "green"; + + my ($l) = $HOSTS{$host}->{'checklist'}; + foreach (@$l) { + my ($url) = $_->{'checkurl'}; + my ($checkfor) = $_->{'checkfor'}; + &main::debug("check_webcontents - checking $host: $url for $checkfor"); + my ($ua) = LWP::UserAgent->new(); + my ($response) = $ua->get($url); + # Fail, response red, couldn't fetch + if (! $response->is_success) { + $summary .= "$url: could not connect\n"; + $message .= "$url: could not connect\n"; + $colour = "red"; + &main::debug("check_webcontents - red, $summary"); + return ("red", $summary, ""); + } + # Check contents + $message = $response->as_string; + if ($message =~ /$checkfor/) { + $message = "$url: OK, $checkfor' found"; + } else { + $summary .= "$url: FAIL, '$checkfor' not found"; + $message .= "$url: FAIL, '$checkfor' not found"; + $colour = "red"; + } + + if ($summary eq "") { + $summary = "ALL OK"; + } + } + &main::debug("check_webcontents - $colour, $summary"); + return ($colour, $summary, $message); +}; + +1;