-
Notifications
You must be signed in to change notification settings - Fork 115
/
monit_adapter.go
163 lines (147 loc) · 5.73 KB
/
monit_adapter.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
package alert
import (
"fmt"
"sort"
"strings"
"time"
"code.cloudfoundry.org/clock"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
)
type MonitAdapter interface {
IsIgnorable() bool
Alert() (Alert, error)
Severity() (severity SeverityLevel, found bool)
}
type monitAdapter struct {
monitAlert MonitAlert
settingsService boshsettings.Service
timeService clock.Clock
}
func NewMonitAdapter(monitAlert MonitAlert, settingsService boshsettings.Service, timeService clock.Clock) MonitAdapter {
return &monitAdapter{
monitAlert: monitAlert,
settingsService: settingsService,
timeService: timeService,
}
}
func (m *monitAdapter) IsIgnorable() bool {
severity, _ := m.Severity()
return severity == SeverityIgnored
}
func (m *monitAdapter) Alert() (Alert, error) {
severity, _ := m.Severity()
return Alert{
ID: m.monitAlert.ID,
Severity: severity,
Title: m.title(),
Summary: m.monitAlert.Description,
CreatedAt: m.createdAt(),
}, nil
}
func (m *monitAdapter) title() string {
settings := m.settingsService.GetSettings()
ips := settings.Networks.IPs()
sort.Strings(ips)
service := m.monitAlert.Service
if len(ips) > 0 {
service = fmt.Sprintf("%s (%s)", service, strings.Join(ips, ", "))
}
return fmt.Sprintf("%s - %s - %s", service, m.monitAlert.Event, m.monitAlert.Action)
}
func (m *monitAdapter) createdAt() int64 {
createdAt, timeParseErr := time.Parse(time.RFC1123Z, m.monitAlert.Date)
if timeParseErr != nil {
createdAt = m.timeService.Now()
}
return createdAt.Unix()
}
func (m *monitAdapter) Severity() (severity SeverityLevel, found bool) {
severity, found = eventToSeverity[strings.ToLower(m.monitAlert.Event)]
if !found {
severity = SeverityDefault
}
return severity, found
}
var eventToSeverity = map[string]SeverityLevel{
"action done": SeverityIgnored,
"checksum failed": SeverityCritical,
"checksum changed": SeverityWarning,
"checksum succeeded": SeverityIgnored,
"checksum not changed": SeverityIgnored,
"connection failed": SeverityAlert,
"connection succeeded": SeverityIgnored,
"connection changed": SeverityError,
"connection not changed": SeverityIgnored,
"content failed": SeverityError,
"content succeeded": SeverityIgnored,
"content match": SeverityIgnored,
"content doesn't match": SeverityError,
"data access error": SeverityError,
"data access succeeded": SeverityIgnored,
"data access changed": SeverityWarning,
"data access not changed": SeverityIgnored,
"execution failed": SeverityAlert,
"execution succeeded": SeverityIgnored,
"execution changed": SeverityWarning,
"execution not changed": SeverityIgnored,
"filesystem flags failed": SeverityError,
"filesystem flags succeeded": SeverityIgnored,
"filesystem flags changed": SeverityWarning,
"filesystem flags not changed": SeverityIgnored,
"gid failed": SeverityError,
"gid succeeded": SeverityIgnored,
"gid changed": SeverityWarning,
"gid not changed": SeverityIgnored,
"heartbeat failed": SeverityError,
"heartbeat succeeded": SeverityIgnored,
"heartbeat changed": SeverityWarning,
"heartbeat not changed": SeverityIgnored,
"icmp failed": SeverityCritical,
"icmp succeeded": SeverityIgnored,
"icmp changed": SeverityWarning,
"icmp not changed": SeverityIgnored,
"monit instance failed": SeverityAlert,
"monit instance succeeded": SeverityIgnored,
"monit instance changed": SeverityIgnored,
"monit instance not changed": SeverityIgnored,
"invalid type": SeverityError,
"type succeeded": SeverityIgnored,
"type changed": SeverityWarning,
"type not changed": SeverityIgnored,
"does not exist": SeverityAlert,
"exists": SeverityIgnored,
"existence changed": SeverityWarning,
"existence not changed": SeverityIgnored,
"permission failed": SeverityError,
"permission succeeded": SeverityIgnored,
"permission changed": SeverityWarning,
"permission not changed": SeverityIgnored,
"pid failed": SeverityCritical,
"pid succeeded": SeverityIgnored,
"pid changed": SeverityWarning,
"pid not changed": SeverityIgnored,
"ppid failed": SeverityCritical,
"ppid succeeded": SeverityIgnored,
"ppid changed": SeverityWarning,
"ppid not changed": SeverityIgnored,
"resource limit matched": SeverityError,
"resource limit succeeded": SeverityIgnored,
"resource limit changed": SeverityWarning,
"resource limit not changed": SeverityIgnored,
"size failed": SeverityError,
"size succeeded": SeverityIgnored,
"size changed": SeverityError,
"size not changed": SeverityIgnored,
"timeout": SeverityCritical,
"timeout recovery": SeverityIgnored,
"timeout changed": SeverityWarning,
"timeout not changed": SeverityIgnored,
"timestamp failed": SeverityError,
"timestamp succeeded": SeverityIgnored,
"timestamp changed": SeverityWarning,
"timestamp not changed": SeverityIgnored,
"uid failed": SeverityCritical,
"uid succeeded": SeverityIgnored,
"uid changed": SeverityWarning,
"uid not changed": SeverityIgnored,
}