您的位置:首页 > 运维架构 > Shell

对log进行shell方法处理

2016-04-01 00:00 423 查看
一、功能描述

对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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell日志分析