分类: 系统运维
2013-05-22 20:07:39
原文地址:一个支持正则表达式的日志关键字统计的shell工具 作者:icucstone
Usage: pafa_mon/statistics_pafa.sh -i|--instance
## default values for the variables below ##
HOST_NAME=$(hostname)
HOST_IP=$(hostname -i)
INSTANCE_NAME=''
INSTANCE_PATH="/wls/applogs/rtlog/"
FILE_NAME='pafa.log'
KEY_WORDS=''
MONTH=$(date +%m)
DAY=$(date +%e)
BEGIN_TIME=''
END_TIME=''
PROGRESS_TYPE=1
########################################################################
#### function for usage of the shell script ############
function Usage(){
echo "Usage: $0 -i|--instance
#### function for setting variables for the shell script #############
function Set_variables(){
if [[ $# == 0 ]]; then
if [[ -n $INSTANCE_NAME ]];then
FILE_PATH="${INSTANCE_PATH}/${INSTANCE_NAME}/"
Choose
exit 0
else
Usage
fi
fi
case "$1" in
"-i") shift
INSTANCE_NAME=$1
shift
Set_variables $@
;;
"--instance") shift
INSTANCE_NAME=$1
shift
Set_variables $@
;;
"-fn") shift
FILE_NAME=$1
shift
Set_variables $@
;;
"--filename") shift
FILE_NAME=$1
shift
Set_variables $@
;;
"-ip") shift
INSTANCE_PATH=$1
shift
Set_variables $@
;;
"--instancepath") shift
INSTANCE_PATH=$1
shift
Set_variables $@
;;
"-kw") shift
KEY_WORDS=$1
shift
Set_variables $@
;;
"--keywords") shift
KEY_WORDS=$1
shift
Set_variables $@
;;
"-bt") shift
BEGIN_TIME=$1
shift
Set_variables $@
;;
"--begintime") shift
BEGIN_TIME=$1
shift
Set_variables $@
;;
"-et") shift
END_TIME=$1
shift
Set_variables $@
;;
"--endtime") shift
END_TIME=$1
shift
Set_variables $@
;;
"-t") shift
PROGRESS_TYPE=$1
shift
Set_variables $@
;;
"--type") shift
PROGRESS_TYPE=$1
shift
Set_variables $@
;;
*) Usage
esac
}
#### function for show the value of the shell script's variables ############
function Show_variables(){
echo HOST_NAME=$HOST_NAME
echo HOST_IP=$HOST_IP
echo INSTANCE_NAME=$INSTANCE_NAME
echo FILE_PATH=$FILE_PATH
echo INSTANCE_PATH=$INSTANCE_PATH
echo FILE_NAME=$FILE_NAME
echo KEY_WORDS=$KEY_WORDS
echo MONTH=$MONTH
echo DAY=$DAY
echo BEGIN_TIME=$BEGIN_TIME
echo END_TIME=$END_TIME
}
#### function for statistic the key-words in the specified file from the last 1 million lines ############
function Statistic_contents(){
tail -1000000 ${FILE_PATH}${FILE_NAME} |awk '/^'${MONTH}'\/'${DAY}'/ && /'"${KEY}"'/ {OFS=""; print "KeyWord=","'"${KEY}:"'",$0} ' | tail -n 10
}
#### function for statistic the key-words arise times in the specified file from the last 1 million lines ############
function Statistic_times(){
tail -1000000 ${FILE_PATH}${FILE_NAME} |awk '/^'${MONTH}'\/'${DAY}'/ && /'"${KEY}"'/ {a++} END{OFS=""; print "KeyWord=","'"${KEY}:"'",a} '
}
#### function for statistic the key-words in the specified file by time ############
function Statistic_contents_bytime(){
if [[ -z $BEGIN_TIME ]];then
if [[ -z $END_TIME ]];then
awk '/^'${MONTH}'\/'${DAY}'/ && /'"${KEY}"'/ {OFS=""; print "KeyWord=","'"${KEY}:"'",$0} ' ${FILE_PATH}${FILE_NAME} | tail -n 10
else
awk '/^'${MONTH}'\/'${DAY}'/ && $2<"'$END_TIME'" && /'"${KEY}"'/ {OFS=""; print "KeyWord=","'"${KEY}:"'",$0}' ${FILE_PATH}${FILE_NAME} | tail -n 10
fi
else
if [[ -z $END_TIME ]];then
awk '/^'${MONTH}'\/'${DAY}'/ && $2>"'$BEGIN_TIME'" && /'"${KEY}"'/ {OFS=""; print "KeyWord=","'"${KEY}:"'",$0}' ${FILE_PATH}${FILE_NAME} | tail -n 10
else
awk '/^'${MONTH}'\/'${DAY}'/ && $2>"'$BEGIN_TIME'" && $2<"'$END_TIME'" && /'"${KEY}"'/ {OFS=""; print "KeyWord=","'"${KEY}:"'",$0}' ${FILE_PATH}${FILE_NAME} | tail -n 10
fi
fi
}
#### function for statistic the key-words arise times in the specified file by time ############
function Statistic_times_bytime(){
if [[ -z $BEGIN_TIME ]];then
if [[ -z $END_TIME ]];then
awk '/^'${MONTH}'\/'${DAY}'/ && /'"${KEY}"'/ {a++} END{OFS=""; print "KeyWord=","'"${KEY}:"'",a}' ${FILE_PATH}${FILE_NAME}
else
awk '/^'${MONTH}'\/'${DAY}'/ && $2<"'$END_TIME'" && /'"${KEY}"'/ {a++} END{OFS=""; print "KeyWord=","'"${KEY}:"'",a}' ${FILE_PATH}${FILE_NAME}
fi
else
if [[ -z $END_TIME ]];then
awk '/^'${MONTH}'\/'${DAY}'/ && $2>"'$BEGIN_TIME'" && /'"${KEY}"'/ {a++} END{OFS=""; print "KeyWord=","'"${KEY}:"'",a}' ${FILE_PATH}${FILE_NAME}
else
awk '/^'${MONTH}'\/'${DAY}'/ && $2>"'$BEGIN_TIME'" && $2<"'$END_TIME'" && /'"${KEY}"'/ {a++} END{OFS=""; print "KeyWord=","'"${KEY}:"'",a}' ${FILE_PATH}${FILE_NAME}
fi
fi
}
#### function for progressing for statistic ##########################
function Progress(){
if [[ -z $KEY_WORDS ]];then
if [[ -f ${FILE_PATH}/pafa.key ]];then
grep -v "^#" ${FILE_PATH}/pafa.key | while read KEY
do
$1
done
else
echo "Attention please !"
echo "You should create the key-word file named pafa.key in the directory ${FILE_PATH} at first !"
exit 1
fi
else
Key_Words="$(echo ${KEY_WORDS} |sed s/':'/' '/g )"
for KEY in $Key_Words
do
$1
done
fi
}
#### function for choose progress type for statistic ###############
function Choose(){
case "$PROGRESS_TYPE" in
"1") Progress "Statistic_contents"
;;
"2") Progress "Statistic_times"
;;
"3") Progress "Statistic_contents_bytime"
;;
"4") Progress "Statistic_times_bytime"
;;
*) Usage
;;
esac
}
#### function for the start of the shell script #################
function Main(){
Set_variables $@
}
Main $@