1. 程式人生 > >CentOS---zabbix使用sendEamil發送報警

CentOS---zabbix使用sendEamil發送報警

ces conda img mage web dma readme gzip 如果

一、sendEmail簡介

  sendEmail是一個輕量級,命令行的SMTP郵件客戶端。如果你需要使用命令行發送郵件,那麽sendEmail是非常完美的選擇:使用簡單並且功能強大.這個被設計用在php、bash
perl和web站點使用。
以上是sendEmail的簡單介紹,千萬不要和sendmail搞混掉了。用了sendEmail你將不在喜歡sendmail了.

二、安裝配置sendEmail

sendEmail下載地址:

http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz

下載sendEmail

[[email protected] ~]# wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
--2017-06-08 05:07:01--  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 126K/s in 0.2s
2017-06-08 05:07:07 (126 KB/s) - 已保存 “sendEmail-v1.56.tar.gz” [29740/29740]) [[email protected] ~]# ls anaconda-ks.cfg install.log install.log.syslog sendEmail-v1.56.tar.gz zabbix-release-2.2-1.el6.noarch.rpm [[email protected] ~]#

安裝:

解壓文件

[[email protected] ~]# tar -zxvf sendEmail-v1.56.tar.gz 

copy--sendEmail到/usr/local/bin/並賦予權限

[[email protected] ~]# cd sendEmail-v1.56
[[email protected] sendEmail-v1.56]# ls
CHANGELOG  README  README-BR.txt  sendEmail  sendEmail.pl  TODO
[[email protected] sendEmail-v1.56]# cp sendEmail /usr/local/bin
[[email protected] sendEmail-v1.56]# cd /usr/local/bin/
[[email protected] bin]# ls
sendEmail
[[email protected] bin]# chmod 755 sendEmail

sendEmail命令選項:

技術分享

創建zabbix服務器端發送郵件腳本

修改/etc/zabbix/zabbix_server.conf

AlertScriptsPath=/etc/zabbix/alertscripts

技術分享

修改後重啟zabbix server

[[email protected] zabbix]# service zabbix-server restart
Shutting down Zabbix server: [確定]
Starting Zabbix server: [確定]
[[email protected] zabbix]# 

創建發送郵件腳本

[[email protected] alertscripts]# vi SendEmail.sh

# Email:
#!/bin/bash
# 
# Filename:    SendEmail.sh
# Revision:    1.0
# Date:        2017/06/07
# Author:      wangxb
# Email:
# Description: zabbix郵件告警腳本
# Notes:       使用sendEmail
#
# 腳本的日誌文件
LOGFILE="/tmp/Email.log"
:>"$LOGFILE"
exec 1>"$LOGFILE"
exec 2>&1

SMTP_server=smtp.163.com                    # SMTP服務器,變量值需要自行修改
username=[email protected]           # 用戶名,變量值需要自行修改 
password=‘zabbix                       # 密碼,變量值需要自行修改
from_email_address=[email protected] # 發件人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

然後執行下面的命令,設置腳本和日誌文件權限

[[email protected] alertscripts]# cd /tmp/
[[email protected] tmp]# mkdir Email.log
[[email protected] tmp]# ll
總用量 64
drwxr-xr-x  2 root root  4096 6月   8 05:43 Email.log
[[email protected] tmp]# chown zabbix:zabbix Email.log
[[email protected] tmp]# chown zabbix:zabbix /etc/zabbix/alertscripts/SendEmail.sh 
[[email protected] tmp]# chmod -x /etc/zabbix/alertscripts/SendEmail.sh 
[[email protected] tmp]# chown -R zabbix:zabbix /etc/zabbix/alertscripts/

測試發送郵件

[[email protected] ~]# bash etc/zabbix/alertscripts/SendEmail.sh [email protected] "測試郵件標題" "測試郵件內容"
bash: etc/zabbix/alertscripts/SendEmail.sh: 沒有那個文件或目錄
[[email protected] ~]# bash /etc/zabbix/alertscripts/SendEmail.sh [email protected] "測試郵件標題" "測試郵件內容"
/etc/zabbix/alertscripts/SendEmail.sh: line 13: /tmp/Email.log: 是一個目錄
/etc/zabbix/alertscripts/SendEmail.sh: line 14: /tmp/Email.log: 是一個目錄
+ /usr/local/bin/sendEmail -s smtp.163.com -xu zabbix@163.com -xp 123456 -f zabbix@163.com -t 115231245@qq.com -u $\262\342\312\324\323\312\274\376\261\352\314\342 -m $\262\342\312\324\323\312\274\376\304\332\310\335 -o message-content-type=text -o message-charset=gb2312
Jun 08 05:53:14 localhost sendEmail[2185]: Email was sent successfully!

CentOS---zabbix使用sendEamil發送報警