use OpenSIPS qw ( log );
use OpenSIPS::Constants;
use DBI;
$my_database=voip;
$my_user=voip;
$my_password=password;
$my_host="127.0.0.1";
$my_platform="Pg";
# For MYSQL database use
# $my_platform="mysql";
sub messagedump {
my $m = shift;
my $name;
my $number;
my $localUA;
my $remoteUA;
my $callID;
my $packetLoss;
my $moslq;
my $moscq;
my $rtd;
my $timeStart;
my $timeStop;
# open F,">>/tmp/opensips-perl-messagedump";
my $fh = $m->getHeader("From");
if($fh =~ /"(.*)"\s+<sip:(\d+)@.*$/)
{
$name=$1;
$number=$2;
}
# print F "NAME:$name\nNUMBER:$number\n";
my $mb = $m->getBody();
# print F "$mb\n";
if($mb =~ /.*CallID:(.+).*/)
{
$callID = $1;
$callID =~ s/\@.*//g; #remove the @symbol and everthing after
}
if($mb =~ /.*LocalAddr:IP=(.+)\s+PORT.*/)
{
$localUA = $1;
}
if($mb =~ /.*RemoteAddr:IP=(.+)\s+PORT.*/)
{
$remoteUA = $1;
}
if($mb =~ /.*RTD=(.+)\s+ESD.*/)
{
$rtd = $1;
}
if($mb =~ /.*PacketLoss:NLR=(\d+\.\d+).*/)
{
$packetLoss=$1;
}
if($mb =~ /.*MOSLQ=(\d.\d)\s+MOSCQ=(\d.\d).*/)
{
$moslq = $1;
$moscq = $2;
}
if($mb =~ /.*START=(.+)\s+STOP=(.+).*/)
{
$timeStart = $1;
$timeStop = $2;
}
# print F "CallID:$callID\n";
# print F "timeStart:$timeStart\n";
# print F "timeStop:$timeStop\n";
# print F "lUA:$localUA\n";
# print F "rUA:$remoteUA\n";
# print F "rtd:$rtd\n";
# print F "moslq:$moslq\n";
# print F "moscq:$moscq\n";
# print F "loss:$packetLoss\n";
my $insert = "INSERT INTO qos VALUES ('$callID','$name','$number','$timeStart','$timeStop','$localUA','$remoteUA','$rtd','$moslq','$moscq','$packetLoss')";
# print F "$insert\n";
# close(F);
my $cn = DBI->connect("DBI:$my_platform:database=$my_database;host=$my_host", "$my_user", "$my_password");
# update record with existing callid if any, otherwise insert the new call quality data
if($perl_my$cn) {
my $q_str;
my $testQuery = $perl_my$cn->prepare("SELECT rtd from qos where callid='$callID'");
$testQuery->execute();
if($testQuery->rows() > 0){
$q_str = "UPDATE qos set timestop='$timeStop', rtd='$rtd', moslq='$moslq', moscq='$moscq', packetloss='$packetLoss' where callid='$callID'";
}
else {
$q_str = "INSERT INTO qos VALUES ('$callID','$name','$number','$timeStart','$timeStop','$localUA','$remoteUA','$rtd','$moslq','$moscq','$packetLoss')";
}
my $query = $perl_my$cn->prepare($q_str);
$query->execute();
}
#email sending
if($moslq < 2.0){
$Mailer = '/usr/sbin/sendmail -t';
open MAIL,"|$Mailer";
print MAIL <<THE_EMAIL;
From: root\@sip.example.com
To: noc\@example.com
Subject: Poor Voice Quality
Content-type: text/plain\n\n
The user $name experienced a MOS score of $moslq
on a call ending at: $timeStop.\n
This call was between $localUA and $remoteUA.
THE_EMAIL
close MAIL;
}
}
return 1;
}
|