一, zabbix 邮件报警设置
原理: 触犯告警器之后 可以配置相应的动作, 在动作里可以发邮件, 微信, 钉钉, 短信等.
告警脚本的放置位置见配置 / usr/local/zabbix/etc/zabbix_server.conf
- LogFile=/usr/local/zabbix/zabbix_server.log
- DBHost=127.0.0.1
- DBName=zabbix
- DBUser=zabbix
- DBPassword=zabbixpwd
- DBPort=3306
- Timeout=30
- AlertScriptsPath=/usr/local/zabbix/alertscripts ## 触发脚本放置位置
- ExternalScripts=/usr/local/zabbix/externalscripts
- LogSlowQueries=3000
python 邮件发送脚本
- /usr/local/zabbix/alertscripts/zabbix_sendmail.py
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- from email.mime.text import MIMEText
- from email.header import Header
- from smtplib import SMTP_SSL
- import sys
- smtpaddr = 'smtp.qq.com'
- myemail='[email protected]' ## 发送邮件的 QQ
- password='mypassword' ## 填写登陆客户端的授权码, 在邮箱里生成同时需要打开 POP3,IMAP 服务
- #f = open('/usr/local/zabbix/.passwd','r')
- #password = f.readline().strip()
- recvmail=sys.argv[1] ## 发送邮件给谁
- subject=sys.argv[2] ## 发送的主题
- content=sys.argv[3] ## 发送的内容
- msg = MIMEText("""%s"""%(content), "plain", "utf-8") ## 发送的内容
- msg['Subject'] = Header(subject, 'utf-8').encode()
- msg['From'] = myemail
- msg['To'] = recvmail
- try:
- smtp = SMTP_SSL( smtpaddr )
- smtp.login(myemail, password)
- smtp.sendmail(myemail, recvmail.split(','), msg.as_string())
- smtp.quit()
- print("success")
- except Exception as e:
- print("fail:"+str(e))
- ## 增加执行权限, 测试脚本是否运行正常能发送邮件
- /usr/local/zabbix/alertscripts/zabbix_sendmail.py [email protected] 'zabbix disk' 'content: disk> 90%'
2, 报警媒介设置邮件告警
脚本放置的名字, 注意是放置在 / usr/local/zabbix/alertscripts 目录下, 添加三个接受的参数, 发送给谁, 主题是什么, 内容是什么 这地方需要添加三个参数
- {
- ALERT.SENDTO
- }
- {
- ALERT.SUBJECT
- }
- {
- ALERT.MESSAGE
- }
添加需要发送的用户媒介设置
发送执行的脚本, 发送给谁, 时间, 级别, 有多个 以逗号隔开
创建触发器动作 在模板或者主机里
设置触发器触发之后的动作 (action)
出问题发送的内容 用户, 执行的脚本
故障恢复之后动作 发送一封恢复的邮件
二, 告警内容自定义和告警抑制
在报警内容加上自定义内容参数 host.name host ip1
Default operation step duration 触发后的操作时间, 通过下面的 steps 来实现操作抑制, 例如 上面设置 30s
步骤 4-4 那么 在 4 次步骤 4*30 2 分钟之后才发送报警邮寄
三, 微信告警
注册创建企业微信
在管理后台添加自建应用
创建好后 获取企业微信的 agentid 和 secrct 填写于脚本
跟邮件一样 在同样位置添加微信报警脚本
- VIM /usr/local/zabbix/alertscripts/zabbix_wx.py
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import JSON
- import sys
- import urllib,urllib2
- agentid = 'xxx' ### 微信自建应用 ID
- corpid = 'xxx' ### 微信企业 ID 在我的企业里面查看
- corpsecret = 'xxx' ### 微信自建应用 secret
- #get tocken
- gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
- token_file = urllib2.urlopen(gettoken_url)
- token_data = token_file.read().decode('utf-8')
- token_json = JSON.loads(token_data)
- my_token = token_json['access_token']
- #send wechart
- touser=sys.argv[1] #many user: 'zhangsan|wangwu'
- content=sys.argv[2] #content
- post_content = {
- "touser":touser,
- "agentid":agentid,
- "msgtype": "text",
- "text":{
- "content":content,
- }
- }
- json_content = JSON.dumps(post_content)
- url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + my_token
- response = urllib2.urlopen(url,json_content)
- print(response.read().decode('utf-8'))
- ## 测试
- /usr/local/zabbix/alertscripts/zabbix_wx.py '通讯录里面的微信名' 'disk is not enough'
添加 创建 2 个变量 发送给谁 发送的内容
报警之后的 action(注意更改发送的脚本名称)
user 添加 注意填写发送名称, 还有报警级别
四: 钉钉告警
钉钉告警跟微信告警类似
告警脚本 / usr/local/zabbix/alertscripts/
- ### 告警机器人脚本
- ### 安装 pip, 安装 request 模块
- yum -y install epel-release
- yum -y install python-pip
- pip install requests
- #!/usr/bin/env python
- import JSON
- import requests
- import sys
- def send_msg(url, remiders, msg):
- headers = {'Content-Type': 'application/json; charset=utf-8'}
- data = {
- "msgtype": "text",
- "at": {
- "atMobiles": remiders,
- "isAtAll": False,
- },
- "text": {
- "content": msg,
- }
- }
- r = requests.post(url, data=JSON.dumps(data), headers=headers)
- return r.text
- if __name__ == '__main__':
- msg = sys.argv[1]
- remiders = []
- url = '告警机器人 webrooturl'
- print(send_msg(url, remiders, msg))
相关配置参数 (发送用户填写 admin 即可)
钉钉工作通知告警脚本
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- #curl 'https://oapi.dingtalk.com/gettoken?corpid=xxx&corpsecret=xxx'
- import JSON,urllib2,sys
- appkey = '钉钉自建应用查看'
- appsecret = '钉钉自建应用查看'
agentid = 钉钉自建应用查看'
- 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'))
- ## 测试
/usr/local/zabbix/alertscripts/zabbix_dd.py 填写钉钉用户 ID 'disk> 90%'
来源: http://www.bubuko.com/infodetail-3347190.html