-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
67 lines (59 loc) · 1.31 KB
/
hook.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
package main
import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"os/exec"
)
// Grafana web hook struct
type hookAlert struct {
EvalMatches []struct {
Metric string `json:"metric"`
Tags interface{} `json:"tags"`
Value float64 `json:"value"`
} `json:"evalMatches"`
ImageUrl string `json:"imageUrl"`
Message string `json:"message"`
RuleId int `json:"ruleId"`
RuleName string `json:"ruleName"`
RuleUrl string `json:"ruleUrl"`
State string `json:"state"`
Title string `json:"title"`
}
var webhookLockChan = make(chan bool, 1)
// basicAuth
func basicAuth() gin.HandlerFunc {
return gin.BasicAuth(gin.Accounts{
Config.Webhook.Username: Config.Webhook.Password,
})
}
// grafana webhook handler
func handlerWebhook(c *gin.Context) {
select {
case webhookLockChan <- true:
defer func() {
<-webhookLockChan
}()
break
default:
logrus.WithFields(logrus.Fields{"controller": "webhook"}).Warnln("busy")
return
}
// parse json
data := new(hookAlert)
err := c.BindJSON(data)
if err != nil {
logrus.Warnln(err)
return
}
// only except exec
if data.State != "ok" {
cmd := exec.Command(Config.Shell.Cmd.Name, Config.Shell.Cmd.Args...)
if err = cmd.Run(); err != nil {
logrus.Warnln(err)
return
}
c.JSON(200, false)
return
}
c.JSON(200, false)
}