]> git.etc.gen.nz Git - spong.git/commitdiff
check content on web pages (new module by Adrian Chadd)
authorJan Willamowius <jan@willamowius.de>
Tue, 4 Oct 2005 09:45:22 +0000 (09:45 +0000)
committerJan Willamowius <jan@willamowius.de>
Tue, 4 Oct 2005 09:45:22 +0000 (09:45 +0000)
src/lib/Spong/Network/plugins/check_webcontents [new file with mode: 0644]

diff --git a/src/lib/Spong/Network/plugins/check_webcontents b/src/lib/Spong/Network/plugins/check_webcontents
new file mode 100644 (file)
index 0000000..9bc9d77
--- /dev/null
@@ -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 <adrian@ucs.uwa.edu.au>
+# $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, "<couldn't connect>");
+               }
+               # 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;