zabbix添加邮件方式报警并实现分组报警的功能
zabbix添加邮件方式报警并实现分组报警的功能
1.编写发送邮件的python脚本
# vim /usr/local/zabbix-3.2.0/share/zabbix/alertscripts]# cat send_mail.py
#!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText from sys import argv mailto_list=[] mail_host="smtpdm-ap-southeast-1.aliyun.com:80" mail_user="alarm@mail-service.chinasoft.com" mail_pass="pass" debug_level=0 def send_mail(to_list,sub,content): me=mail_user msg = MIMEText(content,_subtype='plain',_charset='utf-8') msg['Subject'] = sub msg['From'] = me msg['To'] = ";".join(to_list) try: server = smtplib.SMTP() server.set_debuglevel(debug_level) server.connect(mail_host) server.login(mail_user,mail_pass) server.sendmail(me, to_list, msg.as_string()) server.close() return True except Exception, e: print str(e) return False if __name__ == '__main__': try: mailto_list=argv[1].split(';') sub=argv[2] content=argv[3] except: print "python send_mail.py 'user1@xx.com;user2@xx.com' sub content" exit() if send_mail(mailto_list,sub,content): print "send ok" else: print "send error"
2.创建Media types
name: mail_alarm
type: Script
Script name: send_mail.py
Script parameters:
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
3.创建actions动作
生产上需要在出现报警情况下,不同的主机发送报警给不同的用户
可以将主机进行分组,比如分为 dev 和 ops 运维组,然后添加用户组,将用户组和主机组进行绑定,把用户添加到特定的用户组中,并且发送报警的时候发给特定的用户
具体步骤
a.把主机进行分组
创建主机群组
配置——>主机群组——>创建主机群组,创建主机群组 dev 并将如 web01 主机添加至主机组,同样的,创建主机群组ops并将db01主机添加至主机组
b.把用户进行分组
创建用户群组
管理——>用户群组——>创建用户群组,创建用户群组dev并添加主机群组至该用户群组
添加主机组: 把机器加入到特定的服务器组中
创建用户组: 并且把机器组和用户组绑定
Group name: cart-dev
为创建的用户配置报警媒介
报警内容
Default subject : 故障{TRIGGER.STATUS},服务器名:{HOSTNAME1}发生: {TRIGGER.NAME}故障!
Default message:
告警主机:{HOSTNAME}
告警主机IP地址:{HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
恢复动作
Default subject : {TRIGGER.STATUS}: {TRIGGER.NAME}
Default message:
接收人:{TRIGGER.STATUS}: {TRIGGER.NAME}
告警主机:{HOST.NAME}
主机地址:{HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
事件代码:{EVENT.ID}
告警主机:{HOST.NAME}
主机地址:{HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
事件代码:{EVENT.ID}
邮件示例
通过shell脚本的方式发送邮件,经过测试邮件收到的内容是有问题的,推荐使用python,命令行发送邮件的记录如下
1,为了不影响邮件收发功能需要停止POSTfix:
systemctl stop postfix
systemctl disable postfix
2,安装邮件发送工具mailx:
yum -y install mailx
mail -V
3. 邮箱配置
cp /etc/mail.rc /etc/mail.rc.bak
grep -v "^$" /etc/mail.rc.bak |grep -v "^#" >/etc/mail.rc
# 末尾添加如下内容
vim /etc/mail.rc
# cat /etc/mail.rc
set hold
set append
set ask
set crt
set dot
set keep
set emptybox
set indentprefix="> "
set quote
set sendcharsets=iso-8859-1,utf-8
set showname
set showto
set newmail=nopoll
set autocollapse
set markanswered
ignore received in-reply-to message-id references
ignore mime-version content-transfer-encoding
fwdretain subject date from to
set bsdcompat
# 阿里云发送邮件服务
set from=alarm@mail-service.chinasoft.com
set smtp="smtp://smtpdm-ap-southeast-1.aliyun.com:80"
set smtp-auth-user=alarm@mail-service.chinasoft.com
set smtp-auth-password="pass"
set smtp-auth=login
# 测试语句
echo "zabbix admin mail 0401" |mail -s "alarm@mail-service.chinasoft.com" jack@chinasoft.cn
echo "zabbix admin mail 0401" |mail -s -v "alarm@mail-service.chinasoft.com" jack@chinasoft.cn
修改配置:
# cat /etc/mail.rc
set hold
set append
set ask
set crt
set dot
set keep
set emptybox
set indentprefix="> "
set quote
set sendcharsets=iso-8859-1,utf-8
set showname
set showto
set newmail=nopoll
set autocollapse
set markanswered
ignore received in-reply-to message-id references
ignore mime-version content-transfer-encoding
fwdretain subject date from to
set bsdcompat
# send mail config
# 自建邮箱服务
set from=ywalarm@service.chinasoft.com
set smtp="smtp://mail.service.chinasoft.com"
set smtp-auth-user=ywalarm@service.chinasoft.com
set smtp-auth-password="pass"
set smtp-auth=login
# 测试命令,可以收到邮件
echo "zabbix test mail" |mail -s "ywalarm@service.chinasoft.com" jack@chinasoft.cn