From 64fd441f213731d11c771b3deddb067c563ea2f7 Mon Sep 17 00:00:00 2001 From: mfordjody <11638005@qq.com> Date: Sat, 7 Sep 2024 18:09:00 +0800 Subject: [PATCH 1/2] [horus] Add alarm framework Add License Check --- app/horus/basic/config/file.go | 16 ++++++- app/horus/core/alert/dingtalk.go | 74 ++++++++++++++++++++++++++++++++ app/horus/core/alert/slack.go | 20 +++++++++ deploy/horus/horus.yaml | 13 +++++- 4 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 app/horus/core/alert/dingtalk.go create mode 100644 app/horus/core/alert/slack.go diff --git a/app/horus/basic/config/file.go b/app/horus/basic/config/file.go index 4cd24a27d..5b2767a6b 100644 --- a/app/horus/basic/config/file.go +++ b/app/horus/basic/config/file.go @@ -16,8 +16,10 @@ package config type Config struct { - HttpAddr string `yaml:"HttpAddr"` - Mysql *MysqlConfiguration `yaml:"Mysql"` + Address string `yaml:"address"` + Mysql *MysqlConfiguration `yaml:"mysql"` + DingTalk *DingTalkConfiguration `yaml:"dingTalk"` + Slack *SlackConfiguration `yaml:"slack"` } type MysqlConfiguration struct { @@ -25,3 +27,13 @@ type MysqlConfiguration struct { Addr string `yaml:"addr"` Debug bool `yaml:"debug"` } + +type DingTalkConfiguration struct { + WebhookUrl string `yaml:"webhookUrl"` + Title string `yaml:"title"` + AtMobiles []string `yaml:"atMobiles"` +} + +type SlackConfiguration struct { + WebhookUrl string `yaml:"webhookUrl"` +} diff --git a/app/horus/core/alert/dingtalk.go b/app/horus/core/alert/dingtalk.go new file mode 100644 index 000000000..75b16e5b0 --- /dev/null +++ b/app/horus/core/alert/dingtalk.go @@ -0,0 +1,74 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alert + +import ( + "bytes" + "encoding/json" + "fmt" + "github.com/apache/dubbo-kubernetes/app/horus/basic/config" + "k8s.io/klog/v2" + "net/http" +) + +const Title = "钉钉机器人" + +type content struct { + Content string `json:"content"` +} + +type at struct { + AtMobiles []string `json:"atMobiles"` +} + +type Message struct { + MessageType string `json:"messageType"` + Text content `json:"text"` + At at `json:"at"` +} + +type T struct { + At struct { + AtMobiles []string `json:"atMobiles"` + AtUserIds []string `json:"atUserIds"` + IsAtAll bool `json:"isAtAll"` + } `json:"at"` + Text struct { + Content string `json:"content"` + } `json:"text"` + Msgtype string `json:"msgtype"` +} + +func DingTalkSend(dk *config.DingTalkConfiguration, msg string) { + dtm := Message{MessageType: "text"} + dtm.Text.Content = fmt.Sprintf("%s\n"+ + "【日志详细信息:%s】", Title, msg) + dtm.At.AtMobiles = dk.AtMobiles + bs, err := json.Marshal(dtm) + if err != nil { + klog.Errorf("dingTalk json marshal err:%v\n msg:%v\n", err, msg) + return + } + res, err := http.Post(dk.WebhookUrl, "application/json", bytes.NewBuffer(bs)) + if err != nil { + klog.Errorf("push dingTalk err:%v\n msg:%v\n", err, msg) + } + if res.StatusCode != 200 && res != nil { + klog.Errorf("push dingTalk status code err:%v\n code:%v\n msg:%v\n", err, res.StatusCode, msg) + return + } + klog.Infof("push dingTalk success code:%v\n msg:%v\n", res.StatusCode, msg) +} diff --git a/app/horus/core/alert/slack.go b/app/horus/core/alert/slack.go new file mode 100644 index 000000000..867a5cd22 --- /dev/null +++ b/app/horus/core/alert/slack.go @@ -0,0 +1,20 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package alert + +import "github.com/apache/dubbo-kubernetes/app/horus/basic/config" + +func SlackSend(sk *config.SlackConfiguration) {} diff --git a/deploy/horus/horus.yaml b/deploy/horus/horus.yaml index 707fff914..12c4ee806 100644 --- a/deploy/horus/horus.yaml +++ b/deploy/horus/horus.yaml @@ -18,4 +18,15 @@ address: 0.0.0.0:38089 mysql: name: horus addr: "root:root@tcp(127.0.0.1:3306)/horus?charset=utf8&parseTime=True" - debug: false \ No newline at end of file + debug: false + +dingTalk: + webhookUrl: "https://oapi.dingtalk.com/robot/send?access_token=aa2f3f74d7a2504653ca89b7a673707ba1d04b6d9d320c3572e5464d8f81471e" + title: "工单通知处理" + atMobiles: + - 1500000000 + +slack: + webhookUrl: ~ + proxy: ~ + payload: ~ \ No newline at end of file From 1d50a10d1fbc191de019a7690e16b8b497cba22b Mon Sep 17 00:00:00 2001 From: mfordjody <11638005@qq.com> Date: Sat, 7 Sep 2024 19:29:30 +0800 Subject: [PATCH 2/2] [horus] fix dingtalk alert --- app/horus/core/alert/dingtalk.go | 42 ++++++++++++++++---------------- deploy/horus/horus.yaml | 6 ++--- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/app/horus/core/alert/dingtalk.go b/app/horus/core/alert/dingtalk.go index 75b16e5b0..099a00eed 100644 --- a/app/horus/core/alert/dingtalk.go +++ b/app/horus/core/alert/dingtalk.go @@ -26,20 +26,6 @@ import ( const Title = "钉钉机器人" -type content struct { - Content string `json:"content"` -} - -type at struct { - AtMobiles []string `json:"atMobiles"` -} - -type Message struct { - MessageType string `json:"messageType"` - Text content `json:"text"` - At at `json:"at"` -} - type T struct { At struct { AtMobiles []string `json:"atMobiles"` @@ -52,23 +38,37 @@ type T struct { Msgtype string `json:"msgtype"` } +type content struct { + Content string `json:"content"` +} + +type at struct { + AtMobiles []string `json:"atMobiles"` +} + +type Message struct { + MsgType string `json:"msgtype"` + Text content `json:"text"` + At at `json:"at"` +} + func DingTalkSend(dk *config.DingTalkConfiguration, msg string) { - dtm := Message{MessageType: "text"} + dtm := Message{MsgType: "text"} dtm.Text.Content = fmt.Sprintf("%s\n"+ - "【日志详细信息:%s】", Title, msg) + "【日志:%s】", Title, msg) dtm.At.AtMobiles = dk.AtMobiles bs, err := json.Marshal(dtm) if err != nil { - klog.Errorf("dingTalk json marshal err:%v\n msg:%v\n", err, msg) + klog.Errorf("dingTalk json marshal err:%v\n dtm:%v\n", err, dtm) return } res, err := http.Post(dk.WebhookUrl, "application/json", bytes.NewBuffer(bs)) if err != nil { - klog.Errorf("push dingTalk err:%v\n msg:%v\n", err, msg) + klog.Errorf("send dingTalk err:%v\n msg:%v\n", err, msg) } - if res.StatusCode != 200 && res != nil { - klog.Errorf("push dingTalk status code err:%v\n code:%v\n msg:%v\n", err, res.StatusCode, msg) + if res != nil && res.StatusCode != 200 { + klog.Errorf("send dingTalk status code err:%v\n code:%v\n msg:%v\n", err, res.StatusCode, msg) return } - klog.Infof("push dingTalk success code:%v\n msg:%v\n", res.StatusCode, msg) + klog.Infof("send dingTalk success code:%v\n msg:%v\n", res.StatusCode, msg) } diff --git a/deploy/horus/horus.yaml b/deploy/horus/horus.yaml index 12c4ee806..533565f06 100644 --- a/deploy/horus/horus.yaml +++ b/deploy/horus/horus.yaml @@ -21,10 +21,10 @@ mysql: debug: false dingTalk: - webhookUrl: "https://oapi.dingtalk.com/robot/send?access_token=aa2f3f74d7a2504653ca89b7a673707ba1d04b6d9d320c3572e5464d8f81471e" - title: "工单通知处理" + webhookUrl: ~ + title: ~ atMobiles: - - 1500000000 + - 1500000000 slack: webhookUrl: ~