changed VD_hangup.agi to use AGI output options from servers table

git-svn-id: svn://192.168.202.10@173 3d104415-ff17-0410-8863-d5cf3c621b8a
This commit is contained in:
mattf
2006-08-15 16:31:41 +00:00
parent 9755bd895b
commit e805f91f15
+101 -73
View File
@@ -1,6 +1,6 @@
#!/usr/bin/perl
#
# VD_hangup.agi version 0.3 *DBI-version*
# VD_hangup.agi version 0.4 *DBI-version*
#
# NOTE: This script is only needed on Asterisk 1.2.X and higher systems
#
@@ -28,10 +28,27 @@
# 60615-1101 - Fixed log updates for CLOSER transfer from fronter calls
# 60719-1118 - changed to use DBI-DBD::mysql
# - changed to use /etc/astguiclient.conf for configs
# 60815-1210 - added output options check from database
#
$V = 1; # set to 1 for verbose mode
$M = 1; # set to 1 for 2 line messages mode
$script = 'VD_hangup.agi';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$Fhour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$start_time=$now_date;
$CIDdate = "$mon$mday$hour$min$sec";
$tsSQLdate = "$year$mon$mday$hour$min$sec";
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
$SQLdateBEGIN = $SQLdate;
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
@@ -71,42 +88,44 @@ foreach(@conf)
$i++;
}
# Customized Variables
$server_ip = $VARserver_ip; # Asterisk server IP
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
use DBI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$Fhour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$start_time=$now_date;
$CIDdate = "$mon$mday$hour$min$sec";
$tsSQLdate = "$year$mon$mday$hour$min$sec";
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
$SQLdateBEGIN = $SQLdate;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($V) {print STDERR "Perl Environment Dump:\n";}
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($V) {print STDERR "$i|$ARGV[$i]|\n";}
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
@@ -150,17 +169,13 @@ if ($V) {print STDERR "Perl Environment Dump:\n";}
}
}
}
else
{
if ($V) {print "no command line options set\n";}
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($V)
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
@@ -199,32 +214,35 @@ if ( (
}
if ($V)
{
if ($AGILOG) {$agi_string = "AGI Environment Dump:"; &agi_output;}
if ($V) {print STDERR "AGI Environment Dump:\n";}
foreach $i (sort keys %AGI) {
if ($V) {print STDERR " -- $i = $AGI{$i}\n";}
}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
$VDADcampaign='';
$VDADphone='';
$VDADphone_code='';
if ($V) {print STDERR "AGI Environment Dump: |$unique_id|$channel|$extension|$type|$callerid|$calleridname|$priority|\n";}
if ($DEBUG =~ /^DEBUG$/)
{
### open the error out file for writing ###
open(out, ">>$PATHlogs/HANGUP_test-output.txt")
|| die "Can't open $PATHlogs/HANGUP_test-output.txt: $!\n";
### open the hangup cause out file for writing ###
open(out, ">>$PATHlogs/HANGUP_cause-output.txt")
|| die "Can't open $PATHlogs/HANGUP_cause-output.txt: $!\n";
print out "$now_date|$hangup_cause|$dialstatus|$dial_time|$ring_time|$unique_id|$channel|$extension|$type|$callerid|$calleridname|$priority|\n";
close(out);
}
else {print STDERR "\nDEBUG: $DEBUG\n";}
else
{
if ($AGILOG) {$agi_string = "DEBUG: $DEBUG"; &agi_output;}
}
$callerid =~ s/\"//gi;
@@ -232,54 +250,46 @@ $CIDlead_id = $callerid;
$CIDlead_id = substr($CIDlead_id, 11, 9);
$CIDlead_id = ($CIDlead_id + 0);
if ($V) {print STDERR "\nVD_hangup : $callerid $channel $priority\n";}
if ($M) {print STDERR "+++++ VD hangup START : |$unique_id|$channel|$extension|$type|$callerid|$CIDlead_id|$now_date|$AST_ver|$priority|$calleridname|\n";}
if ($AGILOG) {$agi_string = "VD_hangup : $callerid $channel $priority $CIDlead_id"; &agi_output;}
if ($channel =~ /^Local/)
{
if ( ($PRI =~ /^PRI$/) && ($callerid =~ /\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/) && ( ($dialstatus =~ /BUSY/) || ( ($dialstatus =~ /CHANUNAVAIL/) && ($hangup_cause =~ /^1$|^28$/) ) ) )
{
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
if ($dialstatus =~ /BUSY/) {$VDL_status = 'B'; $VDAC_status = 'BUSY';}
if ($dialstatus =~ /CHANUNAVAIL/) {$VDL_status = 'DC'; $VDAC_status = 'DISCONNECT';}
$stmtA = "UPDATE vicidial_auto_calls set status='$VDAC_status' where callerid = '$callerid';";
if($M){print STDERR "\n\n|$stmtA|\n";}
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($M) {print STDERR "-- VDAC update: |$affected_rows|$CIDlead_id\n";}
if ($AGILOG) {$agi_string = "-- VDAC update: |$affected_rows|$CIDlead_id"; &agi_output;}
$stmtA = "UPDATE vicidial_list set status='$VDL_status' where lead_id = '$CIDlead_id';";
if($M){print STDERR "\n\n|$stmtA|\n";}
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($M) {print STDERR "-- VDAD vicidial_list update: |$affected_rows|$CIDlead_id\n";}
if ($AGILOG) {$agi_string = "-- VDAD vicidial_list update: |$affected_rows|$CIDlead_id"; &agi_output;}
$stmtA = "UPDATE vicidial_log set status='$VDL_status' where uniqueid = '$uniqueid';";
if($M){print STDERR "\n\n|$stmtA|\n";}
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($M) {print STDERR "-- VDAD vicidial_log update: |$affected_rows|$uniqueid|\n";}
if ($AGILOG) {$agi_string = "-- VDAD vicidial_log update: |$affected_rows|$uniqueid|"; &agi_output;}
$dbhA->disconnect();
}
else
{
if ($M) {print STDERR "-- VDhangup Local DEBUG: |$PRI|$callerid|$dialstatus|$hangup_cause|\n";}
if ($AGILOG) {$agi_string = "-- VDhangup Local DEBUG: |$PRI|$callerid|$dialstatus|$hangup_cause|"; &agi_output;}
}
if ($M) {print STDERR "+++++ VDAD START LOCAL CHANNEL: EXITING- $priority\n";}
if ($AGILOG) {$agi_string = "+++++ VDAD START LOCAL CHANNEL: EXITING- $priority"; &agi_output;}
if ($priority > 2) {sleep(1);}
exit;
}
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
########## FIND AND DELETE vicidial_auto_calls ##########
$stmtA = "SELECT lead_id,callerid FROM vicidial_auto_calls where uniqueid = '$uniqueid' limit 1;";
if($M){print STDERR "\n\n|$stmtA|\n";}
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
@@ -295,18 +305,18 @@ $sthA->finish();
if (!$rec_countCUSTDATA)
{
if($M){print STDERR "\nVD hangup: no VDAC record found: $uniqueid $calleridname\n";}
if ($AGILOG) {$agi_string = "VD hangup: no VDAC record found: $uniqueid $calleridname"; &agi_output;}
exit;
}
$stmtA = "DELETE FROM vicidial_auto_calls where uniqueid='$uniqueid' order by call_time desc limit 1;";
$affected_rows = $dbhA->do($stmtA);
if ($M) {print STDERR "-- VDAC record deleted: |$affected_rows| |$VD_lead_id|$uniqueid|$VD_callerid|$server_ip\n";}
if ($AGILOG) {$agi_string = "-- VDAC record deleted: |$affected_rows| |$VD_lead_id|$uniqueid|$VD_callerid|$VARserver_ip"; &agi_output;}
########## FIND AND UPDATE vicidial_log ##########
$stmtA = "SELECT start_epoch,status FROM vicidial_log where uniqueid='$uniqueid' and lead_id='$VD_lead_id' limit 1;";
if($M){print STDERR "\n\n|$stmtA|\n";}
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
@@ -323,7 +333,7 @@ $sthA->finish();
if (!$epc_countCUSTDATA)
{
if($M){print STDERR "\nVD hangup: no VDL record found: $uniqueid $calleridname $VD_lead_id $uniqueid\n";}
if ($AGILOG) {$agi_string = "no VDL record found: $uniqueid $calleridname $VD_lead_id $uniqueid"; &agi_output;}
$secX = time();
$Rtarget = ($secX - 21600); # look for VDCL entry within last 6 hours
@@ -338,7 +348,7 @@ if (!$epc_countCUSTDATA)
$RSQLdate = "$Ryear-$Rmon-$Rmday $Rhour:$Rmin:$Rsec";
$stmtA = "SELECT start_epoch,status,closecallid FROM vicidial_closer_log where lead_id = '$VD_lead_id' and call_date > \"$RSQLdate\" order by call_date desc limit 1;";
if($M){print STDERR "\n\n|$stmtA|\n";}
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
@@ -356,7 +366,7 @@ if (!$epc_countCUSTDATA)
}
if (!$epc_countCUSTDATA)
{
if($M){print STDERR "\nVD hangup: no VDL or VDCL record found: $uniqueid $calleridname $VD_lead_id $uniqueid\n";}
if ($AGILOG) {$agi_string = "no VDL or VDCL record found: $uniqueid $calleridname $VD_lead_id $uniqueid"; &agi_output;}
exit;
}
$VD_seconds = ($now_date_epoch - $VD_start_epoch);
@@ -368,15 +378,15 @@ if ($VD_status =~ /^NA$|^NEW$|^QUEUE$|^XFER$/)
########## FIND AND UPDATE vicidial_list ##########
$stmtA = "UPDATE vicidial_list set status='DROP' where lead_id = '$VD_lead_id';";
if($M){print STDERR "\n\n|$stmtA|\n";}
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($M) {print STDERR "-- VDAD vicidial_list update: |$affected_rows|$VD_lead_id\n";}
if ($AGILOG) {$agi_string = "-- VDAD vicidial_list update: |$affected_rows|$VD_lead_id"; &agi_output;}
}
$stmtA = "UPDATE vicidial_log set $SQL_status end_epoch='$now_date_epoch',length_in_sec='$VD_seconds' where uniqueid = '$uniqueid';";
if($M){print STDERR "\n\n|$stmtA|\n";}
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($M) {print STDERR "-- VDAD vicidial_log update: |$affected_rows|$uniqueid|$VD_status|\n";}
if ($AGILOG) {$agi_string = "-- VDAD vicidial_log update: |$affected_rows|$uniqueid|$VD_status|"; &agi_output;}
@@ -384,14 +394,14 @@ if ($M) {print STDERR "-- VDAD vicidial_log update: |$affected_rows|$uniqueid
########## UPDATE vicidial_closer_log ##########
if (length($VD_closecallid) < 1)
{
if($M){print STDERR "\nVD hangup: no VDCL record found: $uniqueid $calleridname $VD_lead_id $uniqueid\n";}
if ($AGILOG) {$agi_string = "no VDCL record found: $uniqueid $calleridname $VD_lead_id $uniqueid"; &agi_output;}
exit;
}
$VD_seconds = ($now_date_epoch - $VD_start_epoch);
$stmtA = "UPDATE vicidial_closer_log set status='DROP',end_epoch='$now_date_epoch',length_in_sec='$VD_seconds' where closecallid = '$VD_closecallid';";
if($M){print STDERR "\n\n|$stmtA|\n";}
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($M) {print STDERR "-- VDCL update: |$affected_rows|$uniqueid|$VD_closecallid|\n";}
if ($AGILOG) {$agi_string = "-- VDCL update: |$affected_rows|$uniqueid|$VD_closecallid|"; &agi_output;}
@@ -420,3 +430,21 @@ sub checkresult {
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}