/
connector.go
153 lines (133 loc) · 3.93 KB
/
connector.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package nagiosapi
import (
"context"
"encoding/json"
"fmt"
html "html/template"
"net/http"
"strconv"
"time"
"github.com/synyx/tuwat/pkg/connectors"
"github.com/synyx/tuwat/pkg/connectors/common"
"github.com/uptrace/opentelemetry-go-extra/otelzap"
"go.uber.org/zap"
)
type Connector struct {
config Config
client *http.Client
}
type Config struct {
Tag string
NagiosURL string
common.HTTPConfig
}
func NewConnector(cfg *Config) *Connector {
return &Connector{*cfg, cfg.HTTPConfig.Client()}
}
func (c *Connector) Tag() string {
return c.config.Tag
}
func (c *Connector) Collect(ctx context.Context) ([]connectors.Alert, error) {
content, err := c.collectHosts(ctx)
if err != nil {
return nil, err
}
var alerts []connectors.Alert
for hostName, host := range content {
if host.ProblemHasBeenAcknowledged == "1" {
continue
} else if host.NotificationsEnabled == "0" {
continue
} else if i, e := strconv.ParseInt(host.ScheduledDowntimeDepth, 10, 32); e == nil && i > 0 {
continue
} else if host.CurrentState != "0" {
state, err := strconv.ParseInt(host.CurrentState, 10, 32)
if err != nil {
otelzap.Ctx(ctx).DPanic("Cannot parse", zap.Error(err))
}
stateChange, err := strconv.ParseInt(host.LastStateChange, 10, 64)
if err != nil {
otelzap.Ctx(ctx).DPanic("Cannot parse", zap.Error(err))
}
alert := connectors.Alert{
Labels: map[string]string{
"Hostname": hostName,
"Source": c.config.URL,
"Type": "Host",
},
Start: time.Unix(stateChange, 0),
State: connectors.State(state),
Description: "Host down",
Details: host.PluginOutput,
Links: []html.HTML{
html.HTML("<a href=\"" + c.config.NagiosURL + "/cgi-bin/extinfo.cgi?type=1&host=" + hostName + "\" target=\"_blank\" alt=\"Home\">🏠</a>"),
},
}
alert.Silence = c.createSilencer(alert)
alerts = append(alerts, alert)
continue
}
for serviceName, service := range host.Services {
if service.CurrentState == "0" {
continue
} else if service.NotificationsEnabled == "0" {
continue
} else if service.ProblemHasBeenAcknowledged == "1" {
continue
} else if i, e := strconv.ParseInt(service.ScheduledDowntimeDepth, 10, 32); e == nil && i > 0 {
continue
}
state, err := strconv.ParseInt(service.CurrentState, 10, 32)
if err != nil {
otelzap.Ctx(ctx).DPanic("Cannot parse", zap.Error(err))
}
stateChange, err := strconv.ParseInt(service.LastStateChange, 10, 64)
if err != nil {
otelzap.Ctx(ctx).DPanic("Cannot parse", zap.Error(err))
}
alert := connectors.Alert{
Labels: map[string]string{
"Hostname": hostName,
"Source": c.config.URL,
"Type": "Service",
},
Start: time.Unix(stateChange, 0),
State: connectors.State(state),
Description: serviceName,
Details: service.PluginOutput,
Links: []html.HTML{
html.HTML("<a href=\"" + c.config.NagiosURL + "/cgi-bin/extinfo.cgi?type=2&host=" + hostName + "&service=" + serviceName + "\" target=\"_blank\" alt=\"Home\">🏠</a>"),
},
}
alert.Silence = c.createSilencer(alert)
alerts = append(alerts, alert)
}
}
return alerts, nil
}
func (c *Connector) String() string {
return fmt.Sprintf("NagiosAPI (%s)", c.config.URL)
}
func (c *Connector) collectHosts(ctx context.Context) (map[string]host, error) {
otelzap.Ctx(ctx).Debug("getting alerts", zap.String("url", c.config.URL+"/state"))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.config.URL+"/state", nil)
if err != nil {
return nil, err
}
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
var response response
err = decoder.Decode(&response)
if err != nil {
return nil, err
}
if !response.Success {
// TODO(jo): the `content` map is overloaded with an error string
return nil, fmt.Errorf("API failure response")
}
return response.Content, nil
}