From f16c6c99ea5a4a687715b9fab0e6b8e8a1ac5157 Mon Sep 17 00:00:00 2001 From: Pierre Legay Date: Tue, 15 Sep 2020 09:28:23 +0200 Subject: [PATCH] =?UTF-8?q?Script.pl=20adapt=C3=A9s=20au=20nouveau=20forma?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/liste_boursiers.sh | 7 +-- script/load_liste_boursiers.pl | 76 ++++++++++++++++---------------- script/reload_liste_boursiers.pl | 62 ++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 42 deletions(-) create mode 100644 script/reload_liste_boursiers.pl diff --git a/script/liste_boursiers.sh b/script/liste_boursiers.sh index 4d326ef..5380ee1 100644 --- a/script/liste_boursiers.sh +++ b/script/liste_boursiers.sh @@ -1,3 +1,4 @@ +#!/bin/bash PROG=/home/esco/load_liste_boursiers.pl REP=/home/esco/logs @@ -6,11 +7,11 @@ FILE=load_liste_boursiers LOG=$REP/${FILE}.log -echo " " > $LOG -echo " ------ $PROG ------ " >> $LOG +echo " ------ $PROG ------ " > $LOG + $PROG >> $LOG 2>&1 -cat $LOG +/bin/cat $LOG /bin/cat $LOG >> $REP/${FILE}_$(date +%m).log diff --git a/script/load_liste_boursiers.pl b/script/load_liste_boursiers.pl index 1da8383..afe8356 100644 --- a/script/load_liste_boursiers.pl +++ b/script/load_liste_boursiers.pl @@ -27,27 +27,27 @@ my $sftp = "/usr/bin/sftp -b- $adr_ftp"; #la commande scp -sub scp { +sub scp(){ my $addr = shift; my $com = "scp $tmpFile ${addr}:$rep_conf_portail"; - printLog("$com"); + &printLog("$com"); system "$com" || die "scp error: $!"; } #la commande curl -sub curl { +sub curl(){ my $addr = shift; my $result = -1; my $com = sprintf ($curl_format, $addr); - printLog("$com"); - open COM, "$com 2> /tmp/curl.error |" or die $!; + &printLog("$com"); + open COM, "$com 2> /tmp/curl.error && echo |" or die $!; $line = ; print $line, "\n"; if ($line =~ /(\d+),(\d+)/){ - $result = int($1) + int($2); + $result = $1; } close COM; open ERROR, "/tmp/curl.error"; @@ -55,13 +55,13 @@ sub curl { print ; } close ERROR; - printLog( "$result boursiers ont été chargés"); + &printLog( "$result boursiers ont été chargés"); return $result; } # print daté -sub printLog { +sub printLog(){ my @localTime = localtime time; my $horodatage = sprintf( @@ -78,21 +78,21 @@ sub printLog { } #Le controle de la taille du fichier copier: -sub testSize { +sub testSize(){ my $addr = shift; my $sizeOk = shift; my $com = "ssh $addr 'ls -l ${rep_conf_portail}${fileName}'"; open COM, "$com |" or die $!; - if ( size() != $sizeOk ){ + if ( &size() != $sizeOk ){ die "Erreur de copie sur $addr!\n"; } - printLog("copie Ok"); + &printLog("copie Ok"); close COM; } # recuperation de la taille d'un fichier a partir d'une ligne donnée par ls -l -sub size { +sub size(){ my $line = shift; my @col = split ('\s+', $line); @@ -100,20 +100,19 @@ sub size { } # verification du fichier dans tmp renvoie le nombre de ligne de boursier; -sub verifFile { +sub verifFile(){ open FILE, "$tmpFile" or die $!; my $cpt = 0; - { - local $_ = ; - die "ERREUR d'entête : $_" unless /^\"id\";\"ine\";\"echelon\"/; - } + $_ = ; + die "ERREUR d'entête : $_" unless /^\"ine\";\"echelon\"/; while () { chomp; if ($_) { my @col = split('";"', $_); $cpt ++; - die "ERREUR mauvais format de ligne ($cpt) : $_\n " unless $col[2] =~ /^\d+$/; + # print "$_>" . $col[0]. "<>" . $col[1] . "<\n"; + die "ERREUR mauvais format de ligne ($cpt) : $_; \n " unless $col[1] =~ /^\d+\"/; } } close FILE; @@ -123,10 +122,10 @@ sub verifFile { #on ouvre la connexion sftp -printLog("$sftp"); +&printLog("$sftp"); open2 (READ, WRITE, $sftp ) or die "erreur connexion sftp: $!\n"; -printLog("\t connexion ok"); +&printLog("\t connexion ok"); #on recupere une ligne vide pour le prompt print WRITE "\n"; @@ -143,14 +142,14 @@ sub verifFile { print WRITE "ls -l ${prefixeFile}_*.csv\n\n"; $_=; -printLog($_); +&printLog($_); while () { last if /^$prompt$/; print($_); if (/(${prefixeFile}_\d{8}\.csv)$/) { push @listFile, $1; - $sizeFile{$1} = size($_); + $sizeFile{$1} = &size($_); } } @@ -159,35 +158,35 @@ sub verifFile { my $lastFile = $listFile[-1]; my $lastSize = $sizeFile{$lastFile}; -printLog("last : $lastSize $lastFile"); +&printLog("last : $lastSize $lastFile"); # on le recupère dans /tmp if ($lastFile){ print WRITE "get $lastFile $tmpFile\n\n"; while () { last if /^$prompt$/; - printLog($_); + &printLog($_); } } -# on ajoute une ligne avec un compte de test; -open TMP, ">> $tmpFile" or die $!; -print TMP '"11995";"760495646YZ";"3";"2019-11-13 15:09:08";NULL',"\n"; -$lastSize++; -close TMP; +# on ajoute une ligne avec un compte de test; +#open TMP, ">> $tmpFile" or die $!; +#print TMP '"11995";"760495646YZ";"3";"2019-11-13 15:09:08";NULL',"\n"; +#$lastSize += 53; +#close TMP; # on recupere le nombre de lignes du fichier -my $nbBoursierACharger = verifFile(); +my $nbBoursierACharger = &verifFile(); -printLog("$tmpFile ok : $nbBoursierACharger boursiers"); +&printLog("$tmpFile ok : $nbBoursierACharger boursiers"); # copie sur les portails foreach my $portail ( @PORTAIL ) { - scp($portail); - testSize($portail, $lastSize); + &scp($portail); + &testSize($portail, $lastSize); } @@ -195,12 +194,12 @@ sub verifFile { # demande le reload des données foreach my $portail ( @PORTAIL ) { - my $nbBoursierCharge = curl($portail); + my $nbBoursierCharge = &curl($portail); if ($nbBoursierCharge == $nbBoursierACharger) { - printLog("Chargement terminé sans erreur.\n"); + &printLog("Chargement terminé sans erreur.\n"); $nbOk++; } else { - printLog("ERREUR de chargement il devrait avoir $nbBoursierACharger chargement et non pas $nbBoursierCharge"); + &printLog("ERREUR de chargement il devrait avoir $nbBoursierACharger chargement et non pas $nbBoursierCharge"); } } @@ -209,14 +208,13 @@ sub verifFile { if (@listFile > $nbFtpFileToKeep) { for (my $cpt = 0 ; $cpt < @listFile - $nbFtpFileToKeep; $cpt++){ print WRITE "rm $listFile[$cpt]\n\n"; - printLog(); while () { last if /^$prompt$/; - print ; + &printLog($_); } } } } else { - printLog("ERROR : $nbOk portail chargé"); + &printLog("ERROR : $nbOk portail chargé"); } diff --git a/script/reload_liste_boursiers.pl b/script/reload_liste_boursiers.pl new file mode 100644 index 0000000..584c285 --- /dev/null +++ b/script/reload_liste_boursiers.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl + +my $curl_format = 'curl -k https://%s:8443/niveau-bourse/loaddata'; + + +my @PORTAIL= ('ara.giprecia.net', 'cacatoes.giprecia.net'); + + + + +sub curl(){ + my $addr = shift; + + my $result = -1; + + my $com = sprintf ($curl_format, $addr); + &printLog("$com"); + open COM, "$com 2> /tmp/curl.error && echo |" or die $!; + $line = ; + print $line, "\n"; + if ($line =~ /(\d+),(\d+)/){ + $result = $1; + } + close COM; + open ERROR, "/tmp/curl.error"; + while () { + print ; + } + close ERROR; + &printLog( "$result boursiers ont été chargés"); + return $result; +} + + + +# print daté +sub printLog(){ + + my @localTime = localtime time; + my $horodatage = sprintf( + '%d/%.2d/%.2d %.2d:%.2d:%.2d', + (1900 + $localTime[5]) , + $localTime[4]+1, + $localTime[3], + $localTime[2], + $localTime[1], + $localTime[0] + ); + + print $horodatage, "\t" , @_, "\n"; +} + + +my $nbOk= 0; + +# demande le reload des données +foreach my $portail ( @PORTAIL ) { + my $nbBoursierCharge = &curl($portail); +} + + +