Added the ability to use Automated Reports with standard SFTP and FTPSSL remote servers.

git-svn-id: svn://192.168.202.10@3253 3d104415-ff17-0410-8863-d5cf3c621b8a
This commit is contained in:
mattf
2020-06-02 02:40:07 +00:00
parent 7aab76b281
commit 06b5d74ae7
6 changed files with 1774 additions and 71 deletions
+137 -66
View File
@@ -9,7 +9,7 @@
#
# /usr/share/astguiclient/AST_email_web_report.pl --email-list=test@gmail.com --email-sender=test@test.com
#
# Copyright (C) 2017 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
# Copyright (C) 2020 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# CHANGES
# 90225-1247 - First version
@@ -21,6 +21,7 @@
# 170306-0915 - Fixed proper file extensions for different download report types
# 170719-1242 - Added more debug and '8days' date option
# 170825-1114 - Added report filename_override option
# 200601-0941 - Added support to launch an SFTP process using settings containers, added settings container CLI option for Email/FTP settings
#
$txt = '.txt';
@@ -29,6 +30,7 @@ $US = '_';
$MT[0] = '';
$log_to_adminlog=0;
$report_id='';
$container_entry_length=0;
$secX = time();
@@ -143,6 +145,63 @@ if ($Mmin < 10) {$Mmin = "0$Mmin";}
if ($Msec < 10) {$Msec = "0$Msec";}
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$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;}
# if ( ($line =~ /^VARREPORT_host/) && ($CLIREPORT_host < 1) )
# {$VARREPORT_host = $line; $VARREPORT_host =~ s/.*=//gi;}
# if ( ($line =~ /^VARREPORT_user/) && ($CLIREPORT_user < 1) )
# {$VARREPORT_user = $line; $VARREPORT_user =~ s/.*=//gi;}
# if ( ($line =~ /^VARREPORT_pass/) && ($CLIREPORT_pass < 1) )
# {$VARREPORT_pass = $line; $VARREPORT_pass =~ s/.*=//gi;}
# if ( ($line =~ /^VARREPORT_dir/) && ($CLIREPORT_dir < 1) )
# {$VARREPORT_dir = $line; $VARREPORT_dir =~ s/.*=//gi;}
if ( ($line =~ /^VARREPORT_port/) && ($CLIREPORT_port < 1) )
{$VARREPORT_port = $line; $VARREPORT_port =~ s/.*=//gi;}
$i++;
}
# Customized Variables
$server_ip = $VARserver_ip; # Asterisk server IP
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;
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
@@ -164,7 +223,8 @@ if (length($ARGV[0])>1)
print " [--remove-links] = Will parse through the output file and remove any links in the HTML\n";
print " [--log-to-adminlog] = Put an entry in the admin log after this report runs\n";
print " [--report-id=XXXXXXXX] = Report ID in the system to gather settings from\n";
print " NOTE: this will override any other options set below this one\n";
print " [--container-id=XXX] = System settings container to pull CLI flags from, WILL REPLACE ALL CLI FLAGS BELOW!\n";
print " NOTE: the two above options will override any other options set below this one\n";
print " [--email-subject=XYZ] = Email message subject, which will be followed by the date of the report\n";
print " [--email-list=test@test.com:test2@test.com] = send email results to these addresses\n";
print " [--email-sender=vicidial@localhost] = sender for the email results\n";
@@ -222,8 +282,35 @@ if (length($ARGV[0])>1)
$report_id =~ s/:/,/gi;
if ($DB > 0) {print "\n----- REPORT ID: $report_id -----\n\n";}
}
else
if ( (length($report_id) < 1) || ($args =~ /--container_id|--container-id/i) )
{
if ($args =~ /--container_id|--container-id/i)
{
@data_in = split(/--container_id=|--container-id=/,$args);
$container_id = $data_in[1];
$container_id =~ s/ .*//gi;
$stmtA= "SELECT container_entry from vicidial_settings_containers where container_id='$container_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$container_entry = $aryA[0];
$container_entry =~ s/\t|\r|'|;|`|\\\\|\///gi;
$container_entry =~ s/\n/ /gi;
$container_entry_length = length($container_entry);
}
if ($DB) {print "\n----- SETTINGS CONTAINER CLI DEFINED: $container_id ($container_entry_length) -----\n\n";}
if ($container_entry_length > 10)
{
if ($DB) {print "\n----- CLI FLAGS REPLACED: $args ($container_entry) -----\n\n";}
$args = $container_entry;
}
}
if ($args =~ /--ftp-server=/i)
{
@data_in = split(/--ftp-server=/,$args);
@@ -396,56 +483,6 @@ if (length($Bstart_date) < 8)
if (length($begindate) < 8)
{$begindate = $shipdate;}
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$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;}
# if ( ($line =~ /^VARREPORT_host/) && ($CLIREPORT_host < 1) )
# {$VARREPORT_host = $line; $VARREPORT_host =~ s/.*=//gi;}
# if ( ($line =~ /^VARREPORT_user/) && ($CLIREPORT_user < 1) )
# {$VARREPORT_user = $line; $VARREPORT_user =~ s/.*=//gi;}
# if ( ($line =~ /^VARREPORT_pass/) && ($CLIREPORT_pass < 1) )
# {$VARREPORT_pass = $line; $VARREPORT_pass =~ s/.*=//gi;}
# if ( ($line =~ /^VARREPORT_dir/) && ($CLIREPORT_dir < 1) )
# {$VARREPORT_dir = $line; $VARREPORT_dir =~ s/.*=//gi;}
if ( ($line =~ /^VARREPORT_port/) && ($CLIREPORT_port < 1) )
{$VARREPORT_port = $line; $VARREPORT_port =~ s/.*=//gi;}
$i++;
}
# Customized Variables
$server_ip = $VARserver_ip; # Asterisk server IP
if (!$Q)
{
@@ -454,14 +491,6 @@ if (!$Q)
print "\n";
}
if ( (length($report_id) > 1) || ($log_to_adminlog > 0) )
{
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;
}
if (length($report_id) > 1)
{
### Grab Server values from the database
@@ -473,16 +502,13 @@ if (length($report_id) > 1)
{
@aryA = $sthA->fetchrow_array;
$report_destination = $aryA[0];
if ($report_destination =~ /EMAIL/)
if ( ($report_destination =~ /EMAIL/) && ($container_entry_length < 10) )
{
$email_sender = $aryA[1];
$email_list = $aryA[2];
$email_subject = $aryA[3];
$email_subject =~ s/--A--date--B--|--A--today--B--/$year-$mon-$mday/gi;
$email_subject =~ s/--A--yesterday--B--/$Tyear-$Tmon-$Tmday/gi;
$email_subject =~ s/--A--datetime--B--/$year-$mon-$mday $hour:$min:$sec/gi;
}
if ($report_destination =~ /FTP/)
if ( ($report_destination =~ /FTP/) && ($container_entry_length < 10) )
{
$VARREPORT_host = $aryA[4];
$VARREPORT_user = $aryA[5];
@@ -492,6 +518,9 @@ if (length($report_id) > 1)
$location = $aryA[8];
$filename_override = $aryA[9];
if ($DB) {print "RAW LOCATION: |$location|\n";}
$email_subject =~ s/--A--date--B--|--A--today--B--/$year-$mon-$mday/gi;
$email_subject =~ s/--A--yesterday--B--/$Tyear-$Tmon-$Tmday/gi;
$email_subject =~ s/--A--datetime--B--/$year-$mon-$mday $hour:$min:$sec/gi;
$location =~ s/\&/\\&/gi;
$location =~ s/--A--today--B--/$year-$mon-$mday/gi;
$location =~ s/--A--yesterday--B--/$Tyear-$Tmon-$Tmday/gi;
@@ -571,6 +600,48 @@ if (length($report_id) > 1)
{$HTMLfile = "$filename_override$file_extension";}
$HTMLfileLOG = $report_id . "_" . $start_date . "_" . $hms . '' . $txt;
$shipdate='';
### check for SFTP Settings Container
if ($VARREPORT_host =~ /^SFTPCNTR|^FTPSCNTR/)
{
$SFTPcontainer_entry_length=0;
$temp_VARREPORT_host = $VARREPORT_host;
$temp_VARREPORT_host =~ s/^SFTPCNTR|^FTPSCNTR//gi;
$stmtA= "SELECT container_entry from vicidial_settings_containers where container_id='$temp_VARREPORT_host';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$SFTPcontainer_entry = $aryA[0];
$SFTPcontainer_entry =~ s/\t|\r|'|;|`|\\\\//gi;
$SFTPcontainer_entry =~ s/\n/ /gi;
$SFTPcontainer_entry_length = length($SFTPcontainer_entry);
print "$SFTPcontainer_entry\n";
}
if ($SFTPcontainer_entry_length > 10)
{
$FTP_EMAIL_WEB_SCRIPT = 'AST_email_web_report.pl';
if ($VARREPORT_host =~ /^SFTPCNTR/) {$FTP_EMAIL_WEB_SCRIPT = 'AST_email_web_reportSFTP.pl';}
if ($VARREPORT_host =~ /^FTPSCNTR/) {$FTP_EMAIL_WEB_SCRIPT = 'AST_email_web_reportFTPSSL.pl';}
if (!$Q) {print "SFTP server settings detected, launching separate SFTP process: $VARREPORT_host ($temp_VARREPORT_host)|$SFTPcontainer_entry_length \n";}
$ar_command = "$PATHhome/$FTP_EMAIL_WEB_SCRIPT --quiet --remove-images --remove-links --log-to-adminlog --report-id=$report_id --container-id=$temp_VARREPORT_host ";
if ($DB) {print "starting SFTP automated report process in a screen... |ARS$report_id| $ar_command \n";}
`/usr/bin/screen -d -m -S ARS$report_id $ar_command `;
$stmtA="UPDATE vicidial_automated_reports SET run_now_trigger='N' where run_now_trigger='Y' and report_server IN('$server_ip'$THISserver_voicemailSQL) and report_id='$report_id';";
$ARaffected_rows = $dbhA->do($stmtA);
if ($DB) {print "FINISHED: $ARaffected_rows|$stmtA| \n";}
exit;
}
}
}
else
{