Remote Agent Shell Script for HTTP GET URL - Perl Script

If you have information, script, utility, or idea that can be useful for HostMonitor community, you welcome to share information in this forum.
Post Reply
pmc
Posts: 8
Joined: Thu Mar 25, 2004 5:55 pm

Remote Agent Shell Script for HTTP GET URL - Perl Script

Post by pmc »

I needed to have remote agents that did http gets of various URL's, so I could measure response times from various remote locations in a wide area network. Unfortunately, the RMA program doesn't support the URL or HTTP tests.

So, I created a Perl script that does it for me, and then call it from shell script. It does two things - tests whether it can successfully grab the specified URL, and measures the response time. I used the perl2exe program (www.indigostar.com) to convert the Perl script into a single program so it was easy to copy to the remote machine.

To set it up in Hostmon, specify the "Shell Script" test, and set it to call this Perl executable. It accepts two command line parameters.
1. the URL to grab
2. <optional> a phrase to match - if it doesn't find this phrase in the returned page, it fails.

Here's the script, for anyone who would like to use it:

Code: Select all

#!/usr/bin/perl -w
#
########################################################################################################
########################################################################################################
# Program:  hm_webget.perl
# Purpose:  
#
# Author:   jpm
# History:  3/22/04 - created
########################################################################################################
########################################################################################################

# Module includes
use HTTP::Request;
use LWP::UserAgent;
use Time::HiRes qw(gettimeofday tv_interval);

# Data declarations
my ($url, $match_phrase, $hostmon_result);
my ($ua, $request, $response);
my ($time0, $time_elapsed);

# Get the command line arguments.
if (@ARGV < 1)
{
    die "$0: (Usage) perl hm_webget.perl url match_phrase\n";
};
$url = $ARGV[0];

$match_phrase = "NONE";
if (@ARGV == 2)
{
    $match_phrase = $ARGV[1];
}

#############################################################

# Set the starting time
$time0 = [gettimeofday];

# Grab the page from the remote URL
$ua = new LWP::UserAgent;
$request = new HTTP::Request GET => "$url";
$response = $ua->request($request);

# Calculate the elapsed time
$time_elapsed = tv_interval($time0) * 1000.0;
$time_elapsed = sprintf("%.0f",$time_elapsed);


# Print out for debugging info.
# print "Response:\n************\n";
# print $response->content;
# print "\n**********\n";

# Check status of request
if (!$response->is_success)
{
    print "scriptres:Bad:$time_elapsed ms";
    die();
}
else 
{
    $hostmon_result = "Host is alive";
}

# Check match_phrase if specified
if (@ARGV == 2)
{
    if ($response->content =~ /$match_phrase/)
    {
        $hostmon_result = "Host is alive";
    }
    else 
    {
        $hostmon_result = "Bad contents";
    }
}

# Print result to standard output for Hostmon to read.
print "scriptres:$hostmon_result:$time_elapsed ms\n";
close (STDOUT);

die("$0 completed normally");
User avatar
ruscoep
Posts: 14
Joined: Fri Jan 16, 2004 5:15 am

Post by ruscoep »

Hi,

Thanks for the script, is there a way of incorporating a proxy into the HTTP GET?
Post Reply