Simple script which checks the modified time (hours) of a log file

Recently I had to monitor a backup log file. If the modification time stamp of the log file was later than at a certain time then SCOM should fire a warning. In my case if the file was modified later than at 05:00 o’clock (24-hour time notation) an alert should occur. There was no characteristic inside the file to determine the state of the backup nor should it be to complex to fulfill this task.

My approach was to create a timed two state monitor using a vbscript.

Bild

Give this monitor an appropriate name and target. Don’t enable the monitor yet.

Bild

Run every 15 minutes or even less

Bild

Here you insert the script

Bild

Script:

**********************************

Dim oArgs
Set oArgs = WScript.Arguments
Dim oAPI
Set oAPI = CreateObject(“MOM.ScriptAPI”)

Dim objFile
Dim ValueToReturn
Dim oFso
Dim oFile
Dim oBag
Dim Filename
Dim DateLastModified

Set oFso = CreateObject(“Scripting.FileSystemObject”)

‘Path to the log file

Filename = “C:\1\log.txt”

If (oFso.FileExists(Filename)) Then

Set objFile = oFso.GetFile(Filename)

DateLastModified= objFile.DateLastModified

‘Here we just pick the hours of the time stamp. Since we are having 24-hour time notation it is easy to pick the hour-part e.g. 05:00 it would pick the “5″ or in 16:00 it would pick the “16″.

If DatePart(“h”,objFile.DateLastModified) < 5 Then

ValueToReturn = “OK”

Else

ValueToReturn = “FAILED”

End If

Else

‘Here you just could pick the return value 9999 if an error occurs e.g. for use in an three state monitor

ValueToReturn = 9999

End If

Set oBag = oAPI.CreatePropertyBag()
Call oBag.AddValue(“Status”,ValueToReturn)
Call oBag.AddValue(“Filename”,Filename)
Call oBag.AddValue(“DateLastModified”,DateLastModified)
Call oAPI.Return(oBag)

**********************************

Next build the unhealthy expression

Bild

Then build the healthy expression

Bild

Configure the health status of the object

Bild

Finally configure the alert here I just put the variables inside. of course you could add some more text into the alert description :)

Bild

Now as a last step you just have to set an override for the server object or group

Bild

Set the status enabled to true

Bild

About these ads

About scomfaq

Consultant
This entry was posted in Script. Bookmark the permalink.

10 Responses to Simple script which checks the modified time (hours) of a log file

  1. Stephen Turner says:

    Hi,

    Is there a way I could use this script to monitor a file that should be modified every five minutes?

    Thanks in advance

    Steve.

    • scomfaq says:

      Hi Steve

      As I understand you want to raise an alert if the file has not been modified for more than 5 minutes. You just need to change the if condition to…

      If DateDiff(“n”, objFile.DateLastModified, Now) < 5 then

      OK?

      Regards,

      Stefan

      • Turner, Stephen says:

        Hi Stefan,

        I thought so but if I remove the bottom section:

        End If

        Else

        ValueToReturn = 9999

        The script then errors, sorry to be a noob!

        Regards

        Stephen Turner

      • scomfaq says:

        Hi Stephen

        Ok if you want to remove the ValueToReturn=9999 part and just use “OK” and “FAILED”…

        Here the part you are asking:


        If (oFso.FileExists(Filename)) Then

        Set objFile = oFso.GetFile(Filename)

        DateLastModified = objFile.DateLastModified

        If DateDiff(“n”, objFile.DateLastModified, Now) < 5 Then

        ValueToReturn = "OK"

        Else

        ValueToReturn = "FAILED"

        End If

        End If

        Ok?

        Regards,

        Stefan

      • Turner, Stephen says:

        Awesome, Thank you very much.

        Must learn some VB skills!

        Regards

        Stephen Turner
        IT Systems Engineer
        Interserve PLC

        Services, maintenance and building
        Website: http://www.interserve.com
        Tel: 0121 524 8917
        Mob: 07990 800099
        Fax: 0845 838 5998
        Email: Stephen.turner@interserve.com

      • scomfaq says:

        Hi

        Great. You are very welcome!

        Regards,

        Stefan

  2. Stephen Turner says:

    Hi,

    Sorry to be a pain (again) but the script isn’t working as intended. It’s saying the monitor is critical when the minutes in the hour are less than 5 rather that if the file was modified less than 5 minutes ago.

    Any help would be appreciated.

    • scomfaq says:

      Hi Stephen

      I am a bit confused what your requirements are but you could try to change the operator “<" on the line here "If DateDiff(“n”, objFile.DateLastModified, Now) < 5 Then” to “>” (greather than). And see if it is o.k.

      Regards,

      Stefan

  3. Kitaab says:

    HI Stefan,

    I need to monitor an xml file to alert only if the file was not modifies in last 24 hours.

    This file gets modified a couple of times in a day, but if it not modified for more than 24 hours , i wish to get alerted for that.

    Any advice on how i can accomplish that.

  4. Kitaab says:

    I hope you are doing well.. Still looking to find a way around this request

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s