Zabbix利用msmtp+mutt发送邮件报警

Posted 运维工匠实战(如果发现有错误请大家把正确的方法发送给我,方便

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zabbix利用msmtp+mutt发送邮件报警相关的知识,希望对你有一定的参考价值。

 

[root@86 ~]# wget http://jaist.dl.sourceforge.net/project/msmtp/msmtp/1.4.32/msmtp-1.4.32.tar.bz2

 

百度网盘:http://pan.baidu.com/s/1o8w68ci

 

[root@86 msmtp-1.4.32]# pwd
/root/msmtp-1.4.32
[root@86 msmtp-1.4.32]# mkdir -p  /usr/local/msmtp/etc

[root@86 msmtp-1.4.32]#./configure --prefix=/usr/local/msmtp --with-ssl=openssl

 

[root@86 msmtp-1.4.32]# make && make install

 配置msmtp

[root@86 msmtp-1.4.32]# mkdir -p  /usr/local/msmtp/etc

 

vim  /usr/local/msmtp/etc/msmtprc
account default
host smtp.163.com //SMTP服务器
port 25
from clnking@163.com
auth login
tls off
user clnking //邮箱用户名 是clnking 而不是clnking@163.com
password xxxxxx #是授权码,而不是邮箱登录密码(授权码是用于登录第三方邮件客户端的专用密码)注意文章开头的图片
logfile /var/log/zabbix/msmtp.log //日志路径

 

测试msmtp
# cat /usr/local/msmtp/etc/msmtprc 
account default
host smtp.163.com
port 25
from username01@163.com
auth login
tls off
user username01
password xxxxx #这里不是客户端受权密码,并非邮箱的登录密码
logfile /var/log/mmlog
# cat /etc/Muttrc.local 
# Local configuration for Mutt.
set sendmail="/apps/msmtp/bin/msmtp"
set from="clnking@163.com"
set use_from = yes
set editor="vim"
set realname="zabbix of XXXX.com"

[root@86 msmtp-1.4.30]# /usr/local/msmtp/bin/msmtp lin.chen@xinxianm.com  (邮箱必须是 /usr/local/msmtp/etc 里配置的邮箱)所以下面会报错!!(别的信箱也可以)
hello
ctl +d #发送
msmtp: the server did not accept the mail
msmtp: server message: 554 DT:SPM 163 smtp4,DtGowAAnErU7+ExXz+8hAQ--.9856S2 1464662088,please see http://mail.163.com/help/help_spam_16.htm?ip=123.57.36.227&hostid=smtp4&time=1464662088
msmtp: could not send mail (account default from /usr/local/msmtp/etc/msmtprc)
后补:
[root@86 etc]# yum install mutt
修改配置文件:
cat /etc/Muttrc.local 
# Local configuration for Mutt.
set sendmail="/usr/local/msmtp/bin/msmtp"
set from="username01@163.com"
set use_from=yes
set editor="vim"
set realname="zabbix of xxxxx.com"


[root@86 etc]# echo "zabbix test "|mutt -s "chenlin" clnking@163.com
[root@86 etc]# echo "zabbix test "|mutt -s "chenlin" lin.chen@xinxianm.com
======================================================================================
[root@86 msmtp-1.4.30]# /usr/local/msmtp/bin/msmtp clnking@163.com
kkkkkkkkkkkkk^[  
但信箱里没内容
cat /var/log/mmlog
May 31 10:47:00 host=smtp.163.com tls=off auth=on user=clnking from=clnking@163.com recipients=clnking@163.com mailsize=15 smtpstatus=250 smtpmsg=\'250 Mail OK queued as smtp4,DtGowADXJJQP+0xX0LciAQ--.5878S2 1464662820\' exitcode=EX_OK

 因为没有配置:MUTT

[root@86 msmtp-1.4.32]# yum install mutt

 

[root@86 ~]# vi /etc/Muttrc
set sendmail="/usr/local/msmtp/bin/msmtp"
set use_from=yes
set editor="vim"
set realname="clnking@163.com"

[root@86 ~]# echo "test mail 2016 content tom2" |mutt -s "test kkkkk" clnking@163.com
进入邮箱  收到邮件 ------》测试成功!

 

 脚本:

[root@86 alertscripts]# pwd
/usr/local/zabbix/share/zabbix/alertscripts
[root@86 alertscripts]# grep "AlertScriptsPath" /usr/local/zabbix/etc/zabbix_server.conf
### Option: AlertScriptsPath
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/usr/local/zabbix/share/zabbix/alertscripts
[root@86 alertscripts]# cat send_mail 
#!/bin/bash
echo "time:$(date +%m-%d-%H:%M:%S)" >>/var/log/zabbix/test.log
echo "p3_$3" >>/var/log/zabbix/test.log
echo "p2_$2" >>/var/log/zabbix/test.log
echo "p1_$1" >>/var/log/zabbix/test.log
/bin/echo "$3"| /usr/bin/mutt -s "$2" $1
echo "send end ..." >> /var/log/zabbix/test.log
echo "" >> /var/log/zabbix/test.log  

 #开始没成功不知道为什么后来做了以下操作,但当时还没成功 ,第二天成功了!

ln -s /usr/local/msmtp/bin/msmtp /usr/local/bin/msmtp
[root@86 alertscripts]# ps -ef|grep "zabbix_server"
zabbix    8734     1  0 03:00 ?        00:00:00 /usr/local/zabbix/sbin/zabbix_server#并没有跟-c /usr/local/zabbix/etc/zabbix_server.conf

 

# echo "加邮件符件"|mutt -s fujian text@test.com -a /data1/apps/mail.txt 
//邮件内容为"加邮件符件"
//主题为fujian
//如果需要发送多个人的话可以继续添加收件人
//mail.txt为附件(-a + 附件)
echo "加邮件符件"|mutt -s fujianxxxl@mail.com  xxg@163.com -a /data1/apps/mail.txt
// 同时发多人

 经过一段时间的学习,对邮箱报警进行补全:

首先zabbix安装的时候是建的用户(/sbin/nologin)是不能使用bash或其它shell来登录系统,所以要改成/bin/bash

# grep zabbix /etc/passwd
#zabbix:x:498:499:Zabbix Monitoring System:/var/lib/zabbix:/bin/bash
zabbix:x:498:499:Zabbix Monitoring System:/home/zabbix:/bin/bash

查看各个文件的配置:

查看msmtprc的配置文件:

/apps/msmtp/etc

# cat msmtprc 
account default
host smtp.139.com
port 25
from xx(发邮件的箱地址)@139.com
auth login
tls off
user 1352xxx(手机号因为我用的是139邮箱)
password xxxxxx(邮箱密码)
logfile /var/log/mmlog

查看muttrc的配置文件:

# cat /etc/Muttrc.local
# Local configuration for Mutt.
set sendmail="/apps/msmtp/bin/msmtp"
set from="135xxx@139.com"
set use_from = yes
set editor="vim"
set copy=no #这个也是个关建,一定不要忘记
set realname="zabbix of xxxxx.com"

查看zabbix的配置文件:

# grep -E "(AlertScriptsPath|ExternalScripts)" /etc/zabbix/zabbix_server.conf 
### Option: AlertScriptsPath
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/usr/lib/zabbix/alertscripts
### Option: ExternalScripts
# ExternalScripts=${datadir}/zabbix/externalscripts
ExternalScripts=/usr/lib/zabbix/externalscripts

查看脚本:

# cd /usr/lib/zabbix/alertscripts/
# ls
Key  send_mail
#这是我自己写的,目地是查的这个脚本执的过程,这样可以发现发题出在哪里,下面看看一下这两个文件内容:
echo "time:$(date +%m-%d-%H:%M:%S)" >>/var/log/zabbix/test.log
echo "$3" | /usr/bin/mutt -s "$2" "$1" >>/var/log/zabbix/test.log
echo "------------ok---------->$WHO" >>/var/log/zabbix/test.log
echo "p3_$3" >>/var/log/zabbix/test.log
echo "p2_$2" >>/var/log/zabbix/test.log
echo "p1_$1" >>/var/log/zabbix/test.log
#echo ----------> $1 >>/var/log/zabbix/test.log
#echo "send end ..." >> /var/log/zabbix/test.log
#echo "" >> /var/log/zabbix/test.log

# cat send_mail 
#!/bin/bash
set -x
A=$1
T=$2
C=$3
echo $A $T $C
/usr/lib/zabbix/alertscripts/Key "$A" "$T" "$C" >> /tmp/send.log 2>&1
#这个才是一个创新,可以把脚本的执行过程从定向到/etc/send.log中
还有一点就是脚本的权限的问题,以定要给脚本可执行的权限,对于其它用户!!!

以上是关于Zabbix利用msmtp+mutt发送邮件报警的主要内容,如果未能解决你的问题,请参考以下文章

zabbix使用msmtp&&mutt搭建邮件告警服务

ubuntu下使用mutt+msmtp发送邮件

利用 msmtp+mutt+shell来实现邮件告警

CentOS7 msmtp+mutt发送邮件

Linux_Centos使用mutt+msmtp发送邮件

采用Mutt及msmtp配置进行邮件发送