From 0e825d0311a46764cf59dc13474a0398c10bd217 Mon Sep 17 00:00:00 2001 From: Maxime Wojtczak Date: Tue, 5 Mar 2019 08:11:27 +0100 Subject: [PATCH 1/2] feat(description): Provide description template in file. (#7) --- .circleci/config.yml | 2 +- .github/ISSUE_TEMPLATE.md | 5 +++++ .promu.yml | 3 ++- .travis.yml | 4 ++-- README.md | 34 ++++++++++++++--------------- VERSION | 2 +- configuration/configuration.go | 28 +++++++----------------- configuration/configuration_test.go | 17 +++++---------- description-template.tpl | 12 ++++++++++ go.sum | 4 ++-- 10 files changed, 56 insertions(+), 55 deletions(-) create mode 100644 description-template.tpl diff --git a/.circleci/config.yml b/.circleci/config.yml index 6c62c8f..0666f4b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,7 +6,7 @@ executors: # should also be updated. golang: docker: - - image: circleci/golang:1.11 + - image: circleci/golang:1.12 # errcheck requires to be executed from GOPATH for now. working_directory: /go/src/github.com/maxwo/snmp_notifier diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index af5c986..86f8cf9 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -27,6 +27,11 @@ insert command line here ``` +* SNMP notifier command line: +``` +insert command line here +``` + * Prometheus alert file: ``` insert configuration here (if relevant to the issue) diff --git a/.promu.yml b/.promu.yml index 07406da..ca5c2be 100644 --- a/.promu.yml +++ b/.promu.yml @@ -1,6 +1,6 @@ verbose: false go: - version: 1.11.0 + version: 1.12.0 cgo: false repository: path: github.com/maxwo/snmp_notifier @@ -16,6 +16,7 @@ build: -X github.com/prometheus/common/version.BuildDate={{date "20060102-15:04:05"}} tarball: files: + - description-template.tpl - LICENSE - NOTICE crossbuild: diff --git a/.travis.yml b/.travis.yml index eda2d55..807476f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,8 +5,8 @@ language: go # Whenever the Go version is updated here, .circleci/config.yml and .promu.yml # should also be updated. go: -- 1.11.x +- 1.12.x script: - make -- git diff --exit-code \ No newline at end of file +- git diff --exit-code diff --git a/README.md b/README.md index d87654e..dd09083 100644 --- a/README.md +++ b/README.md @@ -95,31 +95,31 @@ usage: snmp_notifier [] A tool to relay Prometheus alerts as SNMP traps Flags: - -h, --help Show context-sensitive help (also try --help-long and --help-man). - --web.listen-address=":9464" - Address to listen on for web interface and telemetry. + -h, --help Show context-sensitive help (also try --help-long and --help-man). + --web.listen-address=:9464 + Address to listen on for web interface and telemetry. --alert.severity-label="severity" - Label where to find the alert severity. + Label where to find the alert severity. --alert.severities="critical,warning,info" - The ordered list of alert severities, from more prioritary to less prioritary. + The ordered list of alert severities, from more prioritary to less prioritary. --alert.default-severity="critical" - The alert severity if none is provided via labels. - --snmp.destination="127.0.0.1:162" - SNMP trap server destination. - --snmp.retries=1 SNMP number of retries + The alert severity if none is provided via labels. + --snmp.destination=127.0.0.1:162 + SNMP trap server destination. + --snmp.retries=1 SNMP number of retries --snmp.trap-oid-label="oid" - Label where to find the trap OID. + Label where to find the trap OID. --snmp.trap-default-oid="1.3.6.1.4.1.1664.1" - Trap OID to send if none is found in the alert labels - --snmp.trap-description-template="\n{{- if (len .Alerts) gt 0 -}}\n{{- range $severity, $alerts := (groupAlertsByLabel .Alerts \"severity\") -}}\nStatus: {{ $severity }}\n{{- range $index, $alert := $alerts }}\n- Alert: {{ $alert.Labels.alertname }}\n Summary: {{ $alert.Annotations.summary }}\n Description: {{ $alert.Annotations.description }}\n{{ end }}\n{{ end }}\n{{ else -}}\nStatus: OK\n{{- end -}}" - SNMP description template. - --log.level="info" Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal] + Trap OID to send if none is found in the alert labels + --snmp.trap-description-template=description-template.tpl + SNMP description template. + --log.level="info" Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal] --log.format="logger:stderr" - Set the log target and format. Example: "logger:syslog?appname=bob&local=7" or "logger:stdout?json=true" - --version Show application version. + Set the log target and format. Example: "logger:syslog?appname=bob&local=7" or "logger:stdout?json=true" + --version Show application version. ``` -Any Go template directive may be used in the `snmp.trap-description-template` option. +Any Go template directive may be used in the `snmp.trap-description-template` file. ## Examples diff --git a/VERSION b/VERSION index 0ea3a94..0d91a54 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.0 +0.3.0 diff --git a/configuration/configuration.go b/configuration/configuration.go index 4020b39..61181b2 100644 --- a/configuration/configuration.go +++ b/configuration/configuration.go @@ -3,6 +3,7 @@ package configuration import ( "fmt" "os" + "path/filepath" "strings" "text/template" @@ -24,19 +25,6 @@ type SNMPNotifierConfiguration struct { } var ( - snmpTrapDescriptionTemplateDefault = ` -{{- if (len .Alerts) gt 0 -}} -{{- range $severity, $alerts := (groupAlertsByLabel .Alerts "severity") -}} -Status: {{ $severity }} -{{- range $index, $alert := $alerts }} -- Alert: {{ $alert.Labels.alertname }} - Summary: {{ $alert.Annotations.summary }} - Description: {{ $alert.Annotations.description }} -{{ end }} -{{ end }} -{{ else -}} -Status: OK -{{- end -}}` snmpDefaultCommunity = "public" snmpCommunityEnvironmentVariable = "SNMP_NOTIFIER_COMMUNITY" ) @@ -45,15 +33,15 @@ Status: OK func ParseConfiguration(args []string) (*SNMPNotifierConfiguration, error) { var ( application = kingpin.New("snmp_notifier", "A tool to relay Prometheus alerts as SNMP traps") - webListenAddress = application.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9464").String() + webListenAddress = application.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9464").TCP() alertSeverityLabel = application.Flag("alert.severity-label", "Label where to find the alert severity.").Default("severity").String() alertSeverities = application.Flag("alert.severities", "The ordered list of alert severities, from more prioritary to less prioritary.").Default("critical,warning,info").String() alertDefaultSeverity = application.Flag("alert.default-severity", "The alert severity if none is provided via labels.").Default("critical").String() - snmpDestination = application.Flag("snmp.destination", "SNMP trap server destination.").Default("127.0.0.1:162").String() + snmpDestination = application.Flag("snmp.destination", "SNMP trap server destination.").Default("127.0.0.1:162").TCP() snmpRetries = application.Flag("snmp.retries", "SNMP number of retries").Default("1").Uint() snmpTrapOidLabel = application.Flag("snmp.trap-oid-label", "Label where to find the trap OID.").Default("oid").String() snmpDefaultOid = application.Flag("snmp.trap-default-oid", "Trap OID to send if none is found in the alert labels").Default("1.3.6.1.4.1.1664.1").String() - snmpTrapDescriptionTemplate = application.Flag("snmp.trap-description-template", "SNMP description template.").Default(snmpTrapDescriptionTemplateDefault).String() + snmpTrapDescriptionTemplate = application.Flag("snmp.trap-description-template", "SNMP description template.").Default("description-template.tpl").ExistingFile() ) log.AddFlags(application) @@ -61,10 +49,10 @@ func ParseConfiguration(args []string) (*SNMPNotifierConfiguration, error) { application.HelpFlag.Short('h') kingpin.MustParse(application.Parse(args)) - descriptionTemplate, err := template.New("description").Funcs(template.FuncMap{ + descriptionTemplate, err := template.New(filepath.Base(*snmpTrapDescriptionTemplate)).Funcs(template.FuncMap{ "groupAlertsByLabel": commons.GroupAlertsByLabel, "groupAlertsByName": commons.GroupAlertsByName, - }).Parse(*snmpTrapDescriptionTemplate) + }).ParseFiles(*snmpTrapDescriptionTemplate) if err != nil { return nil, err } @@ -89,14 +77,14 @@ func ParseConfiguration(args []string) (*SNMPNotifierConfiguration, error) { } trapSenderConfiguration := trapsender.Configuration{ - SNMPDestination: *snmpDestination, + SNMPDestination: (*snmpDestination).String(), SNMPRetries: *snmpRetries, SNMPCommunity: snmpCommunity, DescriptionTemplate: *descriptionTemplate, } httpServerConfiguration := httpserver.Configuration{ - WebListenAddress: *webListenAddress, + WebListenAddress: (*webListenAddress).String(), } configuration := SNMPNotifierConfiguration{ diff --git a/configuration/configuration_test.go b/configuration/configuration_test.go index a1a36f0..aad3ef2 100644 --- a/configuration/configuration_test.go +++ b/configuration/configuration_test.go @@ -16,6 +16,7 @@ package configuration import ( "log" "os" + "path/filepath" "strings" "testing" "text/template" @@ -37,7 +38,7 @@ type Test struct { var tests = []Test{ { - "--web.listen-address=:1234", + "--web.listen-address=:1234 --snmp.trap-description-template=../description-template.tpl", "", SNMPNotifierConfiguration{ alertparser.Configuration{ @@ -59,7 +60,7 @@ var tests = []Test{ false, }, { - "--web.listen-address=:1234 --snmp.destination=127.0.0.2:163 --snmp.retries=4 --snmp.trap-default-oid=4.4.4 --snmp.trap-oid-label=other-oid --alert.default-severity=warning --alert.severity-label=criticity --alert.severities=critical,error,warning,info", + "--web.listen-address=:1234 --snmp.trap-description-template=../description-template.tpl --snmp.destination=127.0.0.2:163 --snmp.retries=4 --snmp.trap-default-oid=4.4.4 --snmp.trap-oid-label=other-oid --alert.default-severity=warning --alert.severity-label=criticity --alert.severities=critical,error,warning,info", "private", SNMPNotifierConfiguration{ alertparser.Configuration{ @@ -81,13 +82,7 @@ var tests = []Test{ false, }, { - "--snmp.trap-description-template=\"{{.lkdfjskl\"", - "", - SNMPNotifierConfiguration{}, - true, - }, - { - "--snmp.trap-default-oid=A.1.1.1", + "--snmp.trap-default-oid=A.1.1.1 --snmp.trap-description-template=../description-template.tpl", "", SNMPNotifierConfiguration{}, true, @@ -112,10 +107,10 @@ func TestParseConfiguration(t *testing.T) { } if err == nil { - descriptionTemplate, err := template.New("description").Funcs(template.FuncMap{ + descriptionTemplate, err := template.New(filepath.Base("description-template.tpl")).Funcs(template.FuncMap{ "groupAlertsByLabel": commons.GroupAlertsByLabel, "groupAlertsByName": commons.GroupAlertsByName, - }).Parse(snmpTrapDescriptionTemplateDefault) + }).ParseFiles("../description-template.tpl") if err != nil { t.Fatal("Error while generating default description template") } diff --git a/description-template.tpl b/description-template.tpl new file mode 100644 index 0000000..26e615e --- /dev/null +++ b/description-template.tpl @@ -0,0 +1,12 @@ +{{- if (len .Alerts) gt 0 -}} +{{- range $severity, $alerts := (groupAlertsByLabel .Alerts "severity") -}} +Status: {{ $severity }} +{{- range $index, $alert := $alerts }} +- Alert: {{ $alert.Labels.alertname }} + Summary: {{ $alert.Annotations.summary }} + Description: {{ $alert.Annotations.description }} +{{ end }} +{{ end }} +{{ else -}} +Status: OK +{{- end -}} \ No newline at end of file diff --git a/go.sum b/go.sum index a351b96..363a0f5 100644 --- a/go.sum +++ b/go.sum @@ -14,8 +14,8 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/geoffgarside/ber v0.0.0-20181018193237-27a1aff36ce6 h1:oQvud7S1g8tBMy3xj0dW1v4sCsrAPGPoiyajFYi8zQU= github.com/geoffgarside/ber v0.0.0-20181018193237-27a1aff36ce6/go.mod h1:x6zPZPDIQQKmaIDbeEzUGnxSmj7raqK6G8m6jkTlgbU= -github.com/go-ole/go-ole v1.2.2 h1:HXmymm3IQ8iAfpqlbbUGLHd+SZrnmI4y1pv+WL/3R7c= -github.com/go-ole/go-ole v1.2.2/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8= +github.com/go-ole/go-ole v1.2.2 h1:QNWhweRd9D5Py2rRVboZ2L4SEoW/dyraWJCc8bgS8kE= +github.com/go-ole/go-ole v1.2.2/go.mod h1:pnvuG7BrDMZ8ifMurTQmxwhQM/odqm9sSqNe5BUI7v4= github.com/go-test/deep v1.0.1 h1:UQhStjbkDClarlmv0am7OXXO4/GaPdCGiUiMTvi28sg= github.com/go-test/deep v1.0.1/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= From d506a9ac55697d0cc84a24b6951e1161a74480b2 Mon Sep 17 00:00:00 2001 From: Maxime Wojtczak Date: Tue, 5 Mar 2019 08:50:29 +0100 Subject: [PATCH 2/2] fix(Dockerfile): Add default template. --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index 52aac89..29fc165 100755 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,7 @@ FROM quay.io/prometheus/busybox:latest LABEL maintainer="Maxime Wojtczak " COPY snmp_notifier /bin/snmp_notifier +COPY description-template.tpl /bin/description-template.tpl EXPOSE 9464 ENTRYPOINT [ "/bin/snmp_notifier" ]