-
Notifications
You must be signed in to change notification settings - Fork 10
/
operations.go
157 lines (139 loc) · 3.72 KB
/
operations.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
package golarm
func setMetric(a *Alarm, v float64, m metric) {
a.value = value{value: v, percentage: false}
a.stats.metric = m
}
func setComparison(a *Alarm, v float64, c comparison) {
a.value = value{value: v, percentage: false}
a.comparison = c
}
func isMetricCorrect(a *Alarm, v float64, m metric) bool {
if a.Err == nil {
switch m {
case freeMetric:
if a.jobType == memoryAlarm || a.jobType == swapAlarm {
return true
}
case usedMetric:
if a.jobType == memoryAlarm || a.jobType == swapAlarm || a.jobType == procAlarm {
return true
}
case timeMetric:
if a.jobType == uptimeAlarm || a.jobType == procAlarm {
return true
}
case statusMetric:
if a.jobType != alertTypeNotDefined && a.jobType == procAlarm {
return true
}
}
a.Err = ErrIncorrectTypeForMetric
}
return false
}
func isComparisonCorrect(a *Alarm, v float64, c comparison) bool {
if a.Err == nil {
if a.comparison == comparisonNotDefined {
switch c {
case above, below, equal, belowEqual, aboveEqual:
if a.jobType != alertTypeNotDefined && a.stats.metric != statusMetric {
return true
}
a.Err = ErrIncorrectTypeForComparison
}
} else {
a.Err = ErrMultipleComparisonDefined
}
}
return false
}
// Free allows to specify that the created alarm will use the free memory as main metric
func (j *Alarm) Free() *Alarm {
if isMetricCorrect(j, notSet, freeMetric) {
setMetric(j, notSet, freeMetric)
}
return j
}
// Used allows to specify that the created alarm will use the used memory as main metric
func (j *Alarm) Used() *Alarm {
if isMetricCorrect(j, notSet, usedMetric) {
setMetric(j, notSet, usedMetric)
}
return j
}
// RunningTime gets the time a process has been running
func (j *Alarm) RunningTime() *Alarm {
if isMetricCorrect(j, notSet, timeMetric) {
setMetric(j, notSet, timeMetric)
}
return j
}
// Status allows to specify the state for a given process
func (j *Alarm) Status(s state) *Alarm {
if isMetricCorrect(j, notSet, statusMetric) {
setMetric(j, notSet, statusMetric)
(*j).stats.proc.state = s
}
return j
}
// Above compares if the specified alarm is greater than the number set
func (j *Alarm) Above(v float64) *Alarm {
if isComparisonCorrect(j, v, above) {
setComparison(j, v, above)
}
return j
}
// AboveEqual compares if the specified alarm is greater or equal than the number set
func (j *Alarm) AboveEqual(v float64) *Alarm {
if isComparisonCorrect(j, v, aboveEqual) {
setComparison(j, v, aboveEqual)
}
return j
}
// BelowEqual compares if the specified alarm is lower or equal than the number set
func (j *Alarm) BelowEqual(v float64) *Alarm {
if isComparisonCorrect(j, v, belowEqual) {
setComparison(j, v, belowEqual)
}
return j
}
// Equal compares if the specified alarm is equal than the number set
func (j *Alarm) Equal(v float64) *Alarm {
if isComparisonCorrect(j, v, equal) {
setComparison(j, v, equal)
}
return j
}
// Below compares if the specified alarm is lower than the number set
func (j *Alarm) Below(v float64) *Alarm {
if isComparisonCorrect(j, v, below) {
setComparison(j, v, below)
}
return j
}
func parsePercentage(percent float64) (float64, error) {
if percent > 100 || percent < 0 {
return 0.0, ErrIncorrectValuesWithPercentage
}
return percent, nil
}
// Percent allows using the value specified as a percentage
func (j *Alarm) Percent() *Alarm {
if j.Err == nil {
if j.value.value == notSet {
(*j).Err = ErrExpectedNumWhenPercentage
return j
}
if j.jobType == uptimeAlarm || j.stats.metric == statusMetric {
(*j).Err = ErrIncorrectTypeForPercentage
return j
}
val, err := parsePercentage(j.value.value)
if err != nil {
(*j).Err = err
} else {
(*j).value = value{value: val, percentage: true}
}
}
return j
}