#!/bin/bash
#
# checking script for ilogtail
# 
#
# version: 0.3.0

gLogtailCheckingToolVersion="0.3.1"

gWarningCount=0
gErrorCount=0

function initCN()
{
    MAKESURE_INSTALLED_INFO='检查Logtail安装'
    MAKESURE_INSTALLED_SUGGESTION='请确认Logtail已经安装或尝试重装Logtail。'
    MAKESURE_INSTALLED_FILE_NOT_EXIST='不存在。'
    MAKESURE_INSTALLED_LINK_ERROR='无法找到ilogtail链接文件。'

    CHECK_LOGTAIL_STATUS_INFO='检查Logtail运行状态'
    CHECK_LOGTAIL_STATUS_STOPPED="Logtail已停止运行"
    CHECK_LOGTAIL_STATUS_SUGGESTION_START='尝试 [/etc/init.d/ilogtaild start] 以启动Logtail。'
    CHECK_LOGTAIL_STATUS_ABNORMAL="Logtail运行进程数异常, 它应该是2"
    CHECK_LOGTAIL_STATUS_SUGGESTION_RESTART='尝试 [/etc/init.d/ilogtaild stop] 以停止Logtail, 并使用 [/etc/init.d/ilogtaild start] 以启动Logtail.'

    CHECK_ALIUID_INFO="检查阿里云账号ID(s)"
    CHECK_ALIUID_NO_ID="没有配置阿里云账号ID。"
    CHECK_ALIUID_SUGGESTION='如果需要配置阿里云账号ID, 点击链接查看帮助: https://help.aliyun.com/document_detail/49007.html'

    CHECK_USER_DEFINED_INFO="检查用户自定义标识"
    CHECK_USER_DEFINED_NO_ID='没有配置用户自定义标识。'
    CHECK_USER_DEFINED_SUGGESTION='如果需要配置用户自定义标识, 点击链接查看帮助: https://help.aliyun.com/document_detail/28983.html'

    CHECK_USER_CONFIG_INFO="检查用户采集配置文件"
    CHECK_USER_CONFIG_NOT_EXIST='用户采集配置文件不存在。'
    CHECK_USER_CONFIG_SUGGESTION='请检查在SLS Project/Logstore中配置了采集配置并应用到了机器组。关于如何配置的帮助请点击: https://help.aliyun.com/document_detail/49010.html'

    CHECK_LOG_FILE_LINK='指定的文件是一个软链接'
    CHECK_LOG_FILE_INVALID_LINK='无效的链接文件'
    CHECK_LOG_FILE_NOT_CHANGED='文件已经在最近'
    CHECK_LOG_FILE_NOT_CHANGED_END='秒内没有变化'
    CHECK_LOG_FILE_NOT_EXIST='指定的文件不存在。'

    CHECK_LOG_FILE_INFO='检查指定的文件'
    CHECK_LOG_FILE_INCLUDED='检查指定文件 ['
    CHECK_LOG_FILE_INCLUDED_END='] 是否包含在采集配置中。' 
    CHECK_LOG_FILE_CONFIG_ERROR='采集配置格式错误。'
    CHECK_LOG_FILE_CONFIG_SUGGESTION='请确认你没有手动修改过 '
    CHECK_LOG_FILE_NO_MATCH='没有配置命中指定的文件。'
    CHECK_LOG_FILE_MULTI_MATCH_BEGIN='多个('
    CHECK_LOG_FILE_MULTI_MATCH=') 配置命中指定的文件。'
    CHECK_LOG_FILE_MULTI_MATCH_SUGGESTION='请确认每个文件应该只有一个配置匹配。如果多个配置命中，Logtail默认会随机选择一个。如果要采集多份请参考 https://help.aliyun.com/document_detail/404995.html'

    CHECK_NETWORK_INFO="检查网络状态"
    CHECK_NETWORK_NO_IP='找不到IP地址 '
    CHECK_NETWORK_NO_UUID='找不到UUID '
    CHECK_NETWORK_SSL_CLOSED="SSL 端口 443 可能被关闭, 请检查防火墙设置。"
    CHECK_NETWORK_SSL_SUGGESTION='你应该打开 443 端口用于 SSL.'
    CHECK_NETWORK_CONFIG_SERVER_INVALID="连接到 config server 错误: "
    CHECK_NETWORK_CONFIG_SERVER_SUGGESTION="检查你的网络配置, 包括链路、防火墙、安全组、DNS。"
    CHECK_NETWORK_NO_CURL="Logtail 检查工具需要 [curl] 但它没有安装。"
    CHECK_NETWORK_NO_CONFIG_SERVER="Logtail 没有配置 config server 地址。"
    CHECK_NETWORK_NO_CONFIG_SERVER_SUGGESTION=$MAKESURE_INSTALLED_SUGGESTION
    CHECK_NETWORK_USE_PROXY="Logtail 使用了代理配置"

    RESULT_STATISTICS_SUGGESTION='  如果日志采集仍然异常, 请查阅SLS官网文案获取更多帮助:'

    CONTINUE_WARING_INPUT="上一项检查有告警, 你想继续检查吗? (Y/n)"
    CONTINUE_ERROR_INPUT="上一项检查有错误, 你想继续检查吗? (Y/n)"

    MAKESURE_ECS="你的服务器是阿里云ECS并且和当前SLS Project属于同一账号吗 ? (y/N) "
    MAKESURE_ALIUID="你的SLS Project所有者账号ID在以上ID中吗 ? (y/N) "
    MAKESURE_ECS_SUGGESTION="请配置用户IDs(账号IDs), 帮助文档\n\033[13G[ https://www.alibabacloud.com/help/doc-detail/49007.htm ]"

    MAKESURE_REGION="请确认你的Project在这个地域 : {"
    MAKESURE_REGION_SUGGESTION="请重新安装Logtail或者重新创建Project\n\033[13G确保Project的地域和Logtail安装时选择的地域相同"

    MAKESURE_IP="请确认机器组中的IP与之相同 : {"
    MAKESURE_USER_DEFINED_ID=" 或机器组中的自定义标识在其中 : {"
    MAKESURE_IP_SUGGESTION="请修正机器组中的IP或自定义标识\n\033[13G保存配置并等待一分钟后再次检查"

    MAKESURE_HEARTBEAT_OK="请确认机器心跳正常并且机器组中的IP与之相同 : {"
    MAKESURE_HEARTBEAT_SUGGESTION="请重新运行脚本，选择第一项以检查机器组的心跳"

    MAKESURE_CONFIG_APPLIED="请确认已将采集配置应用到了机器组"
    MAKESURE_CONFIG_APPLIED_SUGGESTION="请将采集配置应用到机器组"

    MAKESURE_ALL_CORRECT="请确认以上所有检查项均已通过。如果问题持续，请复制所有输出并提交工单。 : (y/N) "
    MAKESURE_ALL_CORRECT_SUGGESTION="请复制所有输出并提交工单。\n\033[13G我们会排查问题。"

    CHOOSE_CHECK_TYPE="请选择需要的检查问题类型 : \n\t\t1. 机器组心跳异常. \n\t\t2. 机器组心跳正常, 但日志无法采集. \n\t类型 :  "

    PARAM_LOG_FILE_WARNING='[logFile] 必须是日志文件的完整路径.'
    PARAM_SUGGESTION="尝试 [./checkingtool.sh --help] 获取更多信息. "

    INPUT_LOG_FILE_PATH="请输入日志文件的完整路径 (eg. /var/log/nginx/access.log) : "
}

