#!/usr/bin/perl ############################################################################### # Auteur : X H # But : voir le perldoc ... # ATTENTION : ne pas oublier de rajouter perl devant le nom du script pour que les paramètres soient pris en compte ############################################################################### use warnings; use strict; use Data::Dumper; use Encode; # pour les problèmes d'accent use Tkx; use threads; # http://perl.mines-albi.fr/DocFr/perlthrtut.html use Thread::Semaphore; my $semaphore = Thread::Semaphore->new(5); # Crée un sémaphore avec le compteur initialisé à cinq my @lst_log = ( 'sqlload_agents.log', 'sqlload_seg_gestion.log', # ... 'sqlload_uch.log', 'sqlload_uop.log' ); # les variables génériques, pour horodater le fichier : my ($jour, $mois, $annee) = (localtime(time() - (60 * 60 * 24)))[3, 4, 5]; my $date_veille = sprintf("%d%02d%02d",(1900+$annee), ($mois+1), $jour); my ($day, $mon, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0]; my $date_fic = sprintf("%d%02d%02d",(1900+$year), ($mon+1), $day); my $date_mois = sprintf("%d%02d",(1900+$year), ($mon+1)); # sélection du répertoire de version pour stockage des fichiers my $rep_fichiers = Tkx::tk___chooseDirectory( -title => "Sélection du répertoire de la version SPOT IF.",); exit 9 unless ($rep_fichiers); # $rep_fichiers = encode( "iso-8859-1",$rep_fichiers); my $rep_jour = encode( "iso-8859-1","$rep_fichiers/$date_fic"); if (-d $rep_jour) { $rep_jour = Tkx::tk___chooseDirectory( -title => "Selection du repertoire ou deposer les fichiers PROD du jour.",); } else { # print LOG "Creation du repertoire $rep_jour.\n"; mkdir $rep_jour or die "Probleme creation repertoire $rep_jour E/S: $!\n"; my $rep_tmp = "$rep_jour/CSV"; mkdir $rep_tmp or die "Probleme creation repertoire $rep_tmp E/S: $!\n"; $rep_tmp = encode( "iso-8859-1","$rep_fichiers/$date_fic/".decode("utf8", "XML Générés")); # $rep_tmp = "$rep_jour/XML Générés"; mkdir $rep_tmp or die "Probleme creation repertoire $rep_tmp E/S: $!\n"; $rep_tmp = encode( "iso-8859-1","$rep_fichiers/$date_fic/".decode("utf8", "Logs Préparateur")); # $rep_tmp = "$rep_jour/Logs Préparateur"; mkdir $rep_tmp or die "Probleme creation repertoire $rep_tmp E/S: $!\n"; } # attente de l'apparition du fichier Tkx::tk___messageBox(-message => "Le fichier ARMEN SPO_$date_veille*_T.tar.gz est il présent dans le répertoire distant ?\n", -icon => "question", -title => "Fichier SPO_$date_veille*_T.Tar.GZ"); # récupération du fichier ARMEN .Tar.GZ dans le répertoire . # my $thr = threads->new(\&recup_fic, 'SPO_*_T.tar.gz', '/transfert/reception', $rep_jour, 'ARMEN'); # si le fichier est déjà archivé my $thr1 = threads->new(\&recup_fic, 'SPO_*_T.tar.gz', "/transfert/reception/DIFARMEN_$date_fic", $rep_jour, 'ARMEN'); # $thr1->detach; # A partir de maintenant, nous nous désintéressons officiellement du thread $thr1->join; my $choix = Tkx::tk___messageBox( -message => "Faut il recuperer le fichier INITSE : SPOT_MAXIMO_$date_mois*.tar.gz ?\n", -type => "yesno", -icon => "question", -title => "Fichier SPOT_MAXIMO_$date_mois*_T.Tar.GZ"); # récupération du fichier INITSE .Tar.GZ dans le répertoire . if($choix eq 'yes') { print "recuperation INITSE\n"; my $thr = threads->new(\&recup_fic, 'SPOT_MAXIMO_*_T.tar.gz', '/transfert/reception', $rep_jour, 'INITSE'); # si le fichier est déjà archivé # my $thr = threads->new(\&recup_fic, 'SPOT_MAXIMO_*_T.tar.gz', "/transfert/reception/INITSE_$date_fic", $rep_jour, 'INITSE'); $thr->detach; # A partir de maintenant, nous nous désintéressons officiellement du thread # } else { } $choix = Tkx::tk___messageBox( -message => "Faut il recuperer le fichier MOA : MOA1_$date_fic*.zip ?\n", -type => "yesno", -icon => "question", -title => "Fichier MOA1_$date_fic*.Zip"); # récupération du fichier MOA .Zip dans le répertoire . if($choix eq 'yes') { print "recuperation MOA\n"; my $thr = threads->new(\&recup_fic, 'MOA1_*.zip', '/transfert/reception', $rep_jour, 'MOA'); # si le fichier est déjà archivé # my $thr = threads->new(\&recup_fic, 'MOA1_*.zip', "/transfert/reception/MOA1_$date_fic", $rep_jour, 'MOA'); $thr->detach; # A partir de maintenant, nous nous désintéressons officiellement du thread # } else { } $choix = Tkx::tk___messageBox( -message => "Faut il recuperer le fichier NINIV : RMDOGNPM_$date_fic*.zip ?\n", -type => "yesno", -icon => "question", -title => "Fichier RMDOGNPM_$date_fic*.Zip"); # récupération du fichier NINIV .Zip dans le répertoire . if($choix eq 'yes') { print "recuperation NINIV\n"; my $thr = threads->new(\&recup_fic, 'RMDOGNPM_*.zip', '/transfert/reception', $rep_jour, 'NINIV'); # si le fichier est déjà archivé # my $thr = threads->new(\&recup_fic, 'RMDOGNPM_*.zip', "/transfert/reception/NINIV_$date_fic", $rep_jour, 'NINIV'); $thr->detach; # A partir de maintenant, nous nous désintéressons officiellement du thread # } else { } $choix = Tkx::tk___messageBox( -message => "Faut il recuperer le fichier RESEAU : RMDOGCSEG_$date_veille*.zip ?\n", -type => "yesno", -icon => "question", -title => "Fichier RMDOGCSEG_$date_veille*.Zip"); # récupération du fichier RESEAU .Zip dans le répertoire . if($choix eq 'yes') { print "recuperation RESEAU\n"; my $thr = threads->new(\&recup_fic, 'RMDOGCSEG_*.zip', '/transfert/reception', $rep_jour, 'RESEAU'); # si le fichier est déjà archivé # my $thr = threads->new(\&recup_fic, 'RMDOGCSEG_*.zip', "/transfert/reception/RESEAU_$date_fic", $rep_jour, 'RESEAU'); $thr->detach; # A partir de maintenant, nous nous désintéressons officiellement du thread # } else { } $choix = Tkx::tk___messageBox( -message => "Faut il recuperer les fichiers SIRH : SPO.GES.N.*.zip SPO.REF.N.*.zip ?\n", -type => "yesno", -icon => "question", -title => "Fichiers SPO.GES.N.*.Zip SPO.REF.N.*.Zip"); # récupération du fichier SIRH .Zip dans le répertoire . if($choix eq 'yes') { my $rep_tmp = "$rep_jour/SIRH"; mkdir $rep_tmp or die "Probleme creation repertoire $rep_tmp E/S: $!\n"; print "recuperation SIRH GES\n"; # my $thrA = threads->new(\&recup_fic, 'SPO.GES.N.*.ZIP', '/transfert/reception', $rep_tmp, 'SIRH GES'); # si le fichier est déjà archivé my $thrA = threads->new(\&recup_fic, 'SPO.GES.N.*.ZIP', "/transfert/reception/SIRH_$date_fic", $rep_tmp, 'SIRH GES'); print "recuperation SIRH REF\n"; # my $thrB = threads->new(\&recup_fic, 'SPO.REF.N.*.ZIP', '/transfert/reception', $rep_tmp, 'SIRH REF'); # si le fichier est déjà archivé my $thrB = threads->new(\&recup_fic, 'SPO.REF.N.*.ZIP', "/transfert/reception/SIRH_$date_fic", $rep_tmp, 'SIRH REF'); foreach my $thr (threads->list) { print "Tread $thr, ", $thr->tid, "\n"; # Ne pas rejoindre le thread principal ni nous-mêmes if ($thr->tid && !threads::equal($thr, threads->self)) { my @tempo = $thr->join; print "Tread $thr join :<", join('><', @tempo), ">\n"; } } # print "fin de boucle Threads.\n"; # } else { } print "debut\n"; # attente de l'apparition des fichiers Tkx::tk___messageBox(-message => "Les fichier T_*.csv sont ils présents dans le répertoire distant ?\n", -icon => "question", -title => "Fichiers T_*.CSV"); # récupération des fichiers CSV dans le répertoire du même nom. my $thr2 = threads->new(\&recup_fic, 'T_*.csv', '/transfert/reception', "$rep_jour/CSV", 'CSV'); $thr2->detach; # A partir de maintenant, nous nous désintéressons officiellement du thread # attente de l'apparition des fichiers Tkx::tk___messageBox(-message => "Les fichier *.xml sont ils présents dans le répertoire distant ?\n", -icon => "question", -title => "Fichiers *.XML"); # récupération des fichiers XML dans le répertoire du même nom. my $thr3 = threads->new(\&recup_fic, "\*$date_fic\*\.xml", '/transfert/emission', encode( "iso-8859-1","$rep_fichiers/$date_fic/".decode("utf8", "XML Générés")), 'XML'); $thr3->detach; # A partir de maintenant, nous nous désintéressons officiellement du thread # récupération des fichiers BAD dans le répertoire LOG. my $thr4 = threads->new(\&recup_fic, '*.bad', '/traces/spotimp/loader', encode( "iso-8859-1","$rep_fichiers/$date_fic/".decode("utf8", "Logs Préparateur")), 'BAD'); $thr4->detach; # A partir de maintenant, nous nous désintéressons officiellement du thread # récupération des fichiers LOG dans le répertoire du même nom. for (@lst_log) { my $thr = threads->new(\&recup_fic, $_, '/traces/spotimp/loader', encode( "iso-8859-1","$rep_fichiers/$date_fic/".decode("utf8", "Logs Préparateur")), 'LOG'); } foreach my $thr (threads->list) { # Ne pas rejoindre le thread principal ni nous-mêmes if ($thr->tid && !threads::equal($thr, threads->self)) { $thr->join; } } =head1 NAME IF_recup_quotidien.pl =head1 SYNOPSIS Interactive mode: perl IF_recup_quotidien.pl =head1 DESCRIPTION Il s'agit d'automatiser la création des répertoire et la récupération quotidienne des fichiers de PROD pour SPOT IF. =head1 ALGORITHME choix du répertoire de version de SPOT IF. création du répertoire et des répertoires CSV, XML, Logs préparateurs dans ce répertoire créé. si besoin, récupération des fichiers DIFARMEN, INITSE, MOA1, NINIV, RESEAU, SIRH d'extension .Tar.GZ ou .Zip dans le répertoire . récupération des fichiers CSV dans le répertoire du même nom. récupération des fichiers XML dans le répertoire du même nom. récupération des fichiers BAD dans le répertoire des Logs. récupération des fichiers LOG dans le répertoire du même nom. =head1 AUTEUR Xavier HERVIEU, beginner in Perl. =cut sub recup_fic{ # récupération des paramètres my ($fic, $source, $rep, $cas) = @_; $semaphore->down; print "Semaphore $cas avant:", Dumper($semaphore); print "fichier '$fic' dans '$source' vers '$rep'.\n"; # transfert via pscp de PUTTY du ou des fichiers `C:/MCOBOX/pscp -sftp -pw \@:$source/$fic "$rep/"` ; $semaphore->up; print "Semaphore $cas apres:", Dumper($semaphore); }