#!/bin/bash

_install() {

    yum install -y umonitor3-agent >/dev/null 2>&1

    local zk_port="2181"
    local zk_server_conf=""
    local zk_servers=`/usr/bin/python $BASEDIR/utils/GetApolloConf.py "regioninfo" "$REGION.zk"`
    for host in $zk_servers;
    do
        local zk_server_conf=$zk_server_conf"$host:$zk_port,"
    done
    local zk_server_conf=`echo $zk_server_conf|sed 's/,$//g'`

    # set zk address
    sed -i "/\"zk\":/s/.*/  \"zk\": \"${zk_server_conf}\",/" /opt/umonitor3/agent-update/etc/cfg.json
    sed -i "/\"zk\":/s/.*/  \"zk\": \"${zk_server_conf}\",/" /opt/umonitor3/agent/etc/cfg.json

    # start
    service agent-update start >/dev/null 2>&1
    /opt/umonitor3/umonitor3-control restart agent
    chkconfig agent-update on >/dev/null 2>&1
}

_check() {
    _check="PASS"

    service agent-update start >/dev/null 2>&1

    # 新机房建设，没有zk这个过不去
    local zk_servers=`/usr/bin/python $BASEDIR/utils/GetApolloConf.py "regioninfo" "$REGION.zk"`
    local skip_umonitor3=0
    for host in $zk_servers;
    do
        ping -c1 $host 1>/dev/null 2>&1
        if [[ $? -eq 0 ]];then
            local skip_umonitor3=1
            break
        fi
    done
    # Check If umontor3agent Is Running
    if [[ $skip_umonitor3 -eq 1 ]];then
        if [[ $(ps -ef | grep umonitor3|grep agent-update | grep -v grep | wc -l) -eq 0 ]]; then
            _check="FAIL"
            PRINT_ERR "[ERROR] umonitor3agent agent-update Not Running."
        fi
        # 起太慢了，跳过
        #if [[ $(ps -ef | grep umonitor3|grep "agent/bin/agent" | grep -v grep | wc -l) -eq 0 ]]; then
        #    _check="FAIL"
        #    PRINT_ERR "[ERROR] umontor3agent agent Not Running."
        #fi
    fi

    if [[ $(chkconfig 2>/dev/null |grep agent-update | grep -c :on) -eq 0 ]]; then
        _check="FAIL"
        PRINT_ERR "[ERROR] agent-update Not Enabled In Chkconfig List"
    fi

}
