forked from AcalephStorage/consul-alerts
/
influxdb-notifier.go
86 lines (72 loc) · 1.92 KB
/
influxdb-notifier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package notifier
import (
log "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/Sirupsen/logrus"
"github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/influxdb/influxdb/client"
)
type InfluxdbNotifier struct {
Enabled bool
Host string `json:"host"`
Username string `json:"username"`
Password string `json:"password"`
Database string `json:"database"`
SeriesName string `json:"series-name"`
}
// NotifierName provides name for notifier selection
func (influxdb *InfluxdbNotifier) NotifierName() string {
return "influxdb"
}
func (influxdb *InfluxdbNotifier) Copy() Notifier {
notifier := *influxdb
return ¬ifier
}
//Notify sends messages to the endpoint notifier
func (influxdb *InfluxdbNotifier) Notify(messages Messages) bool {
config := &client.ClientConfig{
Host: influxdb.Host,
Username: influxdb.Username,
Password: influxdb.Password,
Database: influxdb.Database,
}
influxdbClient, err := client.New(config)
if err != nil {
log.Println("unable to access influxdb. can't send notification. ", err)
return false
}
seriesList := influxdb.toSeries(messages)
err = influxdbClient.WriteSeries(seriesList)
if err != nil {
log.Println("unable to send notifications: ", err)
return false
}
log.Println("influxdb notification sent.")
return true
}
func (influxdb InfluxdbNotifier) toSeries(messages Messages) []*client.Series {
seriesName := influxdb.SeriesName
columns := []string{
"node",
"service",
"checks",
"notes",
"output",
"status",
}
seriesList := make([]*client.Series, len(messages))
for index, message := range messages {
point := []interface{}{
message.Node,
message.Service,
message.Check,
message.Notes,
message.Output,
message.Status,
}
series := &client.Series{
Name: seriesName,
Columns: columns,
Points: [][]interface{}{point},
}
seriesList[index] = series
}
return seriesList
}