Replaced all SELECT * MySQL queries with proper field lists. this altered scripts in bin/agi/agc/vicidial

git-svn-id: svn://192.168.202.10@1268 3d104415-ff17-0410-8863-d5cf3c621b8a
This commit is contained in:
mattf
2009-11-30 04:25:17 +00:00
parent 8b0546a421
commit 86202b04cf
27 changed files with 565 additions and 543 deletions
+84 -52
View File
@@ -1,6 +1,6 @@
#!/usr/bin/perl
#
# AST_manager_send.pl version 2.0.5
# AST_manager_send.pl version 2.2.0
#
# Part of the Asterisk Central Queue System (ACQS)
#
@@ -16,7 +16,7 @@
# scalability over just using a single process. Also, this means that a single
# action execution lock cannot bring the entire system down.
#
# Copyright (C) 2008 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
# Copyright (C) 2009 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# CHANGES
# 50823-1514 - Added commandline debug options with debug printouts
@@ -28,6 +28,7 @@
# 60817-1211 - added more ARGS to go to child process to remove DBI from child
# 61221-1926 - optimize and clean code, lc
# 80418-0901 - reduced time between Actions being sent, raised endless loop timer
# 91129-2146 - removed SELECT STAR and formatting fixes
#
$|++;
@@ -44,7 +45,8 @@ my $COUNTER_OUTPUT=1; # set to 1 to display the counter as the script runs
my ($CLOhelp, $sendonlyone, $TEST, $DB, $DBX, $SYSLOG);
### begin parsing run-time options ###
if (scalar @ARGV) {
if (scalar @ARGV)
{
GetOptions('help!' => \$CLOhelp,
'sendonlyone!' => \$sendonlyone,
'counteroutput!' => \$COUNTER_OUTPUT,
@@ -52,14 +54,16 @@ if (scalar @ARGV) {
'debug!' => \$DB,
'debugX!' => \$DBX );
$DB = 1 if ($DBX);
if ($DB) {
if ($DB)
{
print "----- DEBUGGING -----\n";
print "----- EXTRA-VERBOSE DEBUGGING -----\n" if ($DBX);
print "COUNTER_OUTPUT: $COUNTER_OUTPUT\n";
print "sendonlyone: $sendonlyone\n" if ($sendonlyone);
print "TEST: $TEST\n" if ($TEST);
}
if ($CLOhelp) {
}
if ($CLOhelp)
{
print "\nAST_manager_send.pl\n";
print "allowed run time options:\n";
print " [--help] = This help screen.\n";
@@ -69,22 +73,25 @@ if (scalar @ARGV) {
print " [--debug] or [-v] = Verbose debug messages\n";
print " [--debugX] or [-vv] = Extra-verbose debug messages\n\n";
exit 0;
}
}
}
### end parsing run-time options ###
# Begin Parsing astguiclient config file.
open(CONF, $conf{PATHconf}) || die "can't open " . $conf{PATHconf} . ": " . $! . "\n";
while (my $line = <CONF>) {
while (my $line = <CONF>)
{
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
foreach my $key (qw( PATHhome PATHlogs PATHagi PATHweb PATHsounds PATHmontior
VARserver_ip VARDB_server VARDB_database VARDB_user VARDB_pass VARDB_port)) {
if ($line =~ /^$key/) {
VARserver_ip VARDB_server VARDB_database VARDB_user VARDB_pass VARDB_port))
{
if ($line =~ /^$key/)
{
$conf{$key} = $line;
$conf{$key} =~ s/.*=//gi;
}
}
}
}
$conf{VARDB_port} = '3306' unless ($conf{VARDB_port});
# Connect to DB
@@ -99,11 +106,13 @@ eventLogger($conf{PATHlogs}, 'process', $event_string);
my $one_day_interval = 182; # 2 day loops for 12 months
while ($one_day_interval > 0) {
while ($one_day_interval > 0)
{
my $endless_loop = 1728000; # 2 days at .10 seconds per loop
my $affected_rows;
my $NEW_actions;
while ($endless_loop > 0) {
while ($endless_loop > 0)
{
my $stmtA = "SELECT count(*) from vicidial_manager where server_ip = '" . $conf{VARserver_ip} . "' and status = 'NEW'";
my $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
@@ -111,23 +120,27 @@ while ($one_day_interval > 0) {
print STDERR $NEW_actions . " NEW Actions to send on server " . $conf{VARserver_ip} . " $endless_loop\n" if ($DB);
$sthA->finish();
if ($NEW_actions) {
if ($NEW_actions)
{
my $stmtA = "UPDATE vicidial_manager set status='QUEUE' where server_ip = '" . $conf{VARserver_ip} . "' and status = 'NEW' order by entry_date limit 1";
$affected_rows = $dbhA->do($stmtA);
print STDERR "rows updated to QUEUE: |$affected_rows|\n" if ($DB);
} else {
}
else
{
$affected_rows = 0;
}
}
if ($affected_rows) {
my $stmtA = "SELECT * FROM vicidial_manager where server_ip = '" . $conf{VARserver_ip} . "' and status = 'QUEUE' order by entry_date desc limit 1";
if ($affected_rows)
{
my $stmtA = "SELECT man_id,uniqueid,entry_date,status,response,server_ip,channel,action,callerid,cmd_line_b,cmd_line_c,cmd_line_d,cmd_line_e,cmd_line_f,cmd_line_g,cmd_line_h,cmd_line_i,cmd_line_j,cmd_line_k FROM vicidial_manager where server_ip = '" . $conf{VARserver_ip} . "' and status = 'QUEUE' order by entry_date desc limit 1";
eventLogger($conf{'PATHlogs'}, 'process', "SQL_QUERY|" . $stmtA . "|");
my $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
while (my $vdm = $sthA->fetchrow_hashref) {
while (my $vdm = $sthA->fetchrow_hashref)
{
print STDERR $vdm->{man_id} . "|" . $vdm->{uniqueid} . "|" . $vdm->{channel} . "|" .
$vdm->{action} . "|" . $vdm->{callerid} . "\n" if ($DB);
@@ -145,7 +158,8 @@ while ($one_day_interval > 0) {
$originate_command .= "\n";
my $SENDNOW=1;
if ($originate_command =~ /Action: Hangup|Action: Redirect/) {
if ($originate_command =~ /Action: Hangup|Action: Redirect/)
{
$SENDNOW=0;
print STDERR "\n|checking for dead call before executing|" . $vdm->{callerid} . "|" . $vdm->{uniqueid} . "|\n" if ($DB);
my $stmtB = "SELECT count(*) FROM vicidial_manager where server_ip = '" . $conf{VARserver_ip} . "' and callerid='" . $vdm->{callerid} . "' and status = 'DEAD'";
@@ -154,17 +168,21 @@ while ($one_day_interval > 0) {
my $dead_count = ($sthB->fetchrow_array)[0];
$sthB->finish();
if ($dead_count) {
if ($dead_count)
{
print STDERR "\n|not sending command line is dead|" . $vdm->{callerid} . "|" . $vdm->{uniqueid} . "|\n" if ($DB);
} else {
}
else
{
$SENDNOW=1;
}
}
}
my $event_string = "----BEGIN NEW COMMAND----\nCallerID: " . $vdm->{callerid} . "\n$originate_command----END NEW COMMAND----\n";
eventLogger($conf{'PATHlogs'}, 'process', $event_string);
if ($SENDNOW) {
if ($SENDNOW)
{
my $cPATHlogs = $conf{PATHlogs};
$cPATHlogs =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
$vdm->{cmd_line_b} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
@@ -214,55 +232,65 @@ while ($one_day_interval > 0) {
$event_string = "SQL_QUERY|$stmtA|";
eventLogger($conf{'PATHlogs'}, 'process', $event_string);
} else {
}
else
{
$stmtA = "UPDATE vicidial_manager set status='DEAD' where man_id='" . $vdm->{man_id} . "'";
print STDERR "\n|$stmtA|\n" if ($DB);
$affected_rows = $dbhA->do($stmtA);
$event_string="COMMAND NOT SENT, SQL_QUERY|$stmtA|";
eventLogger($conf{'PATHlogs'}, 'process', $event_string);;
}
}
}
$sthA->finish();
}
}
if ($affected_rows) {
if ($affected_rows)
{
### sleep for 1 hundredth of a second if just send an ACTION
usleep(1*10*1000);
} else {
}
else
{
### sleep for 10 hundredths of a second if no actions sent
usleep(1*100*1000);
}
}
$endless_loop--;
print STDERR "loop counter: |$endless_loop|\r" if($COUNTER_OUTPUT or $DB);
### putting a blank file called "sendmgr.kill" in a directory will automatically safely kill this program
if (-e $conf{PATHhome} . "/sendmgr.kill" or $sendonlyone) {
if (-e $conf{PATHhome} . "/sendmgr.kill" or $sendonlyone)
{
unlink($conf{PATHhome} . "/sendmgr.kill");
$endless_loop = 0;
$one_day_interval = 0;
print "\nPROCESS KILLED MANUALLY... EXITING\n\n";
}
}
my $running_listen = 0;
if ($endless_loop =~ /0$/) {
if ($endless_loop =~ /0$/)
{
### Grab Server values from the database
$servConf = getServerConfig($dbhA, $conf{VARserver_ip});
print "checking to see if listener is dead |$sendonlyone|$running_listen|\n" if($COUNTER_OUTPUT or $DB);
#my @psoutput = `/bin/ps -f --no-headers -A`;
my @psoutput = `/bin/ps -o "%p %a" --no-headers -A`;
foreach my $line (@psoutput) {
foreach my $line (@psoutput)
{
chomp($line);
print "|$line| \n" if ($DBX);
my @psline = split(/\/usr\/bin\/perl /,$line);
if ($psline[1] =~ /AST_manager_li/) {
if ($psline[1] =~ /AST_manager_li/)
{
$running_listen++;
print "SEND RUNNING: |$psline[1]|\n" if ($DB);
}
}
}
unless ($running_listen) {
unless ($running_listen)
{
$sendonlyone++;
print "LISTENER DEAD STOPPING PROGRAM... ATTEMPTING TO START keepalive SCRIPT\n" if ($COUNTER_OUTPUT or $DB);
$event_string = 'LISTENER DEAD STOPPING PROGRAM... ATTEMPTING TO START keepalive SCRIPT|';
@@ -270,15 +298,14 @@ while ($one_day_interval > 0) {
# `/usr/bin/at now < $PATHhome/ADMIN_keepalive_send_listen.at 2>/dev/null 1>&2`;
my $screencmd = "/usr/bin/screen -d -m " . $conf{PATHhome} . "/ADMIN_keepalive_AST_send_listen.pl 2>/dev/null 1>&2";
`$screencmd`;
}
}
}
}
print "DONE... Exiting... Goodbye... See you later... Not really, initiating next loop...$one_day_interval left\n" if($COUNTER_OUTPUT or $DB);
$one_day_interval--;
}
}
$event_string='CLOSING DB CONNECTION|';
eventLogger($conf{'PATHlogs'}, 'process', $event_string);
@@ -299,16 +326,17 @@ exit 0;
# $serverIP : IP of server to get config for.
# Returns:
# hashref with conents of table entry.
sub getServerConfig {
sub getServerConfig
{
my ($dbhA, $serverip) = @_;
my $stmtA = "SELECT * FROM servers where server_ip = '" . $serverip ."';";
my $stmtA = "SELECT server_id,server_description,server_ip,active,asterisk_version,max_vicidial_trunks,telnet_host,telnet_port,ASTmgrUSERNAME,ASTmgrSECRET,ASTmgrUSERNAMEupdate,ASTmgrUSERNAMElisten,ASTmgrUSERNAMEsend,local_gmt,voicemail_dump_exten,answer_transfer_agent,ext_context,sys_perf_log,vd_server_logs,agi_output,vicidial_balance_active,balance_trunks_offlimits,recording_web_link,alt_server_ip,active_asterisk_server,generate_vicidial_conf,rebuild_conf_files,outbound_calls_per_second,sysload,channels_total,cpu_idle_percent,disk_usage,sounds_update,vicidial_recording_limit,carrier_logging_active,vicidial_balance_rank,rebuild_music_on_hold,active_agent_login_server,conf_secret FROM servers where server_ip = '" . $serverip ."';";
my $sthA = $dbhA->prepare($stmtA) or die "preparing: " . $dbhA->errstr;
$sthA->execute or die "executing: $stmtA " . $dbhA->errstr;
my $servConf = $sthA->fetchrow_hashref;
$SYSLOG = 1 if ($servConf->{vd_server_logs} =~ /Y/);
$sthA->finish();
return $servConf;
}
}
# eventLogger usage:
# eventLgger($LogFileDir, $LogType, $EventString);
@@ -316,14 +344,15 @@ sub getServerConfig {
# $LogFilePath : Directory where log files are.
# $LogType : Type of log, ie process, send, launch, full
# $EventString : String to record in log.
sub eventLogger {
sub eventLogger
{
my ($path,$type,$string) = @_;
open(LOG, ">>" . $path . "/action_" . $type . "." . logDate())
|| die "Can't open " . $path . "/action_" . $type . "." .
logDate() . ": " . $! . "\n";
print LOG nowDate() . "|" . $string . "|\n";
close(LOG);
}
}
# getTime usage:
# getTime($SecondsSinceEpoch);
@@ -331,7 +360,8 @@ sub eventLogger {
# $SecondsSinceEpoch : Request time in seconds, defaults to current date/time.
# Returns:
# ($sec, $min, $hour. $day, $mon, $year)
sub getTime {
sub getTime
{
my ($tms) = @_;
$tms = time unless ($tms);
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($tms);
@@ -342,7 +372,7 @@ sub getTime {
$min = "0" . $min if ($min < 10);
$sec = "0" . $sec if ($sec < 10);
return ($sec,$min,$hour,$mday,$mon,$year);
}
}
# nowDate usage:
# nowDate($SecondsSinceEpoch);
@@ -350,11 +380,12 @@ sub getTime {
# $SecondsSinceEpoch : Request time in seconds, defaults to current date/time.
# Returns:
# scalar date/time string (MySQL formatted) ie "2007-01-01 00:00:00"
sub nowDate {
sub nowDate
{
my ($tms) = @_;
my($sec,$min,$hour,$mday,$mon,$year) = getTime($tms);
return $year.'-'.$mon.'-'.$mday.' '.$hour.':'.$min.':'.$sec;
}
}
# logDate usage:
# logDate($SecondsSinceEpoch);
@@ -362,10 +393,11 @@ sub nowDate {
# $SecondsSinceEpoch : Request time in seconds, defaults to current date/time.
# Returns:
# scalar date string ie "2007-01-01"
sub logDate {
sub logDate
{
my ($tms) = @_;
my($sec,$min,$hour,$mday,$mon,$year) = getTime($tms);
return $year . '-' . $mon . '-' . $mday;
}
}
### End of subs