What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Logfile Reader

Votes:

0

Good afternoon,

I'm looking for a script. That does the following. He must be able to read a log file with the following. First File: 12:57:52 - 19/10/18 Last File: 09:50:00 - 22/10/18

If Last File has more than 4 hours of time difference with the time now. There must be an error message.

Is that possible?

Thanks in advance

Tim

logfile prtg script

Created on Oct 22, 2018 10:09:07 AM



6 Replies

Votes:

0

You could engage a script for this - it reads the lines - transfers the values to dates and then you end up with a value - this value will be numeric - you now send upper and lower error limit on the sensor channel and done...

Dim sReturnValue
sReturnValue = -2222222 '-2222222 = generic error / high value cause the datediff could give you negative values in theory

Dim sFile
sFile = WScript.Arguments.Item(0)

Dim bolFileExists
bolFileExists = False

Dim strLine1
Dim strLine2

Dim objFS
Set objFS = CreateObject("Scripting.FileSystemObject")
	If objFS.FileExists(sFile) Then
		bolFileExists = True
		Dim strLine
		Dim intLineCount
		Dim objTS
		Set objTS = objFS.OpenTextFile(sFile, 1)
			intLineCount = 0
			Do While Not objTS.AtEndOfStream
				intLineCount = intLineCount + 1
				strLine = objTS.ReadLine()
				Select Case intLineCount
					Case 1
						strLine1 = strLine
					Case 2
						strLine2 = strLine
					Case Else
						Exit Do 'ignore everything after line 2
				End Select
			Loop
		objTS.Close
		Set objTS = Nothing		
	End If
Set objFS = Nothing

If bolFileExists Then
	If Len(strLine1) > 0 And Len(strLine2) > 0 Then
		strLine1 = Replace(strLine1, "First File:", "", 1, -1, 1)
		strLine2 = Replace(strLine2, "Last File:", "", 1, -1, 1)
		strLine1 = Trim(Split(strLine1,"-")(1)) & " " & Trim(Split(strLine1,"-")(0)) 'first date then time
		strLine2 = Trim(Split(strLine2,"-")(1)) & " " & Trim(Split(strLine2,"-")(0)) 'first date then time
		If Len(strLine1) > 0 And Len(strLine2) > 0 Then
			If IsDate(strLine1) And IsDate(strLine2) Then
				sReturnValue = DateDiff("h", strLine1, strLine2) 'h = hourly / n = Minute 
			Else
				sReturnValue = -5555555 '-5555555 = File Content Error - no date value! / high value cause the datediff could give you negative values in theory
			End If
		Else
			sReturnValue = -4444444 '-4444444 = File Content Error in regards to the date/time values / high value cause the datediff could give you negative values in theory
		End If
	Else
		sReturnValue = -3333333 '-3333333 = File Content Error / high value cause the datediff could give you negative values in theory
	End If
Else
	sReturnValue = -1111111 '-1111111 = File not found error / high value cause the datediff could give you negative values in theory
End If

WScript.echo "<prtg>"
	WScript.echo "<result>"
	WScript.echo "<channel>DateDiffFromLog</channel><value>" & sReturnValue & "</value>"
	WScript.echo "</result>"
WScript.echo "</prtg>"

Test in DOS/CMD like this before you go to PRTG as an ADVANCED SCRIPT EXE/XML sensor (need to copy it on the server folder as well)

CSCRIPT SCRIPTNAME.VBS "c:\temp\testfile.log" 

SCRIPTNAME.VBS of course is your script - CSCRIPT before avoids all those message boxes... easier and better output...

Couldn't test it fully - your date format is different from mine - you have DAY/MONTH/YEAR and I am localized to MONTH/DAY/YEAR - in any case - date should be before time that why the lines 43 and 44 are important...

The script actually transforms the content of your logfile..

Not sure where this log comes from and if there might be a better way to cover this - but this script should at least cover the request you had in regards to the log..

Regards

Florian Rossmark

www.it-admins.com

Created on Oct 22, 2018 3:35:21 PM



Votes:

0

Hi Florian thanks for helping me again.

