#!/bin/bash
# file
DAT_FILE="$(pwd)/checker.data"
LOG_FILE="$(pwd)/checker.log"
LISK_DIR="$(pwd)/lisk-Linux-x86_64"
# public key and IP your node
PUBLICKEY=ff8aa63ae5eeb6cd866b615934163c1a94d25819a85b93856f1cdbcf439005c4
DATE=`date "+%F %H:%M:%S"`
NODEIP=89.40.125.40
[email protected]APIPORT=7000
exec_check() {
# get list of IPs of peers(9 ps)
[[ $(curl -s 'http://127.0.0.1:'$APIPORT'/api/peers?') =~ \"success\":([[:lower:]]+),.*\
\"ip\":\"([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)\",\"port\":[[:digit:]]+,\"state\":2.*\
\"ip\":\"([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)\",\"port\":[[:digit:]]+,\"state\":2.*\
\"ip\":\"([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)\",\"port\":[[:digit:]]+,\"state\":2.*\
\"ip\":\"([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)\",\"port\":[[:digit:]]+,\"state\":2.*\
\"ip\":\"([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)\",\"port\":[[:digit:]]+,\"state\":2.*\
\"ip\":\"([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)\",\"port\":[[:digit:]]+,\"state\":2.*\
\"ip\":\"([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)\",\"port\":[[:digit:]]+,\"state\":2.*\
\"ip\":\"([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)\",\"port\":[[:digit:]]+,\"state\":2.*\
\"ip\":\"([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)\",\"port\":[[:digit:]]+,\"state\":2.* ]]
if [ ! -z ${BASH_REMATCH[1]} ] && [ ${BASH_REMATCH[1]} == true ]; then
for IPs in 2 3 4 5 6 7 8 9 10; do
IPDELEGATES[$IPs - 1]=${BASH_REMATCH[$IPs]}
done
fi
# get local height
[[ $(curl -s 'http://127.0.0.1:'$APIPORT'/api/loader/status/sync') =~ \"success\":([[:lower:]]+),.*\"height\":([[:digit:]]+) ]]
MYHEIGHT=${BASH_REMATCH[2]}
let "MYHEIGHT+=5"
# get system param
FREEMEM=`head /proc/meminfo | grep MemFree | awk '{ print $2 }'`
CPULOAD=`ps aux | awk '{s += $3} END {print s "%"}'`
# restart client if free memory less than ~50 MB
if [ $FREEMEM -lt 50000 ]; then
echo $DATE "Restart miner! Memory is low! MemFree is" $FREEMEM >> "$LOG_FILE"
/bin/bash $LISK_DIR/lisk.sh restart
exit 0
fi
MAXHEIGHT=0
# find the biggest height in peers
for IP in ${IPDELEGATES[@]}; do
[[ $(curl -s 'http://'$IP':'$APIPORT'/api/loader/status/sync') =~ \"success\":([[:lower:]]+),.*\"height\":([[:digit:]]+) ]]
if [ ! -z ${BASH_REMATCH[1]} ] && [ ${BASH_REMATCH[1]} == true ] && [ ${BASH_REMATCH[2]} -gt $MAXHEIGHT ]; then
MAXHEIGHT=${BASH_REMATCH[2]}
MAXHEIGHTPEER=$IP
fi
done
# MAIN CODE
# check our node by peer of the best hieght
[[ $(curl -s 'http://'$MAXHEIGHTPEER':'$APIPORT'/api/delegates/get?publicKey='$PUBLICKEY) =~ \"success\":([[:lower:]]+),.*\"missedblocks\":([[:digit:]]+),.*\"rate\":([[:digit:]]+),\"productivity\":\"([[:digit:]]+\.[[:digit:]]+)\" ]]
SUCCESS=${BASH_REMATCH[1]}
MISSED=${BASH_REMATCH[2]}
RATE=${BASH_REMATCH[3]}
PRODUCTIVITY=${BASH_REMATCH[4]}
if [ -f "$DAT_FILE" ]; then
MISSEDNOW=$(sed -n '1p' "$DAT_FILE")
elif [ ! -z $MISSED]; then echo $MISSED > "$DAT_FILE"
else echo 0 > "$DAT_FILE"
fi
# check node by fork
tail -n 1000 $LISK_DIR/logs.log | grep "\"cause\":3" > /dev/null 2>&1
if [ $? = 0 ]; then
echo "Node forked! Node" $NODEIP "rebuilded! Current height is" $MAXHEIGHT "* Node height is" $MYHEIGHT "* MemFree is" $FREEMEM "* CPU load is" $CPULOAD | mail -v -s "Missed block by forging!" $EMAIL > /dev/null 2>&1
echo $DATE "Node forked! Node rebuilded! Current height is" $MAXHEIGHT "* Node height is" $MYHEIGHT "* MemFree is" $FREEMEM "* CPU load is" $CPULOAD >> "LOG_FILE"
/bin/bash $LISK_DIR/lisk.sh rebuild
fi
if [ ! -z $SUCCESS ] && [ $SUCCESS == true ]; then
if [ $MISSEDNOW -lt $MISSED ]; then
#curl -s 'http://sms.ru/sms/send?api_id=2549E81F-24F5-0990-D873-4911358B12A3&to=375295180951&text=Missed+block!+Check+node!' > /dev/null 2>&1
echo "Missed block! Check all nodes! Current height is" $MAXHEIGHT "* Node height is" $MYHEIGHT "* MemFree is" $FREEMEM "* CPU load is" $CPULOAD | mail -v -s "Missed block by forging!" $EMAIL > /dev/null 2>&1
echo $DATE "Missed block! Current height is" $MAXHEIGHT "* Node height is" $MYHEIGHT "Send SMS, email and restart node!" >> "$LOG_FILE"
echo $MISSED > "$DAT_FILE"
/bin/bash $LISK_DIR/lisk.sh restart
exit 0
fi
if [ $MYHEIGHT -lt $MAXHEIGHT ]; then
echo "Incorrect height! Check node" $NODEIP "! Current height is" $MAXHEIGHT "* MemFree is" $FREEMEM "* CPU load is" $CPULOAD | mail -v -s "Incorrect height of blockchain!" $EMAIL > /dev/null 2>&1
echo $DATE "Incorrect height! Send email! Current height is" $MAXHEIGHT "* Node height is" $MYHEIGHT "* MemFree is" $FREEMEM "* CPU load is" $CPULOAD >> "$LOG_FILE"
exit 0
fi
echo $DATE "It's OK! Current height is" $MAXHEIGHT "* MemFree is" $FREEMEM "* CPU load is" $CPULOAD >> "$LOG_FILE"
else echo $DATE "Can't get info about delegate! Current height is" $MAXHEIGHT "* MemFree is" $FREEMEM "* CPU load is" $CPULOAD >> "$LOG_FILE"
fi
}
# crontab and tail logs
autostart_cron() {
local cmd="crontab"
command -v "$cmd" &> /dev/null
if [ $? -eq 1 ]; then
echo "Failed to execute crontab."
return 1
fi
crontab=$($cmd -l 2> /dev/null | sed '/checker\.sh execute/d' 2> /dev/null)
crontab=$(cat <<-EOF
$crontab
* * * * * $(command -v "bash") $(pwd)/checker.sh execute
EOF
)
printf "$crontab\n" | $cmd - 2> /dev/null
if [ $? -eq 0 ]; then
echo "Crontab updated successfully."
return 0
else
echo "Failed to update crontab."
return 1
fi
}
tail_logs() {
if [ -f "$LOG_FILE" ]; then
tail -f "$LOG_FILE"
fi
}
install_ssmtp() {
if [ $(command -v "ssmtp") ]; then
echo "Existing ssmtp installation found."
echo ""
else
echo "Installing ssmtp..."
echo ""
apt-get -y install ssmtp > /dev/null
if [ $? != 0 ]; then
echo "Failed to install ssmtp."
exit 0
else
echo "Ssmtp installed successfully."
fi
fi
}
tune_ssmtp() {
read -p "Enter your system user name who start lisk client(example: lisk): " user
read -p "Enter your login name(example: lisk.grumlin): " login
read -p "Enter your password in smtp(mail): " pass
read -p "Enter your mail hoster(example: gmail.com): " mail
read -p "Enter your smtp server(example: smtp.gmail.com:465): " smtp
echo $user:$login\@$mail:$smtp > /etc/ssmtp/revaliases
echo "mailhub="$smtp > /etc/ssmtp/ssmtp.conf
echo "rewriteDomain="$mail >> /etc/ssmtp/ssmtp.conf
echo "hostname="$mail >> /etc/ssmtp/ssmtp.conf
echo "UseTLS=YES" >> /etc/ssmtp/ssmtp.conf
echo "AuthUser="$login >> /etc/ssmtp/ssmtp.conf
echo "AuthPass="$pass >> /etc/ssmtp/ssmtp.conf
echo "FromLineOverride=YES" >> /etc/ssmtp/ssmtp.conf
echo " *** If you need to change setting, edit /etc/ssmtp files or just start install else. ***"
}
case $1 in
"execute")
exec_check
;;
"autostart")
autostart_cron
;;
"installsmtp")
install_ssmtp
;;
"tunesmtp")
tune_ssmtp
;;
"logs")
tail_logs
;;
*)
echo "Error: Unrecognized command."
echo ""
echo "Available commands are: autostart installssmtp logs"
;;
esac