#!/bin/bash
#=======================================================================================
#               FILE:    Report.sh
#              USAGE:    bash Report.sh
#        DESCRIPTION:    Copy bash style guide and coding standard .
#                        The default copy example is the current text .
#                        Dont.t find text on other directories .
#            OPTIONS:    see fuction 'usage' below 
#       REQUIREMENTS:    ---
#               BUGS:    ---
#              NOTES:    --- 
#             AUTHOR:    Dr.-Vision. Simple(sp).
#            COMPANY:    China
#            VERSION:    1.0
#            CREATED:    01.22.2016 - 13:15:30
#           REVISION:    01.23.2016
#            PROJECT:    XXX
#          COPYRITHT:    Copyright(c)2002-2020 Shell, All Rights Reserved
#=======================================================================================
#If required, additional information has to be added (e.g copyright note,project assignment).
HOST=`hostname`
TODAY=`date +%Y-%m-%d`
#DATE_TIME=``
#DETECT_PATH="/var/log/xxx"
#DETECT_LIST="detect.log detectorigin.log digRun.log"
#FC_CACHE_LOG="xxx.log.01012093SV.CHN-GX-9-3SV.20160126201857.gz"
#DATA_FILE="${DATA_PATH}/detectorigin.log"
#DATA=`awk '!a[$2]++' ${DATA_FILE}|grep -E "No_Detect|ip_bad|ip_down"|awk '{print $2"\t"$4}'`
#DATA=`awk '!a[$2]++' ${DATA_FILE}|grep -v "200"|awk '{print $2"\t"$4}'`
#DATA_LOG=`awk '!a[$2]++' ${DATA_FILE}|grep -E "No_Detect|ip_bad|ip_down"|awk '{print $2}'`
#echo -e "${DATA}"|awk '{printf("domain=%s; code=%s",$1,$2)}'
#echo $domain $code
DOMAIN_PATH="/usr/local/squid/etc"
DOMAIN_FILE="${DOMAIN_PATH}/domain.conf"
TODAY="`date +%Y-%m-%d`"
LOG_FILE="/tmp/$0_${TODAY}.log"
DOMAIN="`cat -An /usr/local/squid/etc/domain.conf|awk '{print $1,$2,$12}'`"
#RCMS INFO
NAMEID="http://ad.41.cn"
NODE="SH"
# Disk Used
CCM="`df -Th|sed '1d;/ /!N;s/\n//;s/ \+/ /;'|nl|awk '{print $1}'`"
DATA="`df -Th|sed '1d;/ /!N;s/\n//;s/ \+/ /;'|nl`"
USED="85"
USED="5"
IOWAIT="300"
IOWAIT="0.1"
#Disk Write IO
WDDATA="`iostat -x |sed -e '1,5d'|nl|sed '1d;/ /!N;s/\n//;s/ \+/ /;'|awk '{print $1}'`"
WDWAIT="`iostat -x |sed -e '1,5d'|nl|sed '1d;/ /!N;s/\n//;s/ \+/ /;'`"
#
DOMAIN="www.xxx.com"
COUNT="3"
#   Function comments

#===   FUNCTION     ====================================================================
#          NAME:    Record Info.       
#   DESCRIPTION:    Record Sth Info.
#  PARAMETER  1:    ---
#=======================================================================================
function Record_Login_Ping {
    printf "\e[32m ############# 正在生成将要登录设备是否可用的报告 ##############\e[0m\n" ${USED}>>${LOG_FILE}
    DEVICE_INFO=`curl ${RCMSAPI_URL}`
    printf "%s\n" ${DEVICE_INFO}
    
}

function Record_Disk_Used {
    printf "\e[32m ############# 正在生成硬盘使用超过%%%s的报告 ##############\e[0m\n" ${USED}>>${LOG_FILE}
    printf "\e[32m ############# 正在生成硬盘使用超过%%%s的报告 ##############\e[0m\n" ${USED}
for id in ${CCM}
do
    eval `echo -e "${DATA}"|awk -vid=$id '{if($1==id) print "name="$2";type="$3";size="$4";used="$5";avail="$6";use="$7";dmount="$8}'`
    if [[ "$id" == "" ]];then
        echo "$id is null,check Failed"
        exit 1
    fi
    disk_used=$(echo "${use}"|awk -F% '{if($1>'$USED')print $1}')
    if [[ "${disk_used}" != "" ]];then 
        printf "\e[31m ID:%s \t \e[0m\tUsed:%s \t \e[0m Mount:\e[33m %s\e[0m\n" $id ${use} ${dmount} >>${LOG_FILE}
        printf "\e[31m ID:%s \t \e[0m\tUsed:%s \t \e[0m Mount:\e[33m %s\e[0m\n" $id ${use} ${dmount} 
    fi
done
}

