Hack72.Monitor VoIP Devices

Hack 72. Monitor VoIP Devices

The only thing worse than having a VoIP service outage is being the last to know about it.

It's the phone company's job to monitor traditional telephony links. Some legacy phone vendors, such as Avaya, even monitor their PBXs in the field via phone links back to their support headquarters. But no such convenience exists for downtime-wary VoIP administrators. Thanks to Perl, though, good system monitoring for VoIP is within your grasp. In this hack, you'll develop a Perl script that monitors SIP hosts over the network and reports back on their availability.

This script determines that the SIP host is alive by sending a SIP OPTIONS packet to the remote host and receiving a response. This determines not only whether the host is reachable via the network, but also whether the SIP application on the other end is listening and responding to requests.

6.2.1. The Code

Your Perl development environment will need the Time::HiRes module for this hack. Grab it from http://search.cpan.org/dist/Time-HiRes/:

 #!/usr/bin/perl use IO::Socket; use POSIX 'strftime'; use Time::HiRes qw(gettimeofday tv_interval); use Getopt::Long; use strict; my $USAGE = "Usage: sip_ping.pl [-v] [-t] [-s <src_host>] [-p <src_port] <hostname>"; my $RECV_TIMEOUT = 5; # how long in seconds to wait for a response my $sock = IO::Socket::INET->new(Proto => 'udp', LocalPort=>'6655', ReuseAddr=>1)    or die "Could not make socket: $@"; # options my ($verbose, $host, $my_ip, $my_port, $time); GetOptions("verbose|v" => \$verbose,    "source-ip|s=s" => \$my_ip,    "source-port|p=n"=> \$my_port,    "time|t" => \$time) or die "Invalid options:\n\n$USAGE\n"; # figure out who to ping my $host = shift(@ARGV) or die $USAGE; my $dst_addr = inet_aton($host) or die "Could not find host: $host"; my $dst_ip = inet_ntoa($dst_addr); my $portaddr = sockaddr_in(5060, $dst_addr); # figure out who we are $my_ip = "" unless defined($my_ip); $my_port = "6655" unless defined($my_port); # callid is just 32 random hex chars my $callid = ""; $callid .= ('0'..'9', "a".."f")[int(rand(16))] for 1 .. 32; # today's date my $date = strftime('%a, %e %B %Y %I:%M:%S %Z',localtime()); # branch id - see rfc3261 for more info, using time() for uniqueness my $branch="z9hG4bK" . time(); my $packet = qq(OPTIONS sip:$dst_ip SIP/2.0 Via: SIP/2.0/UDP $my_ip:$my_port;branch=$branch From: <sip:ping\@$my_ip> To: <sip:$host> Contact: <sip:ping\@$my_ip> Call-ID: $callid\@$my_ip CSeq: 102 OPTIONS User-Agent: sip_ping.pl Date: $date Allow: ACK, CANCEL Content-Length: 0 ); # send the packet print "Sending: \n\n$packet\n" if $verbose; send($sock, $packet, 0, $portaddr) == length($packet)     or die "cannot send to $host: $!"; my $send_time = [gettimeofday()]; # start the stopwatch my $elapsed; # get the response eval { local $SIG{ALRM} = sub { die "alarm time out" }; alarm $RECV_TIMEOUT; $portaddr = recv($sock, $packet, 1500, 0) or die "couldn't receive: $!"; $elapsed = tv_interval($send_time); # stop the stopwatch alarm 0; 1; } or die($@); # print our output if ($verbose) { printf("After (\%0.2f ms), host said: \n\n\%s\n", $elapsed*1000, $packet);  } elsif ($time) { printf("%0.2f\n", $elapsed*1000); } else { print("$host is alive\n"); } 

6.2.2. Running the Code

Execute this script using a command like this:

 # ./sip_ping.pl 

If you see output like this, the SIP host is indeed running: is alive 

Using the -v option with this command gives more verbose outputspecifically, the contents of the SIP response and the round-trip latency (in milliseconds) of the SIP message exchange.

You can work this technique into other monitoring tools and run it periodically to inform yourself of any outages. Because the majority of commercial VoIP service providers use the SIP protocol, it can be quite useful. You can even use this script to monitor remote SIP handsets and softphone applications, because all SIP hosts, if implemented correctly, respond in the same manner to the request this script sends.

Brian Degenhardt

VoIP Hacks
VoIP Hacks: Tips & Tools for Internet Telephony
ISBN: 0596101333
EAN: 2147483647
Year: 2005
Pages: 156

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net