' ************************************************************************************
' Script:		check_error_logs.vbs
' Descripton:	Search error/log files for preset error messages within in preset time period
' Usage:		cscript check_error_logs.vbs \\server\share
' Author:		Gary Evans
' Date:			24 Dec 2005
' ************************************************************************************
' Updates/Changes
' Date - Initials - Comment
' ************************************************************************************
' To Do
'
' ************* Script Initialisation Statement Go Here ******************************
Option Explicit
Dim aryElem			'Elements in Array
Dim aryFiles()		'Array to Hold File Names
Dim strCount		'Counter
Dim strDir			'Directory from search
Dim strExt			'Required Extension
Dim strFile			'Filename from search
Dim strFileAge		'Max age of file in required period
Dim strMaxAllowed	'Max Number of errors allowed
Dim strNow			'Running time
Dim	strNowDate		'Date of folder to search
Dim strPeriod		'Age Period
Dim strSearchText	'Text to search for
Dim strSearchText1	'Text to search for
Dim strSearchThis	'File to search Search
Dim strTopFldr		'Top Folder To Search From

Dim objDir			'Directory Object used For listing
Dim objExt			'Extension To Convert From
Dim objFile			'File Object
Dim objFSO			'File System Object
Dim objTextStream	'Text Stream Object

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Constants
Const TristateFalse = 0, ForReading = 1, ForWriting = 2, ForAppending = 8


strCount = 0
strExt = "xml"	'file extension of required files
strFileAge = 10	'max age of files to be checked in set periods
strMaxAllowed = 10	'allow up to this many relevant error files
strNow = Now	'Date&Time - Format = "dd/mm/yyyy hh:mm:ss"
strNowDate = Mid(strNow,7,4)&"_"&Mid(strNow,4,2)&"_"&Left(strNow,2)	'Date in format of log folders yyyy_mm_dd
strPeriod = "n"	'vbscript time period:- d=day, h=hour, n=minutes, s=second
strSearchText = "MESSAGE: The underlying connection was closed: Unable to connect to the remote server"	'what to search for
strSearchText1 = "MESSAGE: The operation has timed-out"	'what to search for

' ************* Main Processing Section *********************************************
call fnCheckArgs()

call fnGetFolder(strTopFldr)

Set objDir = strTopFldr

Call fnGoSubFolders(objDir)

Call fnDoBits

' ************* Subroutines and Functions Go Here ************************************
Function fnCheckArgs
	If WScript.Arguments.Count = 1 Then		'Check for arguments passed to script
		strTopFldr = Trim(WScript.arguments(0))&strNowDate
		If Left(strTopFldr,2) <> "\\" Then 
			WScript.Echo "FAILED: -  \\servername\share to check"
			WScript.Quit (2)
		End If
	Else 
		WScript.Echo "FAILED: -  \\servername\share to check"
		WScript.Quit (3)
	End If
End Function

'Get the top folder and check it is accessible
Function fnGetFolder(sFolder)
	On Error Resume Next
	Set strTopFldr = objFSO.GetFolder(sFolder)
	If Err.Number <> 0 Then
		WScript.Echo "Error Connecting to Folder " & sFolder &vbLf &"[" & Err.Number &"] " &Err.Description
	Wscript.Quit Err.Number
	End If
End Function

'Get the sub folders
Function fnGoSubFolders(objDir)
	If objDir <> "\System Volume Information" Then
		Call fnListFiles(objDir)
		For Each strDir In objDir.SubFolders
			Call fnGoSubFolders(strDir)
		Next
	End If
End Function

'Get a list of files in the (sub)folder matching the criteria and add it to the array
Function fnListFiles(objDir)
		For Each strFile In objDir.Files
		objExt = objFSO.GetExtensionName(strFile)		'set the file extension to search for
		Set objFile = objFSO.GetFile(strFile)			'get the details of the file
		If LCase(objExt) = LCase(strExt) And (DateDiff(strPeriod, objFile.DateLastModified, strNow) < strFileAge) Then	'check of extension and age match
			ReDim Preserve aryFiles(strCount)			'if file matches resize array to accomodate new entry
			aryFiles(strCount) = strFile				'add new file to array
			strCount = strCount + 1						'increase count for next file found to be added to array
		End If
	Next
End Function

' Do stuff with the array of matching files
Function fnDoBits
		strCount = 0	'reset counter
		For Each aryElem in aryFiles
		'wscript.echo "file = "&aryElem
		Set objFile = objFSO.GetFile(aryElem)
		Set objTextStream = objFile.OpenAsTextStream(ForReading, TristateFalse)
		strSearchThis = objTextStream.Read(objFile.Size)
		If InStr(strSearchThis, strSearchText) > 0 Or InStr(strSearchThis, strSearchText1) > 0 Then
			strCount = strCount + 1
		End If
		Next
		'WScript.Echo "Count = "&strCount
		If strCount > strMaxAllowed Then
		WScript.echo "Test Failed"
		WScript.Quit (1)
		Else
		WScript.echo "Test Passed"
		WScript.Quit (0)
		End If
End Function
