forked from feiyu563/PrometheusAlert
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebhook.go
50 lines (46 loc) · 1.22 KB
/
webhook.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
package controllers
import (
"PrometheusAlert/models"
"bytes"
"crypto/tls"
"io/ioutil"
"net/http"
"net/url"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
)
func PostToWebhook(text, WebhookUrl, logsign string, contentType string) string {
logs.Info(logsign, "[Webhook]", text)
JsonMsg := bytes.NewReader([]byte(text))
var tr *http.Transport
if proxyUrl := beego.AppConfig.String("proxy"); proxyUrl != "" {
proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse(proxyUrl)
}
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: proxy,
}
} else {
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
client := &http.Client{Transport: tr}
if contentType == "" {
contentType = "application/json"
}
res, err := client.Post(WebhookUrl, contentType, JsonMsg)
if err != nil {
logs.Error(logsign, "[Webhook]", err.Error())
}
result, err := ioutil.ReadAll(res.Body)
if err != nil {
logs.Error(logsign, "[Webhook]", err.Error())
}
defer res.Body.Close()
models.AlertToCounter.WithLabelValues("webhook").Add(1)
ChartsJson.Webhook += 1
logs.Info(logsign, "[Webhook]", string(result))
return string(result)
}