Skip to content

Commit 13a2b49

Browse files
committed
<feature>add dingding alter support
1 parent c07e8eb commit 13a2b49

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

action/action.go

+51
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"errors"
99
"fmt"
10+
"io/ioutil"
1011
"net/http"
1112
"strings"
1213

@@ -50,6 +51,8 @@ func ToActioner(in interface{}) (Action, error) {
5051
a = &HTTP{}
5152
case "slack":
5253
a = &Slack{}
54+
case "dingding":
55+
a = &DingDing{}
5356
default:
5457
return Action{}, fmt.Errorf("unknown action type: %q", typ)
5558
}
@@ -142,3 +145,51 @@ func (s *Slack) Do(c context.Context) error {
142145
resp.Body.Close()
143146
return nil
144147
}
148+
149+
type DingDing struct {
150+
Text string `json:"text" mapstructure:"text"`
151+
}
152+
153+
func (d *DingDing) Do(c context.Context) error {
154+
if config.Opts.DingDingWebhook == "" {
155+
return errors.New("DingDing Webhook not set in config")
156+
}
157+
158+
if d.Text == "" {
159+
return errors.New("missing required field text in DingDing")
160+
}
161+
162+
r, err := http.NewRequest("POST", config.Opts.DingDingWebhook, bytes.NewBufferString(d.Text))
163+
if err != nil {
164+
return err
165+
}
166+
r.Header.Set("Content-Type", "application/json;charset=utf-8")
167+
168+
169+
resp, err := http.DefaultClient.Do(r)
170+
if err != nil {
171+
return err
172+
}
173+
174+
defer resp.Body.Close()
175+
176+
if resp.StatusCode < 200 || resp.StatusCode > 299 {
177+
return fmt.Errorf("non 2xx response code returned: %d", resp.StatusCode)
178+
}
179+
180+
type response struct {
181+
ErrCode uint `json:"errcode"`
182+
ErrMsg string `json:"errmsg"`
183+
}
184+
var res response
185+
body, err := ioutil.ReadAll(resp.Body)
186+
if err != nil {
187+
return err
188+
}
189+
err = json.Unmarshal(body, &res)
190+
if res.ErrCode != 0 {
191+
return fmt.Errorf("response error: %s", res.ErrMsg)
192+
}
193+
return nil
194+
195+
}

cmd/esalert/main.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"io/ioutil"
65
"os"
76
"path/filepath"
@@ -22,7 +21,7 @@ func main() {
2221
}
2322

2423
files := make([]string, 0, 10)
25-
if !fstat.IsDir() {
24+
if fstat != nil && !fstat.IsDir() {
2625
files = append(files, config.Opts.AlertFileDir)
2726
} else {
2827
fileInfos, err := ioutil.ReadDir(config.Opts.AlertFileDir)
@@ -49,7 +48,6 @@ func main() {
4948
log.WithFields(kv).Fatalln("failed to read alert config")
5049
}
5150

52-
fmt.Println(file)
5351
if err := yaml.Unmarshal(b, &alerts); err != nil {
5452
kv["err"] = err
5553
log.WithFields(kv).Fatalln("failed to parse yaml")

config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var Opts struct {
2424
SlackWebhook string `long:"slack-webhook" description:"Slack webhook url, required if using any Slack actions"`
2525
ForceRun string `long:"force-run" description:"If set with the name of an alert, will immediately run that alert and exit. Useful for testing changes to alert definitions"`
2626
LogLevel string `long:"log-level" default:"info" description:"Adjust the log level. Valid options are: error, warn, info, debug"`
27+
DingDingWebhook string `long:"dingding-webhook" description:"dingding webhook url, required if using any Dingding action"`
2728
}
2829

2930
func init() {

0 commit comments

Comments
 (0)