Created
September 7, 2017 16:25
-
-
Save amit-y/5d9d1582ee2c8f6128073c7b3fd0050f to your computer and use it in GitHub Desktop.
New Relic Infrastructure custom integration to monitor TCP ports (using Powershell)
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
integration_name: com.newrelic.tcp-port-monitor | |
instances: | |
- name: localhost | |
command: metrics | |
arguments: | |
server: localhost | |
port: 21 | |
labels: | |
env: development |
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
name: com.newrelic.tcp-port-monitor | |
protocol_version: 1 | |
description: Monitors TCP ports and reports if they are open or not | |
commands: | |
metrics: | |
command: | |
- C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe | |
- .\tcp-port-monitor.ps1 | |
interval: 60 |
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
$output = @{"name" = "com.newrelic.tcp-port-monitor"; "protocol_version" = "1"; integration_version = "0.1.0"} | |
$output.metrics = @() | |
$output.inventory = @{} | |
$output.events = @() | |
$server = $Env:SERVER | |
$port = $Env:PORT | |
$client = New-Object System.Net.Sockets.TcpClient; | |
Try { | |
$client.Connect($server, $port) | |
$conn = 1 | |
} Catch { | |
$conn = 0 | |
} Finally { | |
$client.Dispose() | |
$output.metrics += @{"event_type" = "NewRelicTCPPortMonitor"; "net.portOpen" = $conn; "server" = $server; "port" = ":$($port)"} | |
} | |
$output | ConvertTo-Json -Compress |
View Metrics
The responses are stored within Insights. To query the data, use the following query:
SELECT timestamp, `net.portOpen`, server, port FROM NewRelicTCPPortMonitor
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation Instructions
On Windows, Infrastructure is installed at
C:\Program Files\New Relic\newrelic-infra\
. Drop thetcp-port-monitor.ps1
andcom.newrelic.tcp-port-monitor-definition.yaml
files in thecustom-integrations
folder. Dropcom.newrelic.tcp-port-monitor-config.yaml
into theintegrations.d
folder.Update the path to the correct path to Powershell.exe on your system.
Update the instances to monitor in the config file.
Restart the
New Relic Infrastructure Agent
service.