when I test the script I get the following message in CMD. C: \ TimTest \ Recordingcheck.vbs (43, 3) Run-time error Microsoft VBScript: The subscript is out of range: '[number: 1]'

When I outline line 43 and 44. then I get this result <prtg> <result> <channel> DateDiffFromLog </ channel> <value> -5555555 </ value> </ result> </ prtg>

The whole log looks like this: {{{User logged in, proceed X300> #GETSTATS Capacity: 128.04GB Used: 0.00% Write Protected: 0.00% First File: N / A Last File: N / A Days Recording: 0.0 Model Number: OCZ-VERTEX4 Serial Number: OCZ-04C253O2BG11WA25

  1. END X300> QUIT }}} It is a camera recorder. The purpose of the check is: Whether the recorder is still recording. Sometimes the recorder is off and then he gives this text in the log. That may be ignored. First File: N / A Last File: N / A

Thank you very much again.

Tim

Created on Oct 23, 2018 9:35:39 AM



Votes:

0

Hi Tim,

Didn't even realize it - looks like you got quite hooked up and start to monitor out of the box - very nice!

Since the log you posted first is different from the real log - there of course was a mistake in the script - so I had to account for the differences and adjusted the script a bit...

Dim sReturnValue
sReturnValue = -2222222 '-2222222 = generic error / high value cause the datediff could give you negative values in theory

Dim sFile
sFile = WScript.Arguments.Item(0)

Dim bolFileExists
bolFileExists = False

Dim strLine1
Dim strLine2

Dim objFS
Set objFS = CreateObject("Scripting.FileSystemObject")
	If objFS.FileExists(sFile) Then
		bolFileExists = True
		Dim strLine
		Dim intLineCount
		Dim objTS
		Set objTS = objFS.OpenTextFile(sFile, 1)
			intLineCount = 0
			Do While Not objTS.AtEndOfStream
				strLine = objTS.ReadLine()
				If InStr(1, strLine, "First File:", 1) > 0 Then
					intLineCount = intLineCount + 1
					strLine1 = strLine
				ElseIf InStr(1, strLine, "Last File:", 1) > 0 Then
					intLineCount = intLineCount + 1
					strLine2 = strLine
				End if
				If intLineCount = 2 Then
					Exit Do 'ignore everything after linecount = 2 - meaning both lines have been found
				End If
			Loop
		objTS.Close
		Set objTS = Nothing		
	End If
Set objFS = Nothing

If bolFileExists Then
	If Len(strLine1) > 0 And Len(strLine2) > 0 Then
		strLine1 = Replace(strLine1, "First File:", "", 1, -1, 1)
		strLine2 = Replace(strLine2, "Last File:", "", 1, -1, 1)
		If UBound(Split(strLine1, "-")) = 1 And UBound(Split(strLine2, "-")) = 1 Then
			strLine1 = Trim(Split(strLine1,"-")(1)) & " " & Trim(Split(strLine1,"-")(0)) 'first date then time
			strLine2 = Trim(Split(strLine2,"-")(1)) & " " & Trim(Split(strLine2,"-")(0)) 'first date then time
			If Len(strLine1) > 0 And Len(strLine2) > 0 Then
				If IsDate(strLine1) And IsDate(strLine2) Then
					sReturnValue = DateDiff("h", strLine1, strLine2) 'h = hourly / n = Minute 
				Else
					sReturnValue = -5555555 '-5555555 = File Content Error - no date value! / high value cause the datediff could give you negative values in theory
				End If
			Else
				sReturnValue = -4444444 '-4444444 = File Content Error in regards to the date/time values / high value cause the datediff could give you negative values in theory
			End If
		Else
			sReturnValue = -6666666 '-6666666 = File Content Error - no time - date value found  / high value cause the datediff could give you negative values in theory
		End If
	Else
		sReturnValue = -3333333 '-3333333 = File Content Error / high value cause the datediff could give you negative values in theory
	End If
Else
	sReturnValue = -1111111 '-1111111 = File not found error / high value cause the datediff could give you negative values in theory
End If

WScript.echo "<prtg>"
	WScript.echo "<result>"
	WScript.echo "<channel>DateDiffFromLog</channel><value>" & sReturnValue & "</value>"
	WScript.echo "</result>"
