From eeab024c3a87447605929e009498d523e3e9421b Mon Sep 17 00:00:00 2001 From: Prakash Reddy Date: Mon, 23 Jul 2018 23:01:45 -0400 Subject: [PATCH] first commit --- .gitignore | 4 ++ CHANGELOG.md | 20 +++++++++ LICENSE | 12 ++++++ Makefile | 75 ++++++++++++++++++++++++++++++++++ README.md | 28 +++++++++++++ port-monitor-config.yml.sample | 12 ++++++ port-monitor-definition.yml | 18 ++++++++ src/port-monitor.go | 72 ++++++++++++++++++++++++++++++++ src/port-monitor_test.go | 23 +++++++++++ vendor/vendor.json | 61 +++++++++++++++++++++++++++ 10 files changed, 325 insertions(+) create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 port-monitor-config.yml.sample create mode 100644 port-monitor-definition.yml create mode 100644 src/port-monitor.go create mode 100644 src/port-monitor_test.go create mode 100644 vendor/vendor.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..59a4686 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/bin/ +coverage.xml +/vendor/ +!/vendor/vendor.json diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..303c531 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,20 @@ +# Change Log + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/) +and this project adheres to [Semantic Versioning](http://semver.org/). + +## Unreleased +### Added +- X + +### Changed +- X + +### Removed +- X + +## 0.1.0 - 2018-07-18 +### Added +- Initial version: Includes Metrics and Inventory data \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6bfa205 --- /dev/null +++ b/LICENSE @@ -0,0 +1,12 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/LICENSE-2.0 + +Copyright [2018] New Relic, Inc. + +Licensed under the Apache License, version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by +applicable law or agreed to in writing, software distributed under the License +is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +either express or implied. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..93afa54 --- /dev/null +++ b/Makefile @@ -0,0 +1,75 @@ +INTEGRATION := $(shell basename $(shell pwd)) +BINARY_NAME = nr-$(INTEGRATION) +GO_PKGS := $(shell go list ./... | grep -v "/vendor/") +GO_FILES := $(shell find src -type f -name "*.go") +VALIDATE_DEPS = github.com/golang/lint/golint +DEPS = github.com/kardianos/govendor +TEST_DEPS = github.com/axw/gocov/gocov github.com/AlekSi/gocov-xml + +all: build + +build: clean validate compile test + +clean: + @echo "=== $(INTEGRATION) === [ clean ]: removing binaries and coverage file..." + @rm -rfv bin coverage.xml + +validate-deps: + @echo "=== $(INTEGRATION) === [ validate-deps ]: installing validation dependencies..." + @go get -v $(VALIDATE_DEPS) + +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))" ;\ + if [ -z "$$OUTPUT" ]; then \ + echo "passed." ;\ + else \ + echo "failed. Incorrect syntax in the following files:" ;\ + echo "$$OUTPUT" ;\ + exit 1 ;\ + fi + @printf "=== $(INTEGRATION) === [ validate ]: running golint... " + @OUTPUT="$(shell golint $(GO_PKGS))" ;\ + if [ -z "$$OUTPUT" ]; then \ + echo "passed." ;\ + else \ + echo "failed. Issues found:" ;\ + echo "$$OUTPUT" ;\ + exit 1 ;\ + fi + @printf "=== $(INTEGRATION) === [ validate ]: running go vet... " + @OUTPUT="$(shell go vet $(GO_PKGS))" ;\ + if [ -z "$$OUTPUT" ]; then \ + echo "passed." ;\ + else \ + echo "failed. Issues found:" ;\ + echo "$$OUTPUT" ;\ + exit 1;\ + fi + +validate: validate-deps validate-only + +compile-deps: + @echo "=== $(INTEGRATION) === [ compile-deps ]: installing build dependencies..." + @go get $(DEPS) + @govendor sync + +compile-only: + @echo "=== $(INTEGRATION) === [ compile ]: building $(BINARY_NAME)..." + @go build -o bin/$(BINARY_NAME) $(GO_FILES) + +compile: compile-deps compile-only + +test-deps: compile-deps + @echo "=== $(INTEGRATION) === [ test-deps ]: installing testing dependencies..." + @go get -v $(TEST_DEPS) + +test-only: + @echo "=== $(INTEGRATION) === [ test ]: running unit tests..." + @gocov test $(GO_PKGS) | gocov-xml > coverage.xml + +test: test-deps test-only + +.PHONY: all build clean validate-deps validate-only validate compile-deps compile-only compile test-deps test-only test diff --git a/README.md b/README.md new file mode 100644 index 0000000..09226a2 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# New Relic Infrastructure Integration for monitoring Network Ports + +Reports up or down status for a network (TCP, UDP etc) port + +## Requirements + +### New Relic Infrastructure Agent + +This is the description about how to run the Port Monitor Integration with New Relic Infrastructure agent, so it is required to have the agent installed (see [agent installation](https://docs.newrelic.com/docs/infrastructure/new-relic-infrastructure/installation/install-infrastructure-linux)). + +## Installation + +* Download an archive file for the port-monitor Integration +* Place the executables under `bin` directory and the definition file `port-monitor-definition.yml` in `/var/db/newrelic-infra/newrelic-integrations` +* Set execution permissions for the binary file `nr-port-monitor` +* Place the integration configuration file `port-monitor-config.yml.sample` in `/etc/newrelic-infra/integrations.d` + +## Configuration + +In order to use the Port Monitor Integration it is required to configure `port-monitor-config.yml.sample` file. Firstly, rename the file to `port-monitor-config.yml`. Then, depending on your needs, specify all instances that you want to monitor. Once this is done, restart the Infrastructure agent. + +You can view your data in Insights by creating your own custom NRQL queries. To +do so use **NetworkPortSample** event types. + +## Compatibility + +* Supported OS: linux +* Edition: \ No newline at end of file diff --git a/port-monitor-config.yml.sample b/port-monitor-config.yml.sample new file mode 100644 index 0000000..cc41d9c --- /dev/null +++ b/port-monitor-config.yml.sample @@ -0,0 +1,12 @@ +integration_name: com.newrelic.tcp-port-monitor + +instances: + - name: + command: metrics + arguments: + address: localhost:80 + network: tcp + timeout: 5 + labels: + env: prod + diff --git a/port-monitor-definition.yml b/port-monitor-definition.yml new file mode 100644 index 0000000..de809b4 --- /dev/null +++ b/port-monitor-definition.yml @@ -0,0 +1,18 @@ +name: com.newrelic.tcp-port-monitor +description: Reports status and metrics for tcp-port-monitor service +protocol_version: 1 +os: linux + +commands: + metrics: + command: + - ./bin/nr-tcp-port-monitor + - --metrics + interval: 15 + + inventory: + command: + - ./bin/nr-tcp-port-monitor + - --inventory + prefix: config/nr-tcp-port-monitor + interval: 15 diff --git a/src/port-monitor.go b/src/port-monitor.go new file mode 100644 index 0000000..e5f57df --- /dev/null +++ b/src/port-monitor.go @@ -0,0 +1,72 @@ +package main + +import ( + "net" + "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" +) + +type argumentList struct { + sdkArgs.DefaultArgumentList + Network string `default:"tcp" help:"Network type. Known networks are tcp, tcp4 (IPv4-only), tcp6 (IPv6-only), udp, udp4 (IPv4-only), udp6 (IPv6-only), ip, ip4 (IPv4-only), ip6 (IPv6-only), unix, unixgram and unixpacket"` + Address string `default:":80" help:"Address? host:port"` + Timeout int `default:"5" help:"Timeout in seconds"` +} + +const ( + integrationName = "com.newrelic.tcp-port-monitor" + integrationVersion = "0.1.0" +) + +var args argumentList + +func populateInventory(inventory sdk.Inventory) error { + // Insert here the logic of your integration to get the inventory data + // Ex: inventory.SetItem("softwareVersion", "value", "1.0.1") + // -- + return nil +} + +func populateMetrics(ms *metric.MetricSet) error { + // Insert here the logic of your integration to get the metrics data + // Ex: ms.SetMetric("requestsPerSecond", 10, metric.GAUGE) + // -- + status := 0 + conn, err := net.DialTimeout("tcp", args.Address, time.Duration(args.Timeout)*time.Second) + if err != nil { + status = 0 + } else { + status = 1 + conn.Close() + } + + ms.SetMetric("network", args.Network, metric.ATTRIBUTE) + ms.SetMetric("address", args.Address, 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.Inventory { + fatalIfErr(populateInventory(integration.Inventory)) + } + + if args.All || args.Metrics { + ms := integration.NewMetricSet("NetworkPortSample") + fatalIfErr(populateMetrics(ms)) + } + fatalIfErr(integration.Publish()) +} + +func fatalIfErr(err error) { + if err != nil { + log.Fatal(err) + } +} diff --git a/src/port-monitor_test.go b/src/port-monitor_test.go new file mode 100644 index 0000000..274e7c9 --- /dev/null +++ b/src/port-monitor_test.go @@ -0,0 +1,23 @@ +package main + +import ( + "testing" +) + +func TestPopulateInventory(t *testing.T) { + // Insert here the logic for your tests + actual := 2 + expected := 2 + if actual != expected { + t.Errorf("PopulateInventory was incorrect, got: %d, expected: %d", actual, expected) + } +} + +func TestPopulateMetrics(t *testing.T) { + // Insert here the logic for your tests + actual := "foo" + expected := "foo" + if actual != expected { + t.Errorf("PopulateMetrics was incorrect, got: %s, expected: %s", actual, expected) + } +} diff --git a/vendor/vendor.json b/vendor/vendor.json new file mode 100644 index 0000000..8f697e1 --- /dev/null +++ b/vendor/vendor.json @@ -0,0 +1,61 @@ +{ + "comment": "", + "ignore": "test", + "package": [ + { + "checksumSHA1": "Ksl4kXr4ty0HwjrLRLZOrNz05Bw=", + "path": "github.com/Sirupsen/logrus", + "revision": "ba1b36c82c5e05c4f912a88eab0dcd91a171688f", + "revisionTime": "2017-03-14T19:23:53Z", + "version": "v0.11.5", + "versionExact": "v0.11.5" + }, + { + "checksumSHA1": "jyYVyZKBHiXT9Fecb7iRUyXWwdI=", + "path": "github.com/newrelic/infra-integrations-sdk/args", + "revision": "b31bc81a228f7cb48496dd6c87815a119df35907", + "revisionTime": "2017-07-20T13:55:07Z", + "version": "v1.0", + "versionExact": "v1.0.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": "6eFl1VzgBKkUbpcOdVhSqLz2oD4=", + "path": "github.com/newrelic/infra-integrations-sdk/log", + "revision": "b31bc81a228f7cb48496dd6c87815a119df35907", + "revisionTime": "2017-07-20T13:55:07Z", + "version": "v1.0", + "versionExact": "v1.0.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": "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": "JFeS7JlZ3ntfWcTfyQE0q9KgRi8=", + "path": "golang.org/x/sys/unix", + "revision": "46eaec7899b1dd945c25db17c78ba3f0c58a6613", + "revisionTime": "2017-11-02T20:35:00Z" + } + ], + "rootPath": "source.datanerd.us/FIT/port-monitor-plugin" +}