背景
通过 SNMP 无法获取 华为 healthcheck 健康监测状态,本机建介绍PRTG脚本 通过 EXE 脚本自定义监控参数,该传感器可以接受 json 或者 xml 格式,下来来看看如何是先吧~~~
要监控的信息
环境及配置依赖
pip install paramiko
pip install textfsm
编写脚本
python 脚本
import paramiko
import time
import textfsm
import json
import sys
def connect_device(ip, link_name):
ip = ip
link_name = link_name
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(ip, 22, 'name', 'password', look_for_keys=False)
command = ssh_client.invoke_shell()
time.sleep(0.5)
command.send('screen-length 0 temporary \n')
time.sleep(1)
command.send('display healthcheck name {} \n'.format(link_name))
time.sleep(1)
command.send('display healthcheck name {} verbose \n'.format(link_name))
time.sleep(1)
command_result = command.recv(65535).decode(encoding='utf-8').lower()
ssh_client.close()
tex_temp = open(open('C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML\huawei_health_check_temp.txt', encoding='utf-8'), encoding='utf-8')
result = textfsm.TextFSM(tex_temp).ParseTextToDicts(command_result)
return result
def print_result(dev_result, data):
dev_state = dev_result[0]
link_state = {
"Channel": "{} Statu".format(dev_state['line_Name']),
"CustomUnit": "#",
"Mode": "Absolute"
}
if dev_state['All_State'] == 'up' and dev_state['line_down'] == '0':
link_state["Value"] = 0
data['prtg']['text'] = "Link State: up"
elif dev_state['All_State'] == 'up' and dev_state['line_down'] != '0':
link_state["Value"] = 1
data['prtg']['text'] = "Link State: Some lines are abnormal"
data['prtg']['Warning'] = 1
else:
link_state["Value"] = 2
data['prtg']['text'] = "Link State: down"
data['prtg']['Error'] = 1
data['prtg']['result'].append(link_state)
for one_line in dev_result:
one_link_state = {
"Channel": "{} Check Statu".format(one_line['One_Destination']),
"CustomUnit": "#",
"Mode": "Absolute",
}
if one_line['One_State'] == 'up':
one_link_state['Value'] = 0
else:
one_link_state['Value'] = 2
data['prtg']['result'].append(one_link_state)
print(json.dumps(data, sort_keys=True, indent=2))
def main():
data = {
"prtg": {
"result": [
]
}
}
try:
device_ip = sys.argv[1]
link_name = sys.argv[2]
result = connect_device(device_ip, link_name)
print_result(result, data)
except Exception as e:
data['prtg']['text'] = "Link State: {}".format(e)
print(json.dumps(data, sort_keys=True, indent=2))
if __name__ == '__main__':
main()
Python 获取到的数据
{
"prtg": {
"Warning": 1,
"result": [
{
"Channel": "telecom-china-check Statu",
"CustomUnit": "#",
"Mode": "Absolute",
"Value": 1
},
{
"Channel": "59.36.211.193 Check Statu",
"CustomUnit": "#",
"Mode": "Absolute",
"Value": 0
},
{
"Channel": "223.6.6.6 Check Statu",
"CustomUnit": "#",
"Mode": "Absolute",
"Value": 2
},
{
"Channel": "8.8.8.8 Check Statu",
"CustomUnit": "#",
"Mode": "Absolute",
"Value": 0
}
],
"text": "Link State: Some lines are abnormal"
}
}
textfsm模板
Value Filldown line_Name (\S+)
Value Filldown line_member (\w+)
Value Filldown All_State (up|down)
Value Filldown line_init (\d+)
Value Filldown line_down (\d+)
Value Filldown line_up (\d+)
Value One_State (up|down)
Value Required One_Destination (\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})
Start
^${line_Name}\s+${line_member}\s+${All_State}\s+${line_up}\s+${line_down}\s+${line_init}
^\s+state\s+:\s+${One_State}
^\s+\w+\s+\S+\s+\w+\s+\S\s+ip\S${One_Destination} -> Record
bat文件
使用本地python环境调用脚本
@echo off
"C:\Users\quzq\AppData\Local\Programs\Python\Python310\python.exe" "C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML\huawei_health_check.py" %1 %2
将文件放置PRTG探针
目录为:C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML
配置监信息
点击添加探针选高级exe脚本
配置相关参数
定义告警信息
当探针有一个线路异常时探诊状态为警告
查看线路异常通道,值显示为2的为down状态
参考链接
传感器介绍 https://www.paessler.com/manuals/prtg/exe_script_advanced_sensor 传感器字段介绍 https://www.paessler.com/manuals/prtg/custom_sensors#command_line
|