官方介绍:http://caspian.dotconf.net/menu/Software/SendEmail/
[root@localhost SendEmail]# wget -c http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz --2016-08-30 22:21:50-- http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz 正在解析主机 caspian.dotconf.net... 69.164.196.234 正在连接 caspian.dotconf.net|69.164.196.234|:80... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:29740 (29K) [application/x-gzip] 正在保存至: “sendEmail-v1.56.tar.gz” 100%[=================================================>] 29,740 100K/s in 0.3s 2016-08-30 22:21:53 (100 KB/s) - 已保存 “sendEmail-v1.56.tar.gz” [29740/29740]) [root@localhost SendEmail]# ls sendEmail-v1.56.tar.gz [root@localhost SendEmail]# tar zxvf sendEmail-v1.56.tar.gz sendEmail-v1.56/ sendEmail-v1.56/CHANGELOG sendEmail-v1.56/README sendEmail-v1.56/README-BR.txt sendEmail-v1.56/TODO sendEmail-v1.56/sendEmail sendEmail-v1.56/sendEmail.pl [root@localhost SendEmail]# cd sendEmail-v1.56 [root@localhost sendEmail-v1.56]# cp -a sendEmail /usr/local/bin/ [root@localhost sendEmail-v1.56]# chmod +x /usr/local/bin/sendEmail [root@localhost sendEmail-v1.56]# file /usr/local/bin/sendEmail /usr/local/bin/sendEmail: a /usr/bin/perl -w script text executable
sendemail命令帮助信息
[root@localhost sendEmail-v1.56]# /usr/local/bin/sendEmail sendEmail-1.56 by Brandon Zehm <caspian@dotconf.net> Synopsis: sendEmail -f ADDRESS [options] Required: -f ADDRESS from (sender) email address * At least one recipient required via -t, -cc, or -bcc * Message body required via -m, STDIN, or -o message-file=FILE Common: -t ADDRESS [ADDR ...] to email address(es) -u SUBJECT message subject -m MESSAGE message body -s SERVER[:PORT] smtp mail relay, default is localhost:25 Optional: -a FILE [FILE ...] file attachment(s) -cc ADDRESS [ADDR ...] cc email address(es) -bcc ADDRESS [ADDR ...] bcc email address(es) -xu USERNAME username for SMTP authentication -xp PASSWORD password for SMTP authentication Paranormal: -b BINDADDR[:PORT] local host bind address -l LOGFILE log to the specified file -v verbosity, use multiple times for greater effect -q be quiet (i.e. no STDOUT output) -o NAME=VALUE advanced options, for details try: --help misc -o message-content-type=<auto|text|html> -o message-file=FILE -o message-format=raw -o message-header=HEADER -o message-charset=CHARSET -o reply-to=ADDRESS -o timeout=SECONDS -o username=USERNAME -o password=PASSWORD -o tls=<auto|yes|no> -o fqdn=FQDN Help: --help the helpful overview you're reading now --help addressing explain addressing and related options --help message explain message body input and related options --help networking explain -s, -b, etc --help output explain logging and other output options --help misc explain -o options, TLS, SMTP auth, and more
安装依赖包
[root@localhost sendEmail-v1.56]# yum -y install perl-Net-SSLeay perl-IO-Socket-SSL
报警脚本
[root@localhost lib]# find /usr/ -name alertscripts #脚本存放目录 /usr/local/zabbix/share/zabbix/alertscripts [root@localhost sendEmail-v1.56]# cd /usr/local/zabbix/share/zabbix/alertscripts/
[root@localhost alertscripts]# cat zabbixmail.sh
#!/bin/bash
SMTP_server='smtp.163.com' #smtp服务器
username='zabbix*****@163.com' #用户名
password='********' #密码 提醒:写邮箱客户端授权密码
如您仍选择采用IMAP协议访问,可通过以下方式开启: | ||
1、登录网页版网易邮箱,在【设置】-【POP3/SMTP/IMAP】中开启; |
from_email_address='zabbix****@163.com' #发件人email地址
to_email_address="$1" #收件人email地址,zabbix传入的第一个参数;
message_subject_utf8="$2" #邮件标题,zabbix传入的第三个参数
message_body_utf8="$3" #邮件内容,zabbix传入的第三个内容
# 转换邮件标题为GB2312,解决邮件标题含有中文,收到邮件显示乱码的问题。
message_subject_gb2312=`iconv -t GB2312 -f UTF-8 << EOF
$message_subject_utf8
EOF`
[ $? -eq 0 ] && message_subject="$message_subject_gb2312" || message_subject="$message_subject_utf"
# 转换邮件内容为GB2312,解决收到邮件内容乱码
message_body_gb2312=`iconv -t GB2312 -f UTF-8 << EOF
$message_body_utf8
EOF`
[ $? -eq 0 ] && message_body="$message_body_gb2312" || message_body="$message_body_utf8"
# 发送邮件
sendEmail='/usr/local/bin/sendEmail'
$sendEmail -s "$SMTP_server" -xu "$username" -xp "$password" -f "$from_email_address" -t "$to_email_address" -u "$message_subject" -m "$message_body" -o message-content-type=text -o message-charset=gb2312
/usr/local/bin/sendEmail 命令主程序 -f zabbix*****@163.com 发件人邮箱 -s smtp.163.com 发件人邮箱的smtp服务器 -u "我是邮件主题" 邮件的标题 -o message-content-type=html 邮件内容的格式,html表示它是html格式 -o message-charset=utf8 邮件内容编码 -xu zabbix*****@163.com 发件人邮箱的用户名 -xp ******* 发件人邮箱密码 -m "我是邮件内容" 邮件的具体内容 发送成功之后会有如下提示: Apr 29 22:22:19 server sendEmail[75870]: Email was sent successfully!
设置脚本权限
[root@localhost alertscripts]# chmod +x zabbixmail.sh
[root@localhost alertscripts]# chown zabbix.zabbix zabbixmail.sh
这只zabbix用户报警
打开zabbix——管理——报警媒介类型——创建媒体类型
添加以下3个参数,分别对sendEmail.sh脚本需要的三个参数:收件人、主题、详细内容
{ALERT.SENDTO}
{ALERT.SENDTO}
{ALERT.MESSAGE}
管理——用户——Admin——报警媒介
设置zabbix出发报警动作
配置——动作——创建动作
名称:Action-SendEmail 默认接收人:故障PROBLEM,服务器:{HOSTNAME1}:{HOST.CONN}发生: {TRIGGER.NAME}故障! 默认信息: 告警主机:{HOSTNAME1} 告警时间:{EVENT.DATE} {EVENT.TIME} 告警等级:{TRIGGER.SEVERITY} 告警信息: {TRIGGER.NAME} 告警项目:{TRIGGER.KEY1} 问题详情:{ITEM.NAME}:{ITEM.VALUE} 当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1} 事件ID:{EVENT.ID} 恢复信息:打钩 恢复主题:恢复OK, 服务器:{HOSTNAME1}:{HOST.CONN}的 {TRIGGER.NAME}已恢复! 恢复信息: 告警主机:{HOSTNAME1} 告警时间:{EVENT.DATE} {EVENT.TIME} 告警等级:{TRIGGER.SEVERITY} 告警信息: {TRIGGER.NAME} 告警项目:{TRIGGER.KEY1} 问题详情:{ITEM.NAME}:{ITEM.VALUE} 当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1} 事件ID:{EVENT.ID} 已启用:打钩
切换到操作选项
关闭一台服务器的zabbix_agentd 服务、测试邮件是否能发送成功
[root@test3 /]# /etc/init.d/zabbix_agentd stop
Shutting down zabbix_agentd: [ OK ]
到邮箱查看
告警邮件