Log file scrubber for IIS
I wrote this script quite a few years ago to reduce the size of IIS log files by removing log entries for trivial requests (based on file extension for the request).
The version below only removes successful log entries for any request for gif and jpg images. However, as they tend to make up a large portion of the log this can significantly reduce the size of the file, an improvement for long(er) term storage on busy servers.
It has a couple of short-comings. Error control is minimal, and reporting is non-existent.
' ScrubLogs.vbs
'
' Author: Chris Dent
' Date: 18/05/2005
' Modified: 07/06/2005
'
' Part of Log File Management. Removes successful file requests from targetted
' log files based on the contents of arrFileExtensions.
'
' Cannot be used on todays log file - locked by IIS.
Option Explicit
' The file extensions you want to remove successful requests for
Dim arrFileExtensions
arrFileExtensions = Array("gif", "jpg")
Function GetLogFolders(strServer)
' Returns a dictionary with log file path, site ID and
' comment for each site
Dim objIISWeb, objSites, objNode
On Error Resume Next : Err.Clear
Set objIISWeb = GetObject("IIS://" & strServer & "/W3SVC")
If Err.Number <> 0 Then
WScript.Echo "Failed to connect to metabase: " & Err.Description
End If
On Error Goto 0
Set objSites = CreateObject("Scripting.Dictionary")
For Each objNode in objIISWeb
If LCase(objNode.Class) = "iiswebserver" Then
objSites.Add objNode.Name, _
Array(objNode.LogFileDirectory, objNode.ServerComment)
End If
Next
Set objIISWeb = Nothing
Set GetLogFolders = objSites
Set objSites = Nothing
End Function
Sub ScrubFolders(objSites)
' Loops through objSites, checks the log file folder and checks files
Dim strSite, strLogFolder
Dim objFileSystem, objFolder, objFile
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
For Each strSite in objSites
strLogFolder = objSites(strSite)(0) & "\W3SVC" & strSite
If objFileSystem.FolderExists(strLogFolder) Then
Set objFolder = objFileSystem.GetFolder(strLogFolder)
For Each objFile in objFolder.Files
' Skip files last modified within the last 24 hours
If objFile.DateLastModified < (Now - 1) Then
ScrubLog objFile
End If
Next
End If
Next
Set objFileSystem = Nothing
End Sub
Sub ScrubLog(objFile)
Dim strTempFile, strLine, strLogName, strElement, strExtension
Dim arrLine
Dim objFileSystem, objTempFile, objStream
Dim booFields, booIgnore, booRemoveExt
Dim intFieldCount, intFileName, intStatus, int40x, int50x
strTempFile = objFile.Path & ".temp"
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objTempFile = objFileSystem.CreateTextFile(strTempFile, True, False)
booFields = False
' Process everything in the Log File
Set objStream = objFile.OpenAsTextStream(1,0)
Do While Not objStream.AtEndOfStream
booIgnore = False
strLine = objStream.ReadLine()
arrLine = split(strLine, " ")
' Get the field positions
If (arrLine(0) = "#Fields:") Then
intFieldCount = 0
For Each strElement in arrLine
If strElement = "cs-uri-stem" Then
intFileName = intFieldCount - 1
ElseIf strElement = "sc-status" Then
intStatus = intFieldCount - 1
End If
intFieldCount = intFieldCount + 1
Next
booFields = True
End If
' Just rewrite any line beginning with #
If (Left(arrLine(0), 1) = "#") Then
objTempFile.WriteLine strLine
booIgnore = True
End If
If booFields = True And booIgnore = False Then
' A quick check to see if the cs-uri-stem field has one
' of the extensions we're interested in
booRemoveExt = False
For Each strExtension in arrFileExtensions
If InStr(1, Right(arrLine(intFileName), 3), strExtension, 1) <> 0 Then
booRemoveExt = True
End If
Next
' Write the line back to the new log unless it's a successful request
' for the file extensions we're removing
If booRemoveExt = True Then
int40x = InStr(1, Left(arrLine(intStatus), 2), "40", 1)
int50x = InStr(1, Left(arrLine(intStatus), 2), "50", 1)
If (int40x <> 0) or (int50x <> 0) Then
objTempFile.WriteLine strLine
End If
Else
objTempFile.WriteLine strLine
End If
End If
Loop
' Delete the original log file
Set objStream = Nothing
strLogName = objFile.Name
objFile.Delete
Set objFile = Nothing
' Rename the temp file to match the original
Set objTempFile = Nothing
Set objTempFile = objFileSystem.GetFile(strTempFile)
objTempFile.Name = strLogName
Set objTempFile = Nothing
Set objFileSystem = Nothing
End Sub
'
' Main Code
'
Dim objLogFolders
Set objLogFolders = GetLogFolders("localhost")
ScrubFolders objLogFolders
Set objLogFolders = Nothing
Related posts:
- Listing Trusts A script to enumerate trust information from an Active Directory...
Related posts brought to you by Yet Another Related Posts Plugin.
Respond to this post