#!/bin/bash

g10s_operFile="/opt/UltraPath/log/oper_log"

function print_event()
{
    local fileFlag=""

    if [ `echo "$1"|grep -E -c "[0-9]+"` -eq 0 ] || [ "$2" = "" ]
    then
        echo -e -n "\033[31m[ERROR:2][g10s][`date +%Y-%m-%d_%H:%M:%S`][$0,${FUNCNAME[1]},${BASH_LINENO[0]}]\033[0m ";echo "param error[$1,$2]"
        [ -f "${g10s_operFile}" ] && [ -w "${g10s_operFile}" ] && echo "[`date +%Y-%m-%d_%H:%M:%S`][ERROR:2][g10s][$0,${FUNCNAME[1]},${BASH_LINENO[0]}] param error[$1,$2]" >> "${g10s_operFile}"
        t100s_exit 1
    fi
    if [ "$3" != "" ]
    then
        fileFlag=$3
    else
        fileFlag=${0##*/}
        fileFlag=${fileFlag%%_*}
    fi
    [ -f "${g10s_operFile}" ] && [ -w "${g10s_operFile}" ] && echo "[`date +%Y-%m-%d_%H:%M:%S`][`id -u`][EVENT:$1][${fileFlag}][$0,${FUNCNAME[1]},${BASH_LINENO[0]}] $2">>"${g10s_operFile}"
    return 0
}

function print_error()
{
    local fileFlag=""
    local tmpDebugon=""

    if [ "$2" = "" ] || [ `echo "$1"|grep -E -c "[0-9]+"` -eq 0 ]
    then
        echo -e -n "\033[31m[ERROR:2][g10s][`date +%Y-%m-%d_%H:%M:%S`][$0,${FUNCNAME[1]},${BASH_LINENO[0]}]\033[0m ";echo "param error[$1,$2]"
        [ -f "${g10s_operFile}" ] && [ -w "${g10s_operFile}" ] && echo "[`date +%Y-%m-%d_%H:%M:%S`][ERROR:2][g10s][$0,${FUNCNAME[1]},${BASH_LINENO[0]}] param error[$1,$2]" >> "${g10s_operFile}"
        return 1
    fi
    if [ "$3" != "" ]
    then
        fileFlag=$3
    else
        fileFlag=${0##*/}
        fileFlag=${fileFlag%%_*}
    fi
   
    echo -e -n "\033[31m[ERROR:$1][${fileFlag}][`date +%Y-%m-%d_%H:%M:%S`][$0,${FUNCNAME[1]},${BASH_LINENO[0]}]\033[0m ";echo "$2"
   
    [ -f "${g10s_operFile}" ] && [ -w "${g10s_operFile}" ] && echo  "[`date +%Y-%m-%d_%H:%M:%S`][`id -u`][ERROR:$1][${fileFlag}][$0,${FUNCNAME[1]},${BASH_LINENO[0]}] $2">>"${g10s_operFile}"
    return 0
}

pgrep up_restart_warn 2>/dev/null
if [ $? -eq 0 ]
then
   killall up_restart_warn 2>/dev/null
   if [ $? -eq 0 ]
   then
       echo "succeed to stop up_restart_warn"
       print_event "1020" "The up_restart_warn are stopped successfully."
       exit
   fi
   print_error "1021" "fail to stop up_restart_warn."
   exit
fi
echo "up_restart_warn is not exist"
print_event "1022"  "up_restart_warn is not exist."
