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


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

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

停用相关server:
[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 已停

接下来的步骤比較重要。默认情况下server使用的SMTP并没办法发送邮件到企业组织内部邮箱,对此须要配置企业组织的邮箱信息:
[root@gtlions etc]# tail /etc/mail.rc
# For Linux and BSD, this should be set.
set bsdcompat
 
 
 
set from=[发送人邮箱地址]
set smtp=[smtpserver地址]
set smtp-auth-user=[邮箱username]
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-
posted @ 2016-03-01 09:11  blfshiye  阅读(351)  评论(0编辑  收藏  举报