-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnode-restarter.sh
47 lines (37 loc) · 1.55 KB
/
node-restarter.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
SYNC_WINDOW=3
# 2700 sec = 45 min
MIN_UPTIME=3000
# telegram bot token
TG_TOKEN=$1
# telegram user ID
TG_CHAT_ID=$2
CONTAINER_NAME=$3
if [[ ${CONTAINER_NAME} == "" ]]
then
CONTAINER_NAME="mina_daemon_1"
fi
GREEN="\e[92m"
RED="\e[91m"
NORMAL="\e[39m"
#EXPLORER_HEIGTH=$(curl -s https://api.minaexplorer.com/summary | jq -r .blockchainLength)
STATUS_DATA=$(docker exec mina_daemon_1 mina client status --json | grep -v "Using password from")
MAX_UNVALIDATED_BLOCK=$(jq .highest_unvalidated_block_length_received <<< $STATUS_DATA)
LOCAL_HEIGHT=$(jq .blockchain_length <<< $STATUS_DATA)
UPTIME=$(jq .uptime_secs <<< $STATUS_DATA)
SYNC_STATUS=$(jq .sync_status <<< $STATUS_DATA)
echo -e "\n-------------------------"
echo -e $(date)
echo -e "Local/Explorer: ${LOCAL_HEIGHT}\\${MAX_UNVALIDATED_BLOCK}"
echo -e "Uptime: ${UPTIME} | Status: ${SYNC_STATUS}"
if [[ $(bc -l <<< "${MAX_UNVALIDATED_BLOCK} - ${LOCAL_HEIGHT}") -gt ${SYNC_WINDOW} ]] && [[ ${UPTIME} -gt ${MIN_UPTIME} ]]
then
echo -e ${RED}"ALARM! ${CONTAINER_NAME} node on ${HOSTNAME} is out of sync"${NORMAL}
MSG=$(echo -e "${CONTAINER_NAME} node on ${HOSTNAME} is out of sync\nLocal/Explorer: ${LOCAL_HEIGHT}/${MAX_UNVALIDATED_BLOCK}\nUptime: ${UPTIME} | Status: ${SYNC_STATUS}")
if [[ ${TG_TOKEN} != "" ]]
then
curl -s -H 'Content-Type: application/json' --request 'POST' -d "{\"chat_id\":\"${TG_CHAT_ID}\",\"text\":\"${MSG}\"}" "https://api.telegram.org/bot${TG_TOKEN}/sendMessage"
fi
# restart container
docker restart ${CONTAINER_NAME}
fi