-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat.go
172 lines (163 loc) · 3.68 KB
/
chat.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
// Package notify : 通知処理
package notify
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
"github.com/twsnmp/twsnmpfc/datastore"
)
type discordEnt struct {
Title string `json:"title"`
Color string `json:"color"`
URL string `json:"url"`
Description string `json:"description"`
}
type discordMsg struct {
Embeds []discordEnt `json:"embeds"`
}
func SendChat(c *datastore.NotifyConfEnt, title, level, message string) error {
if !strings.Contains(c.ChatType, "discord") || c.ChatWebhookURL == "" {
return fmt.Errorf("invalid chat params")
}
color := "10070709"
switch level {
case "high":
color = "15548997"
case "low":
color = "15418782"
case "warn":
color = "16705372"
case "normal":
color = "5763719"
case "repair", "info":
color = "5793266"
}
m := discordMsg{
Embeds: []discordEnt{{
Title: title,
Color: color,
Description: message,
URL: c.URL,
}},
}
j, err := json.Marshal(m)
if err != nil {
return err
}
req, err := http.NewRequest(
"POST",
c.ChatWebhookURL,
strings.NewReader(string(j)),
)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
r, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
time.Sleep(time.Second * 3)
if string(r) != "" {
return fmt.Errorf("%s", r)
}
return nil
}
// sendNotifyDiscord : Discordへ通知メッセージを送信する
func sendNotifyDiscord(l *datastore.EventLogEnt) {
if datastore.NotifyConf.ChatWebhookURL == "" {
return
}
nl := getLevelNum(datastore.NotifyConf.Level)
if nl == 3 {
return
}
if l.Level == "repair" {
if !datastore.NotifyConf.NotifyRepair {
return
}
a := strings.Split(l.Event, ":")
if len(a) < 2 {
return
}
// 復帰前の状態を確認する
np := getLevelNum(a[len(a)-1])
if np > nl {
return
}
// 復帰を通知する
title, message := getChatMessage(l, true)
if err := SendChat(&datastore.NotifyConf, title, "repair", message); err != nil {
log.Printf("send discord error=%v", err)
datastore.AddEventLog(&datastore.EventLogEnt{
Type: "system",
Level: "warn",
NodeID: l.NodeID,
NodeName: l.NodeName,
Event: "復帰通知のチャットメッセージを送信できません",
})
return
}
datastore.AddEventLog(&datastore.EventLogEnt{
Type: "system",
Level: "info",
NodeID: l.NodeID,
NodeName: l.NodeName,
Event: "復帰通知のチャットメッセージを送信しました",
})
return
}
np := getLevelNum(l.Level)
if np > nl {
return
}
title, message := getChatMessage(l, false)
if err := SendChat(&datastore.NotifyConf, title, l.Level, message); err != nil {
log.Printf("send discord error=%v", err)
datastore.AddEventLog(&datastore.EventLogEnt{
Type: "system",
Level: "warn",
NodeID: l.NodeID,
NodeName: l.NodeName,
Event: "障害通知のチャットメッセージを送信できません",
})
return
}
datastore.AddEventLog(&datastore.EventLogEnt{
Type: "system",
Level: "info",
NodeID: l.NodeID,
NodeName: l.NodeName,
Event: "障害通知のチャットメッセージを送信しました",
})
}
func getChatMessage(l *datastore.EventLogEnt, repair bool) (string, string) {
subtitle := "障害"
if repair {
subtitle = "復帰"
}
return fmt.Sprintf("%s(%s)", datastore.NotifyConf.Subject, subtitle),
fmt.Sprintf(
`発生日時: %s
状態: %s
タイプ: %s
関連ノード: %s
イベント: %s
`,
formatLogTime(l.Time),
levelName(l.Level),
l.Type,
l.NodeName,
l.Event,
)
}