Tron节点监控脚本使用说明


最近搭建了TRON节点,为了防止节点在生产环境使用过程中,出现问题,所以做了一系列的监控措施。

本说明文档介绍了如何使用Shell脚本和Python脚本来监控Tron节点的状态,并在节点不可用或不同步时通过Telegram发送报警消息。此外,脚本还监控系统资源(CPU、内存和磁盘)的使用情况,并在检测到高使用率时发送报警消息。

一、配置

无论是Shell脚本还是Python脚本,首先需要进行一些配置:

  • 本地节点URL:本地Tron节点的API URL。
  • 公共API URL:公共Tron节点的API URL,用于比较区块高度。
  • Telegram Token:Telegram Bot的API令牌。
  • Chat ID:接收报警消息的Telegram聊天ID。
  • 同步阈值:允许的最大区块高度差,超过该值则认为节点不同步。

二、脚本编写

2.1 Python脚本–监控节点是否正在同步

2.1.1 pyton脚本脚本示例

import requests

# 配置
NODE_URL = "http://localhost:8090/wallet/getnowblock"
PUBLIC_API_URL = "https://api.trongrid.io/wallet/getnowblock"
TELEGRAM_TOKEN = "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
CHAT_ID = "your_chat_id"
SYNC_THRESHOLD = 5  # 允许的最大区块高度差

# 获取区块高度
def get_block_height(url):
    try:
        response = requests.post(url)
        response.raise_for_status()
        data = response.json()
        return data['block_header']['raw_data']['number']
    except Exception as e:
        print(f"Error fetching data from {url}: {e}")
        return None

# 发送报警消息到Telegram
def send_telegram_alert(message):
    url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
    payload = {
        'chat_id': CHAT_ID,
        'text': message
    }
    try:
        response = requests.post(url, json=payload)
        response.raise_for_status()
        print("Alert sent successfully")
    except requests.exceptions.RequestException as e:
        print(f"Error sending alert: {e}")

# 检查本地节点的区块高度
node_height = get_block_height(NODE_URL)
if node_height is None:
    message = "Error: Unable to retrieve block height from local Tron node."
    send_telegram_alert(message)
    print(message)
    exit(1)

# 检查官方节点的区块高度
public_api_height = get_block_height(PUBLIC_API_URL)
if public_api_height is None:
    message = "Error: Unable to retrieve block height from TronGrid API."
    send_telegram_alert(message)
    print(message)
    exit(1)

# 比较区块高度
if (public_api_height - node_height) > SYNC_THRESHOLD:
    message = (f"Warning: Tron node is out of sync.\n"
               f"Local node height: {node_height}\n"
               f"Public API height: {public_api_height}")
    send_telegram_alert(message)
    print(message)
else:
    print(f"Tron node is in sync. Local node height: {node_height}, Public API height: {public_api_height}")

2.1.2 使用说明

  1. 安装依赖:

确保已安装requests库,可以使用以下命令安装:

pip install requests

  1. 保存脚本:
    将脚本保存为check_tron_node.py。

  2. 运行脚本:
    手动运行脚本测试脚本是否可用

python3 check_tron_node.py
  1. 设置定时任务:
    使用crontab设置定时任务,每5分钟检查一次:
crontab -e
*/5 * * * * /usr/bin/python /data/scripts/check_tron_node.py

2.2.3 脚本监控内容说明

  1. 节点可用性

使用requests库发送POST请求到本地节点,如果请求失败,则发送报警消息。

  1. 区块高度同步
    获取本地节点和公共节点的区块高度,比较高度差是否超过阈值。如果超过阈值,则发送报警消息。

2.2 Shell脚本–综合情况监控

2.2.1 shell脚本示例

#!/bin/bash

# 配置部分
NODE_URL="http://localhost:8090/wallet/getnowblock"
PUBLIC_API_URL="https://api.trongrid.io/wallet/getnowblock"
TELEGRAM_TOKEN="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
CHAT_ID="your_chat_id"
SYNC_THRESHOLD=5  # 允许的最大区块高度差

# 获取区块高度
get_block_height() {
    curl -s -X POST $1 | jq -r '.block_header.raw_data.number'
}

# 发送报警消息到Telegram
send_telegram_alert() {
    curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage" -d chat_id=$CHAT_ID -d text="$1"
}

# 检测节点可用性
response=$(curl -s -o /dev/null -w "%{http_code}" -X POST $NODE_URL)
if [ "$response" -ne 200 ]; then
    message="Warning: Tron node is not reachable or not responding. HTTP status code: $response"
    send_telegram_alert "$message"
    echo $message
    exit 1
fi

# 检查区块高度同步
node_height=$(get_block_height $NODE_URL)
public_height=$(get_block_height $PUBLIC_API_URL)
if [ -z "$node_height" ] || [ -z "$public_height" ]; then
    message="Error: Unable to retrieve block heights. Node height: $node_height, Public API height: $public_height"
    send_telegram_alert "$message"
    echo $message
    exit 1
fi

if (( public_height - node_height > SYNC_THRESHOLD )); then
    message="Warning: Tron node is out of sync. Local node height: $node_height, Public API height: $public_height"
    send_telegram_alert "$message"
    echo $message
else
    echo "Tron node is in sync. Local node height: $node_height, Public API height: $public_height"
fi

# 监控系统资源使用情况
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
mem_usage=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
disk_usage=$(df -h | grep '/$' | awk '{print $5}' | sed 's/%//g')

if (( $(echo "$cpu_usage > 85" | bc -l) )); then
    message="Warning: High CPU usage detected: $cpu_usage%"
    send_telegram_alert "$message"
    echo $message
fi

if (( $(echo "$mem_usage > 85" | bc -l) )); then
    message="Warning: High Memory usage detected: $mem_usage%"
    send_telegram_alert "$message"
    echo $message
fi

if (( disk_usage > 85 )); then
    message="Warning: High Disk usage detected: $disk_usage%"
    send_telegram_alert "$message"
    echo $message
fi

2.2.2 使用说明

  1. 安装依赖:

需要jq工具来解析JSON数据,可以使用以下命令安装:

apt-get install jq
  1. 保存脚本:

将脚本保存为check_tron_node.sh,并赋予执行权限:

chmod +x check_tron_node.sh
  1. 运行脚本:

手动运行脚本,检测shell脚本是否可用

./check_tron_node.sh
  1. 设置定时任务:

使用crontab设置定时任务,每5分钟检查一次:

crontab -e
*/5 * * * * /opt/scripts/check_tron_node.sh

2.2.3 脚本监控内容说明

  1. 节点可用性
    使用curl检查本地节点的HTTP响应状态码,如果状态码不是200,则发送报警消息。

  2. 区块高度同步
    获取本地节点和公共节点的区块高度,比较高度差是否超过阈值。如果超过阈值,则发送报警消息。

  3. 系统资源使用情况(仅Shell脚本)
    CPU使用率:通过top命令获取当前CPU使用率,如果使用率超过85%,则发送报警消息。
    内存使用率:通过free命令获取当前内存使用率,如果使用率超过85%,则发送报警消息。
    磁盘使用率:通过df命令获取当前磁盘使用率,如果使用率超过85%,则发送报警消息。
    通过这些监控内容,您可以确保Tron节点的稳定运行,并及时收到任何潜在问题的报警通知。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杰哥的技术杂货铺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值