zabbix5.0告警脚本
作者:蓝眼泪
环境准备
yum install -y openssl-devel
wget https://pypi.python.org/packages/c3/38/d95ddb6cc8558930600be088e174a2152261a1e0708a18bf91b5b8c90b22/requests-2.18.3.tar.gz
tar zxvf requests-2.18.3.tar.gz
cd requests-2.18.3/
python setup.py build
python setup.py install
服务器:{HOST.NAME} 发生:{TRIGGER.NAME}故障!dingding
{
告警主机:{HOST.NAME}
告警地址:{HOST.IP}
监控项目:{ITEM.NAME}
监控取值:{ITEM.LASTVALUE}
告警等级:{TRIGGER.SEVERITY}
当前状态:{TRIGGER.STATUS}
告警信息:{TRIGGER.NAME}
告警时间:{EVENT.DATE} {EVENT.TIME}
事件ID:{EVENT.ID}
}
1 邮件告警
vim /etc/mail.rc
在末尾行处添加如下内容
set bsdcompat
set from=wenjl@gpelec.cn
set smtp=smtp.qiye.aliyun.com
set smtp-auth-user=wenjl@gpelec.cn
set smtp-auth-password=xxxxxx
set smtp-auth=login
测试发送邮件
echo "wenjianliang is testing zabbix-server"| mailx -s "zabbix-server" 136841202@qq.com
2.邮件脚本告警
时间同步
timedatectl
timedatectl list-timezones
timedatectl set-timezone Asia/Shanghai
脚本内容
#!/bin/bash
#export UTF-8
FILE=/tmp/mail.txt
echo "$3" > $FILE
dos2unix -k $FILE
/bin/mail -s "$2" $1 < $FILE
./emailsend001.sh 136841202@qq.com "zabbix-server" "test"
3.企业微信脚本告警
cd /usr/lib/zabbix/alertscripts/
vim weixin.py
corpid='wwc77111ef7c24d3c3' #企业ID
appsecret='xU3AyHSOBAd9RgqHDtWQTF2jruQeOC6VMaS6hGvS_cw' #报警机器人密码
agentid=1000007 #报警机器人ID
脚本内容
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import requests
import sys
import os
import json
import logging
logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s',
datefmt = '%a, %d %b %Y %H:%M:%S',
filename = os.path.join('/tmp','weixin.log'),
filemode = 'a')
corpid='wwc77111ef7c24d3c3'
appsecret='xU3AyHSOBAd9RgqHDtWQTF2jruQeOC6VMaS6hGvS_cw'
agentid=1000007
#获取accesstoken
token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
req=requests.get(token_url)
accesstoken=req.json()['access_token']
#发送消息
msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
touser=sys.argv[1]
subject=sys.argv[2]
toparty='3|4|5|6'
message=sys.argv[2] + "\n\n" +sys.argv[3]
params={
"touser": touser,
"toparty": toparty,
"msgtype": "text",
"agentid": agentid,
"text": {
"content": message
},
"safe":0
}
req=requests.post(msgsend_url, data=json.dumps(params))
logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message)
cd /usr/lib/zabbix/alertscripts/
chmod 777 weixin.py
dos2unix -k weixin.py #格式转换
touch /tmp/weixin.log
chown zabbix:zabbix /tmp/weixin.log #赋权weixin.log
./weixin.py wenjianli136841202 test 123456 #测试发送微信
4 钉钉机器人脚本告警
cd /usr/lib/zabbix/alertscripts
cat dingding.py
脚本内容
#!/usr/bin/env python
#coding:utf-8
#zabbix钉钉报警
import requests,json,sys,os,datetime
webhook="https://oapi.dingtalk.com/robot/send?access_token=3f8dc8b8244b5b3b42297d1b3e16cc52c76d2b61be3786ee804b90e9f5d04a1e"
#说明:这里改为自己创建的机器人的webhook的值
user=sys.argv[1]
text=sys.argv[3]
data={
"msgtype": "text",
"text": {
"content": text
},
"at": {
"atMobiles": [
user
],
"isAtAll": False
}
}
headers = {'Content-Type': 'application/json'}
x=requests.post(url=webhook,data=json.dumps(data),headers=headers)
if os.path.exists("/tmp/dingding.log"):
f=open("/tmp/dingding.log","a+")
else:
f=open("/tmp/dingding.log","w+")
f.write("\n"+"--"*30)
if x.json()["errcode"] == 0:
f.write("\n"+str(datetime.datetime.now())+" "+str(user)+" "+"发送成功"+"\n"+str(text))
f.close()
else:
f.write("\n"+str(datetime.datetime.now()) + " " + str(user) + " " + "发送失败" + "\n" + str(text))
f.close()
赋予脚本执行权限
chmod +x dingding.py
创建上面脚本中的日志路径
touch /tmp/dingding.log
chown zabbix.zabbix /tmp/dingding.log
测试脚本
./dingding.py test 13215029093 "主机123456"