項目中使用了中國電信系統集成公司的虛擬化平台,為通過zabbix監控,接收HyperCenter發送的告警,需要將trap消息中的漢語編碼轉譯。網絡上snmptt資料不多,官網文檔也不甚友好,通過參考 zabbix開源社區公眾號的《第三方平台告警接入、翻譯、關聯恢復》 文章成功完成了工作,特此將經驗分享給有需要的人。
以下為/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