1,安装mail基础组件
安装mail的基础组件:
yum install sendmail -y service sendmail start chkconfig sendmail on yum install -y postfix service postfix start chkconfig postfix on
|
移除旧的组件:
yum remove mailx –y |
源码安装新版本组件:
wget http://nchc.dl.sourceforge.net/project/heirloom/heirloom-mailx/12.4/mailx-12.4.tar.bz2 wget http://www.linuxfromscratch.org/patches/blfs/7.6/mailx-12.4-openssl_1.0.0_build_fix-1.patch tar -xvf mailx-12.4.tar.bz2 mv mailx-12.4-openssl_1.0.0_build_fix-1.patch mailx-12.4 cd mailx-12.4 # 需要打补丁 patch -Np1 -i mailx-12.4-openssl_1.0.0_build_fix-1.patch make make install UCBINSTALL=/usr/bin/install # 做软链接 ln -s /usr/local/bin/mailx /bin/mail ln -s /etc/nail.rc /etc/mail.rc
# 查看安装路径 whereis mailx # 查看版本信息 mailx -V |
配置外部邮箱地址:
[root@hch_test_121_12 ~]# vim /etc/mail.rc set from=mchdba@139.com set smtp=smtp.139.com set smtp-auth-user=mchdba set smtp-auth-password=195xxxxmch set smtp-auth=login |
测试email发送:
echo "zabbix test mail 4" |mail -s "ys zabbix 4" mchdba@139.com 如下图01.png |
然后写一个sh脚本用来发送email:
[root@hch_test_121_12 alertscripts]# vim sendmail.sh
#!/bin/sh
echo "$3" | /bin/mail -s "$2" $1 |
测试sh脚本是否有用:
[root@hch_test_121_12 alertscripts]# bash -x sendmail.sh mchdba@139.com "ys zabbix 15" "zabbix test mail 15" + echo 'zabbix test mail 15' + /bin/mail -s 'ys zabbix 15' mchdba@139.com [root@hch_test_121_12 alertscripts]# |
Ok,测试成功,已经收到email信息了。
2,准备发email的脚本
查看zabbix_server的配置文件路径:
ps -eaf|grepzabbix_server |grep conf
[root@zabbix_serv_121_12 ~]# ps -eaf|grep zabbix_server |grep conf zabbix 6482 1 0 May17 ? 00:00:00 /usr/local/zabbix/sbin/zabbix_server -c /usr/local/zabbix/etc/zabbix_server.conf zabbix 6483 6482 0 May17 ? 00:08:06 /usr/local/zabbix/sbin/zabbix_server: configuration syncer [synced configuration in 0.214299 sec, idle 60 sec] zabbix 6484 6482 0 May17 ? 00:00:15 /usr/local/zabbix/sbin/zabbix_server: db watchdog [synced alerts config in 0.002126 sec, idle 60 sec] [root@zabbix_serv_121_12 ~]# |
在配置文件里面注册脚本路径:
# vim /usr/local/zabbix/etc/zabbix_server.conf # Default: AlertScriptsPath=/usr/local/zabbix/share/zabbix/alertscripts
|
编写发送email的脚本:
# 这里需要将/tmp/email.log赋予zabbix用户权限,不然会因为权限不足而调用报错。 echo """ >> /tmp/email.log chown -R zabbix:zabbix /tmp/email.log
[root@zabbix_serv_121_12 ~]# vim /usr/local/zabbix/share/zabbix/alertscripts/zabbix_sendmail.sh
#!/bin/sh echo "time:$(date +%Y-%m-%d-%H:%M:%S)" >> /tmp/email.log echo " p3_$3" >> /tmp/email.log echo " p2_$2" >> /tmp/email.log echo " p1_$1" >> /tmp/email.log # echo "zabbix test mail" |mail -s "zabbix" yyy@163.com echo "$3" | /bin/mail -s "$2" $1 echo " send end ...">>/tmp/email.log echo " ">>/tmp/email.log |
附带一些邮箱服务器的smtp配置信息:
# QQ enterprise |
PS:附带常用的配置端口信息,如02.png所示:
3,在zabbix界面配置
(1)先配置media type:Administration-> Media types -> Create mediatype -> ,添加各种信息,如下图05.png:
(2)在users里面添加,Administration –> Users-> Create user -> Media ->Add,设置media,如下图06.png:
(3)在actions里面选择配置,Configuratio -> Actions-> Create action –> Operations –> New ->Send to Users -> Add-> Admin(打勾选择一个用户),如08.png所示:
然后选择Select按钮,然后退出点击Add按钮,再点击update按钮,就完成了actions动作管理用户的操作,这个在actions界面就可以看到有提示send message to users:Admin…如下09.png:
(4)在actions中,配置触发这个actions动作的模版,Configuration -> Actions ->Conditions -> New condition -> Add,如下10.png所示:
总结:在actions中添加触发器这些actions的模版后,那么模版中的triggers只要满足条件了,都会触发这个action模版,而则合格actions模版就会调动里面的operation去调用zabbix_sendmail.sh脚本发送email了。
4,make问题记录
# 在64位的环境下,对mailx进行编译时,会出现以下错误: [root@localhost mailx-12.4]# make cc -DMAILRC='"/etc/nail.rc"' -DMAILSPOOL='"/var/mail"' -DSENDMAIL='"/usr/lib/sendmail"' -c openssl.c openssl.c:108: 错误:expected declaration specifiers or ‘...’ before ‘STACK’ openssl.c: In function ‘ssl_select_method’: openssl.c:215: 警告:赋值丢弃了指针目标类型的限定 openssl.c:217: 警告:赋值丢弃了指针目标类型的限定 openssl.c:219: 警告:赋值丢弃了指针目标类型的限定 openssl.c:223: 警告:赋值丢弃了指针目标类型的限定 openssl.c:226: 警告:赋值丢弃了指针目标类型的限定 openssl.c: In function ‘ssl_check_host’: openssl.c:311: 错误:‘STACK’ 未声明 (在此函数内第一次使用) |
解决:
# 网上有一个补丁,可以使用此补丁,来修正错误: wget http://www.linuxfromscratch.org/patches/blfs/7.6/mailx-12.4-openssl_1.0.0_build_fix-1.patch tar -xvf mailx-12.4.tar.bz2 mv mailx-12.4-openssl_1.0.0_build_fix-1.patch mailx-12.4 cd mailx-12.4 # 需要打补丁 patch -Np1 -i mailx-12.4-openssl_1.0.0_build_fix-1.patch
|