KS-Soft. Network Management Solutions
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister    ProfileProfile    Log inLog in 

Remote Agent Shell Script for HTTP GET URL - Perl Script

 
Post new topic   Reply to topic    KS-Soft Forum Index -> Library
View previous topic :: View next topic  
Author Message
pmc



Joined: 25 Mar 2004
Posts: 8

PostPosted: Thu Apr 01, 2004 10:30 am    Post subject: Remote Agent Shell Script for HTTP GET URL - Perl Script Reply with quote

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:

#!/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");
Back to top
View user's profile Send private message
ruscoep



Joined: 16 Jan 2004
Posts: 14

PostPosted: Tue Mar 28, 2006 9:58 am    Post subject: Reply with quote

Hi,

Thanks for the script, is there a way of incorporating a proxy into the HTTP GET?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    KS-Soft Forum Index -> Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group

KS-Soft Forum Index