function initEN()
{
    MAKESURE_INSTALLED_INFO='Check Logtail install files'
    MAKESURE_INSTALLED_SUGGESTION='Make sure you have Logtail installed, or try install Logtail again.'
    MAKESURE_INSTALLED_FILE_NOT_EXIST='doesnt exist.'
    MAKESURE_INSTALLED_LINK_ERROR='Can`t find ilogtail link file.'

    CHECK_LOGTAIL_STATUS_INFO='Check Logtail running status'
    CHECK_LOGTAIL_STATUS_STOPPED="Logtail is stopped"
    CHECK_LOGTAIL_STATUS_SUGGESTION_START='Try [/etc/init.d/ilogtaild start] to start Logtail.'
    CHECK_LOGTAIL_STATUS_ABNORMAL="Logtail running process count is abnormal, it should be 2"
    CHECK_LOGTAIL_STATUS_SUGGESTION_RESTART='Try [/etc/init.d/ilogtaild stop] to stop Logtail, and use [/etc/init.d/ilogtaild start] to start Logtail.'

    CHECK_ALIUID_INFO="Check aliyun user id(s)"
    CHECK_ALIUID_NO_ID="No aliyun user id found."
    CHECK_ALIUID_SUGGESTION='If your config need aliuid, follow this link for more help: https://help.aliyun.com/document_detail/49007.html'

    CHECK_USER_DEFINED_INFO="Check user defined id"
    CHECK_USER_DEFINED_NO_ID='No user defined id found.'
    CHECK_USER_DEFINED_SUGGESTION='If your config need user defined id, follow this link for more help: https://help.aliyun.com/document_detail/28983.html'

    CHECK_USER_CONFIG_INFO="Check user log config file"
    CHECK_USER_CONFIG_NOT_EXIST='User log config file doesnt exist.'
    CHECK_USER_CONFIG_SUGGESTION='Please check collection config is created in SLS Project/Logstore and make sure you have applied config to your machine group'

    CHECK_LOG_FILE_LINK='Specific log file is a linked file'
    CHECK_LOG_FILE_INVALID_LINK='Invalid linked file'
    CHECK_LOG_FILE_NOT_CHANGED='Log file has not changed during last'
    CHECK_LOG_FILE_NOT_CHANGED_END='seconds.'
    CHECK_LOG_FILE_NOT_EXIST='Specific log file doesnt exist.'

    CHECK_LOG_FILE_INFO='Check specific log file'
    CHECK_LOG_FILE_INCLUDED='Check if specific log file ['
    CHECK_LOG_FILE_INCLUDED_END='] is included by user log config.' 
    CHECK_LOG_FILE_CONFIG_ERROR='User log config file format error.'
    CHECK_LOG_FILE_CONFIG_SUGGESTION='Make sure you have not manually modified '
    CHECK_LOG_FILE_NO_MATCH='No match config for your log file.'
    CHECK_LOG_FILE_MULTI_MATCH_BEGIN='Multi('
    CHECK_LOG_FILE_MULTI_MATCH=') configs for your log file.'
    CHECK_LOG_FILE_MULTI_MATCH_SUGGESTION='Please make sure every log file should only match one config. Logtail will choose a matched config randomly if the log file has multi configs. If multi configs are wanted, please refer to https://help.aliyun.com/document_detail/404995.html'

    CHECK_NETWORK_INFO="Check network status"
    CHECK_NETWORK_NO_IP='Can`t find ip address in '
    CHECK_NETWORK_NO_UUID='Can`t find UUID in '
    CHECK_NETWORK_SSL_CLOSED="SSL port 443 may be closed, please check your firewall."
    CHECK_NETWORK_SSL_SUGGESTION='You shold open 443 port for SSL.'
    CHECK_NETWORK_CONFIG_SERVER_INVALID="Connect to config server error: "
    CHECK_NETWORK_CONFIG_SERVER_SUGGESTION="Check your network config including link, firewall, security group and DNS."
    CHECK_NETWORK_NO_CURL="Logtail checking tool require [curl] but it's not installed."
    CHECK_NETWORK_NO_CONFIG_SERVER="Logtail has no config server address."
    CHECK_NETWORK_NO_CONFIG_SERVER_SUGGESTION=$MAKESURE_INSTALLED_SUGGESTION
    CHECK_NETWORK_USE_PROXY="Logtail use proxy config"

    RESULT_STATISTICS_SUGGESTION='  If your log collection is abnormal all the same, please view aliyun log service documents for more help:'

    MAKESURE_ECS="Is your server Alibaba Cloud ECS and belong to the same account of the current SLS Project ? (y/N) "
    MAKESURE_ALIUID="Is your project owner account ID among the above IDs ? (y/N) "
    MAKESURE_ECS_SUGGESTION="please configure user IDs (account IDs), this doc will help\n\033[13G[ https://www.alibabacloud.com/help/doc-detail/49007.htm ]"

    MAKESURE_REGION="please make sure your project is in this region : {"
    MAKESURE_REGION_SUGGESTION="please reinstall your Logtail or recreate your project to\n\033[13Gmake sure your project is located in the same region with your Logtail"

    MAKESURE_IP="please make sure your machine group's ip is same with : {"
    MAKESURE_USER_DEFINED_ID=" or your machine group's userdefined-id is in : {"
    MAKESURE_IP_SUGGESTION="please correct your machine group's ip or userdefined-id,\n\033[13Gsave the configuration, wait 1 minute and check again"

    MAKESURE_HEARTBEAT_OK="please make sure your machine's heartbeat is ok and machine group's ip is same with : {"
    MAKESURE_HEARTBEAT_SUGGESTION="please rerun the script and choose type 1 to check machine group's heartbeat"

    MAKESURE_CONFIG_APPLIED="please make sure you have applied collection config to the machine group"
    MAKESURE_CONFIG_APPLIED_SUGGESTION="please apply collection config to the machine group"

    MAKESURE_ALL_CORRECT="please make sure all the check items above have passed. If the problem persists, please copy all the outputs and submit a ticket in the ticket system. : (y/N) "
    MAKESURE_ALL_CORRECT_SUGGESTION="Please copy all the outputs and submit a ticket in the ticket system.\n\033[13GWe will look into the problem."

    CHOOSE_CHECK_TYPE="please choose which type you want to check : \n\t\t1. MachineGroup heartbeat fail. \n\t\t2. MachineGroup heartbeat is ok, but log files have not been collected. \n\tType :  "

    PARAM_LOG_FILE_WARNING='[logFile] needs log file name with full path.'
    PARAM_SUGGESTION="Try [./checkingtool.sh --help] for more information. "

    INPUT_LOG_FILE_PATH="please input your log file's full path (eg. /var/log/nginx/access.log) : "
}

