Home » Scripts » Check Last Line Only

Contoso.se

Welcome to contoso.se! My name is Anders Bengtsson and this is my blog about Azure infrastructure and system management. I am a senior engineer in the FastTrack for Azure team, part of Azure Engineering, at Microsoft.  Contoso.se has two main purposes, first as a platform to share information with the community and the second as a notebook for myself.

Everything you read here is my own personal opinion and any code is provided "AS-IS" with no warranties.

Anders Bengtsson

MVP
MVP awarded 2007,2008,2009,2010

My Books
Service Manager Unleashed
Service Manager Unleashed
Orchestrator Unleashed
Orchestrator 2012 Unleashed
OMS
Inside the Microsoft Operations Management Suite

Check Last Line Only

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

2 Comments

  1. 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.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.