diff --git a/bin/AST_manager_send.pl b/bin/AST_manager_send.pl index 0ba5b487..402d08fc 100644 --- a/bin/AST_manager_send.pl +++ b/bin/AST_manager_send.pl @@ -16,14 +16,6 @@ # scalability over just using a single process. Also, this means that a single # action execution lock cannot bring the entire system down. # -# For this program to work you need to have the "asterisk" MySQL database -# created and create the tables listed in the CONF_MySQL.txt file, also make sure -# that the machine running this program has read/write/update/delete access -# to that database -# -# In your Asterisk server setup you also need to have several things activated -# and defined. See the CONF_Asterisk.txt file for details -# # Copyright (C) 2006 Matt Florell LICENSE: GPLv2 # # CHANGES @@ -34,451 +26,344 @@ # 60718-1211 - removed need for ADMIN_keepalive_send_listen.at launching # 60814-1712 - added option for no logging to file # 60817-1211 - added more ARGS to go to child process to remove DBI from child +# 61221-1926 - optimize and clean code, lc # -# constants -$COUNTER_OUTPUT=1; # set to 1 to display the counter as the script runs -$US='__'; -$MT[0]=''; +$|++; +use strict; +use DBI; +use Getopt::Long; +use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second + +# constants and globals +my $servConf; +my %conf; +$conf{PATHconf} = '/etc/astguiclient.conf'; # default path to astguiclient configuration file: +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 (length($ARGV[0])>1) -{ - $i=0; - while ($#ARGV >= $i) - { - $args = "$args $ARGV[$i]"; - $i++; +if (scalar @ARGV) { + GetOptions('help!' => \$CLOhelp, + 'sendonlyone!' => \$sendonlyone, + 'counteroutput!' => \$COUNTER_OUTPUT, + 't!' => \$TEST, #Fucntionality doesn't actually exist. + 'debug!' => \$DB, + 'debugX!' => \$DBX ); + $DB = 1 if ($DBX); + 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 ($args =~ /--help/i) - { - print "allowed run time options:\n [-t] = test\n [-debug] = verbose debug messages\n[-debugX] = Extra-verbose debug messages\n\n"; + if ($CLOhelp) { + print "\nAST_manager_send.pl\n"; + print "allowed run time options:\n"; + print " [--help] = This help screen.\n"; + print " [--sendonlyone] = Send only one command\n"; + print " [--nocounteroutput] = Do not display counter as script runs\n"; + print " [-t] = Test Mode\n"; + print " [--debug] or [-v] = Verbose debug messages\n"; + print " [--debugX] or [-vv] = Extra-verbose debug messages\n\n"; + exit 0; } - else - { - if ($args =~ /-debug/i) - { - $DB=1; # Debug flag - } - if ($args =~ /--debugX/i) - { - $DBX=1; - print "\n----- SUPER-DUPER DEBUGGING -----\n\n"; - } - if ($args =~ /-sendonlyone/i) - { - $sendonlyone=1; - print "\n-----SEND ONLY ONE COMMAND -----\n\n"; - } - if ($args =~ /-t/i) - { - $TEST=1; - $T=1; - } - } -} -else -{ -# print "no command line options set\n"; } ### end parsing run-time options ### -# default path to astguiclient configuration file: -$PATHconf = '/etc/astguiclient.conf'; - -open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n"; -@conf = ; -close(conf); -$i=0; -foreach(@conf) - { - $line = $conf[$i]; +# Begin Parsing astguiclient config file. +open(CONF, $conf{PATHconf}) || die "can't open " . $conf{PATHconf} . ": " . $! . "\n"; +while (my $line = ) { $line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi; - if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) ) - {$PATHhome = $line; $PATHhome =~ s/.*=//gi;} - if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) ) - {$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;} - if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) ) - {$PATHagi = $line; $PATHagi =~ s/.*=//gi;} - if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) ) - {$PATHweb = $line; $PATHweb =~ s/.*=//gi;} - if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) ) - {$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;} - if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) ) - {$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;} - if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) ) - {$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;} - if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) ) - {$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;} - if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) ) - {$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;} - if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) ) - {$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;} - if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) ) - {$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;} - if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) ) - {$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;} - $i++; + 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/) { + $conf{$key} = $line; + $conf{$key} =~ s/.*=//gi; + } } +} +$conf{VARDB_port} = '3306' unless ($conf{VARDB_port}); -if (!$VARDB_port) {$VARDB_port='3306';} +# Connect to DB +my $dbhA = DBI->connect("DBI:mysql:" . $conf{VARDB_database} . ":" . $conf{VARDB_server} . ":" . $conf{VARDB_port}, + $conf{VARDB_user}, $conf{VARDB_pass}) or die "Couldn't connect to database: " . DBI->errstr; - &get_time_now; +### Grab Server values from the database +$servConf = getServerConfig($dbhA, $conf{VARserver_ip}); +$SYSLOG = 1 if ($servConf->{vd_server_logs} =~ /Y/); +my $event_string='LOGGED INTO MYSQL SERVER ON 1 CONNECTION|'; +eventLogger($conf{PATHlogs}, 'process', $event_string); -#use lib './lib', '../lib'; -use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second -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; - ### Grab Server values from the database - $stmtA = "SELECT telnet_host,telnet_port,ASTmgrUSERNAME,ASTmgrSECRET,ASTmgrUSERNAMEsend,vd_server_logs FROM servers where server_ip = '$VARserver_ip';"; - $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; +my $one_day_interval = 90; # 1 day loops for 3 months +while ($one_day_interval > 0) { + my $endless_loop = 864000; # 10 days at .20 seconds per loop + my $affected_rows; + my $NEW_actions; + 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; - $sthArows=$sthA->rows; - $rec_count=0; - while ($sthArows > $rec_count) - { - @aryA = $sthA->fetchrow_array; - $DBtelnet_host = "$aryA[0]"; - $DBtelnet_port = "$aryA[1]"; - $DBASTmgrUSERNAME = "$aryA[2]"; - $DBASTmgrSECRET = "$aryA[3]"; - $DBASTmgrUSERNAMEsend = "$aryA[4]"; - $DBvd_server_logs = "$aryA[5]"; - if ($DBtelnet_host) {$telnet_host = $DBtelnet_host;} - if ($DBtelnet_port) {$telnet_port = $DBtelnet_port;} - if ($DBASTmgrUSERNAME) {$ASTmgrUSERNAME = $DBASTmgrUSERNAME;} - if ($DBASTmgrSECRET) {$ASTmgrSECRET = $DBASTmgrSECRET;} - if ($DBASTmgrUSERNAMEsend) {$ASTmgrUSERNAMEsend = $DBASTmgrUSERNAMEsend;} - if ($DBvd_server_logs =~ /Y/) {$SYSLOG = '1';} - else {$SYSLOG = '0';} - $rec_count++; - } - $sthA->finish(); + my $NEW_actions = ($sthA->fetchrow_array)[0]; + print STDERR $NEW_actions . " NEW Actions to send on server " . $conf{VARserver_ip} . " $endless_loop\n" if ($DB); + $sthA->finish(); - - $event_string='LOGGED INTO MYSQL SERVER ON 1 CONNECTION|'; - &event_logger; - -$one_day_interval = 90; # 1 day loops for 3 months -while($one_day_interval > 0) -{ - - - $endless_loop=864000; # 10 days at .20 seconds per loop - - while($endless_loop > 0) - { - - $affected_rows=0; - $NEW_actions=0; - - $stmtA = "SELECT count(*) from vicidial_manager where server_ip = '$VARserver_ip' and status = 'NEW'"; - $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; - $sthA->execute or die "executing: $stmtA ", $dbhA->errstr; - $sthArows=$sthA->rows; - $rec_count=0; - @aryA = $sthA->fetchrow_array; - if($DB){print STDERR $aryA[0]," NEW Actions to send on server $VARserver_ip $endless_loop\n";} - $NEW_actions = "$aryA[0]"; - $rec_count++; - $sthA->finish(); - - if (!$NEW_actions) - { - $affected_rows=0; - } - else - { - $stmtA = "UPDATE vicidial_manager set status='QUEUE' where server_ip = '$VARserver_ip' and status = 'NEW' order by entry_date limit 1"; - $affected_rows = $dbhA->do($stmtA); - if ($DB) {print STDERR "rows updated to QUEUE: |$affected_rows|\n";} + 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 { + $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"; + eventLogger($conf{'PATHlogs'}, 'process', "SQL_QUERY|" . $stmtA . "|"); - if ($affected_rows) - { - $stmtA = "SELECT * FROM vicidial_manager where server_ip = '$VARserver_ip' and status = 'QUEUE' order by entry_date desc limit 1"; - $event_string="SQL_QUERY|$stmtA|"; - &event_logger; + my $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; + $sthA->execute or die "executing: $stmtA ", $dbhA->errstr; - $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) - { - @aryA = $sthA->fetchrow_array; + while (my $vdm = $sthA->fetchrow_hashref) { - if($DB){print STDERR $aryA[0],"|", $aryA[1],"|", $aryA[6],"|", $aryA[7],"|", $aryA[8],"\n";} - $man_id = "$aryA[0]"; - $uniqueid = "$aryA[1]"; - $channel = "$aryA[6]"; - $action = "$aryA[7]"; - $callid = "$aryA[8]"; - $cmd_line_b = "$aryA[9]"; - $cmd_line_c = "$aryA[10]"; - $cmd_line_d = "$aryA[11]"; - $cmd_line_e = "$aryA[12]"; - $cmd_line_f = "$aryA[13]"; - $cmd_line_g = "$aryA[14]"; - $cmd_line_h = "$aryA[15]"; - $cmd_line_i = "$aryA[16]"; - $cmd_line_j = "$aryA[17]"; - $cmd_line_k = "$aryA[18]"; + print STDERR $vdm->{man_id} . "|" . $vdm->{uniqueid} . "|" . $vdm->{channel} . "|" . + $vdm->{action} . "|" . $vdm->{callid} . "\n" if ($DB); - $originate_command = ''; - $originate_command .= "Action: $action\n"; - if (length($cmd_line_b)>3) {$originate_command .= "$cmd_line_b\n";} - if (length($cmd_line_c)>3) {$originate_command .= "$cmd_line_c\n";} - if (length($cmd_line_d)>3) {$originate_command .= "$cmd_line_d\n";} - if (length($cmd_line_e)>3) {$originate_command .= "$cmd_line_e\n";} - if (length($cmd_line_f)>3) {$originate_command .= "$cmd_line_f\n";} - if (length($cmd_line_g)>3) {$originate_command .= "$cmd_line_g\n";} - if (length($cmd_line_h)>3) {$originate_command .= "$cmd_line_h\n";} - if (length($cmd_line_i)>3) {$originate_command .= "$cmd_line_i\n";} - if (length($cmd_line_j)>3) {$originate_command .= "$cmd_line_j\n";} - if (length($cmd_line_k)>3) {$originate_command .= "$cmd_line_k\n";} + my $originate_command = "Action: ". $vdm->{action} . "\n"; + $originate_command .= $vdm->{cmd_line_b} . "\n" if ($vdm->{cmd_line_b}); + $originate_command .= $vdm->{cmd_line_c} . "\n" if ($vdm->{cmd_line_c}); + $originate_command .= $vdm->{cmd_line_d} . "\n" if ($vdm->{cmd_line_d}); + $originate_command .= $vdm->{cmd_line_e} . "\n" if ($vdm->{cmd_line_e}); + $originate_command .= $vdm->{cmd_line_f} . "\n" if ($vdm->{cmd_line_f}); + $originate_command .= $vdm->{cmd_line_g} . "\n" if ($vdm->{cmd_line_g}); + $originate_command .= $vdm->{cmd_line_h} . "\n" if ($vdm->{cmd_line_h}); + $originate_command .= $vdm->{cmd_line_i} . "\n" if ($vdm->{cmd_line_i}); + $originate_command .= $vdm->{cmd_line_j} . "\n" if ($vdm->{cmd_line_j}); + $originate_command .= $vdm->{cmd_line_k} . "\n" if ($vdm->{cmd_line_k}); $originate_command .= "\n"; - $SENDNOW=1; - if ($originate_command =~ /Action: Hangup|Action: Redirect/) - { + my $SENDNOW=1; + if ($originate_command =~ /Action: Hangup|Action: Redirect/) { $SENDNOW=0; - if($DB){print STDERR "\n|checking for dead call before executing|$callid|$uniqueid|\n";} - $stmtB = "SELECT count(*) FROM vicidial_manager where server_ip = '$VARserver_ip' and callerid='$callid' and status = 'DEAD'"; - $sthB = $dbhA->prepare($stmtB) or die "preparing: ",$dbhA->errstr; - $sthB->execute or die "executing: $stmtA ", $dbhA->errstr; - $sthArows=$sthB->rows; - $rec_countH=0; - @aryB = $sthB->fetchrow_array; - $rec_countH = "$aryB[0]"; + print STDERR "\n|checking for dead call before executing|" . $vdm->{callid} . "|" . $vdm->{uniqueid} . "|\n" if ($DB); + my $stmtB = "SELECT count(*) FROM vicidial_manager where server_ip = '" . $conf{VARserver_ip} . "' and callerid='" . $vdm->{callid} . "' and status = 'DEAD'"; + my $sthB = $dbhA->prepare($stmtB) or die "preparing: ",$dbhA->errstr; + $sthB->execute or die "executing: $stmtA ", $dbhA->errstr; + my $dead_count = ($sthB->fetchrow_array)[0]; + $sthB->finish(); - if (!$rec_countH){$SENDNOW=1;} - else - { - $launch_string = "|not sending command line is dead|$callid|$uniqueid|"; - if($DB){print STDERR "\n$launch_string\n";} - } - $sthB->finish(); - - + if ($dead_count) { + print STDERR "\n|not sending command line is dead|" . $vdm->{callid} . "|" . $vdm->{uniqueid} . "|\n" if ($DB); + } else { + $SENDNOW=1; } + } - $event_string="----BEGIN NEW COMMAND----\n$originate_command----END NEW COMMAND----\n"; - &event_logger; + my $event_string = "----BEGIN NEW COMMAND----\n$originate_command----END NEW COMMAND----\n"; + eventLogger($conf{'PATHlogs'}, 'process', $event_string); - if ($SENDNOW) - { - # $tn->buffer_empty; + 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; + $vdm->{cmd_line_c} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; + $vdm->{cmd_line_d} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; + $vdm->{cmd_line_e} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; + $vdm->{cmd_line_f} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; + $vdm->{cmd_line_g} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; + $vdm->{cmd_line_h} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; + $vdm->{cmd_line_i} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; + $vdm->{cmd_line_j} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; + $vdm->{cmd_line_k} =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; -# encode -# $str =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; -# decode -# $str =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg; -# -# --SYSLOG=$SYSLOG --PATHlogs=$PATHlogs --telnet_host=$telnet_host --telnet_port=$telnet_port --ASTmgrUSERNAME=$ASTmgrUSERNAME --ASTmgrSECRET=$ASTmgrSECRET --ASTmgrUSERNAMEsend=$ASTmgrUSERNAMEsend --action=$action --cmd_line_b=$cmd_line_b --cmd_line_c=$cmd_line_c --cmd_line_d=$cmd_line_d --cmd_line_e=$cmd_line_e --cmd_line_f=$cmd_line_f --cmd_line_g=$cmd_line_g --cmd_line_h=$cmd_line_h --cmd_line_i=$cmd_line_i --cmd_line_j=$cmd_line_j --cmd_line_k=$cmd_line_k - - $cPATHlogs = $PATHlogs; - $cPATHlogs =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; - $cmd_line_b =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; - $cmd_line_c =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; - $cmd_line_d =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; - $cmd_line_e =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; - $cmd_line_f =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; - $cmd_line_g =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; - $cmd_line_h =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; - $cmd_line_i =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; - $cmd_line_j =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; - $cmd_line_k =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; - - $launch_string = "$PATHhome/AST_send_action_child.pl --SYSLOG=$SYSLOG --PATHlogs=$cPATHlogs --telnet_host=$telnet_host --telnet_port=$telnet_port --ASTmgrUSERNAME=$ASTmgrUSERNAME --ASTmgrSECRET=$ASTmgrSECRET --ASTmgrUSERNAMEsend=$ASTmgrUSERNAMEsend --action=$action --cmd_line_b=$cmd_line_b --cmd_line_c=$cmd_line_c --cmd_line_d=$cmd_line_d --cmd_line_e=$cmd_line_e --cmd_line_f=$cmd_line_f --cmd_line_g=$cmd_line_g --cmd_line_h=$cmd_line_h --cmd_line_i=$cmd_line_i --cmd_line_j=$cmd_line_j --cmd_line_k=$cmd_line_k $callid $uniqueid $channel"; - &launch_logger; + my $launch = $conf{PATHhome} . "/AST_send_action_child.pl"; + $launch .= " --SYSLOG" if ($SYSLOG); + $launch .= " --PATHlogs=" . $cPATHlogs; + $launch .= " --telnet_host=" . $servConf->{telnet_host}; + $launch .= " --telnet_port=" . $servConf->{telnet_port}; + $launch .= " --ASTmgrUSERNAME=" . $servConf->{ASTmgrUSERNAME}; + $launch .= " --ASTmgrSECRET=" . $servConf->{ASTmgrSECRET}; + $launch .= " --ASTmgrUSERNAMEsend=" . $servConf->{ASTmgrUSERNAMEsend}; + $launch .= " --action=" . $vdm->{action}; + $launch .= " --cmd_line_b=" . $vdm->{cmd_line_b} if ($vdm->{cmd_line_b}); + $launch .= " --cmd_line_c=" . $vdm->{cmd_line_c} if ($vdm->{cmd_line_c}); + $launch .= " --cmd_line_d=" . $vdm->{cmd_line_d} if ($vdm->{cmd_line_d}); + $launch .= " --cmd_line_e=" . $vdm->{cmd_line_e} if ($vdm->{cmd_line_e}); + $launch .= " --cmd_line_f=" . $vdm->{cmd_line_f} if ($vdm->{cmd_line_f}); + $launch .= " --cmd_line_g=" . $vdm->{cmd_line_g} if ($vdm->{cmd_line_g}); + $launch .= " --cmd_line_h=" . $vdm->{cmd_line_h} if ($vdm->{cmd_line_h}); + $launch .= " --cmd_line_i=" . $vdm->{cmd_line_i} if ($vdm->{cmd_line_i}); + $launch .= " --cmd_line_j=" . $vdm->{cmd_line_j} if ($vdm->{cmd_line_j}); + $launch .= " --cmd_line_k=" . $vdm->{cmd_line_k} if ($vdm->{cmd_line_k}); + eventLogger($conf{'PATHlogs'}, 'launch', $launch . " " . + $vdm->{callid} . " " . $vdm->{uniqueid} . " " . $vdm->{channel}); - if ($SYSLOG) - { - system("$PATHhome/AST_send_action_child.pl --SYSLOG=$SYSLOG --PATHlogs=$PATHlogs --telnet_host=$telnet_host --telnet_port=$telnet_port --ASTmgrUSERNAME=$ASTmgrUSERNAME --ASTmgrSECRET=$ASTmgrSECRET --ASTmgrUSERNAMEsend=$ASTmgrUSERNAMEsend --action=$action --cmd_line_b=$cmd_line_b --cmd_line_c=$cmd_line_c --cmd_line_d=$cmd_line_d --cmd_line_e=$cmd_line_e --cmd_line_f=$cmd_line_f --cmd_line_g=$cmd_line_g --cmd_line_h=$cmd_line_h --cmd_line_i=$cmd_line_i --cmd_line_j=$cmd_line_j --cmd_line_k=$cmd_line_k >> $PATHlogs/action_send.$action_log_date \&"); - } - else - { - system("$PATHhome/AST_send_action_child.pl --SYSLOG=$SYSLOG --PATHlogs=$PATHlogs --telnet_host=$telnet_host --telnet_port=$telnet_port --ASTmgrUSERNAME=$ASTmgrUSERNAME --ASTmgrSECRET=$ASTmgrSECRET --ASTmgrUSERNAMEsend=$ASTmgrUSERNAMEsend --action=$action --cmd_line_b=$cmd_line_b --cmd_line_c=$cmd_line_c --cmd_line_d=$cmd_line_d --cmd_line_e=$cmd_line_e --cmd_line_f=$cmd_line_f --cmd_line_g=$cmd_line_g --cmd_line_h=$cmd_line_h --cmd_line_i=$cmd_line_i --cmd_line_j=$cmd_line_j --cmd_line_k=$cmd_line_k \&"); - } + $launch .= " >> " . $conf{PATHlogs} . "/action_send." . logDate() if ($SYSLOG); + system($launch . ' &'); - $launch_string = "SENT $man_id $callid $uniqueid $channel"; - # &launch_logger; + # $launch = "SENT " . $vdm->{man_id} . " " . $vdm->{callid} . ' ' . $vdm->{uniqueid} . ' ' . $vdm->{channel}; + # eventLogger($conf{'PATHlogs'}, 'launch', $launch);; - $stmtA = "UPDATE vicidial_manager set status='SENT' where man_id='$man_id'"; - if($DB){print STDERR "\n|$stmtA|\n";} + my $stmtA = "UPDATE vicidial_manager set status='SENT' where man_id='" . $vdm->{man_id} . "'"; + print STDERR "\n|$stmtA|\n" if ($DB); $affected_rows = $dbhA->do($stmtA); - $event_string="SQL_QUERY|$stmtA|"; - &event_logger; - } - else - { - $stmtA = "UPDATE vicidial_manager set status='DEAD' where man_id='$man_id'"; - if($DB){print STDERR "\n|$stmtA|\n";} + $event_string = "SQL_QUERY|$stmtA|"; + eventLogger($conf{'PATHlogs'}, 'process', $event_string); + } 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|"; - &event_logger; - } - $rec_count++; - } - $sthA->finish(); + $event_string="COMMAND NOT SENT, SQL_QUERY|$stmtA|"; + eventLogger($conf{'PATHlogs'}, 'process', $event_string);; + } + } + $sthA->finish(); } - if ($affected_rows) - { - ### sleep for 10 hundredths of a second - usleep(1*100*1000); - } - else - { - ### sleep for 20 hundredths of a second - usleep(1*200*1000); + if ($affected_rows) { + ### sleep for 10 hundredths of a second + usleep(1*100*1000); + } else { + ### sleep for 20 hundredths of a second + usleep(1*200*1000); } - $endless_loop--; - if( ($COUNTER_OUTPUT) or ($DB) ){print STDERR "loop counter: |$endless_loop|\r";} + $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 '$PATHhome/sendmgr.kill') or ($sendonlyone) ) - { - unlink('$PATHhome/sendmgr.kill'); - $endless_loop=0; - $one_day_interval=0; + 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"; - } - - if ($endless_loop =~ /0$/) - { - &get_time_now; + } + my $running_listen = 0; + if ($endless_loop =~ /0$/) { ### Grab Server values from the database - $stmtA = "SELECT vd_server_logs 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) - { - @aryA = $sthA->fetchrow_array; - $DBvd_server_logs = "$aryA[0]"; - if ($DBvd_server_logs =~ /Y/) {$SYSLOG = '1';} - else {$SYSLOG = '0';} - $rec_count++; - } - $sthA->finish(); + $servConf = getServerConfig($dbhA, $conf{VARserver_ip}); - - if( ($COUNTER_OUTPUT) or ($DB) ){print "checking to see if listener is dead |$sendonlyone|$running_listen|\n";} - #@psoutput = `/bin/ps -f --no-headers -A`; - @psoutput = `/bin/ps -o "%p %a" --no-headers -A`; - - $running_listen = 0; - - $i=0; - foreach (@psoutput) - { - chomp($psoutput[$i]); - - if ($DBX) {print "$i|$psoutput[$i]| \n";} - @psline = split(/\/usr\/bin\/perl /,$psoutput[$i]); - - if ($psline[1] =~ /AST_manager_li/) - { - $running_listen++; - if ($DB) {print "SEND RUNNING: |$psline[1]|\n";} + 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) { + chomp($line); + print "|$line| \n" if ($DBX); + my @psline = split(/\/usr\/bin\/perl /,$line); + if ($psline[1] =~ /AST_manager_li/) { + $running_listen++; + print "SEND RUNNING: |$psline[1]|\n" if ($DB); } - - $i++; } - if (!$running_listen) - { + unless ($running_listen) { $sendonlyone++; - if( ($COUNTER_OUTPUT) or ($DB) ){print "LISTENER DEAD STOPPING PROGRAM... ATTEMPTING TO START keepalive SCRIPT\n";} - $event_string='LISTENER DEAD STOPPING PROGRAM... ATTEMPTING TO START keepalive SCRIPT|'; - &event_logger; + 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|'; + eventLogger($conf{'PATHlogs'}, 'process', $event_string); # `/usr/bin/at now < $PATHhome/ADMIN_keepalive_send_listen.at 2>/dev/null 1>&2`; - `/usr/bin/screen -d -m $PATHhome/ADMIN_keepalive_AST_send_listen.pl 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`; } - + } } - - if( ($COUNTER_OUTPUT) or ($DB) ){print "DONE... Exiting... Goodbye... See you later... Not really, initiating next loop...$one_day_interval left\n";} - + 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|'; - &event_logger; +$event_string='CLOSING DB CONNECTION|'; +eventLogger($conf{'PATHlogs'}, 'process', $event_string); + +$dbhA->disconnect(); + +print "DONE... Exiting... Goodbye... See you later... Really I mean it this time\n" if($COUNTER_OUTPUT or $DB); +exit 0; +# Program ends. - $dbhA->disconnect(); +### Start of subs. - - if( ($COUNTER_OUTPUT) or ($DB) ){print "DONE... Exiting... Goodbye... See you later... Really I mean it this time\n";} - - -exit; - - - - - - - -sub get_time_now #get the current date and time and epoch for logging call lengths and datetimes -{ -($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"; -$action_log_date = "$year-$mon-$mday"; +# getServerConfig usage: +# getServerConfig($dbh, $serverIP); +# Requires: +# $dbh : Database handle to current open DB. +# $serverIP : IP of server to get config for. +# Returns: +# hashref with conents of table entry. +sub getServerConfig { + my ($dbhA, $serverip) = @_; + my $stmtA = "SELECT * 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; } - - - - -sub event_logger -{ -if ($SYSLOG) - { - ### open the log file for writing ### - open(Lout, ">>$PATHlogs/action_process.$action_log_date") - || die "Can't open $PATHlogs/action_process.$action_log_date: $!\n"; - print Lout "$now_date|$event_string|\n"; - close(Lout); - } -$event_string=''; +# eventLogger usage: +# eventLgger($LogFileDir, $LogType, $EventString); +# Requires: +# $LogFilePath : Directory where log files are. +# $LogType : Type of log, ie process, send, launch, full +# $EventString : String to record in log. +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); } -sub launch_logger -{ -if ($SYSLOG) - { - ### open the log file for writing ### - open(LLout, ">>$PATHlogs/action_launch.$action_log_date") - || die "Can't open $PATHlogs/action_launch.$action_log_date: $!\n"; - print LLout "$now_date|$launch_string|\n"; - close(LLout); - } -$event_string=''; +# getTime usage: +# getTime($SecondsSinceEpoch); +# Options: +# $SecondsSinceEpoch : Request time in seconds, defaults to current date/time. +# Returns: +# ($sec, $min, $hour. $day, $mon, $year) +sub getTime { + my ($tms) = @_; + $tms = time unless ($tms); + my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($tms); + $year += 1900; + $mon++; + $mon = "0" . $mon if ($mon < 10); + $mday = "0" . $mday if ($mday < 10); + $min = "0" . $min if ($min < 10); + $sec = "0" . $sec if ($sec < 10); + return ($sec,$min,$hour,$mday,$mon,$year); } + +# nowDate usage: +# nowDate($SecondsSinceEpoch); +# Options: +# $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 { + my ($tms) = @_; + my($sec,$min,$hour,$mday,$mon,$year) = getTime($tms); + return $year.'-'.$mon.'-'.$mday.' '.$hour.':'.$min.':'.$sec; +} + +# logDate usage: +# logDate($SecondsSinceEpoch); +# Options: +# $SecondsSinceEpoch : Request time in seconds, defaults to current date/time. +# Returns: +# scalar date string ie "2007-01-01" +sub logDate { + my ($tms) = @_; + my($sec,$min,$hour,$mday,$mon,$year) = getTime($tms); + return $year . '-' . $mon . '-' . $mday; +} + +### End of subs diff --git a/bin/AST_send_action_child.pl b/bin/AST_send_action_child.pl index cfd2b80c..c45147ee 100644 --- a/bin/AST_send_action_child.pl +++ b/bin/AST_send_action_child.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl # -# AST_send_action_child.pl version 0.4 *DBI-version +# AST_send_action_child.pl version 0.5 *DBI-version # # Part of the Asterisk Central Queue System (ACQS) # @@ -13,20 +13,6 @@ # be spawned by the AST_manager_send.pl script lookup the record in the MySQL DB # to be executed. connect to the manager interface, send the action and logoff # then exit. -# -# Win32 - ActiveState Perl 5.8.0 -# UNIX - Gnome or KDE with Tk/Tcl and perl Tk/Tcl modules loaded -# Both - Net::MySQL, Net::Telnet and Time::HiRes perl modules loaded -# -# For the client program to work in ACQS mode, this program must be running -# -# For this program to work you need to have the "asterisk" MySQL database -# created and create the tables listed in the CONF_MySQL.txt file, also make sure -# that the machine running this program has read/write/update/delete access -# to that database -# -# In your Asterisk server setup you also need to have several things activated -# and defined. See the CONF_Asterisk.txt file for details # # Copyright (C) 2006 Matt Florell LICENSE: GPLv2 # @@ -39,42 +25,77 @@ # 60814-1720 - added option for no logging to file # 60817-1244 - removed all DB calls and config file open to reduce footprint # 61004-1728 - added ability to parse volume control and lookup meetme IDs +# 61220-1720 - optimize and clean code, lc. +# 61221-1942 - additional modification to structure, lc. # -$FULL_LOG = 1; # set to 1 for a full response log to be created in $PATHlogs/action_full.date -$MT[0]=''; +$|++; +use strict; +use Getopt::Long; +use Net::Telnet; -$secX = time(); +### Initialize date/time vars ### +my $secX = time(); #Start time -($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"; -$action_log_date = "$year-$mon-$mday"; +### Initialize run-time variables ### +my ($CLOhelp, $SYSLOG, $PATHlogs, $telnet_host, $telnet_port, $ASTmgrUSERNAME); +my ($ASTmgrSECRET, $ASTmgrUSERNAMEsend, $action, $cmd_line_b, $cmd_line_c); +my ($cmd_line_d, $cmd_line_e, $cmd_line_f, $cmd_line_g, $cmd_line_h); +my ($cmd_line_i, $cmd_line_j, $cmd_line_k, $DB, $DBX); +my $FULL_LOG = 1; ### begin parsing run-time options ### -if (length($ARGV[0])>1) -{ - $i=0; - while ($#ARGV >= $i) - { - $ARGS = "$ARGS $ARGV[$i]\n"; - $i++; - } +if (scalar @ARGV) { + GetOptions('help!' => \$CLOhelp, + 'SYSLOG!' => \$SYSLOG, + 'PATHlogs=s' => \$PATHlogs, + 'telnet_host=s' => \$telnet_host, + 'telnet_port=s' => \$telnet_port, + 'ASTmgrUSERNAME=s' => \$ASTmgrUSERNAME, + 'ASTmgrSECRET=s' => \$ASTmgrSECRET, + 'ASTmgrUSERNAMEsend=s' => \$ASTmgrUSERNAMEsend, + 'action=s' => \$action, + 'cmd_line_b=s' => \$cmd_line_b, + 'cmd_line_c=s' => \$cmd_line_c, + 'cmd_line_d=s' => \$cmd_line_d, + 'cmd_line_e=s' => \$cmd_line_e, + 'cmd_line_f=s' => \$cmd_line_f, + 'cmd_line_g=s' => \$cmd_line_g, + 'cmd_line_h=s' => \$cmd_line_h, + 'cmd_line_i=s' => \$cmd_line_i, + 'cmd_line_j=s' => \$cmd_line_j, + 'cmd_line_k=s' => \$cmd_line_k, + 'debug!' => \$DB, + 'debugX!' => \$DBX, + 'fulllog!' => \$FULL_LOG); - if ($ARGS =~ /--help/i) - { + $DB = 1 if ($DBX); + if ($DB) { + print "\n----- DEBUGGING -----\n\n"; + print "\n----- SUPER-DUPER DEBUGGING -----\n\n" if ($DBX); + print " SYSLOG: $SYSLOG\n" if ($SYSLOG); + print " telnet_host: $telnet_host\n" if ($telnet_host); + print " telnet_port: $telnet_port\n" if ($telnet_port); + print " ASTmgrUSERNAME: $ASTmgrUSERNAME\n" if ($ASTmgrUSERNAME); + print " ASTmgrSECRET: $ASTmgrSECRET\n" if ($ASTmgrSECRET); + print " ASTmgrUSERNAMEsend: $ASTmgrUSERNAMEsend\n" if ($ASTmgrUSERNAMEsend); + print " cmd_line_b: $cmd_line_b\n" if ($cmd_line_b); + print " cmd_line_c: $cmd_line_c\n" if ($cmd_line_c); + print " cmd_line_d: $cmd_line_d\n" if ($cmd_line_d); + print " cmd_line_e: $cmd_line_e\n" if ($cmd_line_e); + print " cmd_line_f: $cmd_line_f\n" if ($cmd_line_f); + print " cmd_line_g: $cmd_line_g\n" if ($cmd_line_g); + print " cmd_line_h: $cmd_line_h\n" if ($cmd_line_h); + print " cmd_line_i: $cmd_line_i\n" if ($cmd_line_i); + print " cmd_line_j: $cmd_line_j\n" if ($cmd_line_j); + print " cmd_line_k: $cmd_line_k\n" if ($cmd_line_k); + print "\n"; + } + if ($CLOhelp) { print "allowed run time options:\n"; print " [--help] = this help screen\n"; - print "required flags:\n"; print " [--SYSLOG] = whether to log actions or not\n"; + print "required flags:\n"; print " [--PATHlogs] = logs directory path\n"; print " [--telnet_host] = IP address to connect to Asterisk Manager\n"; print " [--telnet_port] = port to connect to Asterisk Manager\n"; @@ -85,242 +106,14 @@ if (length($ARGV[0])>1) print " [--cmd_line_X] = lines to send to Manager after action\n"; print " X replaced with b-k (10 lines)\n"; print "\n"; + print "You may prefix an option with 'no' to disable it.\n"; + print " ie. --noSYSLOG or --noFULLLOG\n"; - exit; - } - else - { - if ($ARGS =~ /--SYSLOG=1/) # CLI defined SYSLOG - {$SYSLOG=1;} - else - {$SYSLOG=0;} - if ($ARGS=~ /--PATHlogs=/) # CLI defined PATHlogs - { - @CLIARY = split(/--PATHlogs=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $PATHlogs = $CLIARX[0]; - $PATHlogs =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " PATHlogs: $PATHlogs\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--telnet_host=/) # CLI defined telnet_host - { - @CLIARY = split(/--telnet_host=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $telnet_host = $CLIARX[0]; - $telnet_host =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " telnet_host: $telnet_host\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--telnet_port=/) # CLI defined telnet_port - { - @CLIARY = split(/--telnet_port=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $telnet_port = $CLIARX[0]; - $telnet_port =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " telnet_port: $telnet_port\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--ASTmgrUSERNAME=/) # CLI defined ASTmgrUSERNAME - { - @CLIARY = split(/--ASTmgrUSERNAME=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $ASTmgrUSERNAME = $CLIARX[0]; - $ASTmgrUSERNAME =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " ASTmgrUSERNAME: $ASTmgrUSERNAME\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--ASTmgrSECRET=/) # CLI defined ASTmgrSECRET - { - @CLIARY = split(/--ASTmgrSECRET=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $ASTmgrSECRET = $CLIARX[0]; - $ASTmgrSECRET =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " ASTmgrSECRET: $ASTmgrSECRET\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--ASTmgrUSERNAMEsend=/) # CLI defined ASTmgrUSERNAMEsend - { - @CLIARY = split(/--ASTmgrUSERNAMEsend=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $ASTmgrUSERNAMEsend = $CLIARX[0]; - $ASTmgrUSERNAMEsend =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " ASTmgrUSERNAMEsend: $ASTmgrUSERNAMEsend\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--action=/) # CLI defined action - { - @CLIARY = split(/--action=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $action = $CLIARX[0]; - $action =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " action: $action\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--cmd_line_b=/) # CLI defined cmd_line_b - { - @CLIARY = split(/--cmd_line_b=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $cmd_line_b = $CLIARX[0]; - $cmd_line_b =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " cmd_line_b: $cmd_line_b\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--cmd_line_c=/) # CLI defined cmd_line_c - { - @CLIARY = split(/--cmd_line_c=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $cmd_line_c = $CLIARX[0]; - $cmd_line_c =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " cmd_line_c: $cmd_line_c\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--cmd_line_d=/) # CLI defined cmd_line_d - { - @CLIARY = split(/--cmd_line_d=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $cmd_line_d = $CLIARX[0]; - $cmd_line_d =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " cmd_line_d: $cmd_line_d\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--cmd_line_e=/) # CLI defined cmd_line_e - { - @CLIARY = split(/--cmd_line_e=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $cmd_line_e = $CLIARX[0]; - $cmd_line_e =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " cmd_line_e: $cmd_line_e\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--cmd_line_f=/) # CLI defined cmd_line_f - { - @CLIARY = split(/--cmd_line_f=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $cmd_line_f = $CLIARX[0]; - $cmd_line_f =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " cmd_line_f: $cmd_line_f\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--cmd_line_g=/) # CLI defined cmd_line_g - { - @CLIARY = split(/--cmd_line_g=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $cmd_line_g = $CLIARX[0]; - $cmd_line_g =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " cmd_line_g: $cmd_line_g\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--cmd_line_h=/) # CLI defined cmd_line_h - { - @CLIARY = split(/--cmd_line_h=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $cmd_line_h = $CLIARX[0]; - $cmd_line_h =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " cmd_line_h: $cmd_line_h\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--cmd_line_i=/) # CLI defined cmd_line_i - { - @CLIARY = split(/--cmd_line_i=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $cmd_line_i = $CLIARX[0]; - $cmd_line_i =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " cmd_line_i: $cmd_line_i\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--cmd_line_j=/) # CLI defined cmd_line_j - { - @CLIARY = split(/--cmd_line_j=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $cmd_line_j = $CLIARX[0]; - $cmd_line_j =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " cmd_line_j: $cmd_line_j\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /--cmd_line_k=/) # CLI defined cmd_line_k - { - @CLIARY = split(/--cmd_line_k=/,$ARGS); - @CLIARX = split(/ /,$CLIARY[1]); - if (length($CLIARX[0])>2) - { - $cmd_line_k = $CLIARX[0]; - $cmd_line_k =~ s/\/$| |\r|\n|\t//gi; - if ($DB) {print " cmd_line_k: $cmd_line_k\n";} - } - @CLIARY = @MT; @CLIARX = @MT; - } - if ($ARGS=~ /-debug/i) - { - $DB=1; # Debug flag - } - if ($ARGS=~ /--debugX/i) - { - $DBX=1; - print "\n----- SUPER-DUPER DEBUGGING -----\n\n"; - } - if ($ARGS=~ /-fulllog/i) - { - $FULL_LOG=1; # Full response logging enabled - } - + exit 0; } } -else -{ -#print "no command line options set\n\n"; -} -if (length($action)>1) - { +if ($action) { $PATHlogs =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg; $cmd_line_b =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg; $cmd_line_c =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg; @@ -332,47 +125,47 @@ if (length($action)>1) $cmd_line_i =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg; $cmd_line_j =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg; $cmd_line_k =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg; - - if ($DB) {print " SYSLOG: $SYSLOG\n";} - if ($DB) {print " PATHlogs: $PATHlogs\n";} - if ($DB) {print " telnet_host: $telnet_host\n";} - if ($DB) {print " telnet_port: $telnet_port\n";} - if ($DB) {print " ASTmgrSECRET: $ASTmgrSECRET\n";} - if ($DB) {print " ASTmgrUSERNAME: $ASTmgrUSERNAME\n";} - if ($DB) {print " ASTmgrUSERNAMEsend: $ASTmgrUSERNAMEsend\n";} - if ($DB) {print " action: $action\n";} - if ($DB) {print " cmd_line_b: $cmd_line_b\n";} - if ($DB) {print " cmd_line_c: $cmd_line_c\n";} - if ($DB) {print " cmd_line_d: $cmd_line_d\n";} - if ($DB) {print " cmd_line_e: $cmd_line_e\n";} - if ($DB) {print " cmd_line_f: $cmd_line_f\n";} - if ($DB) {print " cmd_line_g: $cmd_line_g\n";} - if ($DB) {print " cmd_line_h: $cmd_line_h\n";} - if ($DB) {print " cmd_line_i: $cmd_line_i\n";} - if ($DB) {print " cmd_line_j: $cmd_line_j\n";} - if ($DB) {print " cmd_line_k: $cmd_line_k\n";} - - use Net::Telnet (); + if ($DB) { + print " SYSLOG: $SYSLOG\n" if ($SYSLOG); + print " telnet_host: $telnet_host\n" if ($telnet_host); + print " telnet_port: $telnet_port\n" if ($telnet_port); + print " ASTmgrUSERNAME: $ASTmgrUSERNAME\n" if ($ASTmgrUSERNAME); + print " ASTmgrSECRET: $ASTmgrSECRET\n" if ($ASTmgrSECRET); + print " ASTmgrUSERNAMEsend: $ASTmgrUSERNAMEsend\n" if ($ASTmgrUSERNAMEsend); + print " cmd_line_b: $cmd_line_b\n" if ($cmd_line_b); + print " cmd_line_c: $cmd_line_c\n" if ($cmd_line_c); + print " cmd_line_d: $cmd_line_d\n" if ($cmd_line_d); + print " cmd_line_e: $cmd_line_e\n" if ($cmd_line_e); + print " cmd_line_f: $cmd_line_f\n" if ($cmd_line_f); + print " cmd_line_g: $cmd_line_g\n" if ($cmd_line_g); + print " cmd_line_h: $cmd_line_h\n" if ($cmd_line_h); + print " cmd_line_i: $cmd_line_i\n" if ($cmd_line_i); + print " cmd_line_j: $cmd_line_j\n" if ($cmd_line_j); + print " cmd_line_k: $cmd_line_k\n" if ($cmd_line_k); + } - if (!$telnet_port) {$telnet_port = '5038';} + $telnet_port = '5038' if (!$telnet_port); ### connect to asterisk manager through telnet - $tn = new Net::Telnet (Port => $telnet_port, - Prompt => '/.*[\$%#>] $/', - Output_record_separator => '', - Errmode => Return,); + my $tn = new Net::Telnet (Port => $telnet_port, + Prompt => '/.*[\$%#>] $/', + Output_record_separator => '', + Errmode => "return"); #$fh = $tn->dump_log("$PATHlogs/SAC_telnet_log.txt"); # uncomment for telnet log - if (length($ASTmgrUSERNAMEsend) > 3) {$telnet_login = $ASTmgrUSERNAMEsend;} - else {$telnet_login = $ASTmgrUSERNAME;} - $tn->open("$telnet_host"); - $tn->waitfor('/0\n$/'); # print login + my $telnet_login; + if ($ASTmgrUSERNAMEsend) { + $telnet_login = $ASTmgrUSERNAMEsend; + } else { + $telnet_login = $ASTmgrUSERNAME; + } + $tn->open($telnet_host); + $tn->waitfor('/0\n$/'); # print login $tn->print("Action: Login\nUsername: $telnet_login\nSecret: $ASTmgrSECRET\n\n"); - $tn->waitfor('/Authentication accepted/'); # waitfor auth accepted + $tn->waitfor('/Authentication accepted/'); # waitfor auth accepted $tn->buffer_empty; - if ($cmd_line_b =~ /XXYYXXYYXXYYXX/) - { + if ($cmd_line_b =~ /XXYYXXYYXXYYXX/) { # Action: Command # Command: meetme list 8600051 # @@ -381,97 +174,152 @@ if (length($action)>1) # User #: 03 261 261iax Channel: IAX2/261iax-13 (unmonitored) # 1 users in that conference. # --END COMMAND-- - $meetme_command = "Action: Command\nCommand: meetme list $cmd_line_k\n\n"; - print "$now_date|$SYSLOG|\n$meetme_command"; + my $meetme_command = "Action: Command\nCommand: meetme list $cmd_line_k\n\n"; + print nowDate() . "|$SYSLOG|\n$meetme_command"; - @list_meetme = $tn->cmd(String => "$meetme_command", Prompt => '/--END COMMAND-.*/'); - foreach(@list_meetme) - { - if ($list_meetme[$m] =~ /$cmd_line_j /i) - { - $list_meetme[$m] =~ s/User \#: //gi; - @participants = split(/ /, $list_meetme[$m]); + my $participant; + my @list_meetme = $tn->cmd(String => $meetme_command, + Prompt => '/--END COMMAND-.*/'); + foreach my $meetme (@list_meetme) { + if ($meetme =~ /$cmd_line_j /i) { + $meetme =~ s/User \#: //gi; + my @participants = split(/ /, $meetme); $participant = ($participants[0] + 0); - } - # print "$m|$participant|$cmd_line_j|$list_meetme[$m]"; - $m++; } - if ($participant > 0) {$cmd_line_b =~ s/XXYYXXYYXXYYXX/$participant/gi;} + # print "$participant|$cmd_line_j|$meetme"; + } + $cmd_line_b =~ s/XXYYXXYYXXYYXX/$participant/gi if ($participant > 0); $cmd_line_j = ''; $cmd_line_k = ''; - } + } - $originate_command = ''; + my $originate_command; $originate_command .= "Action: $action\n"; - if (length($cmd_line_b)>3) {$originate_command .= "$cmd_line_b\n";} - if (length($cmd_line_c)>3) {$originate_command .= "$cmd_line_c\n";} - if (length($cmd_line_d)>3) {$originate_command .= "$cmd_line_d\n";} - if (length($cmd_line_e)>3) {$originate_command .= "$cmd_line_e\n";} - if (length($cmd_line_f)>3) {$originate_command .= "$cmd_line_f\n";} - if (length($cmd_line_g)>3) {$originate_command .= "$cmd_line_g\n";} - if (length($cmd_line_h)>3) {$originate_command .= "$cmd_line_h\n";} - if (length($cmd_line_i)>3) {$originate_command .= "$cmd_line_i\n";} - if (length($cmd_line_j)>3) {$originate_command .= "$cmd_line_j\n";} - if (length($cmd_line_k)>3) {$originate_command .= "$cmd_line_k\n";} + #if (length($cmd_line_b)>3) {$originate_command .= "$cmd_line_b\n";} + $originate_command .= $cmd_line_b . "\n" if ($cmd_line_b); + $originate_command .= $cmd_line_c . "\n" if ($cmd_line_c); + $originate_command .= $cmd_line_d . "\n" if ($cmd_line_d); + $originate_command .= $cmd_line_e . "\n" if ($cmd_line_e); + $originate_command .= $cmd_line_f . "\n" if ($cmd_line_f); + $originate_command .= $cmd_line_g . "\n" if ($cmd_line_g); + $originate_command .= $cmd_line_h . "\n" if ($cmd_line_h); + $originate_command .= $cmd_line_i . "\n" if ($cmd_line_i); + $originate_command .= $cmd_line_j . "\n" if ($cmd_line_j); + $originate_command .= $cmd_line_k . "\n" if ($cmd_line_k); $originate_command .= "\n"; - print "$now_date|$SYSLOG|\n$originate_command"; - $event_string = "0|$SYSLOG|"; - $event_string .= "\n$originate_command"; - if ( ($FULL_LOG) && ($SYSLOG) ) - {&full_event_logger;} + print nowDate() . "|$SYSLOG|\n$originate_command"; + my $event_string = "0|" . $SYSLOG . "|"; + $event_string .= "\n" . $originate_command; - @list_channels = $tn->cmd(String => "$originate_command", Prompt => '/.*/'); + eventLogger($PATHlogs,'full',$event_string) if ($FULL_LOG and $SYSLOG); + + my @list_channels = $tn->cmd(String => $originate_command, + Prompt => '/.*/'); sleep(3); - if ( ($FULL_LOG) && ($SYSLOG) ) - { - $event_string = "1|$data1|"; $k=0; - foreach(@list_channels) {$event_string .= "$list_channels[$k]"; $k++;} - $read_input_buf = $tn->get(Errmode => Return, Timeout => 1,); - $event_string .= "$read_input_buf"; - &full_event_logger; + my $data1; # ? Useless ? + if ($FULL_LOG and $SYSLOG) { + my $event_string = "1|" . $data1 . "|"; + foreach my $channel (@list_channels) { + $event_string .= $channel; } + my $read_input_buf = $tn->get(Errmode => "return", + Timeout => 1); + $event_string .= $read_input_buf; + eventLogger($PATHlogs,'full',$event_string); + } $tn->buffer_empty; - - @hangup = $tn->cmd(String => "Action: Logoff\n\n", Prompt => "/.*/"); - + #@hangup = $tn->cmd(String => "Action: Logoff\n\n", Prompt => "/.*/"); + $tn->cmd(String => "Action: Logoff\n\n", Prompt => "/.*/"); sleep(2); - if ( ($FULL_LOG) && ($SYSLOG) ) - { - $event_string = "2|$data1|"; $k=0; - foreach(@list_channels) {$event_string .= "$list_channels[$k]"; $k++;} - $read_input_buf = $tn->get(Errmode => Return, Timeout => 1,); - $event_string .= "$read_input_buf"; - &full_event_logger; + if ($FULL_LOG and $SYSLOG) { + my $event_string = "2|" . $data1 . "|"; + foreach my $channel (@list_channels) { + $event_string .= $channel; } + my $read_input_buf = $tn->get(Errmode => "return", + Timeout => 1); + $event_string .= $read_input_buf; + eventLogger($PATHlogs,'full',$event_string); + } $tn->buffer_empty; + $tn->close; +} - $ok = $tn->close; - - - } - -$secZ = time(); -$script_time = ($secZ - $secX); +my $secZ = time(); +my $script_time = ($secZ - $secX); print "DONE execute time: $script_time seconds\n"; -exit; +exit 0; +# Program ends. -sub full_event_logger -{ -if ($SYSLOG) - { - ### open the log file for writing ### - open(Lout, ">>$PATHlogs/action_full.$action_log_date") - || die "Can't open $PATHlogs/action_full.$action_log_date: $!\n"; - print Lout "$now_date|$event_string|\n"; - close(Lout); - } -$event_string=''; + +### Start of subs. + +# eventLogger usage: +# eventLgger($LogFileDir, $LogType, $EventString); +# Requires: +# $LogFilePath : Directory where log files are. +# $LogType : Type of log, ie process, send, launch, full +# $EventString : String to record in log. +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); +# Options: +# $SecondsSinceEpoch : Request time in seconds, defaults to current date/time. +# Returns: +# ($sec, $min, $hour. $day, $mon, $year) +sub getTime { + my ($tms) = @_; + $tms = time unless ($tms); + my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($tms); + $year += 1900; + $mon++; + $mon = "0" . $mon if ($mon < 10); + $mday = "0" . $mday if ($mday < 10); + $min = "0" . $min if ($min < 10); + $sec = "0" . $sec if ($sec < 10); + return ($sec,$min,$hour,$mday,$mon,$year); +} + +# nowDate usage: +# nowDate($SecondsSinceEpoch); +# Options: +# $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 { + my ($tms) = @_; + my($sec,$min,$hour,$mday,$mon,$year) = getTime($tms); + return $year.'-'.$mon.'-'.$mday.' '.$hour.':'.$min.':'.$sec; +} + +# logDate usage: +# logDate($SecondsSinceEpoch); +# Options: +# $SecondsSinceEpoch : Request time in seconds, defaults to current date/time. +# Returns: +# scalar date string ie "2007-01-01" +sub logDate { + my ($tms) = @_; + my($sec,$min,$hour,$mday,$mon,$year) = getTime($tms); + return $year . '-' . $mon . '-' . $mday; +} + +### End of subs + +