function logInfo()
{
    echo -e '[Info]:    ' $*
}

function logInfoEnd()
{
    logInfo $*
    exit
}

function logOK()
{
    echo -en '[Info]:    ' $*
    echo -en '\033[80G \033[32m' && echo [ OK ]
    echo -en '\033[0m'
}

function logWarning()
{
    gWarningCount=`expr $gWarningCount + 1`
    echo -en '[Warning]: ' $*
    echo -en '\033[80G \033[33m' && echo [ Warning ]
    echo -en '\033[0m'
}

function logWarningEnd()
{
    logWarning $*
    exit 1
}

function logError()
{
    gErrorCount=`expr $gErrorCount + 1`
    echo -en '[Error]:   ' $*
    echo -en '\033[80G \033[31m' && echo [ Error ]
    echo -en '\033[0m'
}

function logErrorEnd()
{
    logError $*
    exit 1
}

function logSuggestion()
{
    echo -en '\033[96m' 
    echo -e '[Suggestion]: ' $*
    echo -en '\033[0m'
}

function logInput()
{
    echo -en '\033[40;96m'
    echo -ne '[Input]: ' $*
    echo -en '\033[0m'
}


function checkSystemSupport()
{
    logInfo 'Check system support'
    local systemBit=`getconf LONG_BIT`
    if [ $systemBit == 64 ] 
    then
        logOK 'Check system support OK.'
    else
        logError 'Logtail dont support ' $systemBit ' bit system.'
        exit
    fi 
    echo
}

