Media types --> Email --> Media type --> Update(2)定義收..."/>
您好,登錄后才能下訂單哦!
一、配置郵件告警、微信告警和釘釘告警:
1、配置郵件告警:
(1)定義發(fā)件人:
Administration --> Media types --> Email --> Media type --> Update
(2)定義收件人:
Administration --> Users --> Admin --> Media --> Add --> Update
(3)定義動作:
Configuration --> Actions --> Create action --> Add
備注:自定義告警內(nèi)容
https://www.zabbix.com/documentation/4.0/zh/manual/appendix/macros/supported_by_location
2、配置微信告警:
(1)注冊企業(yè)微信:https://work.weixin.qq.com/
(2)微信掃碼登錄:
我的企業(yè) --> 微工作臺 --> 邀請關(guān)注(使用微信掃描二維碼直接關(guān)注)
應(yīng)用管理 --> 應(yīng)用 --> 自建 --> 創(chuàng)建Zabbix微信告警應(yīng)用
我的企業(yè) --> 企業(yè)信息 --> 企業(yè)ID
通訊錄:
(3)編寫微信告警腳本:
# grep ^AlertScriptsPath /etc/zabbix/zabbix_server.conf
# cd /usr/lib/zabbix/alertscripts
# vim wechat.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#comment: Zabbix微信告警腳本
?
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','wechat.log'),filemode='a')
?
corpid='XXXXXXXX'
appsecret='XXXXXXXX'
agentid='XXXXXXXX'
?
#獲取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']
?
#發(fā)送消息
msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
?
touser=sys.argv[1]
subject=sys.argv[2]
message=sys.argv[2] + "\n\n" +sys.argv[3]
?
params={
"touser": touser,
"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)
# chmod +x wechat.py
備注:上述腳本中的XXXXXXXX需要按照實際情況替換
(4)執(zhí)行wechat.py腳本前的準備工作:
a、配置epel源:# yum -y install epel-release
b、安裝python2-pip軟件包:
# python --version
# yum -y install python2-pip
c、修改pip源為阿里云鏡像源:
# mkdir -pv ~/.pip
# vim ~/.pip/pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
d、升級pip軟件包:
# pip -V
# pip install --upgrade pip
# pip -V
e、安裝Python的requests模塊:# pip install requests
f、創(chuàng)建日志文件,并修改其屬主屬組:
# touch /tmp/wechat.log
# chown zabbix.zabbix /tmp/wechat.log
(5)測試wechat.py腳本:
# /usr/lib/zabbix/alertscripts/wechat.py 'XXXX' '主題:test' '內(nèi)容:wechat alert'
(6)媒介中新增微信告警:
Administration --> Media types --> Create media type --> Media type --> Add
需要3個參數(shù):{ALERT.SENDTO}、{ALERT.SUBJECT}、{ALERT.MESSAGE}
(7)對應(yīng)用戶中設(shè)置報警媒介:
Administration --> Users --> Admin --> Media --> Add --> Update
(8)定義動作:
Configuration --> Actions --> Email --> Clone --> Add
3、配置釘釘告警:
(1)需要服務(wù)器公網(wǎng)出口IP
(2)注冊企業(yè)釘釘:https://oa.dingtalk.com/
(3)釘釘掃碼,輸入管理密碼后登錄:
通訊錄 --> 內(nèi)部通訊錄管理 --> 部門人員 --> 邀請成員加入(使用釘釘掃描二維碼申請加入)
工作臺 --> 應(yīng)用管理 --> 自建應(yīng)用 --> 創(chuàng)建Zabbix釘釘告警應(yīng)用
釘釘開放平臺:https://open-dev.dingtalk.com/
(4)編寫釘釘告警腳本:
# cd /usr/lib/zabbix/alertscripts
# vim dingtalk.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json,urllib2,sys
?
appkey = 'XXXXXXXX'
appsecret = 'XXXXXXXX'
agentid = 'XXXXXXXX'
touser = sys.argv[1]
content = sys.argv[2]
?
tockenurl = 'https://oapi.dingtalk.com/gettoken?corpid=' + appkey + "&corpsecret=" + appsecret
tockenresponse = urllib2.urlopen(tockenurl)
tockenresult = json.loads(tockenresponse.read().decode('utf-8'))
tocken = tockenresult['access_token']
?
sendurl = 'https://oapi.dingtalk.com/message/send?access_token=' + tocken
headers = {
'Content-Type':'application/json'
}
main_content = {
"touser": touser,
"toparty": "",
"agentid": agentid,
"msgtype": "text",
"text": {
"content": content
}
}
main_content = json.dumps(main_content)
req = urllib2.Request(sendurl,headers=headers)
response = urllib2.urlopen(req, main_content.encode('utf8'))
print(response.read().decode('utf-8'))
# chmod +x dingtalk.py
備注:上述腳本中的XXXXXXXX需要按照實際情況替換
(5)測試dingtalk.py腳本:
# /usr/lib/zabbix/alertscripts/dingtalk.py 'XXXX' 'zabbix alert test'
(6)媒介中新增釘釘告警:
Administration --> Media types --> Create media type --> Media type --> Add
需要2個參數(shù):{ALERT.SENDTO}、{ALERT.MESSAGE}
(7)對應(yīng)用戶中設(shè)置報警媒介:
Administration --> Users --> Admin --> Media --> Add --> Update
(8)定義動作:
Configuration --> Actions --> Email --> Clone --> Add
4、告警測試:
(1)停止node-122節(jié)點上的vsftpd:# systemctl stop vsftpd
查看Action log:Reports --> Action log
(2)啟動node-122節(jié)點上的vsftpd:# systemctl start vsftpd
查看Action log:Reports --> Action log
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。