Skip to content

Instantly share code, notes, and snippets.

@amit-y
Created September 7, 2017 16:25
Show Gist options
  • Save amit-y/5d9d1582ee2c8f6128073c7b3fd0050f to your computer and use it in GitHub Desktop.
Save amit-y/5d9d1582ee2c8f6128073c7b3fd0050f to your computer and use it in GitHub Desktop.
New Relic Infrastructure custom integration to monitor TCP ports (using Powershell)
integration_name: com.newrelic.tcp-port-monitor
instances:
- name: localhost
command: metrics
arguments:
server: localhost
port: 21
labels:
env: development
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
$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
@amit-y
Copy link
Author

amit-y commented Sep 7, 2017

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