Skip to content

Commit

Permalink
Merge pull request #5 from sdewitt-newrelic/master
Browse files Browse the repository at this point in the history
Added port attribute, upgraded to v3, and added unit testing.
  • Loading branch information
psomareddy committed Aug 27, 2019
2 parents 872aa32 + 7891424 commit 965a3af
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 62 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -10,7 +10,7 @@ all: build

build: clean validate compile test

clean:
clean:
@echo "=== $(INTEGRATION) === [ clean ]: removing binaries and coverage file..."
@rm -rfv bin coverage.xml

Expand All @@ -22,7 +22,7 @@ validate-only:
@printf "=== $(INTEGRATION) === [ validate ]: running gofmt... "
# `gofmt` expects files instead of packages. `go fmt` works with
# packages, but forces -l -w flags.
@OUTPUT="$(shell gofmt -l $(GO_FILES))" ;\
@OUTPUT="$(shell gofmt -d -l $(GO_FILES))" ;\
if [ -z "$$OUTPUT" ]; then \
echo "passed." ;\
else \
Expand Down
67 changes: 45 additions & 22 deletions src/port-monitor.go
Expand Up @@ -6,9 +6,8 @@ import (
"time"

sdkArgs "github.com/newrelic/infra-integrations-sdk/args"
"github.com/newrelic/infra-integrations-sdk/log"
"github.com/newrelic/infra-integrations-sdk/metric"
"github.com/newrelic/infra-integrations-sdk/sdk"
"github.com/newrelic/infra-integrations-sdk/data/metric"
"github.com/newrelic/infra-integrations-sdk/integration"
)

