Link to home
Start Free TrialLog in
Avatar of andressk
andresskFlag for Colombia

asked on

Help with VB script that search for a specific text string in a log

Hi,

Currently I´m working with the attached script.
It searches in a log file for a text string. This is working pretty fine, but for some logs it shows the date/time parameters that is what I need, but for another ones it doesn´t. So, I´ve been checking the different logs, and the date/time parameters appear on different lines depending of the search string and I´d like that the script shows the 3 or 4 lines above of the found search string to include the date/time parameters.

This is an example of the log:
-------------------
[2011-04-27 06:41:10,151] [INFO ] [com.xxxxxxxxxxxx.util.mail.MailReader:322] App can´t find the inbox server.
javax.mail.MessagingException: Connect failed;
  nested exception is:
      java.net.ConnectException: Connection refused: connect
---------------------
I´d like that the output message when the script finds the string "java.net.ConnectException" includes the lines since the line: "[2011-04-27 06:41:10,151] [INFO ]" until the line: "java.net.ConnectException: Connection refused: connect"

Thanks for any help
strFileName = Wscript.Arguments.Named("file") 'Get file name from argument
If strFileName = "" Then
    Wscript.Echo "Message: You must specify a File Name like this: /file:""blah.txt"""
    Wscript.Quit(4)
End If

strSearch = Wscript.Arguments.Named("search") 'Get search string form argument
If strSearch = "" Then
    Wscript.Echo "Message: You must specify a string to search for like this: /search:""ERROR"""
    Wscript.Quit(4)
End If

If strFailIf="" Then strFailIf="found" End If
If (strFailIf="found") Then
	strFound=3
	strNotFound=0
ElseIf (strFailIf="notfound" or strFailIf="not found") Then
	strFound=0
	strNotFound=3
Else
	strFound=4
	strNotFound=4
	Wscript.Echo "Message: ERROR, set strFailIf"
    Wscript.Quit(4)
End If	

yyyy = Datepart("yyyy",Date())
yy = Right(yyyy, 2)
m = Datepart("m",Date())
mm = Right("0" & m, 2)
d = Datepart("d",Date())
dd = Right("0" & d, 2)

If (InStr(1,strFileName,"${Date}",1) <> 0) Then
	If (InStr(1,strFileName,"${Date}",1) <> 0 AND strDateFormat <> "") Then
		strDateFormat = replace(strDateFormat, Space(1), " & Space(1) & ")
		strDateFormat = replace(strDateFormat, "_", " & ""_"" & ")
		strDateFormat = replace(strDateFormat, "/", " & ""/"" & ")
		strDateFormat = replace(strDateFormat, "-", " & ""-"" & ")
		strDateFormat = replace(strDateFormat, ".", " & ""."" & ")
		strFileName = Replace(strFileName,"${Date}",eval(strDateFormat),1,-1,1)
	Else 
		Wscript.Echo "Message: Error with ${Date} and strDateFormat. Please review instructions at top of script."
		Wscript.Quit(4)
	End If
End If

strPattern = strSearch

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

If Not objFSO.FolderExists("temp") Then
	objFSO.CreateFolder("temp")
End If

PrevLogFile =  "temp\" & strSearch & "-" & mm & "_"  & dd-1 & "_"  & yyyy & ".txt"
If objFSO.FileExists(PrevLogFile) Then
	set objPrevLogFile = objFSO.GetFile(PrevLogFile)
	objPrevLogFile.Delete
End If

PreviousFinds = 0
LogFile = "temp\" & strSearch & "-" & mm & "_"  & dd & "_"  & yyyy & ".txt"
If objFSO.FileExists(LogFile) Then
	Set objLog = objFSO.OpenTextFile(LogFile,1, True)

	
	If objFSO.FileExists(LogFile) Then
	If objFSO.GetFile(LogFile).Size = 0 Then
		Wscript.Echo "Logfile has no content"
		PreviousFinds = 0
	Else
		Set objLog = objFSO.OpenTextFile(LogFile, 1, True)
		PreviousFinds = objLog.ReadAll
		If Not IsNumeric(PreviousFinds) Then
			Wscript.Echo "Contents in logfile not numeric: " & PreviousFinds
			PreviousFinds = 0
		End If
	End if

