As a basic script to wrap this problem you can create a powershell-script in your CustomSensors-folder of PRTG with the following Content:
Param([string]$URL,[int]$warnlevel)
$error.clear()
$ActionPreference = "silentlyContinue"
$errorActionPreference = $ActionPreference
$warningActionpreference = $ActionPreference
# create a table to store the information
$table = New-Object system.Data.DataTable "result"
$col1 = New-Object system.Data.DataColumn channel,string
$col2 = New-Object system.Data.DataColumn value,decimal
$col3 = New-Object system.Data.DataColumn unit,string
$col4 = New-Object system.Data.DataColumn warning,string
$table.columns.add($($col1))
$table.columns.add($($col2))
$table.columns.add($($col3))
$table.columns.add($($col4))
# this function produces a well-formated prtg-xml-output out of a given table
function New-Xml
{
param($RootTag="prtg",$ItemTag="result", $ChildItems="*", $TextTag="OK", $Attributes=$Null)
Begin {
$xml = "<$RootTag>`n"
}
Process {
$xml += " <$ItemTag>`n"
foreach ($child in $_ | Get-Member -Type *Property $childItems)
{
$Name = $child.Name
if (-not "$($_.$name)" -eq "") {
$xml += " <$Name>$($_.$Name)</$Name>`n"
}
}
$xml += " </$ItemTag>`n"
}
End {
$xml += " <text>$TextTag</text>`n"
$xml += "</$RootTag>`n"
$xml
}
}
# initialize the webclient
$webclient = New-Object Net.WebClient
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
# download the page
$response = $webclient.DownloadString($URL)
# for example: $response: "[993][385][0][350][Test]"
# parse the result for 'words'
$regex = [regex]"\[([^\]]+)\]+"
$valArr = ($response | select-string -allmatches $regex | Select-Object -ExpandProperty matches | Select-Object -ExpandProperty value | ForEach {$_ -replace "\[|\]", ""})
# from the example: $valArr.Length: 5
# iterate through the results without the last value
$valueCount = $valArr.length - 2
for ($i=0; $i -le $valueCount; $i++)
{
$row = $table.NewRow();
$index = $i + 1
$row.channel = "Value $index"
$row.value = [int]$valArr[$i]
if ($row.value -gt $warnlevel) { $row.warning = "1"; }
$row.unit = "Count"
$table.Rows.Add($row)
}
# define the last value as response-text
$resultText = $valArr[-1]
# forward the generated table to xml-generator and store the result as $retval
$retval = $table | New-Xml -ChildItems Channel,Value,Unit,CustomUnit,Warning,Error -TextTag $resultText
# return the result
write-host $retval
exit
Now you can add this script to PRTG as a new CustomSensor 'EXE/Script Advanced'.
As Parameter use the url for the HTML-page and a threshold for triggering a warning-state in the format: "[url] [integer-for-maximum]"
For example, enter as parameters:
"http://www.example.org/testing 25"
This sensor returns all values it finds at the given URL as single channels —except for the last value in that row. The last value will not be returned in a sensor channel, but will be used as the sensor's 'last message'.
After that you can define a notification for the 'warning' status and you will be notified with the 'last-message' value.
Kind regards
Add comment