天天看點

zabbix5.0告警腳本

zabbix5.0告警腳本

作者:藍眼淚

環境準備

yum install 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=[email protected]
set smtp=smtp.qiye.aliyun.com
set smtp-auth-user=[email protected]
set smtp-auth-password=xxxxxx
set smtp-auth=login
           
zabbix5.0告警腳本

測試發送郵件

echo "wenjianliang is testing zabbix-server"| mailx -s "zabbix-server"  [email protected]
           

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
           
zabbix5.0告警腳本
zabbix5.0告警腳本
zabbix5.0告警腳本

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
           

測試腳本

zabbix5.0告警腳本
zabbix5.0告警腳本
zabbix5.0告警腳本