"David Amiel" <[EMAIL PROTECTED]> writes:
> I would like to know if someone has tried to simulate a website
> browsing and monitor it with mon ?

In a limited way.

I once had to monitor a site whose WebLogic instance would
occasionally run out of jdbc handles.  Instead of having the
programmers fix the real problem management felt it was better that
operations figure out how to monitor a "transaction"---that is, some
interaction that would hit the database.

The site used cookies, so you had to make a first request to the
"head" page, and then re-send the cookies you got to another URL to
"log-in".

The basic script is attached.  I include it here mostly as a
proof-of-concept.  There are, looking at it, many things I would do
differently now---I'd dump all the info about parameters into a config
file, etc.

But it worked, and I hope it'll be helpful to others.  I use a
much-bastardized version of this script for bench-testing apps, where
I can feed URLs on the command line and just have it spew out the
result with cookie-based session support and so forth.

Mike.
#!/usr/local/bin/perl -w

use strict;
use Data::Dumper;
use Getopt::Long;
use HTTP::Cookies;
use LWP::UserAgent;

# Whether to do all sites
my $all = 0;

# Whether to debut the transactions
my $debug = 0;

# Whether to provide help
my $help = 0;

# Whether to be verbose
my $verbose = 0;

# Figure out what we're doing
GetOptions (all => \$all, debug => \$debug, help => \$help, verbose => \$verbose);

#
# This is a hash of sitenames to test (just the hostname as the key,
# that being what mon is going to pass in).
#
# Each "site" is a reference to an array of "steps".  A "step" is a
# hashref containing, at minimum, a "request" key with an URL to be
# queried as its value.  It may also have a "result" key which is text
# that is looked for in the response content---if the text doesn't
# appear, that request is considered unsuccessful.
#

my %sites = ('label' => [(request => 'http://something/',
                          result => 'What you're looking for')]);

# If they asked for help
if ($help) {

    my $sites;

    foreach my $site (keys %sites) {
        $sites .= "  $site\n";
    }

    die <<END
usage: endtoend.monitor [-a|--all] [-d|--debug] [site...]

Valid sites are:
$sites
END
}

# Just do them all
@ARGV = keys %sites if ($all);

my %errors;

# Look at all specified sites
foreach my $site (sort @ARGV) {

    # If we know about the site
    if ($sites{$site}) {

        print "$site..." if ($verbose);

        # Create a cookie jar
        if (my $cookies = HTTP::Cookies->new (autosave => 1, file => 
"/var/tmp/$site.cookies", ignore_discard => 1)) {

            # Try to load our cookie jar if it's non-zero size
            -s "/var/tmp/$site.cookies" && $cookies->load;

            # Create a new user agent
            if (my $agent = LWP::UserAgent->new (cookie_jar => $cookies)) {

                # While there are more URLs in the sequence
                foreach my $request (@{$sites{$site}}) {

                    # Grab the URL
                    my $URL = $request->{request};

                    # Make the request
                    if (my $response = $agent->get ($URL)) {

                        if ($debug) {
                            print Dumper $response;
                            print "\n", "-" x 80, "\n";
                        }

                        # If the request was successful
                        if ($response->is_success) {

                            # If there's text to look for, and it's not found
                            if (defined $request->{result} && $response->content !~ 
m/$request->{result}/) {
                                push @{$errors{$site}}, {$URL => "Result text not 
found:\n" . $response->content};

                                # No need to keep going for this site
                                last;
                            }
                        } else {

                            # At the very least take the library's message
                            my $message = $response->message;

                            # Append the content if there is any
                            $message .= ":\n" . $response->content if 
($response->content);

                            # Push our error
                            push @{$errors{$site}}, {$URL => $message};

                            # No need to keep going for this site
                            last;
                        }
                    } else {
                        push @{$errors{$site}}, {$URL => $response->message};
                    }
                }

                # If there were errors
                if ($errors{$site}) {
                    print "failed\n" if ($verbose);
                } else {
                    print "successful\n" if ($verbose);
                }
            } else {
                push @{$errors{$site}}, "Couldn't create UserAgent";
            }
        } else {
            push @{$errors{$site}}, "Couldn't create cookie jar";
        }
    } else {
        push @{$errors{$site}}, "Don't know about $site";
    }
}

# If there were any errors
if (%errors) {

    # Print the list of broken hosts
    print join (' ', sort keys %errors), "\n" unless ($verbose);

    # Print the detail for the sites
    foreach my $site (sort keys %errors) {

        if ($debug) {
            print Dumper $errors{$site};
        }

        print "$site had the following issues:\n\n";

        # Iterate over the list of errors
        foreach my $error (@{$errors{$site}}) {

            # If it's a reference
            if (ref $error eq "HASH") {

                # Iterate over the keys
                foreach my $key (sort keys %{$error}) {

                    print "$key -> $error->{$key}\n";
                }
            } else {
                print "$error\n";
            }
        }
    }

    # Exit with errors
    exit 1;
} else {

    # Exit flawlessly
    exit 0;
}

Reply via email to