duniter-scripts/duniter-checkBlock.sh

88 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
## MY VARS ###
myEmail="poka@p2p.legal"
###
MY_PATH="`dirname \"$0\"`"
MY_PATH="`( cd \"$MY_PATH\" && pwd )`"
refName="g1.presles.fr"
date=$(date '+%d-%m-%y à %H:%M')
echo "############################"
echo "$date"
lock=0
lockSync="$MY_PATH/lock-sync"
[[ -n $lockSync ]] && touch $lockSync
PID=$(ps auxf | grep "duniter" | grep -v -E "color=auto|grep|restart" | awk '{ print $2 }')
PID=($(echo "$PID" | tr '\n' ' '))
sendalerte() {
actionAlerte="$1"
date=$(date +"%Hh%M")
mutt -e "set from='duniter-g1@p2p.legal'" -e "set content_type=text/html" -e "set charset=UTF-8" -s "Alerte noeud ğ1 resync à $date !" $myEmail <<MAIL_END
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<body style="color:#1C1C1C; margin:2% 2%;">
<center><h3>Mon noeud ğ1 a été $actionAlerte</h3></center>
<br>
Par précaution, ce scrip a été locké $lock fois.
</body>
</html>
MAIL_END
}
myBMA=$(netstat -pnl 2>/dev/null | grep "duniter" | grep ":45000" | awk '{ print $4 }')
myNode=$(curl -s http://$myBMA/blockchain/current | jq '.number')
refNode=$(curl -s https://$refName/blockchain/current | jq '.number')
if [[ -z "$PID" || -z "$myNode" || -z "$myBMA" ]]; then
chmod u+x $MY_PATH/duniter-restart.sh
$MY_PATH/duniter-restart.sh
sendalerte "redémarré"
exit 0
fi
[[ -e $lockSync ]] && lock=$(cat $lockSync)
if [[ -z $myNode ]]; then
if [[ $lock > 3 ]]; then
rm $lockSync
touch $lockSync
diffNode=6
else
((lock++))
echo $lock > $lockSync
diffNode=0
fi
else
[[ $(cat $lockSync) != 0 ]] && echo 0 > $lockSync
diffNode=$(($refNode - $myNode))
fi
checkg1() {
echo "Mon noeud est au block $myNode tandi que le noeud $refName est à $refNode"
if [[ $myNode < $refNode ]]; then
echo "Mon noeud est à la bourre, il a $diffNode blocks de retard ..."
else
echo "Tout est bon!"
fi
}
checkg1
if [ $diffNode -gt 5 ]; then
[[ $1 == "auto" ]] && sendalerte "resynchronisé"
chmod u+x $MY_PATH/duniter-resync.sh
$MY_PATH/duniter-resync.sh > /dev/null 2>&1
fi
exit 0