-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprometheus.go
More file actions
91 lines (81 loc) · 2.53 KB
/
prometheus.go
File metadata and controls
91 lines (81 loc) · 2.53 KB
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
package main
import (
"bytes"
"encoding/json"
"github.com/fiorix/go-smpp/smpp"
"github.com/fiorix/go-smpp/smpp/pdu/pdufield"
"github.com/fiorix/go-smpp/smpp/pdu/pdutext"
"io/ioutil"
"net/http"
)
type prometheusType struct {
Version string `json:"version"`
GroupKey string `json:"groupKey"`
Status string `json:"status"`
Receiver string `json:"receiver"`
GroupLabels map[string]string `json:"groupLabels"`
CommonLabels map[string]string `json:"commonLabels"`
CommonAnnotations map[string]string `json:"commonAnnotations"`
ExternalURL string `json:"externalURL"`
Alerts []struct {
Status string `json:"status"`
Labels map[string]string `json:"labels"`
Annotations map[string]string `json:"annotations"`
StartsAt string `json:"startsAt"`
EndsAt string `json:"endsAt"`
GeneratorURL string `json:"generatorURL"`
} `json:"alerts"`
}
func prometheusHandler(w http.ResponseWriter, r *http.Request) {
if !config.Prometheus.Enable {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
if r.Method != http.MethodPost {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
user, password, ok := r.BasicAuth()
if !(ok && user == config.Prometheus.User && password == config.Prometheus.Password) {
log.Printf("Bad auth to prometheus from %s", r.RemoteAddr)
w.WriteHeader(http.StatusUnauthorized)
return
}
var prometheus prometheusType
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println(err)
}
defer r.Body.Close()
err = json.Unmarshal(body, &prometheus)
if err != nil {
log.Printf("Unmarshal prometheus json error: %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sms := &bytes.Buffer{}
err = prometheusTmpl.Execute(sms, prometheus)
if err != nil {
log.Printf("Parse prometheus template error: %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for i := range config.Prometheus.Recipient {
_, err = tx.SubmitLongMsg(&smpp.ShortMessage{
Src: config.Prometheus.From,
Dst: config.Prometheus.Recipient[i],
Text: pdutext.UCS2(sms.Bytes()),
Register: pdufield.NoDeliveryReceipt,
})
if err == smpp.ErrNotConnected {
log.Printf("Smpp not connected for send to %s", config.Prometheus.Recipient[i])
http.Error(w, "Oops.", http.StatusServiceUnavailable)
return
}
if err != nil {
log.Printf("Sms send to %s error: %s", config.Prometheus.Recipient[i], err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
}