#!/usr/bin/perl -T -w # $Id: overview.cgi,v 1.1 2003/05/02 11:40:29 suter Exp $ # # Copyright (c) 2001 Mark Suter All rights reserved. # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # # This script gets the given page and for each "view" on this page, loads # that page and gets the uri of the first image. The output is a table of # all the images as anchors to the "view". # # This script should work with most Cricket configs and just needs to # be called with the uri of the target to overview, for example, # # http://www.example.com/cgi-bin/overview.cgi?uri=http://cricket.example.com/cricket/grapher.cgi?target=/servers # # There are three other options: # # cols=number How many columns in the displayed tables (default 2) # skip=number Skip this many images on each view (default 0) # mask=101010 Mask out some pages (default 1111...1111) # # [MJS 12 Mar 2002] Initial version # [MJS 15 Mar 2002] Multiple tables to speed older browser's rendering # [MJS 10 May 2002] Added the mask=010 option to control display # [MJS 15 May 2002] Put date first in title so it line wraps better use strict; use CGI; use LWP; use URI::Escape; use HTML::Entities; ## Trim leading and trailing whitespace sub trim($) { $_[0] =~ m{ \s* (.*) \s* }x; return $1; } ## Unbuffer our output $| = 1; ## Ready our CGI object my $q = CGI->new(); ## Start the page print $q->header(-expires => "+5m"), $q->start_html(-title => "Overview", -bgcolor => 'white'), "\n"; eval { ## Get Ready my $ua = LWP::UserAgent->new(keep_alive => 1, timeout => 10); ## The uri to provide an overview of my $top = HTML::Entities::encode trim uri_unescape $q->param('uri') or die "Missing the uri to overview!\n"; ## How many columns to present (left to right) my $cols = ( defined($q->param('cols')) and $q->param('cols') > 0 ) ? $q->param('cols') : 1 ; ## How many images to skip on each page my $skip = ( defined($q->param('skip')) and $q->param('skip') > 0 ) ? $q->param('skip') : 0 ; ## A mask to hide some graphs, default is inclusion my $i = 0; my %mask = defined($q->param('mask')) ? map { $i++, not $_ } split(//, $q->param('mask')) : (); ## Give a title print $q->h1(join "", "Overview as at ", scalar localtime, " of $top"), "\n"; ## Get the top-level page my $res = $ua->get($top); $res->is_success or die "Request failed: " . $res->message . "\n"; $_ = $res->content; ## Find and possibly display each Cricket view my ($seen, $displayed) = (-1, 0); while (m{ < a \s+ href = " ( [^"]+? grapher.cgi [^"]+? view=([^"]+?) ) " > }sgix) { my ($uri, $view) = ($1, trim uri_unescape($2)); ## Skip this view? exists($mask{++$seen}) and $mask{$seen} and next; ## Download the view page $res = $ua->get($uri); $res->is_success or die "Request failed: " . $res->message . "\n"; ## Start new table (for a row of $cols items)? $displayed % $cols == 0 and print "\n\n"; ## Output the desired image as a link to the page my $code = ($res->content =~ m{ ( < img .+? ) > }gix)[$skip] or die "No image in view $view!\n"; print "\n"; ## End row? $displayed++ % $cols == $cols - 1 and print "\n
$view
$code alt=\"$view\"/>
\n"; } ## Last row incomplete? $displayed % $cols != 0 and print "\n" x ($cols - $displayed % $cols), "\n\n"; }; $@ and print $q->p("There was a problem: $@"), "\n"; ## End the page print $q->end_html, "\n";