zabbix配置STMP使用SSL发送邮件报警

zabbix配置STMP使用SSL发送邮件报警

安装mail

#CentOS
yum install -y mailx

配置STMP(/etc/mail.rc) 尾行追加

set from=m*******[email protected] smtp=smtp.163.com
set smtp-auth-user=m*******[email protected]
set smtp-auth-password=m*****3
set smtp-auth=login

测试邮件是否能发送

echo "hehe" | mail -s 'hhaa' z*********@163.com

使用SSL/TLS

mkdir -p /root/.certs/ 
# 创建目录,用来存放证书;

echo -n | openssl s_client -connect smtp.163.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/163.crt 
# 向163请求证书;

certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/163.crt
# 添加一个SSL证书到证书数据库中;

certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/163.crt 
# 添加一个Global 证书到证书数据库中;

certutil -L -d /root/.certs
# 列出目录下证书;

cd /root/.certs/ && certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i 163.crt
# 让证书受信任;

Notice: Trust flag u is set automatically if the private key is present.
# 如上输出为成功;

添加mail.rc配置文件

set ssl-verify=ignore
set nss-config-dir=/root/.certs

编写发送邮件脚本

如果不进行格式转换或者是替换,可以看到结尾带有win的换行符^M,这会导致邮件发不出去而发生554的错误。解决办法有两种分别是dos2unix进行格式转换或是文本处理器替换。

#!/bin/bash 
contact=$1 
subject=$2 
body=/tmp/mailx.log 
 
echo "$3" >$body 
/usr/bin/dos2unix $body 
#sed -i 's/^M/\n/g' $body && sed -i 's/[[:space:]]//' $body
mail -s "$subject" "$contact" < $body

测试成果

触发报警

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章