/
service.go
276 lines (233 loc) · 5.54 KB
/
service.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package pushover
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"sync/atomic"
"github.com/influxdata/kapacitor/alert"
"github.com/influxdata/kapacitor/keyvalue"
)
type Diagnostic interface {
WithContext(ctx ...keyvalue.T) Diagnostic
Error(msg string, err error)
}
type Service struct {
configValue atomic.Value
diag Diagnostic
}
func NewService(c Config, d Diagnostic) *Service {
s := &Service{
diag: d,
}
s.configValue.Store(c)
return s
}
func (s *Service) Open() error {
return nil
}
func (s *Service) Close() error {
return nil
}
func (s *Service) Update(newConfig []interface{}) error {
if l := len(newConfig); l != 1 {
return fmt.Errorf("expected only one new config object, got %d", l)
}
if c, ok := newConfig[0].(Config); !ok {
return fmt.Errorf("expected config object to be of type %T, got %T", c, newConfig[0])
} else {
s.configValue.Store(c)
}
return nil
}
type testOptions struct {
UserKey string `json:"user-key"`
Message string `json:"message"`
Device string `json:"device"`
Title string `json:"title"`
URL string `json:"url"`
URLTitle string `json:"url-title"`
Sound string `json:"sound"`
Level alert.Level `json:"level"`
}
func (s *Service) TestOptions() interface{} {
c := s.config()
return &testOptions{
UserKey: c.UserKey,
Message: "test pushover message",
Level: alert.Critical,
}
}
func (s *Service) Test(options interface{}) error {
o, ok := options.(*testOptions)
if !ok {
return fmt.Errorf("unexpected options type %t", options)
}
return s.Alert(
o.Message,
o.Device,
o.Title,
o.URL,
o.URLTitle,
o.Sound,
o.UserKey,
o.Level,
)
}
func (s *Service) config() Config {
return s.configValue.Load().(Config)
}
func (s *Service) Alert(message, device, title, URL, URLTitle, sound string, userKey string, level alert.Level) error {
url, post, err := s.preparePost(message, device, title, URL, URLTitle, sound, userKey, level)
if err != nil {
return err
}
resp, err := http.PostForm(url, post)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
pushoverResponse := struct {
Errors []string `json:"errors"`
}{}
err = json.Unmarshal(body, &pushoverResponse)
if err != nil {
return err
}
type response struct {
Error string `json:"error"`
}
r := &response{Error: fmt.Sprintf("failed to understand Pushover response. code: %d content: %s", resp.StatusCode, strings.Join(pushoverResponse.Errors, ", "))}
b := bytes.NewReader(body)
dec := json.NewDecoder(b)
dec.Decode(r)
return errors.New(r.Error)
}
return nil
}
// priority returns the pushover priority as defined by the Pushover API
// documentation https://pushover.net/api
func priority(level alert.Level) int {
switch level {
case alert.OK:
// send as -2 to generate no notification/alert
return -2
case alert.Info:
// -1 to always send as a quiet notification
return -1
case alert.Warning:
// 0 to display as high-priority and bypass the user's quiet hours,
return 0
case alert.Critical:
// 1 to also require confirmation from the user
return 1
}
return 0
}
type postData struct {
Token string
UserKey string
Message string
Device string
Title string
URL string
URLTitle string
Priority int
Sound string
}
func (p *postData) Values() url.Values {
v := url.Values{}
v.Set("token", p.Token)
v.Set("user", p.UserKey)
v.Set("message", p.Message)
v.Set("priority", strconv.Itoa(p.Priority))
if p.Device != "" {
v.Set("device", p.Device)
}
if p.Title != "" {
v.Set("title", p.Title)
}
if p.URL != "" {
v.Set("url", p.URL)
}
if p.URLTitle != "" {
v.Set("url_title", p.URLTitle)
}
if p.Sound != "" {
v.Set("sound", p.Sound)
}
return v
}
func (s *Service) preparePost(message, device, title, URL, URLTitle, sound string, userKey string, level alert.Level) (string, url.Values, error) {
c := s.config()
if !c.Enabled {
return "", nil, errors.New("service is not enabled")
}
if userKey == "" {
userKey = c.UserKey
}
p := postData{
Token: c.Token,
UserKey: userKey,
Message: message,
}
p.Device = device
p.Title = title
p.URL = URL
p.URLTitle = URLTitle
p.Sound = sound
p.Priority = priority(level)
return c.URL, p.Values(), nil
}
type HandlerConfig struct {
// rather than all of a user's devices (multiple device names may
// be separated by a comma)
Device string `mapstructure:"device"`
// Your message's title, otherwise your apps name is used
Title string `mapstructure:"title"`
// A supplementary URL to show with your message
URL string `mapstructure:"url"`
// A title for your supplementary URL, otherwise just URL is shown
URLTitle string `mapstructure:"url-title"`
// The name of one of the sounds supported by the device clients to override
// the user's default sound choice
Sound string `mapstructure:"sound"`
// The User Key or Delivery Group key
UserKey string `mapstructure:"user-key"`
}
type handler struct {
s *Service
c HandlerConfig
diag Diagnostic
}
func (s *Service) Handler(c HandlerConfig, ctx ...keyvalue.T) alert.Handler {
return &handler{
s: s,
c: c,
diag: s.diag.WithContext(ctx...),
}
}
func (h *handler) Handle(event alert.Event) {
if err := h.s.Alert(
event.State.Message,
h.c.Device,
h.c.Title,
h.c.URL,
h.c.URLTitle,
h.c.Sound,
h.c.UserKey,
event.State.Level,
); err != nil {
h.diag.Error("failed to send event to Pushover", err)
}
}