一、概述 sendEmail是一个轻量级,命令行的SMTP邮件客户端。如果你需要使用命令行发送邮件,那么sendEmail是非常完美的选择:使用简单并且功能强大.这个被设计用在php、bash、perl和web站点使
二、sendEmail安装及配置
1、需要安装依赖包
yum install perl-Net-SSLeay perl-IO-Socket-SSL -y
2、下载sendEmial包
cd /usr/local/src/
wget -c http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
3、解压安装
tar -xvf sendEmail-v1.56.tar.gz
cd sendEmail-v1.56/
cp -a sendEmail /usr/local/bin/
chmod +x /usr/local/bin/sendEmail
4、执行sendEmail查看帮助信息
/usr/local/bin/sendEmail
sendEmail-1.56 by Brandon Zehm
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=
-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= -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
4、sendEmail 邮件测试
参数说明
/usr/local/bin/sendEmail #命令主程序
-f from@test.cn #发件人邮箱
-t to@test.cn #收件人邮箱
-s smtp.test.cn #发件人邮箱的smtp服务器
-u "MY测试邮件主题" #邮件的标题
-o message-content-type=html #邮件内容的格式,html表示它是html格式
-o message-charset=utf8 #邮件内容编码
-xu from@test.cn #发件人邮箱的用户名
-xp $passwd #发件人邮箱密码
-m "MY测试的邮件内容" #邮件的具体内容
邮件测试
/usr/local/bin/sendEmail -f my@test.com -t to@test.com -s smtp.test.cn -u "MY测试邮件主题" -o message-content-type=html -o message-charset=utf8 -xu my@test.com -xp $passwd -m "MY测试的邮件内容"
三、Zabbix告警配置及告警测试
1、zabbix自定义的指定目录
grep '^[a-Z]' /etc/zabbix/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
cd /usr/lib/zabbix/alertscripts
2、sendEmail脚本
vim sendEmail.sh
#!/bin/bash
LOGFILE="/tmp/Email.log"
:>"$LOGFILE"
exec 1>"$LOGFILE"
exec 2>&1
SMTP_server='smtp.test.cn' #SMTP服务器,变量值需要自行修改
username='my@test.com' #用户名,变量值需要自行修改
password='$passwd ' #密码,变量值需要自行修改
from_email_address='my@test.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
3、脚本权限
chmod 755 sendEmail.sh
touch /tmp/Email.log
chown zabbix.zabbix /tmp/Email.log
chown zabbix.zabbix sendEmail.sh
4、脚本测试
./sendEmail.sh my@test.com 我的主题测试 内容测试



2.2 发送测试邮件
> /usr/local/zabbix/scripts/send_mail.py 763836801@qq.com 'aaa' 'ccc'
检查邮箱可以看到邮件
注意: 有可能报警发送了邮件,你没收到,这时请查看发件方是不是邮件被退回了。
二、zabbix后台web界面配置
1. 设置media类型
Administration > Media types
name这里可以随便自己定义,type选择scripts;Script name则输入上一步编写的脚本名称;
再添加以下3个参数,分别对应脚本需要的3个参数:收件人地址、主题、详细内容
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}

media

media
-
2.x版本默认会传3个参数
2. 创建action
Configuration>Actions
设置触发条件

Action
设置报警内容和关联报警脚本
修改持续时间为60秒
修改步骤为3,表示触发3次动作

operations
Recovery operations 同上
3. 对用户添加报警方式
Configuration>Users

add

media
4. 设置执行脚本的路径
> vim /usr/local/zabbix/etc/zabbix_server.conf
AlertScriptsPath=/usr/local/zabbix/scripts
设置755执行权限和zabbix用户属组权限
5. 测试
> /etc/init.d/zabbix_agentd stop

dashboard

Action_log

mail
-
自此zabbix邮件报警配置完成~
refer: http://blog.csdn.net/xiegh2014/article/details/56277111
http://www.jianshu.com/p/046314951af3
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29734436/viewspace-2137846/ ,如需转载,请注明出处,否则将追究法律责任。