红联Linux门户
Linux帮助

Linux发送监控指标到内部邮箱

发布时间:2015-04-01 10:02:58来源:linux网站作者:gtlions

数据库的健康监控是个非常重要的工作,重要的指标\KPI监控结果会有专门的采集、监控、告警系统来做相关事情。
而一些不是非常重要的或者还在设计和调试阶段的相关指标,我只是想发送到我自己邮箱,本文就针对在Linux服务器上配置邮件发送监控数据的过程说明。


服务器版本为RHEL 6.2:
[pg@linux ~]# cat /etc/issue
Red Hat Enterprise Linux Server release 6.2 (Santiago)
Kernel \r on an \m


停用相关服务器:
[root@linux etc]# service sendmail stop
[root@linux etc]# service postfix stop                          
[root@linux etc]# service sendmail status
sendmail 已停
sm-client 已停
[root@linux etc]# service postfix status
master 已停


接下来的步骤比较重要,默认情况下服务器使用的SMTP并没办法发送邮件到企业组织内部邮箱,对此需要配置企业组织的邮箱信息:
[root@linux etc]# tail /etc/mail.rc
# For Linux and BSD, this should be set.
set bsdcompat

set from=[发送人邮箱地址]
set smtp=[smtp服务器地址]
set smtp-auth-user=[邮箱用户名]
set smtp-auth-password=[邮箱密码]
set smtp-auth=login


手工测试发送邮件:
[root@linux etc]# echo hello world |mail -s "test" linux@linux.com
[root@linux etc]# python dbcheck.py >dbcheck.txt;cat dbcheck.txt|mail -s dbcheck linux@linux.com
[root@linux etc]# python dbcheck.py >dbcheck.txt;mail -s dbcheck linux@linux.com<dhcheck.txt
[root@linux etc]# python dbcheck.py|mail -s dbcheck linux@linux.com


发送邮件shell脚本:
[pg@linux]$ cat /home/pg/PycharmProjects/dbcheck.sh
#!/bin/sh
. /etc/profile
. ~/.bash_profile
python /home/pg/PycharmProjects/dbcheck.py|mail -s "dbcheck `date +%F' '%T`" linux@linux.com


设置定时调度任务,CRON调用shell脚本:
[pg@linux]$ crontab -l
*/1 * * * * sh /home/pg/PycharmProjects/dbcheck.sh 1>>/home/pg/check.log 2>&1
-EOF-