sendEmail是一款轻量级,巧小,无需安装的邮件发送命令。只需下载,解压就可以使用。
官网地址:http://caspian.dotconf.net/menu/Software/SendEmail/
下载安装sendEmail邮件包
[root@node2 ~]# wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
[root@node2 ~]# tar xvf sendEmail-v1.56.tar.gz
[root@node2 ~]# cp -a sendEmail-v1.56/sendEmail /usr/local/bin/
[root@node2 ~]# ll /usr/local/bin/sendEmail
-rwxr-xr-x 1 root root 80213 Sep 30 2009 /usr/local/bin/sendEmail
[root@node2 ~]# sendEmail -h --帮助
2.编写shell监控脚本
[root@node2 ~]# mkdir -p /etc/zabbix/alertscripts
[root@node2 ~]# vim /etc/zabbix/alertscripts/SendEmail.sh
#!/bin/bash
# Filename: SendEmail.sh
# Date: 2015/01/04
# Description: zabbix邮件告警脚本
# Notes: 使用sendEmail
LOGFILE="/tmp/Email.log"
:>"$LOGFILE"
exec 1>"$LOGFILE"
exec 2>&1
SMTP_server='smtp.sina.com' #SMTP服务器,变量值需要自行修改
username='xxxxxxx@sina.com' #用户名,变量值需要自行修改
password='xxxxxx' #密码,变量值需要自行修改
from_email_address='tingyun12345@sina.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_utf8"
# 转换邮件内容为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'
set -x
$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
[root@node2 ~]# chmod 755 /etc/zabbix/alertscripts/SendEmail.sh
[root@node2 ~]# chown zabbix:zabbix /etc/zabbix/alertscripts/SendEmail.sh
[root@node2 ~]# vim /etc/zabbix/zabbix_server.conf
AlertScriptsPath=/etc/zabbix/alertscripts
[root@node2 ~]# touch /tmp/Email.log
[root@node2 ~]# chown zabbix:zabbix /tmp/Email.log
[root@node2 ~]# /etc/init.d/zabbix-server restart
[root@node2 alertscripts]# ./SendEmail.sh xxxxxxxx@qq.com 111 222 --测试邮件发送
[root@node2 alertscripts]# tailf /tmp/Email.log
+ /usr/local/bin/sendEmail -s smtp.sina.com -xu xxxxxxxx@sina.com -xp system -f xxxxxxxxxx@sina.com -t xxxxxxxxx@qq.com -u 111 -m 222 -o message-content-type=text -o message-charset=gb2312
Oct 27 14:22:11 node2 sendEmail[6872]: Email was sent successfully!
3.在zabbix添加监控报警机制