snmptt解析中文trap消息
作者:互联网
项目中使用了中国电信系统集成公司的虚拟化平台,为通过zabbix监控,接收HyperCenter发送的告警,需要将trap消息中的汉语编码转译。网络上snmptt资料不多,官网文档也不甚友好,通过参考 https://mp.weixin.qq.com/s/0AX_qG_MVYeMWAzylQEZUQ 文章成功完成了工作,特此将经验分享给有需要的人。
以下为/etc/snmp/snmptt.conf文件中的兜底规则,将所有内容记录下来。
EVENT general .* "Default" Normal FORMAT ZBXTRAP $aA $ar $+*
一条/var/log/snmptt.log文件中记录如下(例)
14:19:03 2022/03/31 SNMPv2-SMI::enterprises.49625.10.100.8.1.1 Normal "Default" 10.110.3.14 - ZBXTRAP 10.110.3.14 10.110.3.14 enterprises.49625.10.100.8.1.1.1:1509389294969348096 enterprises.49625.10.100.8.1.1.2:24589 enterprises.49625.10.100.8.1.1.3:0 enterprises.49625.10.100.8.1.1.4:1648701421000 enterprises.49625.10.100.8.1.1.5:4e36e0b6-b63d-4955-97ab-a769d97fb8d4 enterprises.49625.10.100.8.1.1.6:4e36e0b6-b63d-4955-97ab-a769d97fb8d4 enterprises.49625.10.100.8.1.1.7:v_vm enterprises.49625.10.100.8.1.1.8:alert-collector enterprises.49625.10.100.8.1.1.9:3 enterprises.49625.10.100.8.1.1.10: enterprises.49625.10.100.8.1.1.11: enterprises.49625.10.100.8.1.1.12: enterprises.49625.10.100.8.1.1.13: enterprises.49625.10.100.8.1.1.14:01010201 enterprises.49625.10.100.8.1.1.15:E8 99 9A E6 8B 9F E6 9C BA 20 34 65 33 36 65 30 62 36 2D 62 36 33 64 2D 34 39 35 35 2D 39 37 61 62 2D 61 37 36 39 64 39 37 66 62 38 64 34 20 E5 86 85 E5 AD 98 E4 BD BF E7 94 A8 E7 8E 87 E9 98 88 E5 80 BC E5 91 8A E8 AD A6 enterprises.49625.10.100.8.1.1.16:E5 86 85 E5 AD 98 E4 BD BF E7 94 A8 E7 8E 87 3A E5 B7 B2 E8 BE BE E6 88 96 E8 B6 85 E8 BF 87 E9 99 90 E5 AE 9A E9 98 88 E5 80 BC 37 30 2C E5 BD 93 E5 89 8D E5 80 BC 37 39 2E 39 31 enterprises.49625.10.100.8.1.1.17: enterprises.49625.10.100.8.1.1.18: enterprises.49625.10.100.8.1.1.19:d815ea322799765943e592369fa17e76 enterprises.49625.10.100.8.1.1.20:613ae843-b7e5-4b75-8cf9-d6356e497059
可以看出某些字段形似乱码,其实是16进制utf-8编码,需要通过snmtt的EXEC配置脚本进行解析。我们将最关键的第15个字段alarmTitle和第16个字段alarmCause进行解析:
EVENT HyperCenter .1.3.6.1.4.1.49625.10.100.8.1.* "CSTI HyperCenter" Normal EXEC /etc/snmp/HyperCenterTrapTranslate.py $aA "$15" "$16" SDESC CSTi Hyper Center Trap alarm EDESC
特别要注意的是,由于16进制编码字符之间有空格,必须要用引号""包起来,否则脚本会误以为是入参结束而解析失败。
调用的/etc/snmp/HyperCenterTrapTranslate.py脚本代码如下:
#!/usr/bin/python # -*- coding:utf-8 -*- import sys import time import urllib import re def hex_to_chinese(hex_str): chn_msg = "null" if len(hex_str) > 0: try: tmp = "%" + re.sub("\s+","%", hex_str.strip()) chn_msg = urllib.unquote(tmp) except : chn_msg = 'ZBXTRAP can not translate this message' return chn_msg logtime = time.strftime('%H:%M:%S %Y/%m/%d', time.localtime()) snmptrapfile = "/var/log/snmptt/snmptt.log" agentIP = sys.argv[1].strip() alarmTitle = hex_to_chinese(sys.argv[2].strip()).replace("\n", " ") alarmCause = hex_to_chinese(sys.argv[3].strip()).replace("\n", " ") zbxtrapmessage = logtime + " ZBXTRAP " + agentIP + " alarmTitle:" + alarmTitle + " alarmCause:" + alarmCause with open(snmptrapfile, 'a+') as f: f.write(zbxtrapmessage) f.write("\n") f.close()
如此在zabbix界面可以得到如下消息(例)
22:47:33 2022/04/02 alarmTitle:虚拟机 9978069f-c316-44d4-ac07-cfe6fcee1897 内存使用率阈值告警 alarmCause:内存使用率:已达或超过限定阈值70,当前值77.12
标签:8.1,10.100,39,enterprises.49625,trap,snmptt,E5,解析 来源: https://www.cnblogs.com/virtualzzf/p/16094603.html