Script

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

17 Replies to “Simple script which checks the modified time (hours) of a log file

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

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

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

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

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

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

    The Query is comparing the time stamp from file last modified date and today’s date, so it would always raise false alaram where file is not modified for days.

    Can you please provide the script where it stores last modified date always and compare it with current time stamp of the file, if there is any change in the time stamp of the file current time stamp with last modified stamp.It should raise alert.

    Regards,
    Kiran Kumar

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

  5. Hi Stefan

    I have a service that creates multiple log files in a folder. Sometimes the service crashes, but you can’t realy see if it’s working ore not. The only way is to check the logfiles.
    Is it possible to do a script, witch generates an alert, when the modified date of a folder is older than 2 hours?

    Thanks in advance

  6. This script is really excellent and it is the first step to locate the files modified. Now is it possible to locate the modification itself within the file?
    Thanks,
    DOm

  7. Hi Stefan,

    My requirement is to monitor the log file for a servers; trigger an alert when any modification occurs on the file.
    Could you please help on that

    Thanks in advance.

Leave a Reply

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.