Note from Paessler TechSupport: While the File Content Sensor cannot work with wildcards in the moment, the following script should help and achieve this. Please bear in mind, it comes without official support from us, and using it will be at your own risk.
This would be possible with custom scripts - you could create a VBScript and search for the newest file in the directory etc... then open the file as a text-stream and inspect the content and processing it.
Did similar stuff several times myself, either because PRTG did not have a default sensor or I did but I wanted to have the data processed and presented differently.
There are various script examples in the knowledge base here and around the web on how this can be accomplished... In the end if often depends on specific needs so it is "hard" to come up with a generic script... if at all it would be dependent on parameters like path, wildcard filename, search phrase 1 and probably 2 as arguments, then processing this - searching the directory for the file-pattern, using the newest file (modified date) and processing it while searching for the phrases provided. If found, it would just give you a positive response or not found a negative response.
I personally would like it more advanced and specific to the needs - like how many times did the phrase come up - and if the log provides more information - I would even process and provide this.
Understanding that not everyone can write scripts, I thought I provide the below example script that might hopefully help out a bit:
'Execute:
'Argument 0 = File-Path
'Argument 1 = File-Pattern
'Argument 2 = File-Extension
'Argument 3 = Search Phrase
'GenericLogSensor.vbs "C:\path\file.txt" "file*" ".log" "error"
'Returns -1 file not found / 0 keyword not found / 1 keyword was found
Dim strPath, strFilePattern, strFileExtension, strSearchPattern
If WScript.Arguments.Count = 4 Then
strPath = WScript.Arguments.Item(0)
strFilePattern = WScript.Arguments.Item(1)
strFileExtension = WScript.Arguments.Item(2)
strSearchPattern = WScript.Arguments.Item(3)
Else
Wscript.Echo "Usage: cscript GenericLogSensor.vbs ""C:\path\file.txt"" ""file*"" "".log"" ""error"""
Wscript.Quit
End If
If Left(strFileExtension, 1) <> "." Then
strFileExtension = "." & strFileExtension
End If
If Right(strFilePattern, 1) <> "*" Then
strFilePattern = strFilePattern & "*"
End If
Dim strFound
strFound = -1 'default return value
Dim objFS
Set objFS = CreateObject("Scripting.FileSystemObject")
If objFS.FolderExists(strPath) Then
Dim objFolder
Set objFolder = objFS.GetFolder(strPath)
Dim objRE
Set objRE = New RegExp
objRE.IgnoreCase = True
objRE.Pattern = strFilePattern
Dim objFile, objNewestFile
Dim datFileDate
datFileDate = DateAdd("yyyy",-1000,Date())
For Each objFile in objFolder.Files
If LCase(Right(objFile.Name, Len(strFileExtension))) = LCase(strFileExtension) Then
If objRE.Test(objFile.Name) Then
If DateDiff("s",datFileDate,objFile.DateLastModified) > 0 Then
datFileDate = objFile.DateLastModified
Set objNewestFile = objFile
End If
End If
End If
Next
Dim strFoundFileName
On Error Resume Next 'make it easy to understand and modify - not the perfect way to handle this
strFoundFileName = objNewestFile.Name
On Error GoTo 0
If Len(strFoundFileName) > 0 Then 'this now means we found a file
strFound = 0
Dim strLine
Dim objTS
Set objTS = objNewestFile.OpenAsTextStream(1, -0)
Do While Not objTS.AtEndOfStream
strLine = objTS.ReadLine()
If InStr(1, strLine, strSearchPattern, 1) Then
strFound = 1
Exit Do
End If
Loop
objTS.Close
Set objTS = Nothing
End If
Set objNewestFile = Nothing
Set objFolder = Nothing
End If
Set objFS = Nothing
WScript.echo "<prtg>"
WScript.echo "<result>"
WScript.echo "<channel>" & strSearchPattern & "</channel><value>" & strFound & "</value>"
WScript.echo "</result>"
WScript.echo "</prtg>"
This is an Advanced EXE sensor script - please put it in the according folder in your PRTG installation.
The Parameters are:
- path
- expression to find the newest file (without the file-extension)
- file-extension
- keyword to find in the file
Results are:
- -1 = file not found
- 0 = keyword not found in file
- 1 = keyword found in file
Now you can modify the script a bit to your needs and process the found text in the file further - if you want to. Since the results are in XML you should be able to even expand this way further and give PRTG more data / channels you can monitor on. Adding additional arguments might allow you to search for various keywords as well...
Hope this helps some of you!
Regards
Florian Rossmark
www.it-admins.com
Add comment