WScript.echo "</prtg>"

As you can see - now I search in the whole output file for the FIRST FILE and LAST FILE till I found both then I exit the loop and close the file.

Further is there an unexpected value "N / A" in your log file (not available) - this was not accounted for and is another reason you got an error - I changed this and make sure the FIRST FILE and LAST FILE value include a "-" so the error that was raised there is obsolete as well - but it added an error -6666666 - what means that the script found a not valid output format - you might wanna change the error number or play further with it...

Alternative you could adjust the output area starting at line 66 like this:

Dim strChannelTwo
WScript.echo "<prtg>"
	WScript.echo "<result>"
	WScript.echo "<channel>DateDiffFromLog</channel><value>" & sReturnValue & "</value>"
	strChannelTwo = "<channel>InvalidTimeDateValue</channel><value>" 
	If sReturnValue = -6666666 Then
		strChannelTwo =  strChannelTwo & 1 '1 = in error / true state - use channel limits in PRTG and raise error if not 0 so you know more...
	Else
		strChannelTwo =  strChannelTwo & 0 '0 = no error
	End If
	strChannelTwo =  strChannelTwo & "</value>"
	WScript.echo strChannelTwo
	WScript.echo "</result>"
WScript.echo "</prtg>"

This would specifically catch the not a time-date value error - and determine it a bit more...

Theoretically you could add a TEXT output as well and show it in the status for the sensor in PRTG assuming it helps...

'Dim strChannelTwo
Dim strText
WScript.echo "<prtg>"
	WScript.echo "<result>"
	WScript.echo "<channel>DateDiffFromLog</channel><value>" & sReturnValue & "</value>"
	' strChannelTwo = "<channel>InvalidTimeDateValue</channel><value>" 
	' If sReturnValue = -6666666 Then
		' strChannelTwo =  strChannelTwo & 1 '1 = in error / true state - use channel limits in PRTG and raise error if not 0 so you know more...
	' Else
		' strChannelTwo =  strChannelTwo & 0 '0 = no error
	' End If
	' strChannelTwo =  strChannelTwo & "</value>"
	' WScript.echo strChannelTwo
	WScript.echo "</result>"
	strText = "<text>"
	Select Case sReturnValue
		Case -1111111
			strText = strText & "File not found error"
		Case -2222222
			strText = strText & "script - generic error"
		Case -3333333
			strText = strText & "File Content Error"
		Case -4444444
			strText = strText & "File Content Error in regards to the date/time values"
		Case -5555555
			strText = strText & "File Content Error - no date value!"
		Case -6666666
			strText = strText & "File Content Error - no time - date value found"
		Case Else
			strText = strText &  "valid date values found"
	End Select
	strText = strText & "</text>"
	Wscript.Echo strText
WScript.echo "</prtg>"

Hope this helps :-)

Regards

Florian Rossmark

www.it-admins.com

Created on Oct 23, 2018 2:05:01 PM



Votes:

0

Florian

It seems to work well. but I explained it wrong.

The time of the First File does not matter. I want to know the difference with the time now and the last file. Is that possible?

Tim

Created on Oct 24, 2018 1:56:31 PM



Votes:

0

Haha... okay... naughty...

Replace this line sReturnValue = DateDiff("h", strLine1, strLine2) 'h = hourly / n = Minute with this line sReturnValue = DateDiff("h", strLine2, Now()) 'h = hourly / n = Minute

It is either NOW or NOW() - not sure right now... this should give you the time difference as a positive value - if not - exchange STRLINE2 with NOW() in the DATEDIFF what would reverse the resulting time value...

The rest of the script can stay as is cause STRLINE2 will hold the LAST FILE date/time value...

Does that work?

I think we soon get you to write your own scripts :-)

Regards

Florian

Created on Oct 24, 2018 2:06:03 PM



Votes:

0

Florian,

Great it works! There will be a day where I can write a script myself :D. I started doing this a couple of weeks ago. but did not get the answers from the script that I get now :D

thanks again for your help !!

Tim

Created on Oct 25, 2018 7:55:44 AM




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.