-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathhealthcheck
executable file
·29 lines (24 loc) · 1.59 KB
/
healthcheck
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
#!/bin/bash -e
if [ ! -f "$INFRA_LOTUS_HOME/.lotus/sync-complete" ]; then
exit 1
fi
ALLOWED_DELAY=${ALLOWED_DELAY:=3}
#BLOCK_TIME=$(cat /networks/${NETWORK}.json | jq .NetworkParameters.EpochDurationSeconds -r)
BLOCK_TIME=30
GENESIS_TIMESTAMP=$(timeout -k3 3 curl -s http://127.0.0.1:1234/rpc/v0 -X POST -H "Content-Type: application/json" --data '{ "jsonrpc": "2.0", "id":1, "method": "Filecoin.ChainGetGenesis", "params": [] }' | jq '.result.Blocks[0].Timestamp')
CURRENT_TIMESTAMP=$(date +"%s")
DIFFERENCE_IN_TIME=$(( $CURRENT_TIMESTAMP - $GENESIS_TIMESTAMP ))
MIN_ALLOWED_BLOCK=$(( $DIFFERENCE_IN_TIME / $BLOCK_TIME - $ALLOWED_DELAY ))
OUR_BLOCK=$(curl -s --max-time 3 localhost:1234/debug/metrics | grep ^lotus_chain_node_worker_height | awk '{print $2}')
THE_BLOCK=$(curl -s -X POST -H "Content-type: application/json" -d '{"jsonrpc":"2.0","id":1,"method":"eth_blockNumber","params":[]}' localhost:1234/rpc/v1 | awk -F '"' '{print $8}')
if [ ! -z $(echo $OUR_BLOCK | grep "e+") ]
then OUR_BLOCK=$(printf "%0.f" $OUR_BLOCK)
fi
# If local block is lower than minimum allowed block or we can't get block data - the health is bad
if [[ "$OUR_BLOCK" -ge "$MIN_ALLOWED_BLOCK" && ! -z "$OUR_BLOCK" && ! -z "$MIN_ALLOWED_BLOCK" && ! -z "$THE_BLOCK" ]] && \
# Fail readiness probe if Filecoin.EthGetLogs method request takes longer than 10 seconds
timeout 10 curl -s -X POST -H 'Content-Type: application/json' -d "{\"jsonrpc\":\"2.0\",\"method\":\"Filecoin.EthGetLogs\",\"params\":[{\"fromBlock\":\"$THE_BLOCK\",\"toBlock\":\"$THE_BLOCK\"}],\"id\":1}" http://127.0.0.1:1234/rpc/v1; then
exit 0
else
exit 1
fi