VB Script that checks for a string on a log file

andressk
andressk used Ask the Experts™
on
Hello,
I´m currently working with this script that search for a string in a log and alarms if it finds the string.
It is working fine, but for some logs, I see that it´s duplicating the alarm, showing the same event every time the script runs and I´d like to check if there is sometihing that I have to change to make the script only alarms once when it finds the string.

Thank you
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_line1= 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: " & prev_line3 & prev_line2 & prev_line1 & 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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
The problem is that the script would not know that it already alerted previously.  What I have done in the past is create a bread crumb type text file that I place previous alerts in and have the script cross reference it before alerting.  That way I can adjust how often I am alerted on scripts that I run every minute/ 5 minutes.  
Andrew Hancock (VMware vExpert / EE Fellow)VMware and Virtualization Consultant
Fellow 2018
Expert of the Year 2017
Commented:
you may want to consider the following:-

for Alarm's in VMware ESX/ESXi, (if that's what you are using)

XtraVirt's vAlarm

http://xtravirt.com/valarm-free-desktop-tool-for-monitoring-vcenter-alarms

Nick Weaver's vSphere Mini Monitor solve this vCenter Server management issue.

http://nickapedia.com/2010/02/15/vsphere-mini-monitor-now-with-more-cowbell/

Author

Commented:
Hi,
Thanks for the replies, I´m using Orion APM and the script search for different strings in different logs. The issue is not presenting on all cases. I´ve been checkinkg the script and I don´t know if this field is working as it is supposed:

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

Author

Commented:
Thanks, but I did a mistake, wrong zone

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial