Skip to content

Commit

Permalink
fix(Dockerfile): Add template.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Wojtczak committed Mar 5, 2019
2 parents 7438fc8 + d506a9a commit 413a6b7
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion .promu.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
verbose: false
go:
version: 1.11.0
version: 1.12.0
cgo: false
repository:
path: github.com/maxwo/snmp_notifier
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
- git diff --exit-code
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,31 +95,31 @@ usage: snmp_notifier [<flags>]
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

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.3.0
28 changes: 8 additions & 20 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package configuration
import (
"fmt"
"os"
"path/filepath"
"strings"
"text/template"

Expand All @@ -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"
)
Expand All @@ -45,26 +33,26 @@ 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)
application.Version(version.Print("snmp_notifier"))
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
}
Expand All @@ -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{
Expand Down
17 changes: 6 additions & 11 deletions configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package configuration
import (
"log"
"os"
"path/filepath"
"strings"
"testing"
"text/template"
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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,
Expand All @@ -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")
}
Expand Down
12 changes: 12 additions & 0 deletions description-template.tpl
Original file line number Diff line number Diff line change
@@ -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 -}}
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 413a6b7

Please sign in to comment.