Hmm... try this:
Dim strPath
If WScript.Arguments.Count = 1 Then
strPath = WScript.Arguments.Item(0)
Else
Wscript.Echo "Usage: cscript GenericLogSensor.vbs ""C:\path"""
Wscript.Quit
End If
Dim strReturn
strReturn = -99 'default return value = -99 would mean the script didn't find anything - value in minutes
Dim objFS
Set objFS = CreateObject("Scripting.FileSystemObject")
If objFS.FolderExists(strPath) Then
Dim objFolder
Set objFolder = objFS.GetFolder(strPath)
For Each objFile in objFolder.Files
wscript.echo DateDiff("n",objFile.DateCreated,Now())
If DateDiff("n",objFile.DateCreated,Now()) <= strReturn Then
strReturn = DateDiff("n",objFile.DateCreated,Now()) 'this will find the newest file...
End If
Next
Set objFolder = Nothing
End If
Set objFS = Nothing
WScript.echo "<prtg>"
WScript.echo "<result>"
WScript.echo "<channel>Newest file age in minutes</channel><value>" & strReturn & "</value>"
WScript.echo "</result>"
WScript.echo "</prtg>"
The script is an advanced EXE/XML sensor and needs to be placed in the directory:
C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML
It is a VBS script script - so the extension is .VBS - e.g. NewestFile.vbs
You can test it on a command prompt like this:
CSCRIPT NewestFile.vbs "c:\testfolder"
As you can see - it expects a PATH parameter - use text-quotes for the path as well.. even in PRTG when you set the parameters...
Return values:
- it returns a TIME in MINUTES or -99
- -99 means that the script had an issue in general
- 0 means the newest file found was created within 0 minutes
- 1 or 2 or 3 or 4 ...... 999999 means the file was created N minutes ago
So - what do you do with this?
- You create an EXE Advanced sensor pointing it to the directory as parameter
- you edit the sensor channel "Newest file age in minutes" and set limits
- you schedule the sensor to run every 5 minutes
What will happen:
- every 5 minutes the sensor will execute
- if there is a file that is newer then 6 minutes it will raise an error
- this includes -99 what means there was a generic Error
- this includes 0 what means within the past 1 minute
- this accounts for the slack between the sensor execution of every 5 minutes
- this will set the sensor back to NORMAL after the second or third run - meaning after 10 minutes, 15 minutes - assuming you run it every 5 minutes ...
The 5 minutes and 6 minutes is just an example - there is a slack / the execution time is not exactly every 5 minutes, so you need to account for that, that's why I suggested 6 minutes or newer as lower error limit - the NOTIFICATION needs to be adjust as well accordingly - you probably just want ERROR notifications...
Note:
This will not fire for every new file - for this you would need a real file-system monitoring solution that works with AUDITING - but it at least will raise an error state of the sensor if a file (or multiple) have been created...
Hope this helps you - the script could be adjusted more and report more advanced information in theory - but I think this should cover your request already...
Regards
Florian Rossmark
www.it-admins.com
Add comment