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.
The script failed with error code Microsoft VBScript compilation error: Expected end of statement. did you know why and how to correct it?
yes you could, I have a example script for 2 state monitor at http://contoso.se/blog/?p=1575
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
Hi, sorry dont have the VB file around. Let me know if you cant solve it and I will see if I can re-create the VB file in a sandbox.
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
Yes, it works to a share too. Sometimes you can map the share as a drive and use it that way, from inside the script.
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..
I will upload a blog post about this later today, including a working script and how to use it.
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?
Hi Anders,
Teh following link has teh Vbscribt that has tried tocreate an array and check for the last array content, compare and let know the result. Can this be incorportated as SCOM alert rendering one?
http://www.big-fly.net/v2/index.php?option=com_content&view=article&id=48:read-the-last-line-of-a-file-with-vbscript&catid=42:vbscript&Itemid=56
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
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!
[…] Alert when LastModifiedDate of file is older than 5 min Hi Take a look at my script here http://contoso.se/blog/?p=41 You can simple change the > 1 to < 5, and you will get an alert if the file has not been […]
[…] the newest file or how does it work? I have a script that will check last modify time on a file http://contoso.se/blog/?p=41 , that script could check that you have a new file. —– Regards Anders Bengtsson Microsoft MVP – […]
[…] 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. […]