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