如何添加sersync进程监控脚本

sersync监控脚本不但能对相关进程进行监控,还能够自动将停止的进程自动重启。下面就直接讲解一下sersync监控ing脚本的具体操作方法。

环境介绍

sersync安装在/root/shell/下

发送报警的邮件正文在/data/log/sersync/bady.txt

sersync 的日志位置放置在 /data/log/sersync/checksersync.log中

脚本会自动将最新的日志内容添加在邮件正文中

以上路径有操作者自己建立

脚本内容如下

#!/bin/bash
# usage:
#       1.将脚本命名为checkrsync.sh 放到/root/shell
#       2 chmod +x checkrsync.sh 给脚本赋予可执行权限
#       2.添加到计划任务
#       */1 * * * * /root/shell/checkrsync.sh

logfile='/data/log/sersync/checksersync.log';

function sersync_is_running(){
        threadnum=`ps aux|grep sersync2|grep -v grep -wc`;      #此处为监控sersync的监控手段,查找sersync2进程
        if [ "$threadnum" -eq '0' ];then
                echo '0';
        else
                echo '1';
        fi
        return;
}

function current_time(){
        if [ -z "$1" ];then
                format="%Y-%m-%d %H:%M:%S%Z";
        else
                format=$1;
        fi
        echo `date +"$format"`;
        return;
}

function logtofile(){
        echo $(current_time) $2>>$1;
}

function sersync_restart(){
        /root/shell/sersync2 -r -d -o /root/shell/confxml.xml >/dev/null 2>&1;
        sleep 3;

        threadnum=$(sersync_is_running);
        if [ $threadnum -eq '0' ]; then
                echo "0";
        else
                echo '1';
        fi
        return;
}

isrunning=$(sersync_is_running);

mailzt=`cat $logfile|tail -1 > /data/log/sersync/body.txt`

if [ "$isrunning" -eq '0' ];then
        logtofile $logfile "mail sersync service was died.";
        mail -s "test sersync service was died." [email protected] < /data/log/sersync/body.txt

        restart=$(sersync_restart);

        if [ $restart -eq '0' ];then
                logtofile $logfile "sersync service restart failed.";
        mail -s "test sersync service was died." [email protected] < /data/log/sersync/body.txt
        else
                logtofile $logfile "sersync service restart success.";
        mail -s "test sersync service restart success" [email protected] < /data/log/sersync/body.txt
        fi
else
        logtofile $logfile "sersync service is running.";
fi

exit 0;

 

 

test为安装了sersync的主机

[email protected]为发送报警的邮箱

为保证能够发送报警邮件别忘了把sendmail的服务打开

service  sendmail  start

你可能感兴趣的:(职场,休闲,sersync,监控脚本)