Skip to content

Commit 965a3af

Browse files
author
Prakash S. Reddy
authored
Merge pull request #5 from sdewitt-newrelic/master
Added port attribute, upgraded to v3, and added unit testing.
2 parents 872aa32 + 7891424 commit 965a3af

File tree

4 files changed

+206
-62
lines changed

4 files changed

+206
-62
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ all: build
1010

1111
build: clean validate compile test
1212

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

@@ -22,7 +22,7 @@ validate-only:
2222
@printf "=== $(INTEGRATION) === [ validate ]: running gofmt... "
2323
# `gofmt` expects files instead of packages. `go fmt` works with
2424
# packages, but forces -l -w flags.
25-
@OUTPUT="$(shell gofmt -l $(GO_FILES))" ;\
25+
@OUTPUT="$(shell gofmt -d -l $(GO_FILES))" ;\
2626
if [ -z "$$OUTPUT" ]; then \
2727
echo "passed." ;\
2828
else \

src/port-monitor.go

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import (
66
"time"
77

88
sdkArgs "github.com/newrelic/infra-integrations-sdk/args"
9-
"github.com/newrelic/infra-integrations-sdk/log"
10-
"github.com/newrelic/infra-integrations-sdk/metric"
11-
"github.com/newrelic/infra-integrations-sdk/sdk"
9+
"github.com/newrelic/infra-integrations-sdk/data/metric"
10+
"github.com/newrelic/infra-integrations-sdk/integration"
1211
)
1312

