使用:
将脚本保存成.vbs 文件 然后放到 log 文件目录下,双击运行等待结果,如果 log 文件大的话会比较慢 一般 1G 的日志 需要 10 分钟左右时间.
log 文件格式如下:
10.190.211.24 - - [14/Dec/2012:10:17:47 +0800] "POST /ulink/insurance/getAge.action HTTP/1.1" 200 19
10.190.211.10 - - [14/Dec/2012:10:17:46 +0800] "POST /ulink/area/area!gtAreaListByLevel.action HTTP/1.1" 200 1729
10.190.211.14 - - [14/Dec/2012:10:17:47 +0800] "GET /ulink/framelayout/setSession.jsp?currentEntityId=10000400 HTTP/1.1" 200 54
10.190.211.19 - - [14/Dec/2012:10:17:47 +0800] "GET /ulink/images/jc.jpg HTTP/1.1" 404 1214
10.190.211.19 - - [14/Dec/2012:10:17:47 +0800] "GET /ulink/softphone/call-history!saveCallHistoryList.action?pageResult=1355450447-8460416%2C1%2C1092%2C013791567611%2C77700842%2C2012%2F12%2F14+10%3A18%3A08%2C77700842 HTTP/1.1" 200 17
10.190.211.26 - - [14/Dec/2012:10:17:47 +0800] "POST /ulink/framelayout/main-content!doMenu.action HTTP/1.1" 200 682
10.190.211.30 - - [14/Dec/2012:10:17:47 +0800] "POST /ulink/framelayout/main-content!doObjTab.action HTTP/1.1" 200 13848
运行结果:
注意:脚本会扫描脚本文件夹下的所有 .log 和文件夹下的所有.log 文件
以下为代码部分:
'/*****************************************************************/
'/* '作者:小菜 */
'/* */
'/* edit by 2013-10-18 */
'/* */
'/* */
'/*****************************************************************/
Set fso=CreateObject("scripting.filesystemobject")
Set regEx = New RegExp
Dim action(),actionNum() '名字和数量
Dim onTime() '时间
ReDim action(0) '重定义数组
ReDim actionNum(0) '重定义数组
ReDim onTime(0) '重定义数组
Dim reqSuffix,Suffix
Suffix = "log.*" '扫描文件后缀名*****
reqSuffix= "action" '请求的后缀名*****
MsgBox "开始统计", vbOKOnly, "Script start"
timea = Now()
Call folderEx(".")
'输出
folderspec = "."
fso.CreateTextFile(folderspec & "\action分析结果.txt")
Set objFile = fso.OpenTextFile(folderspec & "\action分析结果.txt", 2, True)
For i=0 To UBound(action)-1
objFile.WriteLine(onTime(i)&VBTAB&action(i)&VBTAB&actionNum(i))
Next
objFile.Close
timeb = Now()
MsgBox "统计完成共耗时:"&datediff("s",timea,timeb)&"秒,程序退出", vbOKOnly, "Script exit"
Set fso = Nothing
Set regEx = Nothing
'文件夹递归调用
Sub folderEx(folderspec)
Call main(folderspec)
Set f = fso.GetFolder(folderspec)
Set sFold = f.SubFolders
For Each Fold In sFold
Call folderEx(folderspec & "\" & Fold.Name)
Next
End Sub
'日志主程序
Sub main(folderspec)
Set f=fso.getfolder(folderspec)
Set fs=f.files
'检索日志文件
For each f in fs
'/**********************配置需要处理的日志后缀名*******************/
regEx.pattern= Suffix '查询.LOG*后缀的文件
regEx.ignorecase=False '区分大小写
regEx.global=False '只匹配1次
If regEx.test(fso.getextensionname(f)) then
Set ff=fso.openTextfile(f.path)
Do While ff.AtEndOfStream <> True
LineStr=ff.ReadLine
on error resume next
Search(LineStr)
On Error Goto 0
loop
ff.Close
End If
Next
End Sub
'/*****************************************************************/
'/* */
'/* */
'/* 函数库 */
'/* */
'/* */
'/*****************************************************************/
'查询关键字
Function Search(Str)
Dim Matches
'/**********************配置关键字******************/
If InStrRev(Str,"." & reqSuffix) Then
'/**********************配置关键字样式******************/
str2= split(Str,"""")(1)
str2= split(str2,"""")(0)
str2= split(str2," ")(1)
Set Matches = RegExpTest(".*\." & reqSuffix,str2)
If Matches.count >= 1 Then '如果有搜索结果
aName = Matches(0)
sTime= split(Str,"[")(1)
s= split(sTime,":")
'/*******************设定提取数据库的时间间隔********************/
sTime = s(0)'+":"+s(1)
Call SearchArray(sTime,aName)
End If
End If
End Function
'字符比对
Function RegExpTest(patrn, strng)
'Dim Match, Matches '建立变量。
'Set regEx = New RegExp '建立正则表达式。
regEx.Pattern = patrn'设置模式。
regEx.IgnoreCase = False '设置是否区分字符大小写。
regEx.Global = True '设置全局可用性。
Set RegExpTest = regEx.Execute(strng)'执行搜索。
End Function
'累计
Sub SearchArray(sTime,ActionName)
For i=0 To UBound(onTime)-1
If StrComp(onTime(i),sTime)=0 And StrComp(action(i),ActionName)=0 Then
actionNum(i)=actionNum(i)+1
Exit For
End If
Next
If i = UBound(onTime) Then
action(UBound(action))=ActionName
actionNum(UBound(action))=1
onTime(UBound(action))=sTime
ReDim Preserve action(UBound(action)+1) '重定义数组
ReDim Preserve actionNum(UBound(action)+1) '重定义数组
ReDim Preserve onTime(UBound(action)+1) '重定义数组
End If
End Sub