I have a straightforward need to monitor a few endpoints by whether or not the port is opened/listening. It needs to be done this way due to the nature of the tunnel and security. Simple PowerShell script does the work just fine: (IP addresses sanitized)

Test-NetConnection xxx.xxx.xxx.xxx -p 443

The output is as such:

ComputerName     : xxx.xxx.xxx.xxx
RemoteAddress    : xxx.xxx.xxx.xxx
RemotePort       : 443
InterfaceAlias   : NIC1
SourceAddress    : xxx.xxx.xxx.xxx
TcpTestSucceeded : True

All we want to alert on is is the last lie is "false"

What we get is an alert in any status and the error Response not well-formed: "( ComputerName : xxx.xxx.xxx.xxx RemoteAddress : xxx.xxx.xxx.xxx RemotePort : 443 InterfaceAlias : NIC1 SourceAddress : xxx.xxx.xxx.xxx TcpTestSucceeded : True )" (code: PE132)


Article Comments

Hello,

Thank you for your message.

When using a custom script with the EXE/Script or EXE/Script Advanced sensor, PRTG expects to receive a response which follows a specific format according to the sensor, that you can find in this manual: https://www.paessler.com/manuals/prtg/custom_sensors

Therefore, in the current case you could use the following for the EXE/Script sensor:

$endpoint = Test-NetConnection -ComputerName IP/DNS -port PORT -WarningAction SilentlyContinue
$value = switch($endpoint.TcpTestSucceeded){"True" {1}; "False" {0}}

if ($value -eq 1){
    Write-Output "$($value):TCP test succeeded"
}else{
    Write-Output "$($value):TCP test failed"
}

Otherwise, for the EXE/Script Advanced sensor you need to return a JSON such as the one below:

$endpoint = Test-NetConnection -ComputerName IP/DNS -port PORT -WarningAction SilentlyContinue
$value = switch($endpoint.TcpTestSucceeded){"True" {1}; "False" {0}}

Write-Output @"
{
    "prtg": {
        "result": [
            {
                "channel": "TCP Test",
                "value": $value
            }
        ]
    }
}
"@

Then, when the channel is created you can apply a custom lookup to replace the current values with meaningful information. Here is the manual which describe how to use custom lookup: https://www.paessler.com/manuals/prtg/define_lookups

Finally, you can of course improve the script above to add parameters and then pass the data via PRTG for each of your endpoints, instead of creating many scripts.

param(
    [string] $address = "",
    [int] $port = 0
)

If you have questions, let me know.

Regards.


Sep, 2021 - Permalink

Thank you so much, this has solved my issue and is very useful for us!


Sep, 2021 - Permalink

Thank you very much for your feedback. Glad to hear that it works.

Have a nice day.


Sep, 2021 - Permalink

HI,

I can see this working for me when its true but it doesnt work when it fails and is false because the returned out put is different using powershell these days.

PingSucceeded  : False 

$endpoint = Test-NetConnection -ComputerName IP/DNS -port PORT -WarningAction SilentlyContinue
$value = switch($endpoint.TcpTestSucceeded){"True" {1}; "False" {0}}

if ($value -eq 1){
    Write-Output "$($value):TCP test succeeded"
}else{
    Write-Output "$($value):TCP test failed"
}

can anyone help with how to add in the false state as well.

thanks


Jul, 2022 - Permalink

Hi Marcus,

Thank you for your message.

According to the manual of PowerShell command Test-NetConnection, the latter indeed returns PingSucceeded however this happens when a port is not defined.

When a specific port is provided, it returns a boolean value for TcpTestSucceeded: https://docs.microsoft.com/en-us/powershell/module/nettcpip/test-netconnection?view=windowsserver2022-ps.

Therefore, I invite you to execute the command below manually on the corresponding probe server (where the script is) and make sure that TcpTestSucceeded is returned properly.

Test-NetConnection -ComputerName <IP/DNS> -Port <port>

The command should provide the same output as the one visible in the first post of this article.

Regards.


Jul, 2022 - Permalink