Tron节点监控脚本使用说明

2024-05-24 21:20

本文主要是介绍Tron节点监控脚本使用说明,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 一、配置
  • 二、脚本编写
    • 2.1 Python脚本--监控节点是否正在同步
      • 2.1.1 pyton脚本脚本示例
      • 2.1.2 使用说明
      • 2.2.3 脚本监控内容说明
    • 2.2 Shell脚本--综合情况监控
      • 2.2.1 shell脚本示例
      • 2.2.2 使用说明
      • 2.2.3 脚本监控内容说明

最近搭建了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 ]; thenmessage="Warning: Tron node is not reachable or not responding. HTTP status code: $response"send_telegram_alert "$message"echo $messageexit 1
fi# 检查区块高度同步
node_height=$(get_block_height $NODE_URL)
public_height=$(get_block_height $PUBLIC_API_URL)
if [ -z "$node_height" ] || [ -z "$public_height" ]; thenmessage="Error: Unable to retrieve block heights. Node height: $node_height, Public API height: $public_height"send_telegram_alert "$message"echo $messageexit 1
fiif (( public_height - node_height > SYNC_THRESHOLD )); thenmessage="Warning: Tron node is out of sync. Local node height: $node_height, Public API height: $public_height"send_telegram_alert "$message"echo $message
elseecho "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) )); thenmessage="Warning: High CPU usage detected: $cpu_usage%"send_telegram_alert "$message"echo $message
fiif (( $(echo "$mem_usage > 85" | bc -l) )); thenmessage="Warning: High Memory usage detected: $mem_usage%"send_telegram_alert "$message"echo $message
fiif (( disk_usage > 85 )); thenmessage="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节点的稳定运行,并及时收到任何潜在问题的报警通知。

这篇关于Tron节点监控脚本使用说明的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/999566

相关文章

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Before和BeforeClass的区别及说明

《Before和BeforeClass的区别及说明》:本文主要介绍Before和BeforeClass的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Before和BeforeClass的区别一个简单的例子当运行这个测试类时总结Before和Befor

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(