一、功能描述
对log进行数据筛取,用切割的方式,获取record_time、function_name、Error_state、client_ip、tv_id、description等数据并插入到本地数据库的error_log表中
二、log模型
[2016-03-09 16:54:05:726] HttpSendLiveStream ERROR!!
[473 58.254.216.34:18297 TV4016]:: caught up by live stream! curr:17775[19886->19885]
[2016-03-09 16:54:07:219] LiveStateCheck:: TV6047 not received pkt for 15 seconds!!
[2016-03-09 16:54:07:951] LiveStateCheck:: TV6046 not received pkt for 15 seconds!!
[2016-03-09 16:54:08:105] HlsCheckStealingLink:: HLS Relay :client 209.126.64.42 is stealing!
三、完整代码
#/bin/bash
#
# Script: log_run_shell
# Author: Charlotte
# Date2016/3/29
#
# Platform: Linux
#
#
##################################################
# Define files and variasles here
##################################################
# id time
cdn_path=`pwd`
ID="0"
sign="0"
touch "$cdn_path/"cdn.txt
:> "$cdn_path"cdn.txt
HOSTNAME="127.0.0.1"
PORT="3306"
USERNAME="root"
PASSWORD="123456"
DBNAME="operation_monitor"
TABLENAME="error_log"
##################################################
# Beginning of main
##################################################
while read LINE
do
if [ $sign = "1" ] ; then # record no.2
# if [${LINE:1:2} = "["]; then
IP=`echo $LINE | awk -F ' ' '{print $2}' | awk -F ':' '{print $1}'`
tv_key=`echo $LINE | awk -F '[] ]' '{print $3}'`
duration_time=`echo $LINE | awk -F '[:[]' '{print $6}'`
# fi
description=${LINE#*::}
sign="0"
insert_sql="insert into ${TABLENAME}(id,function_name,description,record_time) values('','$model','$description',$record_time)"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${insert_sql}"
else # record no.1
echo $LINE | grep -E "ERROR|FATAL|WARNING" 1>/dev/null
if [ $? != "0" ] ; then
continue
fi
record_time=`echo $LINE | awk -F '[]. :[-]' '{print $2$3$4$5$6$7}'`
model=`echo $LINE | awk -F ' ' '{print $3}'`
state=`echo $LINE | awk -F '[ !]' '{print $4}'`
sign="1"
ID=$[($ID+1)]
fi
done <"$cdn_path/"record_model.log
# End of script