End If
	
End If

If objFSO.FileExists( strFileName ) Then
  Set objFile = objFSO.GetFile(strFileName)
  If( IsEmpty( objFile ) = True ) Then
      WScript.Echo "Message: OBJECT NOT INITIALIZED"
      WScript.Echo "Statistic: 0"
      WScript.Quit(4)
    End If
     
  else
    WScript.Echo "Message: The file """ & strFileName & """ was not found!<BR>This could indicate that the file does not exist or you do not have the correct credentials for monitoring. <BR>" 
    WScript.Echo "Statistic: 0"
    WScript.Quit(strNotFound)
End If

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = strPattern
objRegEx.IgnoreCase = True
Set objReadFile = objFSO.OpenTextFile(strFileName, ForReading)
x=1
Do Until objReadFile.AtEndOfStream
    strSearchString = objReadFile.ReadLine
    Set colMatches = objRegEx.Execute(strSearchString)  
    If colMatches.Count > 0 Then
        For Each strMatch in colMatches   
			If int(x) > int(PreviousFinds) Then		
				Wscript.Echo "Statistic: " & PreviousFinds+1
				Wscript.Echo prev_line 
				Wscript.Echo "Message: New search for """ & strSearch & """ was found: " & strSearchString
					Set objLog = objFSO.OpenTextFile(LogFile, 2, True)
					objLog.WriteLine(Now & " - "  & PreviousFinds+1)
					objLog.Close
				Wscript.Quit(StrFound) 
			End If
		x=x+1
        Next
    End If
    prev_line1=strSearchString
Loop
If int(x) >1 Then
	Wscript.Echo "Statistic: " & PreviousFinds
    Wscript.Echo "Message: New search for """ & strSearch & """ was found, but previous searches did find """ & strSearchString & """."
    Wscript.Quit(StrFound) 	
End If
Wscript.Echo "Statistic: " & PreviousFinds 
Wscript.Echo "Message: Search for """ & strSearch & """ was not found."
Wscript.Quit(strNotFound)
objReadFile.Close

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of techhealth
techhealth
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andressk

ASKER

Hi techhealth,

Thanks for your replay, I already have added the lines but when I run the script, it doesn´t show the search string.

What am I doing wrong?
strFileName = Wscript.Arguments.Named("file") 'Get file name from argument
If strFileName = "" Then
    Wscript.Echo "Message: You must specify a File Name like this: /file:""blah.txt"""
    Wscript.Quit(4)
End If

strSearch = Wscript.Arguments.Named("search") 'Get search string form argument
If strSearch = "" Then
    Wscript.Echo "Message: You must specify a string to search for like this: /search:""ERROR"""
    Wscript.Quit(4)
End If

If strFailIf="" Then strFailIf="found" End If
If (strFailIf="found") Then
	strFound=3
	strNotFound=0
ElseIf (strFailIf="notfound" or strFailIf="not found") Then
	strFound=0
	strNotFound=3
Else
	strFound=4
	strNotFound=4
	Wscript.Echo "Message: ERROR, set strFailIf"
    Wscript.Quit(4)
End If	

yyyy = Datepart("yyyy",Date())
yy = Right(yyyy, 2)
m = Datepart("m",Date())
mm = Right("0" & m, 2)
d = Datepart("d",Date())
dd = Right("0" & d, 2)

If (InStr(1,strFileName,"${Date}",1) <> 0) Then
	If (InStr(1,strFileName,"${Date}",1) <> 0 AND strDateFormat <> "") Then
		strDateFormat = replace(strDateFormat, Space(1), " & Space(1) & ")
		strDateFormat = replace(strDateFormat, "_", " & ""_"" & ")
		strDateFormat = replace(strDateFormat, "/", " & ""/"" & ")
		strDateFormat = replace(strDateFormat, "-", " & ""-"" & ")
		strDateFormat = replace(strDateFormat, ".", " & ""."" & ")
		strFileName = Replace(strFileName,"${Date}",eval(strDateFormat),1,-1,1)
	Else 
		Wscript.Echo "Message: Error with ${Date} and strDateFormat. Please review instructions at top of script."
		Wscript.Quit(4)
	End If
End If

strPattern = strSearch

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

