Last active
November 14, 2019 10:30
-
-
Save AlexanderC/c4f2a8e08c30b40b3117fa14a7e8c8f9 to your computer and use it in GitHub Desktop.
Ethereum/geth synchronization check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Using crontab command: | |
# */10 * * * * $HOME/bc_integrity.sh ropsten 30311 200 'http://slackhook' >> $HOME/bc_integrity.log 2>&1 | |
# You might want to cleanup log file, use crontab command: | |
# 0 8 * * * echo "$(tail -10000 $HOME/bc_integrity.log)" > $HOME/bc_integrity.log | |
network=$1 # ethereum network in letters (mainnet, ropsten, rinkeby etc.) | |
port=$2 # local geth port | |
threshold=$3 # number of blocks behind | |
slackHook=$4 # slack hook url | |
curBlock=$(geth --exec "eth.blockNumber" attach http://localhost:${port}) | |
if [ $? -ne 0 ]; then | |
curBlock=0 | |
fi | |
# Get latest block number from etherscan | |
etherscanDomain="api.etherscan.io" | |
if [ "$network" != "mainnet" ]; then | |
etherscanDomain="api-$network.etherscan.io" | |
fi | |
echo "> Checking blockchain state on ${network} at $(date)" | |
ethBlock=$(( $(curl -s -X GET "https://${etherscanDomain}/api?module=proxy&action=eth_blockNumber" | grep -Eo '"result":.*?[^\\]"' | cut -d \: -f 2 | cut -d \" -f 2) )); | |
echo "> Latest block number from Etherscan: $ethBlock" | |
echo "> Latest block number from localhost:${port} : $curBlock" | |
# Check if node is out of sync. | |
# Will send notification when current block number is <threshold> blocks behind latest block on etherscan. | |
if [[ ( $(( ${ethBlock} - ${curBlock} )) -ge $threshold ) ]]; then | |
echo "> Blockchain out of sync or stoped working!" | |
if [[ -n $slackHook ]]; then | |
echo "> Notify via Slack..." | |
curl -s -X POST -H 'Content-type: application/json' --data "{\"text\": \"Our blockchain is out of sync w/ $(( ${ethBlock} - ${curBlock} )) blocks on ${network} on ${HOSTNAME}.\"}" "$slackHook" | |
fi | |
else | |
echo "> Blockchain is fine..." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment