0%

logs_search

在特定时间统计接口数

日志统计

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash

HOST_NAME=$(hostname)
LOGS_DIR='/home/phimor/apache-tomcat-7.0.73/logs/'
LOG_FILE="localhost_access*"
INTERFACE_NAME='getToken'

START_TIME='2018-12-28 10:00:00'
END_TIME='2018-12-28 10:10:00'
start_time_seconds=$(date -d "${START_TIME}" +%s)
end_time_seconds=$(date -d "${END_TIME}" +%s)

get_seconds(){
seconds=$((${end_time_seconds}-${start_time_seconds}))
echo ${seconds}
}

count_gettoken_request_every_second(){
printf "请求时间\t\t\t请求数\t\t接口名\t\t\n"
while [[ ${start_time_seconds} -le ${end_time_seconds} ]]; do
#echo "output: ${start_time_seconds}"
#current_time=$(date -d "@${start_time_sesconds}" +"%F %H:%M:%S")
local current_time=$(echo "${start_time_seconds}" |awk '{print strftime ("%F %T",$0)}')
local second=$(echo "${current_time: -1}")
local search_time_regex="${current_time}${second}*"
#echo ${current_time}
#echo ${second}
#echo ${search_time_regex}
local gettoken_count=$(grep "${search_time_regex}" ${LOGS_DIR}${LOG_FILE} | grep ${INTERFACE_NAME} | wc -l)
echo -e "${current_time}\t\t${gettoken_count}\t\tgetToken"
true $(( start_time_seconds++ ))
done
}

count_gettoken_request_every_second > ${HOST_NAME}.txt