Skip to content

Instantly share code, notes, and snippets.

@d10r
Created July 18, 2020 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d10r/28054275555637429aace117a2979957 to your computer and use it in GitHub Desktop.
Save d10r/28054275555637429aace117a2979957 to your computer and use it in GitHub Desktop.
minimal script for stateless monitoring/alerting (stall detection) for an Ethereum/ARTIS node via its RPC interface
#!/bin/bash
# Statelessly checks if the given chain (identified by its rpc) is running.
# Definition of running: the most recent block was authored less than the given number of seconds ago
#
# argument $1: URL to the RPC node to query
# argument $2: threshold for how old - in seconds - the most recent block may be. If older, the chain is considered as not running
# argument $3: space separated list of recipients the Email alert should go to
#
# Note that this script is not designed to give any feedback about what's wrong in case it fails. It just sounds an alarm if it doesn't run through as expected.
#
# Script dependencies: curl, mail (e.g. from mailutils package), python and jq
set -e
set -u
rpc=$1
maxBlockAge=$2
alertRecipients=$3
errMsg="unexpected error"
# This is called when the script exits, no matter if due to regular exit or some error case
function alertAndExit {
if [[ $errMsg != "" ]]; then
echo "something went wrong for $rpc: $errMsg" | mail -s "$rpc stalling" $alertRecipients
fi
}
trap alertAndExit EXIT
# get block height (last block)
heightHex=$(curl --silent --data '{"method":"eth_blockNumber","params":[],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST $rpc | jq ".result")
# get timestamp of that block. Note that the variable $height has quotes already included
blockTSHex=$(curl --silent --data '{"method":"eth_getBlockByNumber","params":['$heightHex', false],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST $rpc | jq ".result.timestamp")
blockTS=$(python <<EOF
print(int($blockTSHex, 16))
EOF
)
# check if that timestamp is more than the given threshold in the past
now=$(date +%s)
blockAge=$(($now-blockTS))
if (( $blockAge > $maxBlockAge )); then
errMsg="most recent block too old - was authored $blockAge seconds ago"
exit 1
fi
errMsg=""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment