检查邮件服务器的postfix-pop3-mysql服务状态的脚本

实时监控邮件服务器的状态需要脚本任务做支撑

#!/bin/bash

#Purpose: Check Postfix and Pop3 MySQL service status
#Author: andylhz
#Date: 2010-07-29


dt=`date '+%Y:%m:%d:%H:%M'`
mailer=root@test.cn
mailer2=138XXXXXXXX@139.com
host=mail.test.cn

#Dovecot服务的检查 存在则发邮件显示正常,否则就重启服务


dovecot=`ps aux |grep pop3-login | grep -v "grep" |awk {'print $11'} | uniq`

if [ "$dovecot" == "pop3-login" ]; then
       echo "$dt <$host> pop3 service is ok!" >/tmp/pop_ok;
       mail -s "pop3-OK(IP-154)"  $mailer < /tmp/pop_ok;
   else
       echo "$dt  <$host> pop3 service is down! will start!" >/tmp/pop_down;
       mail -s  "pop3-down(IP-)" -c $mailer2  $mailer < /tmp/pop_down;
       service  dovecot start;
fi

#postfix服务的检查 存在则发邮件显示正常,否则就重启服务


postfix=`ps aux |grep postfix | grep -v "grep" |grep -v "root"| awk {'print $1'} | uniq`

if [ "$postfix" == "postfix" ]; then

        echo "$dt <$host> postfix service is ok!" >/tmp/postfix_ok;
        mail  -s "postfix-OK(IP)"  $mailer < /tmp/postfix_ok;

   else
       echo "$dt <$host> postfix service is down! will start!">/tmp/postfix_down;
       mail -s "postfix-down(IP)" -c $mailer2 $mailer < /tmp/postfix_down;
       service  postfix start;

fi

#Mysql 的状态检查,看是否存在mysql?存在则发邮件报告正常,否则就重启服务


mysql=`ps aux |grep mysql |grep -v "grep" |awk {'print $1'} |grep mysql`

if [ "$mysql" == "mysql" ]; then

        echo "$dt <$host> mysql is ok! " >/tmp/mysql_ok;  
        mail -s "mysql-OK(IP)" $mailer < /tmp/mysql_ok;
     
  else
    
       echo "$dt <$host> mysql is down! will start!" > /tmp/mysql_down;
       mail -s "mysql-down(IP)" -c $mailer2 $mailer < /tmp/mysql_down;
        service  mysqld start;

fi
 

将上述保存成可执行的文件,添加cron计划任务中,每10分钟运行一次



     本文转自andylhz 51CTO博客,原文链接:http://blog.51cto.com/andylhz2009/494420,如需转载请自行联系原作者



上一篇:50款免费 PSD 名片设计模板源文件下载《下篇》


下一篇:16 个免费和收费的视频、多媒体 WordPress 主题