由于需要检查EOS Rpc API的健康运行情况,所以需要一个检查的脚本定期运行
监控需要发送微信消息,先参考《推荐一个简单的服务器监控,发送消息给微信的方案「Server酱」》,注册下面需要用的SCKEY
新建api_monitor.sh
,内容如下
#!/bin/bash
# Update these for your own settings
declare -A APIS=( [eos-mainnet]=https://eos-mainnet.bcskill.com [get-scatter]=https://nodes.get-scatter.com )
WE_CHAT_WEBHOOK=https://sc.ftqq.com/SCKEY(去上面的参考去申请).send
#最新区块时间`head_block_time`与本地时间存在时差,不确定具体原因,暂时比对时间为容差为24h
DELAY=60*60*24
# Do not change below here
for K in "${!APIS[@]}"
do
# Check the endpoint
JSON=$(curl -s ${APIS[$K]}/v1/chain/get_info)
# Fail if curl was unsuccessful
if [ $? -ne 0 ]; then
curl -s -X POST --data "text=\"\`$K\` API node cannot be accessed @ ${APIS[$K]}\"&desp=EOS Monitor Error" $WE_CHAT_WEBHOOK
continue
fi
# Calculate the diff
HEAD=$(echo $JSON | jq -r .head_block_time)
BLOCK=$(date --date=$HEAD +"%s")
NOW=$(date +"%s")
DIFF="$(($NOW-$BLOCK))"
# Fail if head block is older than acceptable delay
if [[ $DIFF -gt $DELAY ]]; then
curl -s -X POST --data "text=\"\`$K\` API head block time @ ${APIS[$K]}/v1/chain/get_info is lagging more than $DELAY seconds: $HEAD\"&desp=EOS Monitor Error" $WE_CHAT_WEBHOOK
fi
done
依赖
返回结果依赖jq
解析,需要先安装下
sudo apt-get install jq
修改文件权限
chmod 755 api_monitor.sh
运行
每30秒检查一次
watch -n 30 ./api_monitor.sh
等出问题时,会直接发送消息到绑定的微信
参考:https://github.com/BlockMatrixNetwork/eos-bp-failover/tree/master/external-rpc-api-monitor