-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
246 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
{ | ||
"addr": "localhost:23329", | ||
"dingTalk": { | ||
"enable": true | ||
}, | ||
"wexin": { | ||
"enable": false, | ||
"corpID": "", | ||
"agentId": "", | ||
"secret": "", | ||
"encodingAESKey": "" | ||
} | ||
"addr": "localhost:23329", | ||
"dingTalk": { | ||
"enable": true, | ||
"templateFile": "message-template.md", | ||
"messageType": "markdown" | ||
}, | ||
"wexin": { | ||
"enable": false, | ||
"corpID": "", | ||
"agentId": "", | ||
"secret": "", | ||
"encodingAESKey": "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,58 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"log" | ||
"encoding/json" | ||
"io/ioutil" | ||
"log" | ||
) | ||
|
||
// Config 配置 | ||
type Config struct { | ||
Addr string `json:"addr"` | ||
DingTalk DingTalk `json:"dingTalk"` | ||
Addr string `json:"addr"` | ||
DingTalk DingTalk `json:"dingTalk"` | ||
|
||
Weixin Weixin `json:"weixin"` | ||
Weixin Weixin `json:"weixin"` | ||
} | ||
|
||
// Weixin 微信配置 | ||
type Weixin struct { | ||
Enable bool | ||
CorpID string `json:"corpID"` | ||
AgentID string `json:"agentId"` | ||
Secret string `json:"secret"` | ||
EncodingAESKey string `json:"encodingAESKey"` | ||
Enable bool | ||
CorpID string `json:"corpID"` | ||
AgentID string `json:"agentId"` | ||
Secret string `json:"secret"` | ||
EncodingAESKey string `json:"encodingAESKey"` | ||
} | ||
|
||
type AlarmMessage struct { | ||
Level string // 告警等级 P1 | ||
Type string // 类型 PROBLEM,OK | ||
Endpoint string // 主机host或者ip | ||
Desc string // 告警描述 | ||
Condition string // 告警条件 | ||
Count string // 当前告警次数 | ||
Time string // 告警时间 | ||
} | ||
|
||
// DingTalk 钉钉配置 | ||
type DingTalk struct { | ||
Enable bool `json:"enable"` | ||
// Level 等级, 只发送level 及其以下的消息 | ||
Enable bool `json:"enable"` | ||
// Level 等级, 只发送level 及其以下 的消息 | ||
|
||
Level uint `json:"level"` | ||
Level uint `json:"level"` | ||
TemplateFile string | ||
MessageType string // markdown ,text | ||
} | ||
|
||
// Read 读取配置 | ||
func Read() Config { | ||
bytes, err := ioutil.ReadFile("cfg.json") | ||
if err != nil { | ||
log.Fatalln("need file: cfg.json") | ||
} | ||
var cfg Config | ||
if err = json.Unmarshal(bytes, &cfg); err != nil { | ||
log.Fatalln("config file error", err.Error()) | ||
} | ||
|
||
return cfg | ||
bytes, err := ioutil.ReadFile("cfg.json") | ||
if err != nil { | ||
log.Fatalln("need file: cfg.json") | ||
} | ||
var cfg Config | ||
if err = json.Unmarshal(bytes, &cfg); err != nil { | ||
log.Fatalln("config file error", err.Error()) | ||
} | ||
|
||
return cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## 告警 | ||
|
||
- 告警等级: {{.Level}} | ||
- 告警类型: {{.Type}} | ||
- 告警指标: {{.Condition}} | ||
- 告警主机: {{.Host}} | ||
- 告警时间: {{.Time}} | ||
- 告警说明: {{.Desc}},已持续{{.Count}}分钟 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,38 @@ | ||
package sender | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/labstack/echo" | ||
"github.com/sdvdxl/dinghook" | ||
"errors" | ||
"github.com/labstack/echo" | ||
"github.com/sdvdxl/dinghook" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
type DingTalk struct { | ||
} | ||
|
||
func (d *DingTalk) Send(token string, content string) error { | ||
if token == "" { | ||
return errors.New("need dingding token") | ||
} | ||
func (d *DingTalk) Send(token string, content, msgType string) error { | ||
if token == "" { | ||
return errors.New("need dingding token") | ||
} | ||
|
||
// 发送钉钉 | ||
ding := dinghook.NewDing(token) | ||
result := ding.SendMessage(dinghook.Message{Content: content}) | ||
log.Println(result) | ||
if !result.Success { | ||
log.Println("token:", token) | ||
return echo.NewHTTPError(http.StatusBadRequest, result.ErrMsg) | ||
} | ||
// 发送钉钉 | ||
ding := dinghook.NewDing(token) | ||
var result dinghook.Result | ||
if msgType == dinghook.MsgTypeMarkdown { | ||
result = ding.SendMarkdown(dinghook.Markdown{Title: "告警", Content: content}) | ||
} else { | ||
result = ding.SendMessage(dinghook.Message{Content: content}) | ||
} | ||
log.Println(result) | ||
if !result.Success { | ||
log.Println("token:", token, " send result:", result) | ||
return echo.NewHTTPError(http.StatusBadRequest, result.ErrMsg) | ||
} | ||
|
||
return nil | ||
return nil | ||
} | ||
|
||
func NewDingTalk() *DingTalk { | ||
return &DingTalk{} | ||
return &DingTalk{} | ||
} |
Oops, something went wrong.