A, alarm messages ZABBIX
principles: may configure the operation of the alarm device after committing, where the operation can send email, the micro-channel, nails, and other messages.
Place the alarm configuration script, see /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
Send e-mail python script
/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, set up e-mail alerts alert the media
Script placement name, attention is placed in the / usr / local / zabbix under / alertscripts directory, add three acceptable parameters, sent to whom, what the subject is, content is what this place needs to add three parameters
{ALERT.SENDTO}
ALERT.SUBJECT} {
{} ALERT.MESSAGE
Add media need to send user settings
Send the script executed, sent to whom, time, level, there are multiple separated by commas
Create a trigger action in the template or mainframe
Action (action) after setting the trigger to fire
the script sent contents of the user's problem, executed
After a fault recovery action to send an e-mail recovery
Second, custom alarm contents and alarm suppression
in an alarm parameter content plus custom content host.name host ip1
operation Default operation step duration time after the trigger, the operation is implemented through the following Steps suppressed, e.g. 30s disposed above
step 4-4 then it sends an alarm mailed after 4 * 30 2 minutes step 4
Third, the micro-channel alarm
registered enterprises create micro letter
to add the background in the management of self-built application
after creating a good micro enterprise to obtain a letter of agentid and secrct fill in the script
with the same message add micro-channel alarm script in the same position
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'
Add to create two variables sent to the content who sent the
action after the alarm (note change the name of the script sent)
the User added attention. Send name, as well as the alarm level
four: Alarm nail
nails alarm with micro-channel similar warning
alarm script / 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))
Configuration parameters (admin can send the user to fill in)
nails work alarm notification script
#!/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%'