duniter-scripts/duniter-checkBlock.sh

88 lines
2.0 KiB
Bash
Raw Permalink Normal View History

2020-06-24 21:11:49 +02:00
#!/bin/bash
2020-11-10 23:47:27 +01:00
## MY VARS ###
myEmail="poka@p2p.legal"
###
MY_PATH="`dirname \"$0\"`"
MY_PATH="`( cd \"$MY_PATH\" && pwd )`"
refName="g1.presles.fr"
2020-06-24 21:11:49 +02:00
date=$(date '+%d-%m-%y à %H:%M')
echo "############################"
echo "$date"
lock=0
2020-11-10 23:47:27 +01:00
lockSync="$MY_PATH/lock-sync"
2020-06-24 21:11:49 +02:00
[[ -n $lockSync ]] && touch $lockSync
PID=$(ps auxf | grep "duniter" | grep -v -E "color=auto|grep|restart" | awk '{ print $2 }')
PID=($(echo "$PID" | tr '\n' ' '))
2020-11-10 23:47:27 +01:00
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
}
2020-11-11 00:13:26 +01:00
myBMA=$(netstat -pnl 2>/dev/null | grep "duniter" | grep ":45000" | awk '{ print $4 }')
myNode=$(curl -s http://$myBMA/blockchain/current | jq '.number')
2020-11-11 00:13:26 +01:00
refNode=$(curl -s https://$refName/blockchain/current | jq '.number')
2020-11-10 23:50:49 +01:00
2020-11-11 00:13:26 +01:00
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
2020-06-24 21:11:49 +02:00
fi
[[ -e $lockSync ]] && lock=$(cat $lockSync)
2020-11-10 23:47:27 +01:00
if [[ -z $myNode ]]; then
2020-06-24 21:11:49 +02:00
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
2020-11-10 23:47:27 +01:00
diffNode=$(($refNode - $myNode))
2020-06-24 21:11:49 +02:00
fi
checkg1() {
2020-11-10 23:47:27 +01:00
echo "Mon noeud est au block $myNode tandi que le noeud $refName est à $refNode"
2020-06-24 21:11:49 +02:00
2020-11-10 23:47:27 +01:00
if [[ $myNode < $refNode ]]; then
echo "Mon noeud est à la bourre, il a $diffNode blocks de retard ..."
else
echo "Tout est bon!"
fi
2020-06-24 21:11:49 +02:00
}
checkg1
2020-11-10 23:47:27 +01:00
if [ $diffNode -gt 5 ]; then
[[ $1 == "auto" ]] && sendalerte "resynchronisé"
2020-11-11 00:13:26 +01:00
chmod u+x $MY_PATH/duniter-resync.sh
$MY_PATH/duniter-resync.sh > /dev/null 2>&1
2020-06-24 21:11:49 +02:00
fi
exit 0