企业级监控软件Zabbix搭建部署之使用mutt+msmtp配置Zabbix邮件报警
[root@Zabbix-Server ~]# wget http://nchc.dl.sourceforge.net/sourceforge/msmtp/msmtp-1.4.17.tar.bz2
[root@Zabbix-Server ~]# tar xf msmtp-1.4.17.tar.bz2
[root@Zabbix-Server ~]# cd msmtp-1.4.17
[root@Zabbix-Server msmtp-1.4.17]# mkdir /usr/local/msmtp
[root@Zabbix-Server msmtp-1.4.17]# yum -y install gnutls-devel gnutls
[root@Zabbix-Server msmtp-1.4.17]# ./configure --prefix=/usr/local/msmtp
[root@Zabbix-Server msmtp-1.4.17]# make
[root@Zabbix-Server msmtp-1.4.17]# make install
[root@Zabbix-Server msmtp-1.4.17]# cd /usr/local/msmtp/
[root@Zabbix-Server msmtp]# ls
bin share
[root@Zabbix-Server msmtp]# mkdir etc 创建目录
[root@Zabbix-Server msmtp]# cd etc/
[root@Zabbix-Server etc]# ls
[root@Zabbix-Server etc]# vim msmtprc 创建配置文件
配置文件如下:
account default
host smtp.ym.163.com #发送邮件服务器
port 25 #发送端口
from noreply@youlongteng.com #发送的邮箱
auth login
tls off #这里如果使用on的话会报 "msmtp: cannot use a secure authenti cation method"错误
user noreply@youlongteng.com #邮箱用户
password 123.com. #邮箱的密码,明文保存的需要注意,可以设置权限
logfile /var/log/mmlog #日志的路径
[root@Zabbix-Server etc]# ll /usr/local/msmtp/etc/msmtprc
-rwxr-xr-x 1 zabbix root 195 Oct 11 23:52 /usr/local/msmtp/etc/msmtprc
[root@Zabbix-Server etc]#
保存测试
[root@Zabbix-Server etc]# /usr/local/msmtp/bin/msmtp xxoo@xxxx.com
This is my mail
www.rsyslog.net
[root@Zabbix-Server etc]#
ctrl+d写完就可以退出,然后服务器查看是否收到
[root@Zabbix-Server log]# cat mmlog
Dec 31 10:10:12 host=smtp.ym.163.com tls=off auth=on user=xxoo@xxxx.com from=xxoo@xxxx.com recipients=xxoo@xxxx.com mailsize=406 smtpstatus=250 smtpmsg='250 2.0.0 Ok: queued as EDBC41101988' exitcode=EX_OK
Dec 31 10:10:12 host=smtp.ym.163.com tls=off auth=on user=xxoo@xxxx.com from=xxoo@xxxx.com recipients=xxoo@xxxx.com mailsize=397 smtpstatus=250 smtpmsg='250 2.0.0 Ok: queued as 1E8911101684' exitcode=EX_OK
Dec 31 10:10:12 host=smtp.ym.163.com tls=off auth=on user=xxoo@xxxx.com from=xxoo@xxxx.com recipients=sxxoo@xxxx.com mailsize=396 smtpstatus=250 smtpmsg='250 2.0.0 Ok: queued as 7801E11011F0' exitcode=EX_OK
[root@Zabbix-Server log]#
安装mutt
[root@Zabbix-Server etc]# yum -y install mutt
修改配置文件
vim /etc/Muttrc.local
# Local configuration for Mutt.
set sendmail="/usr/local/msmtp/bin/msmtp"
set use_from=yes
set realname="xxoo@xxxx.com" 你的发送邮箱地址
set editor="vim"
[root@Zabbix-Server log]# ll /etc/Muttrc.local
-rw-r--r-- 1 root root 151 Oct 10 18:28 /etc/Muttrc.local
[root@Zabbix-Server log]#
测试安装是否成功
错误提示:如下图是提示添加附件错误。。。。。。
[root@Zabbix-Server msmtp]# echo "testmail" | mutt -s "test" -a /tmp/file xxoo@xxxx.com
Can't stat zhangkeyuan@youlongteng.com: No such file or directory
zhangkeyuan@youlongteng.com: unable to attach file.
正确方法如下:
[root@Zabbix-Server msmtp]# echo "This is a test mail " >> /tmp/file
需要注意的是,在CentOS6下面,我们需要按照如下的方法去下,添加附件应该放置到最后面,这样才能成功,如果-a放到邮件地址前面会报错
如果有多个邮件地址,就写多个即可
[root@Zabbix-Server msmtp]# echo "testmail" | mutt -s "test" xxoo@xxxx.com -a /tmp/file
[root@Zabbix-Server msmtp]# echo "testmail" | mutt -s "test" xxoo@xxxx.com 431054426@qq.com -a /tmp/file
这样就测试成功查看日志记录
[root@Zabbix-Server msmtp]# tail /var/log/mmlog -n 2
Oct 05 16:40:03 host=smtp.ym.163.com tls=off auth=on user=xxoo@xxxx.com from=xxoo@xxxx.com recipients=xxoo@xxxx.com mailsize=598 smtpstatus=250 smtpmsg='250 2.0.0 Ok: queued as AB0F11480DCF' exitcode=EX_OK
Oct 05 16:51:30 host=smtp.ym.163.com tls=off auth=on user=xxoo@xxxx.com from=xxoo@xxxx.com recipients=xxoo@xxxx.com mailsize=734 smtpstatus=250 smtpmsg='250 2.0.0 Ok: queued as 29FC51481F38' exitcode=EX_OK
[root@Zabbix-Server msmtp]#
经验之谈技巧
在创建之前首先修改配置文件
首先查找脚本路径
[root@Zabbix-Server alertscripts]# locate externalscripts
/usr/local/zabbix/share/zabbix/alertscripts
[root@Zabbix-Server alertscripts]# pwd
/usr/local/zabbix/share/zabbix/alertscripts
[root@Zabbix-Server alertscripts]#
修改配置文件,默认情况下是支持的。如果不知道可以查找
vim /usr/local/zabbix/etc/zabbix_server.conf,去掉392行的注释
创建脚本
[root@Zabbix-Server alertscripts]# cat zabbix_monitor.sh
[root@dota-db-10 alertscripts]# cat baojing.sh 如下脚本来自生产环境:
[root@dota-db-10 alertscripts]#
[root@Zabbix-Server alertscripts]# chmod 755 zabbix_monitor.sh
[root@Zabbix-Server alertscripts]# chown zabbix root zabbix_monitor.sh
记得用zabbix用户测试是否能发送
AlertScriptsPath=${datadir}/zabbix/alertscripts 这个路径如果不知道请查找
/usr/local/share/zabbix/alertscripts/
monitoring—-Events—–查看是否正常如果报错会有提示
如果是这样 需要给root修改权限
/root/sent: Permission denied (errno = 13)
/root/sent: Permission denied (errno = 13)
/root/sent: Permission denied (errno = 13)
/root/sent: Permission denied (errno = 13)
解决:
[root@dota-db-10 /]# ll /root/ -d
dr-xr-x---. 6 zabbix root 4096 Oct 11 23:52 /root/
[root@dota-db-10 /]# ll /root/s
sent soft/
[root@dota-db-10 /]# ll /root/sent
-rwxrwxrwx 1 zabbix root 42959 Oct 11 23:58 /root/sent
[root@dota-db-10 /]#
权限注意
[root@dota-db-10 alertscripts]# cat baojing.sh
#!/bin/bash
echo "$3" | mutt -s "$2" $1
[root@dota-db-10 alertscripts]# ll baojing.sh -d
-rwxr-xr-x 1 zabbix root 40 Oct 11 23:41 baojing.sh
[root@dota-db-10 alertscripts]#
如下切记一定要配置,不然无法收到邮件
chmod zabbix:root /var/log/zext_msmtp.log
chmod 777 /var/log/zext_msmtp.log
chmod 755 /usr/local/msmtp/etc/msmtprc
chown zabbix:root /usr/local/msmtp/etc/msmtprc
chmod 755 /var/log/mmlog
chown zabbix:root /var/log/mmlog