Tron节点监控脚本使用说明

最近搭建了TRON节点,为了防止节点在生产环境使用过程中,出现问题,所以做了一系列的监控措施。本说明文档介绍了如何使用Shell脚本和Python脚本来监控Tron节点的状态,并在节点不可用或不同步时通过Telegram发送报警消息。此外,脚本还监控系统资源(CPU、内存和磁盘)的使用情况,并在检

最近搭建了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 chec...

剩余50%的内容订阅专栏后可查看

  • 原创
  • 学分: 0
  • 标签:
点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。
该文章收录于 Web3
0 订阅 12 篇文章

0 条评论

请先 登录 后评论
杰哥的技术杂货铺
杰哥的技术杂货铺
0x6e60...2aa2
添加微信:web3coding ,备注:【登链社区读者】即可加入读者交流群