Untitled
unknown
plain_text
2 years ago
2.3 kB
7
Indexable
#!/usr/bin/perl -w
use strict;
use lib '/usr/local/res/pm/';
use web_function;
use Getopt::Std;
# Script che invia una notifica se viene caricato un nuovo file nell'area FTP
# schedulato in crontab gira ogni 5 minuti
# args:
# crs-alert-upload-ftp.pl -t : test configurazione x chiara
# Leggo configurazione
my $FILE_CFG = "/usr/local/res/conf/ftp-alert-upload.json";
# Formato json
#{
# "directory" : { "mail_to": "mail che ricevono la notifica", "nome" : "nome ftp"}
#}
my $opts = {};
getopts('t', $opts);
my $cfg_text = '';
open(FILE, '<', $FILE_CFG );
while(my $l = <FILE>) { $cfg_text .= $l; }
close(FILE);
my $json = JSON->new();
my $cfg = $json->decode($cfg_text);
# Test configurazione
if (exists $opts->{t} && $opts->{t} == 1) {
print "Directory caricate:\n";
print join("\n", sort keys %{$cfg})."\n";
print "Tot: " . (scalar keys %{$cfg}) ."\n";
exit;
}
# scorro tra le directory
foreach my $k (sort keys %{$cfg}) {
# Notifica nuovi file per maccione
&find_new_file({
path => $k,
ago_n => 5, # 5 minuti
mail_to => $cfg->{$k}->{mail_to},
nome => $cfg->{$k}->{nome}
});
}
sub find_new_file {
my $args = shift;
# * mandatory
my $path = $args->{path}; # * path dove effettuare la ricerca
my $mail_to = $args->{mail_to}; # * mail dove notificare
my $nome = $args->{nome}; # nome area FTP
# $ago_n = file + nuovi di X minuti fa. Default: 800 minuti
my $ago_n = exists $args->{ago_n} ? $args->{ago_n} : 5;
# cerco nuovi file
my $find_command = `find $path -type f -newermt "$ago_n minutes ago"`;
# se trovo nuovi file
if($find_command ne '') {
my @files = split("\n", $find_command);
# Elimino path dai file
for(my $i = 0;$i < scalar @files; $i++) {
$files[$i] =~ s/$path//;
}
# Invio la notifica
my $content = join("<br>\n", @files);
my $mail_args = {
from => 'noreply@resbinaria.com',
to => $mail_to,
subject => "Nuovi file nell'area FTP $nome",
content => qq(
Sono stati caricati nuovi file nell'area FTP $nome:<br>
$content
),
no_restful_log => 1,
};
&web_function::email_to($mail_args);
}
}
Editor is loading...
Leave a Comment