If Not objFSO.FolderExists("temp") Then
	objFSO.CreateFolder("temp")
End If

PrevLogFile =  "temp\" & strSearch & "-" & mm & "_"  & dd-1 & "_"  & yyyy & ".txt"
If objFSO.FileExists(PrevLogFile) Then
	set objPrevLogFile = objFSO.GetFile(PrevLogFile)
	objPrevLogFile.Delete
End If

PreviousFinds = 0
LogFile = "temp\" & strSearch & "-" & mm & "_"  & dd & "_"  & yyyy & ".txt"
If objFSO.FileExists(LogFile) Then
	Set objLog = objFSO.OpenTextFile(LogFile,1, True)

	
	If objFSO.FileExists(LogFile) Then
	If objFSO.GetFile(LogFile).Size = 0 Then
		Wscript.Echo "Logfile has no content"
		PreviousFinds = 0
	Else
		Set objLog = objFSO.OpenTextFile(LogFile, 1, True)
		PreviousFinds = objLog.ReadAll
		If Not IsNumeric(PreviousFinds) Then
			Wscript.Echo "Contents in logfile not numeric: " & PreviousFinds
			PreviousFinds = 0
		End If
	End if

End If
	
End If

If objFSO.FileExists( strFileName ) Then
  Set objFile = objFSO.GetFile(strFileName)
  If( IsEmpty( objFile ) = True ) Then
      WScript.Echo "Message: OBJECT NOT INITIALIZED"
      WScript.Echo "Statistic: 0"
      WScript.Quit(4)
    End If
     
  else
    WScript.Echo "Message: The file """ & strFileName & """ was not found!<BR>This could indicate that the file does not exist or you do not have the correct credentials for monitoring. <BR>" 
    WScript.Echo "Statistic: 0"
    WScript.Quit(strNotFound)
End If

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = strPattern
objRegEx.IgnoreCase = True
Set objReadFile = objFSO.OpenTextFile(strFileName, ForReading)
x=1
prev_line3= objReadFile.ReadLine
prev_line2= objReadFile.ReadLine
prev_ine1= objReadFile.ReadLine

Do Until objReadFile.AtEndOfStream
    strSearchString = objReadFile.ReadLine
    Set colMatches = objRegEx.Execute(strSearchString)  
    If colMatches.Count > 0 Then
        For Each strMatch in colMatches   
			If int(x) > int(PreviousFinds) Then		
				Wscript.Echo "Statistic: " & PreviousFinds+1

                       Wscript.Echo "Message: New search for """ & strSearch & """ was found: " 
                        Wscript.Echo prev_line3 
                        Wscript.Echo prev_line2 
                        Wscript.Echo prev_line1 
                        Wscript.Echo strSearchString
                              Set objLog = objFSO.OpenTextFile(LogFile, 2, True)
                              objLog.WriteLine(Now & " - "  & PreviousFinds+1)
                              objLog.Close
                        Wscript.Quit(StrFound) 
                        				
			End If
		x=x+1
        Next
    End If

    prev_line3=prev_line2
    prev_line2=prev_line1
    prev_line1=strSearchString

Loop
If int(x) >1 Then
	Wscript.Echo "Statistic: " & PreviousFinds
    Wscript.Echo "Message: New search for """ & strSearch & """ was found, but previous searches did find """ & strSearchString & """."
    Wscript.Quit(StrFound) 	
End If
Wscript.Echo "Statistic: " & PreviousFinds 
Wscript.Echo "Message: Search for """ & strSearch & """ was not found."
Wscript.Quit(strNotFound)
objReadFile.Close

Open in new window

So when the the file your script was search definitely contains the target string, the script still didn't return anything?  If that's the case, please take out the code that involves "prev_line" and try again.  Those lines really don't change the script logic, so I suspect the script still won't work after you remove those code.  It's somewhere else in the script that has problems.  Please let me know the result either way.
Yeah, with the old script it returns the search string and the line that contains it.
Maybe I did something wrong adding the lines?

Thanks
The problem was at this line:
Wscript.Echo "Message: New search for """ & strSearch & """ was found: " & strSearchString, I´ve added the lines prev_line3, prev_line2, prev_line1 without the  Wscript.Echo parameter and it seems to be working as expected.

Thanks