type argumentList struct {
Expand All @@ -20,42 +19,66 @@ type argumentList struct {

const (
integrationName = "com.newrelic.tcp-port-monitor"
integrationVersion = "0.1.0"
integrationVersion = "2.0.0"
)

var args argumentList
var (
args argumentList
netDialTimeout = net.DialTimeout
)

func splitPort(address string) string {
slices := strings.Split(address, ":")
if len(slices) == 1 {
return "80"
}

return slices[1]
}

func populateMetrics(ms *metric.MetricSet) error {
func populateMetrics(ms *metric.Set) {
network := strings.TrimSpace(args.Network)
address := strings.TrimSpace(args.Address)
port := splitPort(address)
status := 0
conn, err := net.DialTimeout(network, address, time.Duration(args.Timeout)*time.Second)
if err != nil {
status = 0
} else {

conn, err := netDialTimeout(
network,
address,
time.Duration(args.Timeout)*time.Second,
)

if err == nil {
status = 1
conn.Close()
}

ms.SetMetric("network", network, metric.ATTRIBUTE)
ms.SetMetric("address", address, metric.ATTRIBUTE)
ms.SetMetric("port", port, metric.ATTRIBUTE)
ms.SetMetric("status", status, metric.GAUGE)
return nil
}

func main() {
integration, err := sdk.NewIntegration(integrationName, integrationVersion, &args)
fatalIfErr(err)

if args.All || args.Metrics {
ms := integration.NewMetricSet("NetworkPortSample")
fatalIfErr(populateMetrics(ms))
func panicOnErr(err error) {
if err != nil {
panic(err)
}
fatalIfErr(integration.Publish())
}

func fatalIfErr(err error) {
if err != nil {
log.Fatal(err)
func main() {
integration, err := integration.New(
integrationName,
integrationVersion,
integration.Args(&args),
)
panicOnErr(err)

entity := integration.LocalEntity()

args.NriAddHostname = true
if args.All() || args.Metrics {
populateMetrics(entity.NewMetricSet("NetworkPortSample"))
}

panicOnErr(integration.Publish())
}
123 changes: 114 additions & 9 deletions src/port-monitor_test.go
@@ -1,23 +1,128 @@
package main

import (
"fmt"
"net"
"testing"
"time"

"github.com/newrelic/infra-integrations-sdk/data/metric"
)

func TestPopulateInventory(t *testing.T) {
// Insert here the logic for your tests
actual := 2
expected := 2
type mockConn struct{}

func (c *mockConn) Read(b []byte) (n int, err error) {
return 0, nil
}
func (c *mockConn) Write(b []byte) (n int, err error) {
return 0, nil
}
func (c *mockConn) Close() error {
return nil
}
func (c *mockConn) LocalAddr() net.Addr {
return nil
}
func (c *mockConn) RemoteAddr() net.Addr {
return nil
}
func (c *mockConn) SetDeadline(t time.Time) error {
return nil
}
func (c *mockConn) SetReadDeadline(t time.Time) error {
return nil
}
func (c *mockConn) SetWriteDeadline(t time.Time) error {
return nil
}

func TestSplitPort(t *testing.T) {
// Test without a port
expected := "80"
actual := splitPort("mangatsika")
if actual != expected {
t.Errorf("splitPort, got: %s, expected: %s", actual, expected)
}

// Test with a valid port
expected = "8080"
actual = splitPort("mangatsika:8080")
if actual != expected {
t.Errorf("PopulateInventory was incorrect, got: %d, expected: %d", actual, expected)
t.Errorf("splitPort, got: %s, expected: %s", actual, expected)
}
}

func TestPopulateMetrics(t *testing.T) {
// Insert here the logic for your tests
actual := "foo"
expected := "foo"
oldNetDialTimeout := netDialTimeout
defer func() { netDialTimeout = oldNetDialTimeout }()

args.Network = "tcp"
args.Address = "localhost:8080"
args.Timeout = 12

// populateMetrics shoud set status = 1 when DialTimeout fails.
netDialTimeout = func(
network, address string,
timeout time.Duration,
) (net.Conn, error) {
return nil, fmt.Errorf("mangatsika")
}
ms := metric.NewSet("TestEvent", nil)

expected := 0.0
populateMetrics(ms)
actual, ok := ms.Metrics["status"]
if !ok {
t.Errorf("populateMetrics, expected status attribute but found none")
}
if actual != expected {
t.Errorf("populateMetrics, got: %f, expected: %f", actual, expected)
}

// populateMetrics shoud set status = 1 when DialTimeout succeeds.
netDialTimeout = func(
network, address string,
timeout time.Duration,
) (net.Conn, error) {
return &mockConn{}, nil
}
ms = metric.NewSet("TestEvent", nil)

expected = 1.0
populateMetrics(ms)
actual, ok = ms.Metrics["status"]
if !ok {
t.Errorf("populateMetrics, expected status attribute but found none")
}
if actual != expected {
t.Errorf("populateMetrics, got: %f, expected: %f", actual, expected)
}

expected2 := "tcp"
actual2, ok := ms.Metrics["network"]
if !ok {
t.Errorf("populateMetrics, expected network attribute but found none")
}
if actual2 != expected2 {
t.Errorf("populateMetrics, got: %s, expected: %s", actual2, expected2)
}

expected2 = "localhost:8080"
actual2, ok = ms.Metrics["address"]
if !ok {
t.Errorf("populateMetrics, expected address attribute but found none")
}
if actual2 != expected2 {
t.Errorf("populateMetrics, got: %s, expected: %s", actual2, expected2)
}

expected2 = "8080"
populateMetrics(ms)
actual2, ok = ms.Metrics["port"]
if !ok {
t.Errorf("populateMetrics, expected port attribute but found none")
}
if actual != expected {
t.Errorf("PopulateMetrics was incorrect, got: %s, expected: %s", actual, expected)
t.Errorf("populateMetrics, got: %f, expected: %f", actual, expected)
}
}
74 changes: 45 additions & 29 deletions vendor/vendor.json
Expand Up @@ -11,44 +11,60 @@
"versionExact": "v0.11.5"
},
{
"checksumSHA1": "jyYVyZKBHiXT9Fecb7iRUyXWwdI=",
"checksumSHA1": "ZfId5ZYn7jmL0xTxooHcxrLlXoA=",
"path": "github.com/newrelic/infra-integrations-sdk/args",
"revision": "b31bc81a228f7cb48496dd6c87815a119df35907",
"revisionTime": "2017-07-20T13:55:07Z",
"version": "v1.0",
"versionExact": "v1.0.0"
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
"revisionTime": "2019-08-26T13:48:57Z",
"version": "v3.4.0",
"versionExact": "v3.4.0"
},
{
"checksumSHA1": "4t7HNYy4VZjvfFY6PsrzU8g1hJQ=",
"path": "github.com/newrelic/infra-integrations-sdk/cache",
"revision": "b31bc81a228f7cb48496dd6c87815a119df35907",
"revisionTime": "2017-07-20T13:55:07Z",
"version": "v1.0",
"versionExact": "v1.0.0"
"checksumSHA1": "yU9Wr91yeGkqhQqVhwgeQ3xQgJU=",
"path": "github.com/newrelic/infra-integrations-sdk/data/event",
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
"revisionTime": "2019-08-26T13:48:57Z"
},
{
"checksumSHA1": "6eFl1VzgBKkUbpcOdVhSqLz2oD4=",
"checksumSHA1": "EOoxXGqFDPkCvj7wec2S5kCXY2I=",
"path": "github.com/newrelic/infra-integrations-sdk/data/inventory",
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
"revisionTime": "2019-08-26T13:48:57Z"
},
{
"checksumSHA1": "pyalPjuL/fgXjdxXlwI92sLSG8M=",
"path": "github.com/newrelic/infra-integrations-sdk/data/metric",
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
"revisionTime": "2019-08-26T13:48:57Z",
"version": "v3.4.0",
"versionExact": "v3.4.0"
},
{
"checksumSHA1": "DdUizp/J5c7mAFZg2hoz4rRPOYM=",
"path": "github.com/newrelic/infra-integrations-sdk/integration",
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
"revisionTime": "2019-08-26T13:48:57Z",
"version": "v3.4.0",
"versionExact": "v3.4.0"
},
{
"checksumSHA1": "6/DhD+/LhC/k3NaLgkpd6iE8b5E=",
"path": "github.com/newrelic/infra-integrations-sdk/log",
"revision": "b31bc81a228f7cb48496dd6c87815a119df35907",
"revisionTime": "2017-07-20T13:55:07Z",
"version": "v1.0",
"versionExact": "v1.0.0"
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
"revisionTime": "2019-08-26T13:48:57Z",
"version": "v3.4.0",
"versionExact": "v3.4.0"
},
{
"checksumSHA1": "v6d2OtRT+Xxb8Ytv3L7RzWU9cHI=",
"path": "github.com/newrelic/infra-integrations-sdk/metric",
"revision": "b31bc81a228f7cb48496dd6c87815a119df35907",
"revisionTime": "2017-07-20T13:55:07Z",
"version": "v1.0",
"versionExact": "v1.0.0"
"checksumSHA1": "BsHp62Etporko0d+4GpVxrP2qtw=",
"path": "github.com/newrelic/infra-integrations-sdk/persist",
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
"revisionTime": "2019-08-26T13:48:57Z"
},
{
"checksumSHA1": "QAOEN0X+lEiWTXI7LjOWj1u+yfs=",
"path": "github.com/newrelic/infra-integrations-sdk/sdk",
"revision": "b31bc81a228f7cb48496dd6c87815a119df35907",
"revisionTime": "2017-07-20T13:55:07Z",
"version": "v1.0",
"versionExact": "v1.0.0"
"checksumSHA1": "I7hloldMJZTqUx6hbVDp5nk9fZQ=",
"path": "github.com/pkg/errors",
"revision": "27936f6d90f9c8e1145f11ed52ffffbfdb9e0af7",
"revisionTime": "2019-02-27T00:00:51Z"
},
{
"checksumSHA1": "JFeS7JlZ3ntfWcTfyQE0q9KgRi8=",
Expand All @@ -57,5 +73,5 @@
"revisionTime": "2017-11-02T20:35:00Z"
}
],
"rootPath": "github.com/newrelic/nri-port-monitor"
"rootPath": "github.com/sdewitt-newrelic/nri-port-monitor"
}

0 comments on commit 965a3af

Please sign in to comment.