要配置Surge以监控和异常处理,您可以按照以下步骤操作:
创建配置文件
在project/或root/目录下创建一个surge.config.json文件:
{
"enable_surge": true,
"exceptions": {
"端口卡异常": {
"type": "ping",
"threshold": 1,
"window": 1
},
"CPU供应异常": {
"type": "cpu",
"threshold": 8,
"window": 5
},
"磁盘异常": {
"type": "drw",
"threshold": 5,
"window": 24
}
},
"surrogate": {
"enable": true,
"enable_threshold": 1,
"disable_threshold": 5,
"window_last": 3,
"window_last_24": 24
}
}
在 script.py 中配置
在 script.py 中导入Surge:
import surge surge.surging().enable()
处理异常
在 script.py 中调用Surge处理异常:
try:
# 运行代码
result = code()
except surge.SurgingException as e:
print(f"异常:{e}")
if e.type in surge EXCEPTIONS:
# 处理异常
surrogate = surge.Surge()
surrogate.handle(e.type, e, result)
设置阈值和窗口策略
设置阈值和窗口策略:
surge.surging().enable_threshold = 1 # CPU供应阈值 surge.surging().disable_threshold = 5 # CPU供应阈值降低 surge.surging().window_last = 3 # 窗口策略设置为3秒
处理未监控的资源
手动添加处理未监控资源的异常:
file = '交换机磁盘读写速度.log'
with open(file, 'r') as f:
lines = [line.strip() for line in f.readlines()]
for line in lines:
if line.startswith('交换机磁盘读写速度') and float(line.split()[2]) < 5:
surrogate = surge.Surge()
surrogate.handle('磁盘异常', line, result)
设置日志级别
设置 surrogate 的日志级别:
surge.surging().log = 'surging.log' surge.surging().level = 1
配置 surrogate 的阈值
设置 surrogate 的阈值:
surge.surging().enable_threshold = 1 surge.surging().disable_threshold = 5
确保 surrogate 的可用性和日志设置
确保 surrogate 被启用,并配置其日志级别,以获取详细监控信息。
测试
测试配置后,运行 script.py,确保异常被及时处理,没有影响系统性能。
通过以上步骤,您可以配置Surge来监控系统资源,并根据异常情况处理,确保异常发生时及时发现和处理。








京公网安备11000000000001号
京ICP备11000001号