-
-
Save tareqy/bb66a0f898922e832417847b9a9ad1ea to your computer and use it in GitHub Desktop.
| Param([string]$url) | |
| $warningpreference = "silentlyContinue" | |
| $global:resultText = "OK" | |
| # 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,string | |
| $col3 = New-Object system.Data.DataColumn unit,string | |
| $col4 = New-Object system.Data.DataColumn customUnit,string | |
| $col5 = New-Object system.Data.DataColumn warning,string | |
| $col6 = New-Object system.Data.DataColumn float,string | |
| $col7 = New-Object system.Data.DataColumn mode, string | |
| $table.columns.add($($col1)) | |
| $table.columns.add($($col2)) | |
| $table.columns.add($($col3)) | |
| $table.columns.add($($col4)) | |
| $table.columns.add($($col5)) | |
| $table.columns.add($($col6)) | |
| $table.columns.add($($col7)) | |
| # To get this script running with PRTGNetworkMonitor you need to enable RemoteSigned-scripting for 32bit-processes. | |
| # This can be done with running 'c:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe "Set-ExecutionPolicy RemoteSigned"' on a system with default paths | |
| # this function produces a well-formated xml-output out of a given table | |
| function New-Xml | |
| { | |
| param($RootTag="ROOT",$ItemTag="ITEM", $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 | |
| } | |
| } | |
| $web = New-Object Net.WebClient | |
| Try { | |
| $response = $web.DownloadString($url) | |
| } | |
| Catch { | |
| Write-Warning "$($error[0])" | |
| } | |
| # Stubbing input for development: | |
| #$response="Active connections: 291`r`n | |
| #server accepts handled requests`r`n | |
| # 16630948 16630949 31070465`r`n | |
| #Reading: 6 Writing: 179 Waiting: 106" | |
| $responseArray = $response.split(�`r`n�) | |
| ForEach ($responseLine in $responseArray){ | |
| if ($responseLine.startswith('Active')) { | |
| $keyval = $responseLine.split(":") | |
| $row = $table.NewRow(); | |
| $row.channel = "$($keyval[0])"; | |
| $row.value = $keyval[1] | |
| $row.unit = "Count" | |
| $row.mode = "absolute" | |
| $table.Rows.Add($row) | |
| } | |
| if ($responseLine.startswith('Reading:')) { | |
| ($responseLine -match "Reading: [0-9]+") | out-null | |
| $keyval = $matches[0].split(": ") | |
| $row = $table.NewRow(); | |
| $row.channel = "$($keyval[0])"; | |
| $row.value = $keyval[2] | |
| $row.unit = "Count" | |
| $row.mode = "absolute" | |
| $table.Rows.Add($row) | |
| ($responseLine -match "Writing: [0-9]+") | out-null | |
| $keyval = $matches[0].split(": ") | |
| $row = $table.NewRow(); | |
| $row.channel = "$($keyval[0])"; | |
| $row.value = $keyval[2] | |
| $row.unit = "Count" | |
| $row.mode = "absolute" | |
| $table.Rows.Add($row) | |
| ($responseLine -match "Waiting: [0-9]+") | out-null | |
| $keyval = $matches[0].split(": ") | |
| $row = $table.NewRow(); | |
| $row.channel = "$($keyval[0])"; | |
| $row.value = $keyval[2] | |
| $row.unit = "Count" | |
| $row.mode = "absolute" | |
| $table.Rows.Add($row) | |
| } | |
| if (!$responseLine.length -eq 0 -and ! $responseLine.startswith('Active connections:') -and ! $responseLine.startswith('server') -and ! $responseLine.startswith('Reading:')) { | |
| $values = $responseLine.trim().split(" ") | |
| $row = $table.NewRow(); | |
| $row.channel = "Accepts"; | |
| $row.value = "$($values[0])" | |
| $row.unit = "Count" | |
| $row.mode = "difference" | |
| $table.Rows.Add($row) | |
| $row = $table.NewRow(); | |
| $row.channel = "Handled"; | |
| $row.value = $values[1] | |
| $row.unit = "Count" | |
| $row.mode = "difference" | |
| $table.Rows.Add($row) | |
| $row = $table.NewRow(); | |
| $row.channel = "Requests"; | |
| $row.value = $values[2] | |
| $row.unit = "Count" | |
| $row.mode = "difference" | |
| $table.Rows.Add($row) | |
| } | |
| } | |
| # forward the generated table to xml-generator and store the result | |
| $retval = $table | New-Xml -RootTag prtg -ItemTag result -ChildItems Channel,Value,Unit,CustomUnit,Warning,Float,Mode -TextTag $resultText | |
| # return the result | |
| write-host $retval |
Updated version of the code
`
param (
[string]$url
)
$WarningPreference = 'SilentlyContinue'
$resultText = 'OK'
Set TLS version to 1.2 or 1.3
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13
Create a table to store the information
$table = New-Object System.Data.DataTable 'result'
$columns = @('channel', 'value', 'unit', 'customUnit', 'warning', 'float', 'mode')
foreach ($col in $columns) {
$table.Columns.Add((New-Object System.Data.DataColumn $col, 'System.String'))
}
Function to produce a well-formatted XML output from a given table
function New-Xml {
param (
[string]$RootTag = 'ROOT',
[string]$ItemTag = 'ITEM',
[string]$TextTag = 'OK',
[string[]]$Attributes = $null
)
$xml = "<$RootTag>`n"
foreach ($row in $table.Rows) {
$xml += " <$ItemTag>`n"
foreach ($col in $Attributes) {
$value = $row[$col]
if ($value) {
$xml += " <$col>$value</$col>`n"
}
}
$xml += " </$ItemTag>`n"
}
$xml += " <text>$TextTag</text>`n"
$xml += "</$RootTag>`n"
$xml
}
Download response from the URL
$web = New-Object Net.WebClient
Try {
$response = $web.DownloadString($url)
# Stubbing input for development:
#$response="Active connections: 291`r`n
#server accepts handled requestsrn
16630948 16630949 31070465rn
#Reading: 6 Writing: 179 Waiting: 106"
} Catch {
Write-Warning "$($error[0])"
exit
}
Process response
$responseArray = $response -split "rn"
$responseLine=''
$prevResponseLine=''
foreach ($responseLine in $responseArray) {
if ($responseLine -match '^Active connections:\s*(\d+)') {
$row = $table.NewRow()
$row.channel = 'Active connections'
$row.value = $matches[1]
$row.unit = 'Count'
$row.mode = 'absolute'
$table.Rows.Add($row)
} elseif ($responseLine -match '^Reading:\s*(\d+) Writing:\s*(\d+) Waiting:\s*(\d+)') {
# Create a hash table to map channels to values
$channelMap = @{
'Reading' = $matches[1]
'Writing' = $matches[2]
'Waiting' = $matches[3]
}
# Iterate over each entry in the hash table and add rows to the DataTable
foreach ($channel in $channelMap.Keys) {
$row = $table.NewRow()
$row.channel = $channel
$row.value = $channelMap[$channel]
$row.unit = 'Count'
$row.mode = 'absolute'
$table.Rows.Add($row)
}
} elseif ($prevResponseLine -eq 'server accepts handled requests' -and $responseLine -match '^\s*(\d+)\s+(\d+)\s+(\d+)') {
# Create a hash table to map channels to values
$channelMap = @{
'Accepts' = $matches[1]
'Handled' = $matches[2]
'Requests' = $matches[3]
}
# Iterate over each entry in the hash table and add rows to the DataTable
foreach ($channel in $channelMap.Keys) {
$row = $table.NewRow()
$row.channel = $channel
$row.value = $channelMap[$channel]
$row.unit = 'Count'
$row.mode = 'difference'
$table.Rows.Add($row)
}
}
$prevResponseLine=$responseLine
}
Generate XML and output the result
$retval = New-Xml -RootTag 'prtg' -ItemTag 'result' -Attributes $columns -TextTag $resultText
Write-Output $retval
`
Here is an updated version of the script above:
param (
[string]$url
)
$WarningPreference = 'SilentlyContinue'
$resultText = 'OK'
Set TLS version to 1.2 or 1.3
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13
Create a table to store the information
$table = New-Object System.Data.DataTable 'result'
$columns = @('channel', 'value', 'unit', 'customUnit', 'warning', 'float', 'mode')
foreach ($col in $columns) {
$table.Columns.Add((New-Object System.Data.DataColumn $col, 'System.String'))
}
Function to produce a well-formatted XML output from a given table
function New-Xml {
param (
[string]$RootTag = 'ROOT',
[string]$ItemTag = 'ITEM',
[string]$TextTag = 'OK',
[string[]]$Attributes = $null
)
}
Download response from the URL
$web = New-Object Net.WebClient
Try {
$response = $web.DownloadString($url)
#server accepts handled requests
rn16630948 16630949 31070465
rn#Reading: 6 Writing: 179 Waiting: 106"
} Catch {
Write-Warning "$($error[0])"
exit
}
Process response
$responseArray = $response -split "
rn"$responseLine=''
$prevResponseLine=''
foreach ($responseLine in $responseArray) {
if ($responseLine -match '^Active connections:\s*(\d+)') {
$row = $table.NewRow()
$row.channel = 'Active connections'
$row.value = $matches[1]
$row.unit = 'Count'
$row.mode = 'absolute'
$table.Rows.Add($row)
} elseif ($responseLine -match '^Reading:\s*(\d+) Writing:\s*(\d+) Waiting:\s*(\d+)') {
# Create a hash table to map channels to values
$channelMap = @{
'Reading' = $matches[1]
'Writing' = $matches[2]
'Waiting' = $matches[3]
}
}
Generate XML and output the result
$retval = New-Xml -RootTag 'prtg' -ItemTag 'result' -Attributes $columns -TextTag $resultText
Write-Output $retval