gLogtailBinDir="/usr/local/ilogtail"

function findLogtailInstallLocation()
{
    # check cmd rpm
    if hash rpm 2>/dev/null;then
       # t4
       t4Flag=`rpm -qa | grep ali-sls-ilogtail | grep t4`
       if [ $? == 0 ]
       then
       gLogtailBinDir="/opt/taobao/install/ilogtail"
       return
       fi

       # ant
       antFlag=`rpm -qa | grep ali-sls-ilogtail | grep ant`
       if [ $? == 0 ]
       then
       gLogtailBinDir="/opt/taobao/install/ilogtail"
       return
       fi
    else
        logWarning 'No command rpm, use default logtail install dir:' $gLogtailBinDir
    fi

}

findLogtailInstallLocation
#logInfo 'Logtail install dir: '$gLogtailBinDir
cd $gLogtailBinDir

gLogtailBinFile="ilogtail"
gLogtailInstallFiles=(
"ilogtail_config.json"
"/etc/init.d/ilogtaild"
"ilogtail"
)

function checkInstallFiles()
{
    logInfo $MAKESURE_INSTALLED_INFO
    for installFile in ${gLogtailInstallFiles[@]}
    do
        if [ -f $installFile ]
        then
            logOK 'Install file: ' $installFile 'exists.'
        else
            logError 'Install file: ' $installFile $MAKESURE_INSTALLED_FILE_NOT_EXIST
            logSuggestion $MAKESURE_INSTALLED_SUGGESTION
        fi
    done
    
    logtailRealBinPath=`ls -l ilogtail | grep -oP '/[0-9a-zA-Z\./=-_]*' 2>/dev/null`
    logtailVersion=`echo $logtailRealBinPath | grep -oP '[0-9\.]*' 2>/dev/null`
    #echo $logtailRealBinPath $logtailVersion
    if [ $? -ne 0 ]
    then
        logError $MAKESURE_INSTALLED_LINK_ERROR
        logSuggestion $MAKESURE_INSTALLED_SUGGESTION
        echo
        return 1
    fi
    if [ -f $logtailRealBinPath ]
    then
        logOK 'Bin file: ' $logtailRealBinPath 'exists.'
        logOK 'Logtail version : ' $logtailVersion
    else
        logError 'Logtail bin file: ' $logtailRealBinPath $MAKESURE_INSTALLED_FILE_NOT_EXIST
        logSuggestion $MAKESURE_INSTALLED_SUGGESTION
    fi
    echo
}

