脚本如下:
#!/bin/sh
###############################################
# #
# creator: lvnian #
# datetime: 29/04/2016 #
# #
###############################################
scripts_dir=`dirname $0`
function menu() {
clear
echo "*********************MENU OPS*****************"
echo "* *"
echo "* 1、指定时间段获取日志 *"
echo "* 2、指定URI获取日志 *"
echo "* 3、指定时间段及URI获取日志 *"
echo "* 4、exit *"
echo "* *"
echo "**********************************************"
read -p "输入编号: " number
}
function time_select(){
echo -e "\t请输入你要筛选的时间段(10:00 - 15:00)\n\t如果筛选时间段初始时间为当前时间(-15:00):\c"
read time
date_time=`head -1 $Logfile |awk -F"[[ / :]" ‘{print "["$5"/"$6"/"$7}‘`
start_time=`echo "${time}" |awk -F"-" ‘{print $1}‘|sed ‘s/[ \t]*$//g‘ `
if [ -z "${start_time}" ]
then
echo -e "\t你没输入筛选的开始时间,默认为当前时间`date +%Y%H:%M`:\n"
start_time=`date +%H:%M`
fi
start_time=${date_time}:${start_time}
echo ${start_time}
stop_time=`echo $time|awk -F‘-‘ ‘{print $2}‘|sed ‘s/[ \t]*//g‘ `
if [ -z "${stop_time}" ]
then
echo -e "\t输入时间格式有误!格式如下:\n"
sleep 1
time_uri_select
fi
stop_time=${date_time}:${stop_time}
echo ${time} $uri $start_time $stop_time
file=`basename ${Logfile} `
}
function uri_select() {
echo -e "\t请输入你选筛选的 URI : \c"
read uri
file=`basename ${Logfile} `
}
function select_time() {
time_select
[ ! -d /tmp/backup/time ] && mkdir /tmp/backup/time/ -p
awk -v start_time=$start_time -v stop_time=$stop_time ‘{if ($4>start_time && $4<stop_time) print $0 }‘ ${Logfile} > /tmp/backup/time/time.${file}
awk ‘{S[$1]++}END{for(n in S) print S[n]" "n }‘ /tmp/backup/time/time.${file} |sort -rn > /tmp/backup/time/time.sort.${file}
awk ‘{S[$1]++}END{for(n in S) print S[n]" "n }‘ /tmp/backup/time/time.${file} |sort -rn > /tmp/backup/time/time.sort.${file}
awk ‘{S[$7]++}END{for(n in S) print S[n]" "n }‘ /tmp/backup/time/time.${file} |sort -rn > /tmp/backup/time/time.URI.sort.${file}
}
function select_uri() {
uri_select
[ ! -d /tmp/backup/uri ] && mkdir /tmp/backup/uri/ -p
awk -v uri=$uri ‘{if ($7==‘uri‘) print $0 }‘ ${Logfile} > /tmp/backup/uri/uri.${file}
awk ‘{S[$1]++}END{for(n in S) print S[n]" "n }‘ /tmp/backup/uri/uri.${file} |sort -rn > /tmp/backup/uri/uri.sort.${file}
}
function select_time_uri() {
time_select
uri_select
[ ! -d /tmp/backup/time_uri ] && mkdir /tmp/backup/time_uri/ -p
awk -v start_time=$start_time -v stop_time=$stop_time ‘{if ($4>start_time && $4<stop_time) print $0 }‘ ${Logfile} > /tmp/backup/time_uri/time.${file}
awk -v uri=$uri ‘{if ($7==‘uri‘) print $0 }‘ /tmp/backup/time_uri/time.${file} > /tmp/backup/time_uri/time.uri.${file}
awk ‘{S[$1]++}END{for(n in S) print S[n]" "n }‘ /tmp/backup/time_uri/time.uri.${file} |sort -rn > /tmp/backup/time_uri/time.uri.sort.${file}
}
function main() {
while true
do
menu
read -p "请输入你要筛选的日志(/path/logfile.log): " Logfile
case $number in
1)
select_time
read -p "回车继续: ";;
2)
select_uri
read -p "回车继续: ";;
3)
select_time_uri
read -p "回车继续: ";;
*)
exit;;
esac
done
}
main
#########################################################################################
#########################################################################################
#########################################################################################
#########################################################################################
#########################################################################################
[lvnian@lvnian LogAnalyze]# sh SelectLog.sh
*********************MENU OPS*****************
* *
* 1、指定时间段获取日志 *
* 2、指定URI获取日志 *
* 3、指定时间段及URI获取日志 *
* 4、exit *
* *
**********************************************
输入编号: 1
请输入你要筛选的日志(/path/logfile.log): /tmp/backup/access_www.log
请输入你要筛选的时间段(10:00 - 15:00)
如果筛选时间段初始时间为当前时间(-15:00):00:00 - 00:15
[29/Apr/2016:00:00
00:00 - 00:15 [29/Apr/2016:00:00 [29/Apr/2016:00:15
回车继续:
*********************MENU OPS*****************
* *
* 1、指定时间段获取日志 *
* 2、指定URI获取日志 *
* 3、指定时间段及URI获取日志 *
* 4、exit *
* *
**********************************************
输入编号: 2
请输入你要筛选的日志(/path/logfile.log): /tmp/backup/access_www.log
请输入你选筛选的 URI : /lvnian/js/front/header.js?t=1461859586659
回车继续:
*********************MENU OPS*****************
* *
* 1、指定时间段获取日志 *
* 2、指定URI获取日志 *
* 3、指定时间段及URI获取日志 *
* 4、exit *
* *
**********************************************
输入编号: 3
请输入你要筛选的日志(/path/logfile.log): /tmp/backup/access_www.log
请输入你要筛选的时间段(10:00 - 15:00)
如果筛选时间段初始时间为当前时间(-15:00):00:06 - 11:55
[29/Apr/2016:00:06
00:06 - 11:55 /lvnian/js/front/header.js?t=1461859586659 [29/Apr/2016:00:06 [29/Apr/2016:11:55
请输入你选筛选的 URI : /front/piechartinfo?platId=2434
回车继续:
[lvnian@lvnian backup]$ tree
.
├── access_www.log
├── time
│ ├── time.access_www.log
│ ├── time.sort.access_www.log
│ └── time.URI.sort.access_www.log
├── time_uri
│ ├── time.access_www.log
│ ├── time.uri.access_www.log
│ └── time.uri.sort.access_www.log
└── uri
├── uri.access_www.log
└── uri.sort.access_www.log
3 directories, 9 files
[lvnian@lvnian backup]$ tree
.
├── access_www.log
├── time
│ ├── time.access_www.log
│ ├── time.sort.access_www.log
│ └── time.URI.sort.access_www.log
├── time_uri
│ ├── time.access_www.log
│ ├── time.uri.access_www.log
│ └── time.uri.sort.access_www.log
└── uri
├── uri.access_www.log
└── uri.sort.access_www.log
3 directories, 9 files
[lvnian@lvnian backup]$ pwd
/tmp/backup
[lvnian@lvnian backup]$@iZ23eoou07sZ backup]$ tree
.
├── access_www.log
├── time
│ ├── time.access_www.log
│ ├── time.sort.access_www.log
│ └── time.URI.sort.access_www.log
├── time_uri
│ ├── time.access_www.log
│ ├── time.uri.access_www.log
│ └── time.uri.sort.access_www.log
└── uri
├── uri.access_www.log
└── uri.sort.access_www.log
3 directories, 9 files
[lvnian@lvnian backup]$ pwd
/tmp/backup
[lvnian@lvnian backup]$@iZ23eoou07sZ backup]$ pwd
/tmp/backup
[lvnian@lvnian backup]$脚本功能:
1、可以根据时间段来获取这个时间段的全部日志,并且筛选出这个时间段的访问IP数量排序以及相同URI访问次数排序!
2、可以根据输入的指定URI获取整个日志文件所有访问这个uri的日志信息,以及这个访问这个uri的来源ip访问次数排序。
3、可以同时根据输入的时间段和URI,筛选出这个时间段访问这个URI的全部日志,以及在这个时间段访问这个URI的来源IP次数排序
本文出自 “奋斗吧” 博客,转载请与作者联系!
原文:http://lvnian.blog.51cto.com/7155281/1768960