1413
type argumentList struct {
@@ -20,42 +19,66 @@ type argumentList struct {
2019

2120
const (
2221
integrationName = "com.newrelic.tcp-port-monitor"
23-
integrationVersion = "0.1.0"
22+
integrationVersion = "2.0.0"
2423
)
2524

26-
var args argumentList
25+
var (
26+
args argumentList
27+
netDialTimeout = net.DialTimeout
28+
)
29+
30+
func splitPort(address string) string {
31+
slices := strings.Split(address, ":")
32+
if len(slices) == 1 {
33+
return "80"
34+
}
35+
36+
return slices[1]
37+
}
2738

28-
func populateMetrics(ms *metric.MetricSet) error {
39+
func populateMetrics(ms *metric.Set) {
2940
network := strings.TrimSpace(args.Network)
3041
address := strings.TrimSpace(args.Address)
42+
port := splitPort(address)
3143
status := 0
32-
conn, err := net.DialTimeout(network, address, time.Duration(args.Timeout)*time.Second)
33-
if err != nil {
34-
status = 0
35-
} else {
44+
45+
conn, err := netDialTimeout(
46+
network,
47+
address,
48+
time.Duration(args.Timeout)*time.Second,
49+
)
50+
51+
if err == nil {
3652
status = 1
3753
conn.Close()
3854
}
3955

4056
ms.SetMetric("network", network, metric.ATTRIBUTE)
4157
ms.SetMetric("address", address, metric.ATTRIBUTE)
58+
ms.SetMetric("port", port, metric.ATTRIBUTE)
4259
ms.SetMetric("status", status, metric.GAUGE)
43-
return nil
4460
}
4561

46-
func main() {
47-
integration, err := sdk.NewIntegration(integrationName, integrationVersion, &args)
48-
fatalIfErr(err)
49-
50-
if args.All || args.Metrics {
51-
ms := integration.NewMetricSet("NetworkPortSample")
52-
fatalIfErr(populateMetrics(ms))
62+
func panicOnErr(err error) {
63+
if err != nil {
64+
panic(err)
5365
}
54-
fatalIfErr(integration.Publish())
5566
}
5667

57-
func fatalIfErr(err error) {
58-
if err != nil {
59-
log.Fatal(err)
68+
func main() {
69+
integration, err := integration.New(
70+
integrationName,
71+
integrationVersion,
72+
integration.Args(&args),
73+
)
74+
panicOnErr(err)
75+
76+
entity := integration.LocalEntity()
77+
78+
args.NriAddHostname = true
79+
if args.All() || args.Metrics {
80+
populateMetrics(entity.NewMetricSet("NetworkPortSample"))
6081
}
82+
83+
panicOnErr(integration.Publish())
6184
}

src/port-monitor_test.go

Lines changed: 114 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,128 @@
11
package main
22

33
import (
4+
"fmt"
5+
"net"
46
"testing"
7+
"time"
8+
9+
"github.com/newrelic/infra-integrations-sdk/data/metric"
510
)
611

7-
func TestPopulateInventory(t *testing.T) {
8-
// Insert here the logic for your tests
9-
actual := 2
10-
expected := 2
12+
type mockConn struct{}
13+
14+
func (c *mockConn) Read(b []byte) (n int, err error) {
15+
return 0, nil
16+
}
17+
func (c *mockConn) Write(b []byte) (n int, err error) {
18+
return 0, nil
19+
}
20+
func (c *mockConn) Close() error {
21+
return nil
22+
}
23+
func (c *mockConn) LocalAddr() net.Addr {
24+
return nil
25+
}
26+
func (c *mockConn) RemoteAddr() net.Addr {
27+
return nil
28+
}
29+
func (c *mockConn) SetDeadline(t time.Time) error {
30+
return nil
31+
}
32+
func (c *mockConn) SetReadDeadline(t time.Time) error {
33+
return nil
34+
}
35+
func (c *mockConn) SetWriteDeadline(t time.Time) error {
36+
return nil
37+
}
38+
39+
func TestSplitPort(t *testing.T) {
40+
// Test without a port
41+
expected := "80"
42+
actual := splitPort("mangatsika")
43+
if actual != expected {
44+
t.Errorf("splitPort, got: %s, expected: %s", actual, expected)
45+
}
46+
47+
// Test with a valid port
48+
expected = "8080"
49+
actual = splitPort("mangatsika:8080")
1150
if actual != expected {
12-
t.Errorf("PopulateInventory was incorrect, got: %d, expected: %d", actual, expected)
51+
t.Errorf("splitPort, got: %s, expected: %s", actual, expected)
1352
}
1453
}
1554

1655
func TestPopulateMetrics(t *testing.T) {
17-
// Insert here the logic for your tests
18-
actual := "foo"
19-
expected := "foo"
56+
oldNetDialTimeout := netDialTimeout
57+
defer func() { netDialTimeout = oldNetDialTimeout }()
58+
59+
args.Network = "tcp"
60+
args.Address = "localhost:8080"
61+
args.Timeout = 12
62+
63+
// populateMetrics shoud set status = 1 when DialTimeout fails.
64+
netDialTimeout = func(
65+
network, address string,
66+
timeout time.Duration,
67+
) (net.Conn, error) {
68+
return nil, fmt.Errorf("mangatsika")
69+
}
70+
ms := metric.NewSet("TestEvent", nil)
71+
72+
expected := 0.0
73+
populateMetrics(ms)
74+
actual, ok := ms.Metrics["status"]
75+
if !ok {
76+
t.Errorf("populateMetrics, expected status attribute but found none")
77+
}
78+
if actual != expected {
79+
t.Errorf("populateMetrics, got: %f, expected: %f", actual, expected)
80+
}
81+
82+
// populateMetrics shoud set status = 1 when DialTimeout succeeds.
83+
netDialTimeout = func(
84+
network, address string,
85+
timeout time.Duration,
86+
) (net.Conn, error) {
87+
return &mockConn{}, nil
88+
}
89+
ms = metric.NewSet("TestEvent", nil)
90+
91+
expected = 1.0
92+
populateMetrics(ms)
93+
actual, ok = ms.Metrics["status"]
94+
if !ok {
95+
t.Errorf("populateMetrics, expected status attribute but found none")
96+
}
97+
if actual != expected {
98+
t.Errorf("populateMetrics, got: %f, expected: %f", actual, expected)
99+
}
100+
101+
expected2 := "tcp"
102+
actual2, ok := ms.Metrics["network"]
103+
if !ok {
104+
t.Errorf("populateMetrics, expected network attribute but found none")
105+
}
106+
if actual2 != expected2 {
107+
t.Errorf("populateMetrics, got: %s, expected: %s", actual2, expected2)
108+
}
109+
110+
expected2 = "localhost:8080"
111+
actual2, ok = ms.Metrics["address"]
112+
if !ok {
113+
t.Errorf("populateMetrics, expected address attribute but found none")
114+
}
115+
if actual2 != expected2 {
116+
t.Errorf("populateMetrics, got: %s, expected: %s", actual2, expected2)
117+
}
118+
119+
expected2 = "8080"
120+
populateMetrics(ms)
121+
actual2, ok = ms.Metrics["port"]
122+
if !ok {
123+
t.Errorf("populateMetrics, expected port attribute but found none")
124+
}
20125
if actual != expected {
21-
t.Errorf("PopulateMetrics was incorrect, got: %s, expected: %s", actual, expected)
126+
t.Errorf("populateMetrics, got: %f, expected: %f", actual, expected)
22127
}
23128
}

vendor/vendor.json

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,60 @@
1111
"versionExact": "v0.11.5"
1212
},
1313
{
14-
"checksumSHA1": "jyYVyZKBHiXT9Fecb7iRUyXWwdI=",
14+
"checksumSHA1": "ZfId5ZYn7jmL0xTxooHcxrLlXoA=",
1515
"path": "github.com/newrelic/infra-integrations-sdk/args",
16-
"revision": "b31bc81a228f7cb48496dd6c87815a119df35907",
17-
"revisionTime": "2017-07-20T13:55:07Z",
18-
"version": "v1.0",
19-
"versionExact": "v1.0.0"
16+
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
17+
"revisionTime": "2019-08-26T13:48:57Z",
18+
"version": "v3.4.0",
19+
"versionExact": "v3.4.0"
2020
},
2121
{
22-
"checksumSHA1": "4t7HNYy4VZjvfFY6PsrzU8g1hJQ=",
23-
"path": "github.com/newrelic/infra-integrations-sdk/cache",
24-
"revision": "b31bc81a228f7cb48496dd6c87815a119df35907",
25-
"revisionTime": "2017-07-20T13:55:07Z",
26-
"version": "v1.0",
27-
"versionExact": "v1.0.0"
22+
"checksumSHA1": "yU9Wr91yeGkqhQqVhwgeQ3xQgJU=",
23+
"path": "github.com/newrelic/infra-integrations-sdk/data/event",
24+
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
25+
"revisionTime": "2019-08-26T13:48:57Z"
2826
},
2927
{
30-
"checksumSHA1": "6eFl1VzgBKkUbpcOdVhSqLz2oD4=",
28+
"checksumSHA1": "EOoxXGqFDPkCvj7wec2S5kCXY2I=",
29+
"path": "github.com/newrelic/infra-integrations-sdk/data/inventory",
30+
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
31+
"revisionTime": "2019-08-26T13:48:57Z"
32+
},
33+
{
34+
"checksumSHA1": "pyalPjuL/fgXjdxXlwI92sLSG8M=",
35+
"path": "github.com/newrelic/infra-integrations-sdk/data/metric",
36+
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
37+
"revisionTime": "2019-08-26T13:48:57Z",
38+
"version": "v3.4.0",
39+
"versionExact": "v3.4.0"
40+
},
41+
{
42+
"checksumSHA1": "DdUizp/J5c7mAFZg2hoz4rRPOYM=",
43+
"path": "github.com/newrelic/infra-integrations-sdk/integration",
44+
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
45+
"revisionTime": "2019-08-26T13:48:57Z",
46+
"version": "v3.4.0",
47+
"versionExact": "v3.4.0"
48+
},
49+
{
50+
"checksumSHA1": "6/DhD+/LhC/k3NaLgkpd6iE8b5E=",
3151
"path": "github.com/newrelic/infra-integrations-sdk/log",
32-
"revision": "b31bc81a228f7cb48496dd6c87815a119df35907",
33-
"revisionTime": "2017-07-20T13:55:07Z",
34-
"version": "v1.0",
35-
"versionExact": "v1.0.0"
52+
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
53+
"revisionTime": "2019-08-26T13:48:57Z",
54+
"version": "v3.4.0",
55+
"versionExact": "v3.4.0"
3656
},
3757
{
38-
"checksumSHA1": "v6d2OtRT+Xxb8Ytv3L7RzWU9cHI=",
39-
"path": "github.com/newrelic/infra-integrations-sdk/metric",
40-
"revision": "b31bc81a228f7cb48496dd6c87815a119df35907",
41-
"revisionTime": "2017-07-20T13:55:07Z",
42-
"version": "v1.0",
43-
"versionExact": "v1.0.0"
58+
"checksumSHA1": "BsHp62Etporko0d+4GpVxrP2qtw=",
59+
"path": "github.com/newrelic/infra-integrations-sdk/persist",
60+
"revision": "34c2ee2fcae25e47eb7ec7f76ea3e53904bab365",
61+
"revisionTime": "2019-08-26T13:48:57Z"
4462
},
4563
{
46-
"checksumSHA1": "QAOEN0X+lEiWTXI7LjOWj1u+yfs=",
47-
"path": "github.com/newrelic/infra-integrations-sdk/sdk",
48-
"revision": "b31bc81a228f7cb48496dd6c87815a119df35907",
49-
"revisionTime": "2017-07-20T13:55:07Z",
50-
"version": "v1.0",
51-
"versionExact": "v1.0.0"
64+
"checksumSHA1": "I7hloldMJZTqUx6hbVDp5nk9fZQ=",
65+
"path": "github.com/pkg/errors",
66+
"revision": "27936f6d90f9c8e1145f11ed52ffffbfdb9e0af7",
67+
"revisionTime": "2019-02-27T00:00:51Z"
5268
},
5369
{
5470
"checksumSHA1": "JFeS7JlZ3ntfWcTfyQE0q9KgRi8=",
@@ -57,5 +73,5 @@
5773
"revisionTime": "2017-11-02T20:35:00Z"
5874
}
5975
],
60-
"rootPath": "github.com/newrelic/nri-port-monitor"
76+
"rootPath": "github.com/sdewitt-newrelic/nri-port-monitor"
6177
}

0 commit comments

Comments
 (0)