|
@@ -0,0 +1,67 @@
|
|
1
|
+package httplistener
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "bytes"
|
|
5
|
+ "encoding/json"
|
|
6
|
+ "fmt"
|
|
7
|
+ "github.com/juju/loggo"
|
|
8
|
+ "github.com/spf13/viper"
|
|
9
|
+ "github.com/thoj/go-ircevent"
|
|
10
|
+ "net/http"
|
|
11
|
+)
|
|
12
|
+
|
|
13
|
+var log = loggo.GetLogger("HTTPListener")
|
|
14
|
+
|
|
15
|
+type HTTPListener struct {
|
|
16
|
+ http http.Server
|
|
17
|
+ irc *irc.Connection
|
|
18
|
+}
|
|
19
|
+
|
|
20
|
+func New(irc *irc.Connection) (*HTTPListener, error) {
|
|
21
|
+ hl := HTTPListener{}
|
|
22
|
+ hl.irc = irc
|
|
23
|
+ hl.http = http.Server{Addr: viper.GetString("http.listen")}
|
|
24
|
+
|
|
25
|
+ mux := http.NewServeMux()
|
|
26
|
+ if viper.IsSet("http.listeners.grafana") {
|
|
27
|
+ mux.HandleFunc("/grafana", hl.grafanaAlertHandler)
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ hl.http.Handler = mux
|
|
31
|
+ log.Infof("Listening for HTTP requests on %s", viper.GetString("http.listen"))
|
|
32
|
+ go hl.http.ListenAndServe()
|
|
33
|
+ return &hl, nil
|
|
34
|
+}
|
|
35
|
+
|
|
36
|
+type grafanaMatch struct {
|
|
37
|
+ Metric string
|
|
38
|
+ Value float32
|
|
39
|
+}
|
|
40
|
+
|
|
41
|
+type grafanaAlert struct {
|
|
42
|
+ Title string
|
|
43
|
+ RuleName string
|
|
44
|
+ RuleUrl string
|
|
45
|
+ State string
|
|
46
|
+ ImageUrl string
|
|
47
|
+ Message string
|
|
48
|
+ EvalMatches []grafanaMatch
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+func (hl *HTTPListener) grafanaAlertHandler(w http.ResponseWriter, request *http.Request) {
|
|
52
|
+ if request.Method != "POST" {
|
|
53
|
+ http.NotFound(w, request)
|
|
54
|
+ return
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ var alert grafanaAlert
|
|
58
|
+ buf := new(bytes.Buffer)
|
|
59
|
+ buf.ReadFrom(request.Body)
|
|
60
|
+ json.Unmarshal(buf.Bytes(), &alert)
|
|
61
|
+ msg := fmt.Sprintf("[Grafana] [%s] %s: %s.", alert.State, alert.RuleName, alert.Message)
|
|
62
|
+ for _, match := range alert.EvalMatches {
|
|
63
|
+ msg += fmt.Sprintf(" %s:%f", match.Metric, match.Value)
|
|
64
|
+ }
|
|
65
|
+ msg += " " + alert.RuleUrl
|
|
66
|
+ hl.irc.Privmsgf(viper.GetString("http.listeners.grafana"), msg)
|
|
67
|
+}
|