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

測試成果

觸發報警

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