sip.sh

Author Eric Meddaugh
Compatibility Xymon 4.2
Requirements Perl
Download None
Last Update 2008-09-11

Monitor SIP on an Asterisk server. Needs access to UDP port 5060.

Client configuration

None.

Server configuration

Add following to the hobbitlaunch.cfg file

[sip]
        ENVFILE /home/hobbit/server/etc/hobbitserver.cfg
        NEEDS hobbitd
        CMD  $BBHOME/ext/sip.sh
        LOGFILE $BBSERVERLOGS/sip.log
        INTERVAL 1m

Add the “sip” tag in bb-hosts

Monitor Code

sip.sh – Place in ext directory, this is the main script hobbit calls.

Show Code ⇲

Hide Code ⇱

#!/bin/sh
 
COLUMN=sip
TIMEOUT=10
 
$BBHOME/bin/bbhostgrep --no-down ${COLUMN} |
while read L
do
     set $L
     IP="$1"
     HOSTNAME="$2"
     COLOR=green
 
# linux use "/usr/bin/time -p" instead of ptime
     /bin/ptime $BBHOME/ext/sip_ping.pl $IP >$BBTMP/${COLUMN}.out 2>&1
     SECONDS=`egrep "^real" $BBTMP/${COLUMN}.out | awk '{printf $2}'`
     OK=`/bin/egrep -c "alive" $BBTMP/${COLUMN}.out`
     BAD=`/bin/egrep -c "timeout" $BBTMP/${COLUMN}.out`
 
     if [ ${OK} -eq 0 ] ; then
        COLOR=red
        MSG="SIP query failed
 
`cat $BBTMP/${COLUMN}.out | egrep -vi user`
 
Seconds: ${SECONDS}
"
     else
        COLOR=green
        MSG="SIP query succeeded
 
`cat $BBTMP/${COLUMN}.out | egrep -vi user`
 
Seconds: ${SECONDS}
"
  fi
 
  $BB $BBDISP "status $HOSTNAME.$COLUMN $COLOR `date`
 
  $MSG"
 
  $RM $BBTMP/${COLUMN}.out
done
 
exit 0


sip_ping.pl – Place in ext folder

Show Code ⇲

Hide Code ⇱

#!/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 = "127.0.0.1" unless defined($my_ip);
$my_port = "6655" unless defined($my_port);
 
# callid is just 32 randomn 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 via rfc3261 pour plus d?infos, avec
# utilisation de time( ) pour l?unicité
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 reponse
eval
{
  local $SIG{ALRM} = sub { die "Host timeout" };
  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($@);
 
# affiche la sortie
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 ");
  printf("(%0.2fms)\n", $elapsed*1000);
}
  • 2008-09-11
    • Initial release
  • monitors/asterisk-sip.txt
  • Last modified: 2009/11/23 05:31
  • (external edit)