-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgolarm.go
358 lines (324 loc) · 8.27 KB
/
golarm.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package golarm
import (
"errors"
"syscall"
"github.com/carlescere/scheduler"
"github.com/cloudfoundry/gosigar"
)
// check interval set to 5 seconds as default
var Duration = 5
// alarms pool
var Alarms = make([]*Alarm, 0)
var notSet = 123456.123456
// Error codes returned by failures when trying to create the alert chain
var (
ErrAlarmTypeNotDefined = errors.New("Bad chain. Alarm type not defined")
ErrComparisonNotDefined = errors.New("Bad chain. Alarm comparison not defined")
ErrExpectedNumWhenPercentage = errors.New("Bad chain. A number is needed for applying a percentage")
ErrIncorrectTypeForFree = errors.New("Alarm type not set or trying to use Free with something different than memory or swap memory")
ErrIncorrectTypeForUsed = errors.New("Alarm type not set or trying to use Free with something different than memory, swap memory or memory used by a proc")
ErrIncorrectTypeForTime = errors.New("Alarm type not set or trying to use RunningTime with something different than uptime or uptime by a proc")
ErrIncorrectTypeForStatus = errors.New("Alarm type not set or trying to use Status with something different than SysteProcAlarm")
ErrMultipleComparisonDefined = errors.New("Alarm comparison already defined")
ErrIncorrectTypeForAbove = errors.New("Alarm type not set or trying to use Above with a status metric")
ErrIncorrectTypeForBelow = errors.New("Alarm type not set or trying to use Below with a status metric")
ErrIncorrectTypeForPercentage = errors.New("Couldn't apply percentage to uptime/status Alarms")
ErrIncorrectValuesWithPercentage = errors.New("Couldn't apply percentage")
ErrInexistentPid = errors.New("Pid does not exist")
ErrIncorrectTypeForComparison = errors.New("Alarm type not set or trying to use an incorrect comparison with this type of Alarm")
ErrIncorrectTypeForMetric = errors.New("Alarm type not set or trying to use an incorrect metric with this type of Alarm")
)
type alarmType int
type comparison int
type value struct {
value float64
percentage bool
}
// Alarm defines a running alarm
type Alarm struct {
metricsManager sigarMetrics
quit chan bool
result chan bool
Err error
task func()
jobType alarmType
comparison comparison
value value
stats stats
}
const (
comparisonNotDefined comparison = iota
above
aboveEqual
equal
below
belowEqual
)
const (
alertTypeNotDefined alarmType = iota
loadAlarm
memoryAlarm
swapAlarm
uptimeAlarm
procAlarm
)
type sigarMetrics interface {
sigar.Sigar
getProcState(int) (sigar.ProcState, error)
getProcMem(int) (sigar.ProcMem, error)
getProcTime(int) (sigar.ProcTime, error)
getUpTime() (sigar.Uptime, error)
}
type concreteSigar struct {
sigar.ConcreteSigar
}
func (c *concreteSigar) getUpTime() (sigar.Uptime, error) {
p := sigar.Uptime{}
err := p.Get()
return p, err
}
func (c *concreteSigar) getProcState(pid int) (sigar.ProcState, error) {
p := sigar.ProcState{}
err := p.Get(pid)
return p, err
}
func (c *concreteSigar) getProcMem(pid int) (sigar.ProcMem, error) {
p := sigar.ProcMem{}
err := p.Get(pid)
return p, err
}
func (c *concreteSigar) getProcTime(pid int) (sigar.ProcTime, error) {
p := sigar.ProcTime{}
err := p.Get(pid)
return p, err
}
// AddAlarm adds an alarm to the pool and starts it immediately
func AddAlarm(a *Alarm) error {
if a.Err == nil {
go func(b *Alarm) {
scheduler.Every(Duration).Seconds().NotImmediately().Run(func() {
check(b)
})
}(a)
go func(b *Alarm) {
for {
select {
case fired := <-b.result:
if fired {
b.execute()
}
}
}
}(a)
Alarms = append(Alarms, a)
return nil
}
return a.Err
}
func compare(value1, value2 float64, c comparison) bool {
switch c {
case above:
return value1 > value2
case aboveEqual:
return value1 >= value2
case equal:
return value1 == value2
case below:
return value1 < value2
case belowEqual:
return value1 <= value2
}
return false
}
// SetMetricsManager allows to set a specific sigar manager
func (j *Alarm) SetMetricsManager(m sigarMetrics) {
(*j).metricsManager = m
}
func pidExists(pid int) bool {
killErr := syscall.Kill(pid, syscall.Signal(0))
return killErr == nil
}
func check(Alarm *Alarm) {
if Alarm.Err == nil {
switch Alarm.jobType {
case loadAlarm:
Alarm.result <- compare(
getLoadAverage(
Alarm.stats.period,
Alarm.metricsManager,
Alarm.value.percentage),
Alarm.value.value,
Alarm.comparison)
case uptimeAlarm:
Alarm.result <- compare(
getUptime(
Alarm.metricsManager),
Alarm.value.value,
Alarm.comparison)
case procAlarm:
switch Alarm.stats.metric {
case usedMetric:
Alarm.result <- compare(
getPidMemory(Alarm.stats.proc.pid,
Alarm.metricsManager,
Alarm.value.percentage),
Alarm.value.value,
Alarm.comparison)
case timeMetric:
Alarm.result <- compare(
getPidTime(Alarm.stats.proc.pid,
Alarm.metricsManager),
Alarm.value.value,
Alarm.comparison)
case statusMetric:
Alarm.result <- compare(
float64(getPidState(Alarm.stats.proc.pid,
Alarm.metricsManager)),
float64(Alarm.stats.proc.state),
equal)
}
case memoryAlarm:
switch Alarm.stats.metric {
case freeMetric:
Alarm.result <- compare(
float64(getActualFreeMemory(
Alarm.metricsManager,
Alarm.value.percentage,
)),
Alarm.value.value,
Alarm.comparison)
case usedMetric:
Alarm.result <- compare(
float64(getActualUsedMemory(
Alarm.metricsManager,
Alarm.value.percentage,
)),
Alarm.value.value,
Alarm.comparison)
}
case swapAlarm:
switch Alarm.stats.metric {
case freeMetric:
Alarm.result <- compare(
float64(getActualFreeSwap(
Alarm.metricsManager,
Alarm.value.percentage,
)),
Alarm.value.value,
Alarm.comparison)
case usedMetric:
Alarm.result <- compare(
float64(getActualUsedSwap(
Alarm.metricsManager,
Alarm.value.percentage,
)),
Alarm.value.value,
Alarm.comparison)
}
}
}
}
// SystemLoad creates an alarm based on load metric
// P is the period needed for calculating the load, and it could be OneMinPeriod, FiveMinPeriod or FifteenMinPeriod
func SystemLoad(p period) *Alarm {
a := &Alarm{
jobType: loadAlarm,
value: value{
value: notSet,
percentage: false},
result: make(chan bool),
stats: stats{
period: p,
metric: 0,
},
}
a.SetMetricsManager(&concreteSigar{})
return a
}
// SystemProc creates an alarm based on a process specified by PID
func SystemProc(pid uint) *Alarm {
a := &Alarm{
jobType: procAlarm,
value: value{
value: notSet,
percentage: false},
result: make(chan bool),
stats: stats{
metric: 0,
period: 0,
proc: proc{
pid: pid,
state: Unknown,
}},
}
if !pidExists(int(pid)) {
a.Err = ErrInexistentPid
}
a.SetMetricsManager(&concreteSigar{})
return a
}
// SystemMemory creates an alarm based on memory metrics
func SystemMemory() *Alarm {
a := &Alarm{
jobType: memoryAlarm,
value: value{
value: notSet,
percentage: false},
result: make(chan bool),
stats: stats{
metric: 0,
period: 0,
},
}
a.SetMetricsManager(&concreteSigar{})
return a
}
// SystemSwap creates an alarm based on swap memory metrics
func SystemSwap() *Alarm {
a := &Alarm{
jobType: swapAlarm,
value: value{
value: notSet,
percentage: false},
result: make(chan bool),
stats: stats{
metric: 0,
period: 0,
},
}
a.SetMetricsManager(&concreteSigar{})
return a
}
// SystemUptime creates an alarm based on system uptime
func SystemUptime() *Alarm {
a := &Alarm{
jobType: uptimeAlarm,
value: value{
value: notSet,
percentage: false},
result: make(chan bool),
stats: stats{
metric: 0,
period: 0,
},
}
a.SetMetricsManager(&concreteSigar{})
return a
}
func (j *Alarm) execute() {
if j.Err == nil {
j.task()
}
}
// Run allows a func to be specified.
// This callback will be executed when the alarm is fired
func (j *Alarm) Run(f func()) *Alarm {
if j.Err == nil {
if (j.comparison == comparisonNotDefined) && j.stats.metric != statusMetric {
(*j).Err = ErrComparisonNotDefined
return j
}
(*j).task = f
}
return j
}