function checkLogtailStatus()
{
    logInfo $CHECK_LOGTAIL_STATUS_INFO
    PPIDS_RAW=($(pgrep  -l -x -P 1,0 "ilogtail" | awk '{print $1}'))
    PPIDS_UPDATE=($(pgrep  -l -x -P 1,0 "ilogtail_[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}" | awk '{print $1}'))
    PPIDS=(${PPIDS_RAW[@]} ${PPIDS_UPDATE[@]})
    CPIDS=()
    for ppid in ${PPIDS[*]};
    do
        CPIDS=(${CPIDS[*]} $(pgrep -l -x -P "$ppid" "ilogtail.*" | awk '{print $1}'))
    done
    if [ ${#PPIDS[*]} == 0 ]
    then
        logError $CHECK_LOGTAIL_STATUS_STOPPED
        logSuggestion $CHECK_LOGTAIL_STATUS_SUGGESTION_START
        RETVAL=1
        return $RETVAL
    fi
    if [ ${#PPIDS[*]} != 1 -o ${#CPIDS[*]} != 1 ]
    then
        echo logtail ppid: ${PPIDS[*]}, cpid: ${CPIDS[*]}
        logError $CHECK_LOGTAIL_STATUS_ABNORMAL
        logSuggestion $CHECK_LOGTAIL_STATUS_SUGGESTION_RESTART
        RETVAL=2
        return $RETVAL
    else
        logOK "Logtail is runnings."
        RETVAL=0
        return $RETVAL
    fi
}

g_aliuidDir="/etc/ilogtail/users"

aliuids=()

function checkAliuid()
{
    logInfo $CHECK_ALIUID_INFO
    aliuids=(`ls $g_aliuidDir`)
    if [ ${#aliuids[@]} == 0 ] 
    then
        # logInfo  $CHECK_ALIUID_NO_ID'
        logWarning $CHECK_ALIUID_NO_ID
        logSuggestion $CHECK_ALIUID_SUGGESTION
    fi
    for aliuid in ${aliuids[@]}
    do
        logOK "aliyun user id : " $aliuid '.'
    done
    echo
}

gUserDefinedIdFile="/etc/ilogtail/user_defined_id"
gUserDefinedIdDetail=""

if [ -f $gUserDefinedIdFile ]
then
    gUserDefinedIdDetail=`cat $gUserDefinedIdFile`
fi

function checkUserDefinedId()
{
    logInfo $CHECK_USER_DEFINED_INFO
    if [ -f $gUserDefinedIdFile ]
    then
        logOK 'User defined id is :' `cat $gUserDefinedIdFile` '.'
    else
        # logInfo $CHECK_USER_DEFINED_NO_ID
        logWarning $CHECK_USER_DEFINED_NO_ID $gUserDefinedIdFile
        logSuggestion $CHECK_USER_DEFINED_SUGGESTION
    fi    
}

gUserConfigFile="user_log_config.json"

function checkUserConfigFile()
{
    logInfo $CHECK_USER_CONFIG_INFO
    if [ -f $gUserConfigFile ]
    then
        logOK 'User config file exists, configs :'
        grep "##" /usr/local/ilogtail/user_log_config.json | awk '{printf $1}'
        echo ""
        return 0
    else
        logWarning $CHECK_USER_CONFIG_NOT_EXIST $gUserConfigFile
        logSuggestion $CHECK_USER_CONFIG_SUGGESTION
        echo
        return 1
    fi    
    echo
}


function checkSpecificLogFileStatus()
{
    if [ -L "$1" ]
    then 
        logWarning $CHECK_LOG_FILE_LINK
        if [ -f "$1" ]
        then
            logInfo 'Valid linked file'
        else
            logWarning $CHECK_LOG_FILE_INVALID_LINK
        fi
        return
    fi
    if [ -f "$1" ]
    then
        logOK 'Specific log file exists.'
        # check file's update time
        logFileModifyTime=`stat -c %Y "$1"`
        nowTime=`date +%s`
        # echo $logFileModifyTime $nowTime
        deltaTime=`expr $nowTime - $logFileModifyTime`
        # echo $deltaTime
        if [ $deltaTime -gt '900' ]
        then
            logWarning $CHECK_LOG_FILE_NOT_CHANGED $deltaTime $CHECK_LOG_FILE_NOT_CHANGED_END
        fi
        return 0
    else
        logWarning $CHECK_LOG_FILE_NOT_EXIST
        return 1
    fi   
}

function checkSpecificLogFile()
{
    local specificFilePath="$1"
    if [ ${specificFilePath::1} != '/' ]
    then
        logErrorEnd $PARAM_LOG_FILE_WARNING
    fi
    # Stop shell wildcard character expansion
    set -f
    logInfo $CHECK_LOG_FILE_INFO
    logInfo $CHECK_LOG_FILE_INCLUDED $specificFilePath $CHECK_LOG_FILE_INCLUDED_END
    checkSpecificLogFileStatus "$specificFilePath"
    if [ -f $gUserConfigFile ]
    then
        # find key words: file_pattern, log_path
        logfileNameAll=`awk -F'[",]' '/file_pattern/{printf $4","}' $gUserConfigFile 2>/dev/null`
        logfilePathAll=`awk -F'[",]' '/log_path/{printf $4","}' $gUserConfigFile 2>/dev/null`
        logstoreAll=`awk -F'[",]' '/category/{printf $4","}' $gUserConfigFile 2>/dev/null`
        logProjectAll=`awk -F'[",]' '/project_name/{printf $4","}' $gUserConfigFile 2>/dev/null`
        # split
        OLD_IFS="$IFS" 
        IFS="," 
        logfileNames=($logfileNameAll)
        logfilePaths=($logfilePathAll) 
        logstores=($logstoreAll)
        logProjects=($logProjectAll) 
        IFS="$OLD_IFS"

        if [ ${#logfileNames[*]} -ne ${#logfilePaths[*]} ]
        then
            logError $CHECK_LOG_FILE_CONFIG_ERROR
            logSuggestion $CHECK_LOG_FILE_CONFIG_SUGGESTION $gUserConfigFile
            return 1
        fi

        specificFileName=${specificFilePath##*/}
        specificFileDir=${specificFilePath%/*}
        # echo $specificFileName $specificFileDir
        # check every config
        matchCount=0
        for((i=0;i<${#logfileNames[*]};i++));  
        do   
            # echo 'for' ${logfileNames[$i]} ${logfilePaths[$i]}
            tmpFileName=${logfileNames[$i]}
            tmpFilePath=${logfilePaths[$i]}
            tmpFilePath=${tmpFilePath%/}
            tmpFilePath=`expr $tmpFilePath'/*'`
            # echo $tmpFilePath

            if [[ "$specificFileDir" = $tmpFilePath  || "$specificFileDir" = ${tmpFilePath%/*} ]];then 
                #echo 'Dir matched.'
                if [[ "$specificFileName" = $tmpFileName ]];then 

                    logOK 'Matched config found:'
                    logInfo '[Project] ->' ${logProjects[$i]}
                    logInfo '[Logstore] ->' ${logstores[$i]}
                    logInfo '[LogPath] ->' ${logfilePaths[$i]}
                    logInfo '[FilePattern] ->' ${logfileNames[$i]}

                    matchCount=`expr $matchCount + 1`
                fi
            fi
        done  

        if [ $matchCount = 0 ] 
        then
            logError $CHECK_LOG_FILE_NO_MATCH
            logSuggestion $CHECK_USER_CONFIG_SUGGESTION
        elif [ $matchCount -gt 1 ]
        then
            logWarning $CHECK_LOG_FILE_MULTI_MATCH_BEGIN $matchCount $CHECK_LOG_FILE_MULTI_MATCH
            logSuggestion $CHECK_LOG_FILE_MULTI_MATCH_SUGGESTION
        fi
        
        # echo ${#logfileNames[*]} ${#logfilePaths[*]} $logfileNames $logfilePaths
    fi
    echo
    # Start shell wildcard character expansion
    set +f
}

gAppInfoFile="app_info.json"
gLogtailIpConfig=`awk -F'[",]' '/ip/{printf $4}' $gAppInfoFile 2>/dev/null`

function checkNetwork()
{
    logInfo $CHECK_NETWORK_INFO
    # logInfo "Local address:"
    # LC_ALL=C ifconfig|grep "inet "
    local logtailUUIDConfig=`awk -F'[",]' '/UUID/{printf $4}' $gAppInfoFile 2>/dev/null`


    if [ $gLogtailIpConfig ]
    then
        logInfo 'Logtail is using ip: ' $gLogtailIpConfig
    else
        logWarning $CHECK_NETWORK_NO_IP $gAppInfoFile
        # logSuggestion 'Make sure your logtail is running.'
    fi


    if [ $logtailUUIDConfig ]
    then
        logInfo 'Logtail is using UUID: ' $logtailUUIDConfig
    else
        logWarning $CHECK_NETWORK_NO_UUID
        # logSuggestion 'Make sure your logtail is running.'
    fi

    # check port 443 status
    logInfo "Check SSL status"
    sslStatus=`sudo service iptables status | grep 'DROP ' | grep 'tcp dpt:443'`
    if [ $? -ne 0 ] 
    then
        logOK "SSL status OK."
    else
        logWarning $CHECK_NETWORK_SSL_CLOSED
        logSuggestion $CHECK_NETWORK_SSL_SUGGESTION
    fi

    # check is proxy configged
    proxyFlag=`grep -oP config.sls-proxy.aliyun-inc.com ilogtail_config.json`
    # echo proxyFlag  ${#proxyFlag[@]} $proxyFlag
    if [ $? -ne 0 ]
    then
        logInfo "Check logtail config server"
        # find config_server_address
        configServerLink=`grep -oP '"config_server_address"[\ ]*:[\ ]*"http://[0-9a-zA-Z\./=-]*.com"' ilogtail_config.json | grep -oP 'http://[0-9a-zA-Z\./=-]*.com'`
        if [ $? == 0 ] 
        then
            logInfo "config server address: " $configServerLink 
            if command -v curl &>/dev/null;then
                #echo curl --connect-timeout 10 -m 20 $configServerLink
                configServerLink="http${configServerLink#*:}"
                curl --connect-timeout 10 -m 20 $configServerLink > '/tmp/logtailchecktooltmp.tmp' 2>/dev/null
                curlResult=`grep 'curl' /tmp/logtailchecktooltmp.tmp`
                if [ $? == 0 ]
                then
                    logError $CHECK_NETWORK_CONFIG_SERVER_INVALID $curlResult
                    logSuggestion $CHECK_NETWORK_CONFIG_SERVER_SUGGESTION
                else
                    logOK "Logtail config server HTTP OK"
                fi
                rm '/tmp/logtailchecktooltmp.tmp'

                configServerSslLink="https${configServerLink#*:}"
                curl --connect-timeout 10 -m 20 $configServerSslLink > '/tmp/logtailchecktooltmp.tmp' 2>/dev/null
                curlResult=`grep 'curl' /tmp/logtailchecktooltmp.tmp`
                if [ $? == 0 ]
                then
                    logError $CHECK_NETWORK_CONFIG_SERVER_INVALID $curlResult
                    logSuggestion $CHECK_NETWORK_CONFIG_SERVER_SUGGESTION
                else
                    logOK "Logtail config server HTTPS OK"
                fi
                rm '/tmp/logtailchecktooltmp.tmp'
            else
                logWarning $CHECK_NETWORK_NO_CURL
            fi
        else
            logError $CHECK_NETWORK_NO_CONFIG_SERVER
            logSuggestion $CHECK_NETWORK_NO_CONFIG_SERVER_SUGGESTION
        fi
    else
        logWarning $CHECK_NETWORK_USE_PROXY
    fi
    echo
}

function checkProfileStatus()
{
    logInfo 'Last logtail log'
    tail -n 5 ilogtail.LOG
    echo
    
    logInfo 'Logtail app info : '
    cat $gAppInfoFile 

    logInfo 'Check profile status'
    if [ -f snapshot/ilogtail_profile.LOG ]
    then
        logInfo 'ilogtail_profile.LOG'
        stat snapshot/ilogtail_profile.LOG | grep Modify
        return 0
    fi

    if [ -f logtail_profile_snapshot ]
    then
        logInfo 'logtail_profile_snapshot'
        stat logtail_profile_snapshot | grep Modify
        return 0
    fi
    return 1
}

function showVersion()
{
    logInfo 'Logtail checking tool version : '$gLogtailCheckingToolVersion
}

function printHelp()
{
    echo
    echo 'VERSION'
    echo '    Logtail checking tool version : '$gLogtailCheckingToolVersion
    echo 
    echo 'SYNOPSIS'
    echo '    checkingtool.sh [OPTION]'
    echo 
    echo 'DESCRIPTION'
    echo '    Check logtail environment.'
    
    echo 
    
    echo 'OPTIONS'
    echo '    --logFile <LogFilePath>   : check if specific log file <LogFilePath> is included by user config and logtail environment status.'
    echo '    --envOnly                 : only check logtail environment status.'
    echo '    --logFileOnly             : only check if specific log file <LogFilePath> is included by user config'
    echo
    echo 'RETURN'
    echo '    Error count checkingtool found.'
    echo 
    echo 
    echo 'For example:'
    
    echo '   use [./checkingtool] to check iteractively.'
    echo '   use [./checkingtool --logFile /user/local/myapp/error.log] to check if the log file is included by your config and logtail environment status.'
    echo

    echo '  If all outputs are OK, please view aliyun log service documents for more help:'
    echo -e '   \033[4m\033[33mhttps://help.aliyun.com/document_detail/49911.html'
    echo -en '\033[0m'
}


function resultStatistics()
{
    local retCode=0
    echo 'Check complete.'

    if [ $gErrorCount == 0 ] 
    then
        if [ $gWarningCount == 0 ] 
        then
            echo -e '\033[32m' && echo ' All checking items are [ OK ]. '
            echo -en '\033[0m'
        else
            echo -e '\033[33m' && echo ' [ ' $gWarningCount ' ] warning(s) found.'
            echo -en '\033[0m'
            retCode=1
        fi

        echo $RESULT_STATISTICS_SUGGESTION
        echo -e ' \033[4m\033[33mhttps://help.aliyun.com/document_detail/49911.html'
        echo -en '\033[0m' 
    else
        echo -e '\033[33m' && echo ' [ ' $gWarningCount ' ] warning(s) found.'
        echo -en '\033[0m'
        echo -e '\033[31m' && echo ' [ ' $gErrorCount ' ] error(s) found.'
        echo -en '\033[0m'
        retCode=2
    fi 
    
    echo  
    return $retCode
}

function continueOrNot()
{
    if [ $gErrorCount == 0 ]
    then
        if [ $gWarningCount -gt 0 ]
        then
            logInput $CONTINUE_WARING_INPUT
        else
            return
        fi
    else
        logInput $CONTINUE_ERROR_INPUT
    fi
    gWarningCount=0
    gErrorCount=0
    read inp
    inp=`echo "$inp" | tr '[:upper:]' '[:lower:]'`
    if [[ $inp == "" || $inp == "y" ]]
    then 
        return
    else
        exit 1
    fi

}

showVersion

function checkAll()
{
    checkSystemSupport
    checkInstallFiles
    checkLogtailStatus
    checkAliuid
    checkUserDefinedId
    checkUserConfigFile
    checkNetwork
    checkProfileStatus
    resultStatistics
    return $?
}

function makesureECS()
{
    logInput $MAKESURE_ECS
    read inp
    inp=`echo "$inp" | tr '[:upper:]' '[:lower:]'`
    if [[ $inp != "y" ]]
    then 
        checkAliuid

        if [ ${#aliuids[@]} == 0 ]
        then
            logErrorEnd $MAKESURE_ECS_SUGGESTION
        else
            logInput $MAKESURE_ALIUID
            read inp
            inp=`echo "$inp" | tr '[:upper:]' '[:lower:]'`
            if [[ $inp == "y" ]]
            then 
                return
            else 
                logErrorEnd $MAKESURE_ECS_SUGGESTION
            fi
        fi

    else
        return
    fi
}


function makesureRegion()
{
    region=`cat ilogtail_config.json | grep "cluster" | awk -F'[",]' '{ print $4; }'`
    logInput $MAKESURE_REGION $region "}  (y/N) : "
    read inp
    inp=`echo "$inp" | tr '[:upper:]' '[:lower:]'`
    if [[ $inp == "y" ]]
    then 
        return
    else
        logErrorEnd $MAKESURE_REGION_SUGGESTION
        exit 1
    fi

}

function makesureIP()
{
    logInput $MAKESURE_IP $gLogtailIpConfig "} " $MAKESURE_USER_DEFINED_ID  $gUserDefinedIdDetail "} (y/N) : "
    read inp
    inp=`echo "$inp" | tr '[:upper:]' '[:lower:]'`
    if [[ $inp == "y" ]]
    then 
        return
    else
        logErrorEnd $MAKESURE_IP_SUGGESTION
        exit 1
    fi
}

function makesureMachineGroupHeartBeat()
{
    logInput $MAKESURE_HEARTBEAT_OK $gLogtailIpConfig "} (y/N) : "
    read inp
    inp=`echo "$inp" | tr '[:upper:]' '[:lower:]'`
    if [[ $inp == "y" ]]
    then 
        return
    else
        logErrorEnd $MAKESURE_HEARTBEAT_SUGGESTION
        exit 1
    fi
}

function makesureConfigApplied()
{
    logInput $MAKESURE_CONFIG_APPLIED " (y/N) : "
    read inp
    inp=`echo "$inp" | tr '[:upper:]' '[:lower:]'`
    if [[ $inp == "y" ]]
    then 
        return
    else
        logErrorEnd $MAKESURE_CONFIG_APPLIED_SUGGESTION
        exit 1
    fi
}


function checkMachineGroupHeartBeat()
{
    checkInstallFiles
    continueOrNot
    checkLogtailStatus
    continueOrNot
    checkNetwork
    continueOrNot
    makesureECS
    makesureRegion
    makesureIP

    logInput $MAKESURE_ALL_CORRECT
    read inp
    inp=`echo "$inp" | tr '[:upper:]' '[:lower:]'`
    if [[ $inp == "y" ]]
    then 
        checkAll
        local checkRes=$?
        if [ $checkRes -eq 0 ]; then
            logInfoEnd $MAKESURE_ALL_CORRECT_SUGGESTION
        elif [ $checkRes -eq 1 ]; then
            logWarningEnd $MAKESURE_ALL_CORRECT_SUGGESTION
        else
            logErrorEnd $MAKESURE_ALL_CORRECT_SUGGESTION
        fi
    else
        exit
    fi
}


function checkLogFile()
{
    makesureMachineGroupHeartBeat
    makesureConfigApplied

    logInput $INPUT_LOG_FILE_PATH
    read logPath
    checkSpecificLogFile "$logPath"
    continueOrNot

    logInput $MAKESURE_ALL_CORRECT
    read inp
    inp=`echo "$inp" | tr '[:upper:]' '[:lower:]'`
    if [[ $inp == "y" ]]
    then
        checkAll
        local checkRes=$?
        if [ $checkRes -eq 0 ]; then
            logInfoEnd $MAKESURE_ALL_CORRECT_SUGGESTION
        elif [ $checkRes -eq 1 ]; then
            logWarningEnd $MAKESURE_ALL_CORRECT_SUGGESTION
        else
            logErrorEnd $MAKESURE_ALL_CORRECT_SUGGESTION
        fi
    else
        exit
    fi

}

CHOOSE_LANGUAGE="Please choose language 请选择语言 : \n\t\t1. 中文. \n\t\t2. English. \n\t语言 :  "
function interactiveCheck()
{
    logInput $CHOOSE_LANGUAGE
    read inp
    inp=`echo "$inp" | tr '[:upper:]' '[:lower:]'`
    case $inp in
    1)
        initCN
    ;;
    2)
        initEN
    ;;
    *)
        logWarning "Unknown input. "
        echo 
    esac

    logInput $CHOOSE_CHECK_TYPE
    read inp
    inp=`echo "$inp" | tr '[:upper:]' '[:lower:]'`
    case $inp in
    1)
        checkMachineGroupHeartBeat
    ;;
    2)
        checkLogFile
    ;;
    *)
        logWarning "Unknown input. "
        echo 
        exit 1
    esac
}

if [ ${LANG::5} == 'zh_CN' ]
then
    initCN
else
    initEN
fi

if [ $# == 0 ]
then
    interactiveCheck
else
    case "$1" in
    --logFile)
    if [ $# -ne 2 ] 
    then
        logWarning $PARAM_LOG_FILE_WARNING
        logSuggestion $PARAM_SUGGESTION
        exit 1
    else
        checkSpecificLogFile "$2"
        checkAll
    fi
    ;;
    --envOnly)
    checkAll
    ;;
    --logFileOnly)
    if [ $# -ne 2 ]
    then
        logWarning $PARAM_LOG_FILE_WARNING
        logSuggestion $PARAM_SUGGESTION
        exit 1
    else
        checkSpecificLogFile "$2"
        resultStatistics
    fi
    ;;
    --help)
    printHelp
    ;;
    *)
    logWarning "Unknow param, " $PARAM_SUGGESTION
    echo 
    ;;
    esac
fi

exit $gErrorCount
