Created
July 18, 2020 11:34
-
-
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
This file contains 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 | |
# 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