-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg_alarm.go
96 lines (87 loc) · 2.39 KB
/
msg_alarm.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"text/template"
)
const (
ColorGood = "#2EB886"
ColorWarn = "#DAA038"
ColorDanger = "#A30100"
)
type EventCloudWatchAlarmMessage struct {
Color string
AlarmName string `json:"alarmName"`
State struct {
Value string `json:"value"`
Reason string `json:"reason"`
ReasonData string `json:"reasonData"`
Timestamp string `json:"timestamp"`
} `json:"state"`
PreviousState struct {
Value string `json:"value"`
Reason string `json:"reason"`
ReasonData string `json:"reasonData"`
Timestamp string `json:"timestamp"`
} `json:"previousState"`
Configuration struct {
Description string `json:"description"`
Metrics []struct {
ID string `json:"id"`
MetricStat struct {
Metric struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
Dimensions struct {
InstanceID string `json:"InstanceId"`
} `json:"dimensions"`
} `json:"metric"`
Period int `json:"period"`
Stat string `json:"stat"`
} `json:"metricStat,omitempty"`
ReturnData bool `json:"returnData"`
Expression string `json:"expression,omitempty"`
Label string `json:"label,omitempty"`
} `json:"metrics"`
} `json:"configuration"`
}
type EventCloudWatchAlarm struct {
Source string
DetailType string
Detail json.RawMessage
Parsed EventCloudWatchAlarmMessage
}
func NewEventCloudWatchAlarm(source string, detailType string, detail json.RawMessage) EventCloudWatchAlarm {
return EventCloudWatchAlarm{
Source: source,
DetailType: detailType,
Detail: detail,
}
}
func (m EventCloudWatchAlarm) readTemplate() (*template.Template, error) {
return readTemplate("alarm", "change.json")
}
func (m EventCloudWatchAlarm) generateMessage(tmpl *template.Template) (string, error) {
if err := json.Unmarshal(m.Detail, &m.Parsed); err != nil {
return "", fmt.Errorf("error unmarshalling event: %w", err)
}
m.Parsed.Color = m.getColor(m.Parsed.State.Value)
var buf bytes.Buffer
if err := tmpl.Execute(&buf, m.Parsed); err != nil {
return "", fmt.Errorf("error executing template, %w", err)
}
return buf.String(), nil
}
func (m EventCloudWatchAlarm) getColor(value string) string {
switch value {
case "ALARM":
return ColorDanger
case "OK":
return ColorGood
case "INSUFFICIENT_DATA":
return ColorWarn
default:
return ColorWarn
}
}