IIS Log Archiving
Written by jf on June 24, 2008You need to archive your IIS Log often so as not to get your log folder full with HTTP Logs.
I have been searching for some quick implemented solutions for performing this IIS Log archiving task and found some quiet nice discussions and article about it. Here are the links to the different post and forums that talk about a solution to solve this issue:
http://blogs.thesitedoctor.co.uk/tim/2007/02/10/Automatically+Delete+Old+IIS+Log+Files.aspx
http://www.iislogs.com/ (Tool to automate maintenance of IIS Log)
http://forums.webhostautomation.com/showthread.php?t=5053
http://forums.iis.net/p/1022450/1388469.aspx
On my side i need something with a bit more functionality so, i modified some of the scripts that i could find on the different article related above and came up with a solution that can.
- Compress each log file found in your websites folder
- FTP the compressed files on a foreign server ( Keeping historic of your IIS log ) Uses Chillkat Free FTP ActiveX
- Delete them from your disk afterward
You can launch this process everyday and there will be no log that is older than a specified number of days on your server.
Requirement for this solution to work:
You can download the script here.
See the entire script in the full post.
Option Explicit
Const ftpSite = "yourftpsite"
Const ftpUsername = "ftpusername"
Const ftpPassword = "ftppassword"
Const bDEBUG = True
Const zipProgram = "C:\gzip\gzip.exe" ' Path to your gzip utility
Const zipExtension = ".gz"
Dim objFSO, objFolder, objF, objWS
Dim strYear, strDay, strMonth, strDate
Dim strBaseFTPPath
Dim bFTPUpload
bFTPUpload = False
'If no arguments passed quit the script
If WScript.Arguments.Count = 0 Then
WScript.Echo "weblogs.vbs startdirectory [ftppath]"
WScript.Quit (1)
End If
'Check if there is the FTP path Argument sent
If WScript.Arguments.Count > 1 Then
bFTPUpload = True
strBaseFTPPath = wscript.arguments (1)
End If
strDate = Date()
strYear = DatePart ("yyyy", strDate)
strDay = DatePart ("d", strDate)
strMonth = DatePart ("m", strDate)
wscript.echo "Script run on date " & strDate
Wscript.echo "log files more than 5 days old will be zipped."
wscript.echo " "
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objWS = CreateObject ("WScript.Shell")
Set objFolder = objFSO.GetFolder (wscript.arguments (0))
If objFolder.SubFolders.Count > 0 Then
For Each objF in objFolder.SubFolders
If bDebug Then wscript.echo "Checking folder " & objFolder.Path
DoDirectory (objF)
Next
Else
If bDebug Then wscript.echo "Going inside Log File Folder " & objFolder.Path
DoDirectory (objFolder)
End If
Set objFSO = Nothing
Set objWS = Nothing
Sub DoDirectory (objFold)
Dim objUserDir, objFile, iDiff
For Each objUserDir in objFold.SubFolders
DoDirectory (objUserDir)
Next
For Each objFile in objFold.Files
If bDebug Then Wscript.echo "Checking file: " & objFile.Path
If LCase (Right (objFile.Name, 4)) = ".log" and LCase (Left (objFile.Name, 2)) = "ex" Then
Dim strCheckString,strFTPPath,strZipPath, strY, strM, strD, strCommand, iResult
strCheckString = Mid (objFile.Name, 3, 6)
strY = "20" & Left (strCheckString, 2)
strM = Mid (strCheckstring, 3, 2)
strD = Right (strCheckString, 2)
strFTPPath = strBaseFTPPath & strY & "/" & strM & "/"
strZipPath = objFile.Path & zipExtension
iDiff = DateDiff ("d", strM & "/" & strD & "/" & strY, strDate)
If iDiff > 5 Then
strCommand = zipProgram & " " & objFile.Path
If bDebug Then wscript.echo vbTab & "zip it: " & strCommand
iResult = objWS.Run (strCommand, 0, "true")
If bFTPUpload Then
If bDebug Then wscript.echo vbTab & "upload it: " & "FTPUpload(" & strZipPath & "," & strFTPPath & "," & ftpSite & "," & ftpUsername & "," & ftpPassword & ", True)"
wscript.echo FTPUpload(strZipPath, strFTPPath, ftpSite, ftpUsername, ftpPassword, True)
End If
If iResult <> 0 Then
If bDebug Then wscript.echo vbTab & "result = " & iResult
Else
'Deletes the zip archive after upload is completed
If bDebug Then wscript.echo vbTab & "To Deleted = " & strZipPath
objFSO.DeleteFile (strZipPath)
End If
Else
If bDebug Then wscript.echo vbTab & "don't zip it"
End If
End If
Next
End Sub
Function FTPUpload( locFile, targetDir, host, user, password, blnMkDir )
' This function uses the free ChilkatFTP ActiveX component
' to upload a single file.
' The remote directory can be specified, but the remote
' file name will be the same as the local file name.
' The function is based on Chilkat's own sample for the ChilkatFTP2 component
' (which is not free): http://www.example-code.com/vbscript/ftpPutFile.asp
'
' Arguments:
' locFile [string] the (path and) file name of the file to be uploaded
' targetDir [string] the (relative) path of the remote target directory;
' if empty, the current remote directory will be used
' host [string] the remote host name (e.g. "ftp.mydomain.org")
' user [string] the login name for the remote host
' password [string] the password for the login account
' blnMkDir [boolean] if True, the remote directory will be created if it
' doesn't exist, otherwise the function will fail if
' the remote directory doesn't exist
'
' The ChilkatFTP ActiveX component can be downloaded from:
' http://www.chilkatsoft.com/download/FtpActiveX.msi
' Documentation can be found at:
' http://www.chilkatsoft.com/refdoc/xChilkatFtpRef.html
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
' Standard housekeeping
Dim objFSO, objFTP, ok, strRemFile
' Extract the local file name and extension only from its path
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
With objFSO
strRemFile = .BuildPath( targetDir, .GetFileName( locFile ) )
End With
Set objFSO = Nothing
' Create a ChilkatFTP object
Set objFTP = CreateObject( "ChilkatFTP.ChilkatFTP.1" )
' pass the connection properties to the object
objFTP.Username = user
objFTP.Password = password
objFTP.Hostname = host
' Connect, abort and return error message on failure
ok = objFTP.Connect( )
If ( ok <> 1 ) Then
FTPUpload = objFTP.LastErrorText
Set objFTP = Nothing
Exit Function
End If
If targetDir <> "" Then
' If specified, create target directory
If blnMkDir = True Then
objFTP.CreateRemoteDir targetDir
End If
' Change directory remotely, abort and return error message on failure
ok = objFTP.ChangeRemoteDir( targetDir )
If ( ok <> 1 ) Then
FTPUpload = objFTP.LastErrorText
objFTP.Disconnect()
Set objFTP = Nothing
Exit Function
End If
End If
' Upload the file, abort and return error message on failure
ok = objFTP.PutFile( locFile, strRemFile )
If ( ok <> 1 ) Then
FTPUpload = objFTP.LastErrorText
End If
' Disconnect,and release the object
objFTP.Disconnect( )
Set objFTP = Nothing
' Return result
FTPUpload = "Upload succeeded"
End Function
You will also need to create a bat file to run your script this is an example of the command in the file.
IISLogarchiver.vbs C:\WINDOWS\system32\LogFiles\W3SVC1404973120\ /MyLogArchive/
This command line will get all the file in the specified path and then upload it to a the FTP Path /MyLogArchive/[Year of file]/[Month of File]








Shout Box RSS Feed



