#!/bin/bash

# 属于什么发行版Linux
system_type=$(cat /etc/os-release)

# ping 2次该ip, 超时设置10s(如果10s内没ping通，就停止ping)
# -c: 表示次数，1 为1次
# -w: 表示deadline, time out的时间，单位为秒，100为100秒。
# 使用 ping 命令测试主机是否可达
host="www.baidu.com"
ping -c 2 -w 10 $host &> /dev/null
# 检查 ping 命令的返回状态
if [ $? -eq 0 ]; then
    echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` ping network success!" >>/var/log/ping_network.log

    # 检测宝塔
    bt_process=`ps -ef|grep BT|grep -v grep|wc -l`
    if [ $bt_process -gt 0 ]; then
        echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` bt run success." >>/var/log/ping_network.log
    else
        echo -e "[ ERROR ] time: `date "+%Y-%m-%d %H:%M:%S"` bt run failed!" >>/var/log/ping_network.log
        /etc/init.d/bt restart
        bt_process_check=`ps -ef|grep BT|grep -v grep|wc -l`
        if [ $bt_process_check -gt 0 ]; then
            echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` bt restart success." >>/var/log/ping_network.log
        else
            echo -e "[ ERROR ] time: `date "+%Y-%m-%d %H:%M:%S"` bt is being upgraded!" >>/var/log/ping_network.log
            curl http://download.bt.cn/install/update6.sh|bash
            echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` bt upgraded success!" >>/var/log/ping_network.log
        fi
    fi

    # 检测 mysql
    if [ ! -x "/etc/init.d/mysqld" ]; then
        echo -e "[ ERROT ] time: `date "+%Y-%m-%d %H:%M:%S"` mysql not installed!" >>/var/log/ping_network.log
    else
        mysql_port=`netstat -nlt|grep 3306|wc -l`
        if [ $mysql_port -gt 0 ]; then
            echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` mysql run success." >>/var/log/ping_network.log
        else
            echo -e "[ ERROR ] time: `date "+%Y-%m-%d %H:%M:%S"` mysql run failed!" >>/var/log/ping_network.log
            /etc/init.d/mysqld start
            echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` mysql start success!" >>/var/log/ping_network.log
        fi
    fi

    # 检测 mongodb
    if [ ! -x "/etc/init.d/mongodb" ]; then
        echo -e "[ ERROT ] time: `date "+%Y-%m-%d %H:%M:%S"` mongo not installed!" >>/var/log/ping_network.log
    else
        mongo_port=`netstat -nlt|grep 27017|wc -l`
        if [ $mongo_port -gt 0 ]; then
            echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` mongo run success." >>/var/log/ping_network.log
        else
            echo -e "[ ERROR ] time: `date "+%Y-%m-%d %H:%M:%S"` mongo run failed!" >>/var/log/ping_network.log
            /etc/init.d/mongodb start
            echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` mongo start success!" >>/var/log/ping_network.log
        fi
    fi

    # 检测 nginx
    nginx_process=`ps -ef|grep nginx|grep -v grep|wc -l`
    if [ $nginx_process -gt 1 ]; then
        echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` nginx run success." >>/var/log/ping_network.log
    else
       echo -e "[ ERROR ] time: `date "+%Y-%m-%d %H:%M:%S"` nginx run failed!" >>/var/log/ping_network.log
       /etc/init.d/nginx start
       echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` nginx start success!" >>/var/log/ping_network.log
    fi

    # 检测 frpc
    frpc_process=`ps -ef|grep frpc|grep -v grep|wc -l`
    if [ $frpc_process -gt 1 ]; then
        echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` frpc run success." >>/var/log/ping_network.log
    else
       echo -e "[ ERROR ] time: `date "+%Y-%m-%d %H:%M:%S"` frpc run failed!" >>/var/log/ping_network.log
       /www/server/panel/pyenv/bin/python3.7 /www/server/panel/pyenv/bin/supervisorctl restart frpc-m:frpc-m_00
       /www/server/panel/pyenv/bin/python3.7 /www/server/panel/pyenv/bin/supervisorctl restart frpc-b:frpc-b_00
       sleep 4
       echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` frpc start success!" >>/var/log/ping_network.log
    fi

    # 检测项目record
    record_port=`netstat -nlt|grep 8000|wc -l`
    if [ $record_port -gt 0 ]; then
        echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` pecord run success." >>/var/log/ping_network.log
    else
        echo -e "[ ERROR ] time: `date "+%Y-%m-%d %H:%M:%S"` record run failed!" >>/var/log/ping_network.log
        /www/server/panel/pyenv/bin/python3.7 /www/server/panel/pyenv/bin/supervisorctl restart record:record_00
        echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` record start success!" >>/var/log/ping_network.log
    fi

    # 检测UDP组播服务
    multicast_port=`lsof -i:3333 | wc -l`
    if [ $multicast_port -gt 0 ]; then
        echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` udp multicast run success." >>/var/log/ping_network.log
    else
        echo -e "[ ERROR ] time: `date "+%Y-%m-%d %H:%M:%S"` udp multicast run failed." >>/var/log/ping_network.log
        systemctl restart multicast.service
        echo -e "[ OK ] time: `date "+%Y-%m-%d %H:%M:%S"` udp multicast start success." >>/var/log/ping_network.log
    fi
else
    echo -e "[ ERROR ] time: `date "+%Y-%m-%d %H:%M:%S"` ping network failed!" >>/var/log/ping_network.log
    if [[ $system_type == *CentOS* ]]; then
        systemctl restart network
    elif [[ $system_type == *Ubuntu* ]]; then
        netplan apply
    fi
    /www/server/panel/pyenv/bin/supervisorctl reload
    echo -e "[ INFO ] time: `date "+%Y-%m-%d %H:%M:%S"` trying to restart network and supervisor!" >>/var/log/ping_network.log
fi