Skip to content

Commit

Permalink
Prometheus企微机器人告警
Browse files Browse the repository at this point in the history
  • Loading branch information
qishu321 committed Sep 5, 2023
1 parent ba17925 commit a9905da
Show file tree
Hide file tree
Showing 16 changed files with 827 additions and 92 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
- 简单的工作流执行
- etcd的备份和回档
- k8s的多集群管理
- 基于go实现Prometheus企微机器人告警
```bash
##k8s的多集群管理
- 目前实现的功能:
kubeconfig的管理,存储到数据库里,然后根据这个实现多集群的管理,多集群可以任意切换
web创建namespace、svc;web查看pod的日志、webssh登录pod、web获取集群监控汇总详情等
web创建namespace、svc;web查看pod的日志、web查看pod的yaml、webssh登录pod、web获取集群监控汇总详情等
Prometheus企微机器人告警
```

## 部署方法
Expand Down Expand Up @@ -81,6 +83,8 @@ git clone https://github.com/qishu321/cmdb-ops-flow.git
## 预览
<img src="https://github.com/qishu321/cmdb-ops-flow/blob/main/doc/kube-config.png?raw=true" style="zoom: 25%;" />

<img src="https://github.com/qishu321/cmdb-ops-flow/blob/main/doc/wx-alert.png?raw=true" style="zoom: 25%;" />

<img src="https://github.com/qishu321/cmdb-ops-flow/blob/main/doc/getpodyaml.png?raw=true" style="zoom: 25%;" />

<img src="https://github.com/qishu321/cmdb-ops-flow/blob/main/doc/webssh_pod.png?raw=true" style="zoom: 25%;" />
Expand Down
27 changes: 27 additions & 0 deletions api/api_prom/alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package api_prom

import (
"cmdb-ops-flow/conf"
"cmdb-ops-flow/models/prometheus"
"cmdb-ops-flow/utils/msg"
"cmdb-ops-flow/utils/result"
"github.com/gin-gonic/gin"

svc_prome "cmdb-ops-flow/service/prometheus"
"net/http"
)

var defaultRobot = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="

func Alter(c *gin.Context) {
aaa := defaultRobot + conf.Wxhookkey
var notification prometheus.Notification
err := c.BindJSON(&notification)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
data := svc_prome.SendMessage(notification, aaa)
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, data, msg.GetErrMsg(code)))
}
76 changes: 76 additions & 0 deletions api/flow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package api

import (
"cmdb-ops-flow/models"
"cmdb-ops-flow/utils/goflow"
"cmdb-ops-flow/utils/msg"
"cmdb-ops-flow/utils/result"
"cmdb-ops-flow/utils/ssh"
"github.com/gin-gonic/gin"
"net/http"
"sort"
)

var FLW = goflow.New()

func FlowAPI(c *gin.Context) {
var data models.Job
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

// 获取 CheckJobgroup 的数据
list, err := models.CheckJobgroups(data.Jobgroup)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

// 创建一个 goplow 流程
flow := goflow.New()

sort.Slice(list, func(i, j int) bool {
return list[i].Jobleve < list[j].Jobleve
})
// 遍历每个 job
for _, job := range list {
job := job // 创建一个本地副本以避免在 goroutine 中的数据竞争
flow.Add(job.Jobname, []string{}, func(res map[string]interface{}) (interface{}, error) {
serverResults := make(map[string]string)
// 遍历每个服务器
for _, cmdb := range job.Cmdbnames {
// 遍历每个脚本
for _, script := range job.Scriptnames {
// 执行命令
commandOutput, err := ssh.ExecuteRemoteCommand(&script, &cmdb)
if err != nil {
serverResults[cmdb.PrivateIP+"_"+script.Name+"_executeCommand"] = err.Error()
} else {
serverResults[cmdb.PrivateIP+"_"+script.Name+"_commandOutput"] = "命令执行成功:\n" + commandOutput // 添加命令执行结果
}

// 创建文件
_, err = ssh.CreateRemoteFile(&script, &cmdb)
if err != nil {
serverResults[cmdb.PrivateIP+"_"+script.Name+"_createFile"] = err.Error()
} else {
serverResults[cmdb.PrivateIP+"_"+script.Name+"_createFile"] = "文件创建成功"
}
}
}

return serverResults, nil
})
}

// 执行流程
results, err := flow.Do()
//fmt.Println(results)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, (&result.Result{}).Ok(200, results, msg.GetErrMsg(200)))
}
3 changes: 2 additions & 1 deletion conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (

Md5Key string
Encryptkey string

Wxhookkey string
DbHost string
DbPort string
DbUser string
Expand All @@ -40,6 +40,7 @@ func LoadServer(file *ini.File) {
func LoadApp(file *ini.File) {
Md5Key = file.Section("app").Key("Md5Key").String()
Encryptkey = file.Section("app").Key("Encryptkey").String()
Wxhookkey = file.Section("app").Key("Wxhookkey").String()

}

Expand Down
1 change: 1 addition & 0 deletions conf/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ WriteTimeout: 60
[app]
Md5Key = 123456789
Encryptkey = 987123admin65412
Wxhookkey =
[database]
Db = mysql
DbHost = 192.168.2.81
Expand Down
Binary file added doc/wx-alert.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 17 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ go 1.20

require (
github.com/bwmarrin/snowflake v0.3.0
github.com/danbai225/go-logs v0.2.2
github.com/gin-gonic/gin v1.9.1
github.com/go-ini/ini v1.67.0
github.com/gorilla/websocket v1.5.0
github.com/jinzhu/gorm v1.9.16
github.com/ncruces/zenity v0.10.10
github.com/opentracing/opentracing-go v1.2.0
github.com/prometheus/client_golang v1.11.1
github.com/sirupsen/logrus v1.6.0
github.com/prometheus/client_golang v1.12.1
github.com/uber/jaeger-client-go v2.30.0+incompatible
go.etcd.io/etcd/client/v3 v3.5.9
golang.org/x/crypto v0.11.0
Expand All @@ -23,13 +24,15 @@ require (

require (
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
github.com/akavel/rsrc v0.10.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand All @@ -50,10 +53,12 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/josephspurrier/goversioninfo v1.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/kpango/fastime v1.1.9 // indirect
github.com/kpango/glg v1.6.15 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
Expand All @@ -65,27 +70,29 @@ require (
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/prometheus/common v0.34.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.etcd.io/etcd/api/v3 v3.5.9 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.9 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/image v0.10.0 // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/genproto v0.0.0-20210917145530-b395a37504d4 // indirect
google.golang.org/grpc v1.41.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
Loading

0 comments on commit a9905da

Please sign in to comment.