RE: [nsp] Measuring latency between two points

From: Blaz Zupan (blaz@amis.net)
Date: Tue Apr 30 2002 - 14:03:29 EDT


> Can you send me that script. I'm looking to invoke something similar within
> our network to measure latency.

As I've received quite a few requests like the above, I'm sending this to the
list. Maybe someone will find this perl script useful. It needs the Net::SNMP
module. Tested only with the default perl installation on FreeBSD 4.5 and a
7206VXR running 12.2(7a).

You need to have a read/write SNMP community enabled on the router using
something like the commands below. x.x.x.x is of course the IP address of the
box you're running this script on.

        access-list 1 permit x.x.x.x
        snmp-server community test RW 1

The example below sends 50 packets of 100 bytes each to the selected IP
address from the selected router and the prints out the average ping time in
miliseconds and the success rate (percentage of packets that arrived at the
destination).

Hope you people find it useful for something. If you do something interesting
with it, drop me an e-mail.

Please no comments about how one might write more efficient (or clean) perl,
it was not written to be the best example of a perl script...

#! /usr/bin/perl
use strict;
use vars qw($session $error $response);
use Socket;

# number of packets to send
my $numofpackets = 50;

# packet size of sent packets
my $packetsize = 100;

# address to ping
my $ipaddress = '192.168.50.10';

# router to ping from
my $router = 'cisco.domain.com';

# SNMP community
my $community = 'test';

use Net::SNMP;

($session, $error) = Net::SNMP->session(
  -hostname => shift || $router,
  -community => shift || $community,
  -port => shift || 161
);

if (!defined($session)) {
    printf("ERROR: %s.\n", $error);
    exit 1;
}

$response = $session->set_request(('.1.3.6.1.4.1.9.9.16.1.1.1.16.333', INTEGER, 6,
                                   '.1.3.6.1.4.1.9.9.16.1.1.1.16.333', INTEGER, 5,
                                   '.1.3.6.1.4.1.9.9.16.1.1.1.15.333', OCTET_STRING, 'SLA',
                                   '.1.3.6.1.4.1.9.9.16.1.1.1.2.333', INTEGER, 1,
                                   '.1.3.6.1.4.1.9.9.16.1.1.1.3.333', OCTET_STRING, inet_aton($ipaddress),
                                   '.1.3.6.1.4.1.9.9.16.1.1.1.4.333', INTEGER, $numofpackets,
                                   '.1.3.6.1.4.1.9.9.16.1.1.1.5.333', INTEGER, $packetsize
                                  ));

if (!defined($response)) {
    printf("ERROR: %s.\n", $session->error());
    $session->close();
    exit 1;
}

# start ping
$response = $session->set_request('.1.3.6.1.4.1.9.9.16.1.1.1.16.333', INTEGER, 1);

my $finished;
while ($finished != 1) {
    my $oid;
    $oid = '.1.3.6.1.4.1.9.9.16.1.1.1.14.333';
    $response = $session->get_request($oid);
    $finished = $response->{$oid};
    sleep(1);
}

# take a look at the result
my $sndpkts_oid = '.1.3.6.1.4.1.9.9.16.1.1.1.9.333';
my $rcvpkts_oid = '.1.3.6.1.4.1.9.9.16.1.1.1.10.333';
$response = $session->get_request(($sndpkts_oid,$rcvpkts_oid));

if (!defined($response)) {
    printf("ERROR: %s.\n", $session->error());
    $session->close();
    exit 1;
}

my $sndpkts = $response->{$sndpkts_oid};
my $rcvpkts = $response->{$rcvpkts_oid};
my $successrate = $rcvpkts*100/$sndpkts;
my $avgpingtime;

if ($rcvpkts > 0) {
    my $avgpingtime_oid = '.1.3.6.1.4.1.9.9.16.1.1.1.12.333';

    $response = $session->get_request(($avgpingtime_oid,$sndpkts_oid,$rcvpkts_oid));

    if (!defined($response)) {
        printf("ERROR: %s.\n", $session->error());
        $session->close();
        exit 1;
    }
    $avgpingtime = $response->{$avgpingtime_oid};
} else {
    $avgpingtime = 0;
}

print "Average Ping Time = $avgpingtime\nSuccess Rate = $successrate%\n";

# cleanup
$response = $session->set_request('.1.3.6.1.4.1.9.9.16.1.1.1.16.333', INTEGER, 6);

$session->close();

exit 0;



This archive was generated by hypermail 2b29 : Sun Aug 04 2002 - 04:11:55 EDT