Home » Microsoft Operations Manager 2005 » Monitor File Modification

Monitor File Modification

Sometimes it can be difficult to monitor a application. For example even if the application hand the process can still be there. A solution can be to monitor if the application still write anything to the logfile. I have written a simple VB script that will monitor that.

strComputer = "."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("c:\log.txt")
Const EVENT_WARNING = 2
Set objShell = CreateObject("Wscript.Shell")
If DateDiff("n", objFile.DateLastModified, Now) > 1 Then
objShell.LogEvent EVENT_WARNING, _
"objFile is older than 4 hours. Please investigate. The file is: " & objFile
end If

If DateDiff("n", objFile.DateLastModified, Now) > 1 Then
This line is interesting. “n” menas minute, in this case an alert will be generated if the file haven’t been updated for a minute.
You can switch “n” to one of the following:

m – months
d – days
h – hours
n – minutes
s – seconds

If the file isent newer than your settings a event will be generated in the local event viewer. That event we can collect with MOM.

This line controlls which file to monitor
Set objFile = objFSO.GetFile("c:\log.txt")

How-to make MOM send alerts

  • Insert the script into MOM
  • Create a rule that monitor the event viewer
  • Create a rule that runs the script

You insert the script as a VBscript in Administrator Console under
Administrator Console
-Management Packs
–Scripts

You dont have to create any parameters for that script.

Create a new computer group and add you server to that group. Create a new rule group and associate your new computer group with that rule group. Under event rules create a new rule with the following settings

Data Provider> Provider name: Application
Data Provider> Windows NT Event log
Criteria> From source: WSH
Criteria> of type: Warning
Alert: markGenerate alert

Then one more rule that will run the script

Data Provider> Choose a time provider with a suitable time, or create a new (Modify…) 
Responses> Add your script (Add -> Launch a script)

That should do it. Note that it can take some minutes before the new rules works.


15 Comments

  1. The script failed with error code Microsoft VBScript compilation error: Expected end of statement. did you know why and how to correct it?

  2. So can your script be used in a ‘Timed Script Two State Monitor’?

    I have a client who wants the ‘newest file’ in a directory monitored. If the newest log file has a date modified time that is 5 minutes or older they want email notification and a Restart of the service.

    So if the newest is located in the listed directory, how would your script look?

    Directory = \\servername\Logs\Prod\Order\PRODORDER_*.log

  3. Hi,

    This script would do just fine for a little project that I have; however, I keep getting errors when I run it, Can you please upload the VBS somewhere so that I can download it?

    Thanks in advance

  4. Would this work with folders on shares? we need to monitor few folders on the shares, to alert, if no files are written to it every 30 mins..

  5. I have a VBS scripted created for this, but it results in a script error. Is can I email it and you can check where I am going wrong?

  6. hi, sorry dont have exactly that script written. But if you look around on the blog you will find a number of examples of scripts working with files. If you manage to build the VB script I have guides and how-to posts about implement it into Operations Manager

  7. Hi Anders,

    I need to monitor a logfile. The logfile gets updated randomly. The start of the updated line is either of these three:

    WARNING SQLSERVER:
    ERROR SQLSERVER:
    CRITICAL ERROR SQLSERVER:

    Need a script that will check for the LAST LINE updated for the word WARNING or ERROR Or CRITICAL and alert accordingly with the contents that follow after “SQLSERVER :”till the end of the file!

    Thanks!

  8. […] As you can see, it is normal FTP commands inside that file. If you want to test download a file from a FTP you dont have to check the ftp_log.txt file, instead you can check when the downloaded file was last updated, according to this post. You can also use this script to search your logfile for words, and generate events. […]

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.