-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmess.go
179 lines (141 loc) · 4.34 KB
/
mess.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"encoding/json"
"fmt"
"github.com/valyala/fasthttp"
)
// 消息结构体
type Message struct {
Receiver string `json:"receiver"`
Status string `json:"firing"`
Alerts []Alerts `json:"alerts"`
GroupLabels GroupLabels `json:"groupLabels"`
CommonLabels CommonLabels `json:"commonLabels"`
CommonAnnotations string `json:"commonAnnotations"`
ExternalURL string `json:"externalURL"`
Version string `json:"version"`
GroupKey string `json:"groupKey"`
}
/**
* 消息结构体Alerts
*/
type Alerts struct {
Status string `json:"status"`
Labels Labels `json:"labels"`
Annotations Annotations `json:"annotations"`
StartsAt string `json:"startsAt"`
EndsAt string `json:"endsAt"`
GeneratorURL string `json:"generatorURL"`
}
/**
* 消息结构体Alerts.labels
*/
type Labels struct {
Alertname string `json:"alertname"`
Instance string `json:"instance"`
Job string `json:"job"`
Role string `json:"role"`
Service string `json:"service"`
}
/**
* 消息结构体Alerts.annotations
*/
type Annotations struct {
Description string `json:"Description"`
Summary string `json:"Summary"`
}
/**
* 消息结构体Alerts.GroupLabels
*/
type GroupLabels struct {
Alertname string `json:"alertname"`
}
/**
* 消息结构体Alerts.CommonLabels
*/
type CommonLabels struct {
Alertname string `json:"alertname"`
Job string `json:"job"`
Role string `json:"role"`
Service string `json:"service"`
}
/**
* ResponseMessage返回的消息
*/
type ResponseMessage struct {
Status string `json:"status"`
Alertname string `json:"alertname"`
Host string `json:"host"`
Role string `json:"role"`
Description string `json:"description"`
Summary string `json:"summary"`
StartsAt string `json:"startsAt"`
EndsAt string `json:"endsAt"`
}
/**
* 发送给telegram的消息格式
*/
type Telegram struct {
Chat_id string `json:"chat_id"`
Text string `json:"text"`
Disable_notification bool `json:"disable_notification"`
}
func SendMessage(response []ResponseMessage, url string, chat string) {
for _, v := range response {
//var responseStr string
var (
message Telegram
)
message = Telegram{
Chat_id: chat,
Text: message.ParuseUri(v),
Disable_notification: true,
}
data, _ := json.Marshal(message)
message.ReqPost(url, data)
}
}
func (this *Telegram) ParuseUri(req ResponseMessage) (UriText string) {
switch req.Status {
case "firing":
UriText = fmt.Sprintf("告警类型: %s\n事件类型: %s\n告警主机: %s\n主机角色: %s\n告警摘要: %s\n告警描述: %s\n故障开始时间: %s\n", req.Status, req.Alertname, req.Host, req.Role, req.Summary, req.Description, req.StartsAt)
case "resolved":
UriText = fmt.Sprintf("故障恢复\n\n告警类型: %s\n事件类型: %s\n告警主机: %s\n主机角色: %s\n告警摘要: %s\n告警描述: %s\n故障恢复时间: %s\n", req.Status, req.Alertname, req.Host, req.Role, req.Summary, req.Description, req.EndsAt)
}
//UriText = fmt.Sprintf("告警类型: %s\n事件类型: %s\n告警主机: %s\n主机角色: %s\n告警摘要: %s\n告警描述: %s\n故障开始时间: %s\nEndsAt: %s\n", req.Status, req.Alertname, req.Host, req.Role, req.Summary, req.Description, req.StartsAt, req.EndsAt)
return
}
func (this *Telegram) ReqPost(url string, data []byte) {
req := &fasthttp.Request{}
req.SetRequestURI(url)
req.SetBody(data)
req.Header.SetContentType("application/json")
req.Header.SetMethod("POST")
resp := &fasthttp.Response{}
client := &fasthttp.Client{}
if err := client.Do(req, resp); err != nil {
fmt.Println("请求失败:", err.Error())
return
}
b := resp.Body()
fmt.Println("result:\r\n", string(b))
}
func (this *Message) FormatBody(msg Message) (response []ResponseMessage) {
for _, v := range msg.Alerts {
var (
mess ResponseMessage
)
mess = ResponseMessage{
Status: v.Status,
Alertname: v.Labels.Alertname,
Host: v.Labels.Instance,
Role: v.Labels.Role,
Description: v.Annotations.Description,
Summary: v.Annotations.Summary,
StartsAt: v.StartsAt,
EndsAt: v.EndsAt,
}
response = append(response, mess)
}
return
}