.scriptComm.sh
Jump to navigation
Jump to search
#!/bin/sh
# comm entre serveurs
# ["srv1 srv2 ... srvn"] "codeFile"
# APA 20180108
# APA 20180325 server list
# APA 20180406 cache added
[ $# -eq 0 ] &&exit
# initialisation des variables
baseDir="$(realpath "$(dirname $0)"/..)"
cacheFile=/tmp/"$(basename $0)"$$
# stock code en cache avec des accès limités
cat "${@: -1}" >"${cacheFile}"
chmod 500 "${cacheFile}"
chown intrasys: "${cacheFile}"
hostList=$(hostList.sh "${@:1:$(($# -1))}")
# signature du code
nameBase="$(cat ${cacheFile} |tr -c '[:alnum:]' '_' |cut -c 1-68 |tr -s '_' |sed -r 's#_$##' )"-$(md5sum ${cacheFile} |cut -c 1-2 )
resultName="${baseDir}"/log/comm.$(echo "${hostList}" |sha512sum | cut -c 1-16)."${nameBase}".gz
# si resultat non en cache, lancer le traitement
if [ -z "$(find "${resultName}" -mmin -6 2>/dev/null )" ] ;then
# lancement du code
## Enleve les lignes en doublons par serveur. Puis ne garde que les lignes superieur aux nombre de serveurs/2.
script.sh "${hostList}" "${cacheFile}" 2>/dev/null >/dev/null
countNumber=0
for hostName in $hostList ;do
cachePath="${baseDir}"/log/old/${hostName}-"${nameBase}"-$(date '+%Y%m%d')
zcat "${cachePath}".gz 2>/dev/null \
|awk '!seen[$0]++' \
|grep . |tee "${cacheFile}".result
[ "$(head -1 "${cacheFile}".result |wc -l)" -ge 1 ] &&((countNumber++))
done \
>"${cacheFile}".result2
triggerValue=$((countNumber / 2))
awk 'NR == FNR {count[$0]++; next}; count[$0] > '"${triggerValue}" "${cacheFile}".result2 "${cacheFile}".result2 \
| awk '!seen[$0]++' \
| gzip >"${cacheFile}".result
# mise en cache
mv "${cacheFile}".result "${resultName}"
fi
# affiche le resultat
zcat "${resultName}"
# clean cache
rm -f /tmp/"$(basename $0)"$$* 2>/dev/null