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



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

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

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

接下来的步骤比较重要,默认情况下服务器使用的SMTP并没办法发送邮件到企业组织内部邮箱,对此需要配置企业组织的邮箱信息:
[root@gtlions 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@gtlions etc]# echo hello world |mail -s "test" gtlions@lai.com
[root@gtlions etc]# python dbcheck.py >dbcheck.txt;cat dbcheck.txt|mail -s dbcheck gtlions@lai.com
[root@gtlions etc]# python dbcheck.py >dbcheck.txt;mail -s dbcheck gtlions@lai.com<dhcheck.txt
[root@gtlions etc]# python dbcheck.py|mail -s dbcheck gtlions@lai.com

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

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

相关内容