本文介绍zabbix通过lykchat发送告警信息配置过程。
lykchat代码在https://github.com/lykops/lykchat/
步骤
编写脚本
1)、查看服务器端的配置文件etc/zabbix_server.conf的AlertScriptsPath变量,这就是告警脚本路径。
cat /usr/local/zabbix/etc/zabbix_server.conf | grep ^AlertScriptsPath
AlertScriptsPath=/usr/local/zabbix/scripts
2)、把告警脚本send_alter.py放到/usr/local/zabbix/scripts下,并授予执行权限,chmod a+x send_alter.py。
请看附件中告警脚本例子
登陆zabbix web界面
配置“示警媒介类型”
新增告警类型
配置告警类型
配置用户发送告警信息
用户配置
为用户配置发送告警类型
配置动作
新增动作
配置动作
默认信息和恢复信息的内容:
{
‘IP’ : ‘{HOST.IP}’ ,
‘Hostname’ : ‘{HOSTNAME}’ ,
‘Host’ : ‘{HOST.NAME}’ ,
‘Event_Date’ : ‘{EVENT.DATE} {EVENT.TIME}’ ,
‘Event_Age’ : ‘{EVENT.AGE}’ ,
‘Event_Status’ : ‘{EVENT.STATUS}’ ,
‘Trigger’ : ‘{TRIGGER.NAME}’ ,
‘Status’ : ‘{TRIGGER.STATUS}’ ,
‘Trigger_Level’ : ‘{TRIGGER.SEVERITY}’ ,
‘Event_ID’ : ‘{EVENT.ID}’ ,
‘Trigger_ID’ : ‘{TRIGGER.ID}’ ,
‘Item_Name’ : ‘{ITEM.NAME}’ ,
‘Item_Key’ : ‘{ITEM.KEY}’ ,
‘Item_Value’ : ‘{ITEM.VALUE}’ ,
‘ITEM_LASTVALUE’ : ‘{ITEM.LASTVALUE}’
}
条件
操作
附录
shell脚本
如果使用shell脚本发送告警的话,需要这些调整。要点如下:
请把配置动作中的内容改为
‘{HOST.IP}’
‘{HOSTNAME}’
‘{HOST.NAME}’
‘{EVENT.DATE} {EVENT.TIME}’
‘{EVENT.AGE}’
‘{EVENT.STATUS}’
‘{TRIGGER.NAME}’
‘{TRIGGER.STATUS}’
‘{TRIGGER.SEVERITY}’
‘{EVENT.ID}’
‘{TRIGGER.ID}’
‘{ITEM.NAME}’
‘{ITEM.KEY}’
‘{ITEM.VALUE}’
‘{ITEM.LASTVALUE}’
然后根据参数变量进行修改,可以使用$*把所有的变量输出到一个文件进行调试。
把变量拼接成输出,在通用lykchat的发送信息url。
http://127.0.0.1/sendmsg?username=zabbix&pwd=123456&friendfield=1&friend=微信号&content=内容
告警脚本例子
告警脚本代码send_alter.py,内容如下:
#! /usr/bin/python
# coding:utf-8
# -*- coding : utf-8 -*-
import sys, time, requests
SEND_MESS_ID_LIST = re.split("," , sys.argv[1])
# web页面的发送者可以同时支持多个微信号,通过逗号分割。在web页面的管理-用户-用户中配置
VAR_LIST = sys.argv[3:]
EVENT_MESSAGE = {}
for VAR in VAR_LIST :
VAR = str(VAR).replace("\r\n" , "")
DICT = eval(VAR)
EVENT_MESSAGE.update(DICT)
# 参数过来的是数组,所以需要转化为字典
IP = EVENT_MESSAGE["IP"]
HOST = EVENT_MESSAGE["Host"]
TRIGGER = EVENT_MESSAGE["Trigger"]
STATUS = EVENT_MESSAGE["Status"].lower()
TRIGGER_ID = EVENT_MESSAGE["Trigger_ID"]
ITEM_NAME = EVENT_MESSAGE["Item_Name"]
ITEM_KEY = EVENT_MESSAGE["Item_Key"]
EVENT_ID = EVENT_MESSAGE["Event_ID"]
ITEM_VALUE = EVENT_MESSAGE["Item_Value"]
EVENT_AGE = EVENT_MESSAGE["Event_Age"]
EVENT_DATETIME = EVENT_MESSAGE["Event_Date"].replace(r"." , "-")
TRIGGER_LEVEL = EVENT_MESSAGE["Trigger_Level"]
if TRIGGER_LEVEL == "Not classified" :
TRIGGER_LEVEL = "Not_classified"
if STATUS == "ok" :
STATUS_CN = "恢复"
else :
STATUS_CN = "故障"
SEND_MESSAGE_LEVEL = ["High" , "Disaster"]
# 通过短信发送的报警信息的事件级别列表
SEND_MAIL_LEVEL = ["Warning" , "Average" , "High" , "Disaster"]
# SEND_MAIL_LEVEL = ["Not_classified" , "Information" , "Warning" , "Average" , "High" , "Disaster"]
# 通过邮件发送的报警信息的事件级别列表
TRIGGER_LEVEL_LIST = {"Not_classified" : "未定义" , "Information" : "通知" , "Warning" : "警告" , "Average" : "一般严重" , "High" : "严重" , "Disaster" : "灾难"}
TRIGGER_LEVEL_CN = TRIGGER_LEVEL_LIST[TRIGGER_LEVEL]
# 警告级别的中文
MESSHEADER_QINZHUI_LIST = {"Not_classified" : "注意" , "Information" : "注意" , "Warning" : "处理" , "Average" : "尽快处理" , "High" : "立即处理" , "Disaster" : "立即处理灾难"}
MESSHEADER_QINZHUI = MESSHEADER_QINZHUI_LIST[TRIGGER_LEVEL]
# 告警信息标题前缀
TIMEARRAY = time.strptime(EVENT_DATETIME, "%Y-%m-%d %H:%M:%S")
EVENT_TIMESTAMP = int(time.mktime(TIMEARRAY))
NOW_TIMESTAMP = time.time()
NOW_TIMESTAMP = re.split("\." , str(NOW_TIMESTAMP))[0]
NOW_DATETIME = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(NOW_TIMESTAMP)))
NOW_TIME = time.strftime("%H:%M", time.localtime(int(NOW_TIMESTAMP)))
# 转化时间
EVENT_TIEM = time.strftime("%H:%M", time.localtime(EVENT_TIMESTAMP))
# 故障发送时间
TIME_INTERVAL = int(NOW_TIMESTAMP) - int(EVENT_TIMESTAMP)
# 这次故障时间和现在时间差
if IP == "127.0.0.1" :
IP_END = ""
else :
IP_END = "-" + re.split("\." , IP)[-1]
def send_lykchat(SEND_MESS_ID) :
TRIG_MESS = HOST.replace(" " , "") + r"的" + TRIGGER.replace(" " , "")
VALUE_MESS = ITEM_VALUE.replace(" " , "")
if STATUS == "ok" :
SEND_CONTEXT = TRIG_MESS + r"在" + NOW_TIME + r"恢复,故障持续" + str(TIME_INTERVAL / 60) + "分钟" + r",值为" + VALUE_MESS
else :
SEND_CONTEXT = MESSHEADER_QINZHUI + ":" + TRIG_MESS + r"在" + EVENT_TIEM + r"发生" + TRIGGER_LEVEL_CN + r"故障,已持续" + str(TIME_INTERVAL / 60) + "分钟" + r"值为" + VALUE_MESS
SEND_CONTEXT = SEND_CONTEXT + "\nIP地址 : " + IP + "\n"
SEND_CONTEXT = SEND_CONTEXT + r"主机名 : " + HOST + "\n"
SEND_CONTEXT = SEND_CONTEXT + r"故障级别 : " + TRIGGER_LEVEL_CN + "\n"
SEND_CONTEXT = SEND_CONTEXT + r"发生时间 : " + EVENT_DATETIME + "\n"
SEND_CONTEXT = SEND_CONTEXT + r"持续时长 : " + str(TIME_INTERVAL / 86400) + "天" + str((TIME_INTERVAL % 86400) / 3600) + "小时" + str((TIME_INTERVAL % 3600) / 60) + "分钟" + str(TIME_INTERVAL % 60) + "秒\n"
SEND_CONTEXT = SEND_CONTEXT + r"目前状态 : " + STATUS_CN + "\n"
SEND_CONTEXT = SEND_CONTEXT + r"触发器名 : " + TRIGGER + "\n"
SEND_CONTEXT = SEND_CONTEXT + r"当前值 : " + ITEM_VALUE + "\n"
SEND_CONTEXT = SEND_CONTEXT + r"发送时间: " + NOW_DATETIME + "\n\n"
# 发送邮件正文
try :
url = 'http://127.0.0.1/sendmsg?username=zabbix&pwd=123456&friendfield=1&friend=' + str(SEND_MESS_ID) + '&content=' + str(SEND_CONTEXT)
requests.get(url)
except :
pass
for SEND_MESS_ID in SEND_MESS_ID_LIST :
send_lykchat(SEND_MESS_ID)