Zabbix定义报警机制
1. 修改zabbix配置文件
#取消注释或添加一行 cat -n /etc/zabbix/zabbix_server.conf |grep --color=auto "AlertScriptsPath" 484 ### Option: AlertScriptsPath 490 # AlertScriptsPath=${datadir}/zabbix/alertscripts 492 AlertScriptsPath=/usr/lib/zabbix/alertscripts
2. 编写邮件脚本
[root@db01 ~]# cd /usr/lib/zabbix/alertscripts/ [root@db01 alertscripts]# ll total 16 -rwxr-xr-x. 1 root root 1113 May 17 13:59 email_monitor.py -rwxr-xr-x. 1 zabbix zabbix 1586 May 13 23:41 send_mail.py -rwxr-xr-x. 1 root root 1307 May 14 04:54 send_mail.py.bak -rw-r--r--. 1 root root 186 May 14 07:02 test.py
由于国外邮件测试过程颇为坎坷,就用到万能的outlook做为报警邮件
[root@db01 alertscripts]# cat email_monitor.py #!/bin/env python # coding:utf-8 import sys from smtplib import SMTP as SMTP from email.mime.text import MIMEText Email_sender = 'monxxx@outlook.com' # 发送方邮箱 Sender_passwd = 'rxxx6' # 填入发送方邮箱的授权码 Send_to = '158xxx3@163.com' # 收件人邮箱 #这个可以通过zabbix传参进来,不过考虑到以后如果有多人写列表池比较方便就没有用zabbix传邮箱地址方式 SMTP_HOST = "smtp-mail.outlook.com" SMTP_PORT = 587 def Email_monitor(subject,content): msg_from = Email_sender passwd = Sender_passwd msg_to = Send_to #print(type(content),content,'#######') msg = MIMEText(content) # 正文 msg['Subject'] = subject # 主题 msg['From'] = msg_from msg['To'] = msg_to s = SMTP(host=SMTP_HOST, port=SMTP_PORT) # 邮件服务器及端口号 s.starttls() s.login(msg_from, passwd) s.sendmail(msg_from, msg_to, msg.as_string()) s.quit() if __name__ == '__main__': try: mailto_list = sys.argv[1].split(';') # 第一个参数是zabbix传的发送给谁(接受邮箱账号),上面说了这个没有用到,写死了邮件接收账号 sub = sys.argv[2] # 第二个参数是zabbix传的主题信息 content = sys.argv[3] # 第三个参数是zabbix传的报警内容(后面会贴上报警内容,及恢复内容) Email_monitor(sub, content) print("发送成功".decode('utf-8')) # python2版本打印会出现乱码 except Exception as e: print("发送失败".decode('utf-8')) [root@db01 alertscripts]#
3. 测试脚本
python email_monitor.py bc python邮件测试发送-003 邮件内容233333
4. 微信报警脚本
[root@db01 alertscripts]# cat send_mail.py #!/bin/env python # coding:utf-8 import requests import json import sys def get_access_token(): """ 获取微信全局接口的凭证(默认有效期俩个小时) 如果不每天请求次数过多, 通过设置缓存即可 https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login """ result = requests.get( url="https://api.weixin.qq.com/cgi-bin/token", params={ "grant_type": "client_credential", "appid": "wx2499da7621f818e8", "secret": "6239e3dfc5af686777ea40b9f3df5f48", } ).json() if result.get("access_token"): access_token = result.get('access_token') else: access_token = None return access_token def sendmsg(openid, msg): # openid: 关注者的微信号 access_token = get_access_token() body = { "touser": openid, "msgtype": "text", "text": { "content": msg } } response = requests.post( url="https://api.weixin.qq.com/cgi-bin/message/custom/send", params={ 'access_token': access_token }, data=bytes(json.dumps(body, ensure_ascii=False)) ) # 这里可根据回执code进行判定是否发送成功(也可以根据code根据错误信息) result = response.json() print(result) if __name__ == '__main__': try: mailto_list = sys.argv[1].split(';') sub = sys.argv[2] content = sys.argv[3] print(mailto_list,sub,content) sendmsg('oYm3A06plEcQCItTxAFgoh18E-7M', content) except: pass [root@db01 alertscripts]#
#晚上再登录已经登录不上去163了就不截图了
5. Zabbix上配置
这里笔者就不读写了,直接传送门:http://blog.51cto.com/jinlong/2051106
6. 报警操作及恢复操作(记得再动作里配置)
接收人:{TRIGGER.STATUS}: {TRIGGER.NAME}
告警主机:{HOST.NAME}
主机地址:{HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
事件代码:{EVENT.ID}
接收人:{TRIGGER.STATUS}: {TRIGGER.NAME}
恢复主机:{HOST.NAME}
主机地址:{HOST.IP}
恢复时间:{EVENT.DATE} {EVENT.TIME}
恢复等级:{TRIGGER.SEVERITY}
恢复信息:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
事件代码:{EVENT.ID}