Created
August 30, 2018 17:20
-
-
Save tareqy/bb66a0f898922e832417847b9a9ad1ea to your computer and use it in GitHub Desktop.
PRTG NGINX Sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment