I wrote a script to check only the last line of a file. The scripts checks the last line every time it run. If you search my blog you will find a number of script to read logfiles. Create a new two state script monitor where you include the script below. In my example script I looks in the C:\temp\myfile.txt file for the word “Warning” ( varWarPos = Instr(strLine, “Warning”) )
- Unhealthy Expression
- Property[@Name=’Status’] Contains warning
- Healthy Expression
- Property[@Name=’Status’] Contains ok
- Alert description
- You could write any alert description here, but if you include the following parameters you will see the whole line and the status in the alert description.
- State $Data/Context/Property[@Name=’Status’]
- Line $Data/Context/Property[@Name=’Line’]$
Set oAPI = CreateObject(“MOM.ScriptAPI”)
Set oBag = oAPI.CreatePropertyBag()
Const ForReading = 1
strFile = “C:\TEMP\myfile.txt”
Set FSO = CreateObject(“Scripting.FileSystemObject”)
Set inFile = FSO.OpenTextFile(strFile)
lines = Split( inFile.ReadAll, vbLF )
lineCount = UBound(lines)
Set FSO = CreateObject(“Scripting.FileSystemObject”)
Set TextFile = FSO.OpenTextFile(strFile, ForReading)
For i = 1 to lineCount
TextFile.ReadLine
Next
strLine = TextFile.ReadLine
varWarPos = Instr(strLine, “Warning”)
If varWarPos > 0 Then
varStatus = “Warning”
varLine = strLine
End If
TextFile.Close
If varStatus = “Warning” Then
Call oBag.AddValue(“Line”, varLine)
Call oBag.AddValue(“Status”,”warning”)
Call oAPI.Return(oBag)
Else
Call oBag.AddValue(“Status”,”ok”)
Call oAPI.Return(oBag)
End If
Discovery Sources in Service Manager 2010
Runbook servers in different time zones
Thanks! really good comment
Hi Anders!
I think you might want to modify that script a bit to avoid exceptions on empty files and also to avoid an unnecessary for-loop. For example like this:
————–
Option Explicit
Const ForReading = 1
Dim strFile, FSO, inFile, FileInfo, lines, strLine, varWarPos, varStatus, varLine
strFile = “C:\TEMP\myfile.txt”
Set FSO = CreateObject(“Scripting.FileSystemObject”)
‘// Check that the file is not empty before trying to read it
‘// In VBScript, you get an exception when reading empty files
Set FileInfo = FSO.GetFile(strFile)
If FileInfo.Size > 0 Then
Set inFile = FSO.OpenTextFile(strFile)
lines = Split( inFile.ReadAll, vbLf )
‘// Since we allready have the entire file as an array of line,
‘// just pick the last line/index/post/whateveryouwannacallit from the array
‘// and put it in the strLine variable and there’s the last line for you.
strLine = lines(UBound(lines))
varWarPos = Instr(strLine, “Warning”)
If varWarPos > 0 Then
varStatus = “Warning”
varLine = strLine
End If
inFile.Close
Else
‘// File is empty, handle it here… or not.
WScript.Echo “File is empty!”
End If
————-
Now, it might be a good idea, also, to check if the program writing to that particular file is adding a line-feed (new row) after each write. If that’s the case I would add a
——-
If Trim(Len(lines(UBound(lines)))) > 0 Then
strLine = lines(UBound(lines))
Else
strLine = lines(UBound(lines)-1)
End If
——-
Instead of the single strLine = lines(UBound(lines)) line.
And, yes. I omitted the Opsmgr-parts since they would be the same. Just place the propertybag stuff inside the If-clause unless you want to fill it with empty variables.