-
Notifications
You must be signed in to change notification settings - Fork 493
/
service.go
77 lines (68 loc) · 1.33 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
package sensu
import (
"bytes"
"encoding/json"
"errors"
"github.com/influxdata/kapacitor"
"log"
"net/http"
)
type Service struct {
url string
source string
logger *log.Logger
}
func NewService(c Config, l *log.Logger) *Service {
return &Service{
url: c.URL,
source: c.Source,
logger: l,
}
}
func (s *Service) Open() error {
return nil
}
func (s *Service) Close() error {
return nil
}
func (s *Service) Alert(name, output string, level kapacitor.AlertLevel) error {
var status int
switch level {
case kapacitor.OKAlert:
status = 0
case kapacitor.InfoAlert:
status = 0
case kapacitor.WarnAlert:
status = 1
case kapacitor.CritAlert:
status = 2
default:
status = 3
}
postData := make(map[string]interface{})
postData["name"] = name
postData["source"] = s.source
postData["output"] = output
postData["status"] = status
var post bytes.Buffer
enc := json.NewEncoder(&post)
err := enc.Encode(postData)
if err != nil {
return err
}
resp, err := http.Post(s.url, "application/json", &post)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
type response struct {
Error string `json:"error"`
}
r := &response{Error: "failed to understand Sensu response"}
dec := json.NewDecoder(resp.Body)
dec.Decode(r)
return errors.New(r.Error)
}
return nil
}