function Record_Disk_IO {
    printf "\e[32m ############# 正在生成硬盘写延时报告 ##############\e[0m\n" >>${LOG_FILE}
    printf "\e[32m ############# 正在生成硬盘写延时报告 ##############\e[0m\n" 
	for id in ${WDDATA}
	do
     eval `echo -e "${WDWAIT}"|awk -vid="${id}" '{if($1==id) printf("device=%s;rrqm=%s;wrqm=%s;rs=%s;ws=%s;\
rsecs=%s;wsecs=%s;avgrqsz=%s;svqqusz=%s;await=%s;svctm=%s;util=%s;",$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)}'`
		if [[ "$id" == "" ]];then
        echo "$id is null,check Failed"
        exit 1
		fi
		#echo "device=$device rrqm=$rrqm wrqm=$wrqm rs=$rs ws=$ws rsecs=$rsecs wsecs=%s avgrqsz=$avgrqsz \
		#svqqusz=$svqqusz await=$await svctm=$svctm util=$util"
		wio_wait=$(echo "${await}"|awk '{if($1>'$IOWAIT')print $1}')
		if [[ "${wio_wait}" != "" ]];then 
			printf "\e[31m ID:%s \t \e[0m\tDevice Name:\e[33m%s \t \e[0m Write_IO_Wait(ms):\e[33m %s\e[0m\n" $id ${device} ${wio_wait} >>${LOG_FILE}
			printf "\e[31m ID:%s \t \e[0m\tDevice Name:\e[33m%s \t \e[0m Write_IO_Wait(ms):\e[33m %s\e[0m\n" $id ${device} ${wio_wait} 
		fi
	done
}

function Record_Ping_Time {
    printf "\e[32m ############# 正在%s到Cache的延时报告 ##############\e[0m\n" ${HOST}>>${LOG_FILE}
    printf "\e[32m ############# 正在%s到Cache的延时报告 ##############\e[0m\n" ${HOST}
    data=`ping ${DOMAIN} -c ${COUNT}`
    echo "${data}"|tail -n 2
    echo "${data}"|tail -n 2 >>"${LOG_FILE}"
}

function Record_Log {
    printf "################ Start #############\n">${LOG_FILE}
    printf "Domain\tStatus\tURL\t\n">>${LOG_FILE}
    for domain in `echo "${DATA_LOG}"`
    do
        #echo "$domain"
        eval `echo -e "${DATA}"|awk -vdomain="${domain}" '{if($1==domain) printf("domain=%s;code=%s",$1,$2)}'`
        #echo "$domain $code"
        #eval `awk -vdomain="${domain}" '{if($1==domain) printf("domain=%s;url=%s",$1,$11)}' ${DOMAIN_FILE}`
        url=$(echo -e `echo -e "$DOMAIN"|grep ${domain}`|awk -F":" '{print $2}')
        echo "DOMAIN=$domain\tCode=${code}\tURL=`echo $url|awk -F":" "{print $2}"`"
        # awk -vdomain="${domain}" '{if($1==domain) printf "url=%s",$11}' "${DOMAIN_FILE}"
        echo -e "${domain}\t${code}\t${url}">>${LOG_FILE}
    done
    printf "################ End #############\n">>${LOG_FILE}
    printf "\e[32m ################ Parse Log Finished #############\e[0m\n"
    printf "\e[32m 探测结果保存路径:Detect File:${LOG_FILE} \e[0m\n"
    
}

#   Section comments

#---------------------------------------------------------------------------------------
#   Define Shell Main .
#---------------------------------------------------------------------------------------
#Record_Disk_Used
#Record_Ping_Time
#Record_Upper_Time
#Record_Disk_IO
#Record_Login_Ping
#if [[ -f ${DATA_FILE} ]];then
#   Record_Log
#else
#       printf "\e[32m ################ Parse Log [ Failed ]##############\e[0m\n">>${LOG_FILE}
#       printf "\e[32m ################ Parse Log [ Failed ]##############\e[0m\n"
#       exit 1
#fi
#   line end comments