-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmetrics.go
202 lines (163 loc) · 3.69 KB
/
metrics.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
package golarm
type period int
type metric int
type state int
type procType int
var (
states = map[string]float64{
"S": 1.0,
"R": 2.0,
"T": 3.0,
"Z": 4.0,
"D": 5.0,
}
)
const (
freeMetric metric = iota + 1
usedMetric
timeMetric
statusMetric
)
// Linux process states to be used with status alarms
const (
Sleeping state = iota + 1
Running
Stopped
Zombie
Idle
Unknown
)
type load struct {
period period
}
type proc struct {
state state
pid uint
}
type stats struct {
period period
proc proc
metric metric
}
// Load average can be calculated for the last one minute, five minutes and fifteen minutes respectively. Load average is an indication of whether the system resources (mainly the CPU) are adequately available for the processes (system load) that are running, runnable or in uninterruptible sleep states during the previous n minutes.
const (
OneMinPeriod period = iota + 1
FiveMinPeriod
FifteenMinPeriod
)
func getLoadAverage(p period, manager sigarMetrics, percentage bool) float64 {
average, err := manager.GetLoadAverage()
value := 0.0
if err != nil {
return value
}
switch p {
case OneMinPeriod:
value = float64(average.One)
case FiveMinPeriod:
value = float64(average.Five)
case FifteenMinPeriod:
value = float64(average.Fifteen)
}
if percentage {
value *= 10
}
return value
}
func getPidState(pid uint, manager sigarMetrics) float64 {
value, err := manager.getProcState(int(pid))
if err != nil {
return 6.0
}
return states[string(value.State)]
}
func getPidMemory(pid uint, manager sigarMetrics, percentage bool) float64 {
memory, err := manager.getProcMem(int(pid))
if err != nil {
return 0.0
}
value := float64(memory.Resident / 1048576)
if percentage {
return 100.0 * (value / (getTotalMemory(manager) / 1048576))
}
return value
}
// get running time for PID in minutes
func getPidTime(pid uint, manager sigarMetrics) float64 {
value, err := manager.getProcTime(int(pid))
if err != nil {
return 0.0
}
return float64(value.Total / 1000)
}
func getTotalMemory(manager sigarMetrics) float64 {
mem, err := manager.GetMem()
if err != nil {
return 0.0
}
return float64(mem.Total)
}
func getTotalSwap(manager sigarMetrics) float64 {
swap, err := manager.GetSwap()
if err != nil {
return 0.0
}
return float64(swap.Total)
}
func getUsedSwap(manager sigarMetrics) float64 {
swap, err := manager.GetSwap()
if err != nil {
return 0.0
}
return float64(swap.Used)
}
func getFreeSwap(manager sigarMetrics) float64 {
swap, err := manager.GetSwap()
if err != nil {
return 0.0
}
return float64(swap.Free)
}
func getActualUsedMemory(manager sigarMetrics, percentage bool) float64 {
mem, err := manager.GetMem()
if err != nil {
return 0.0
}
value := float64(mem.ActualUsed) / 1048576
if percentage {
return 100.0 * (value / (getTotalMemory(manager) / 1048576))
}
return value
}
func getActualFreeMemory(manager sigarMetrics, percentage bool) float64 {
mem, err := manager.GetMem()
if err != nil {
return 0.0
}
value := float64(mem.ActualFree) / 1048576
if percentage {
return 100.0 * (value / (getTotalMemory(manager) / 1048576))
}
return value
}
func getActualFreeSwap(manager sigarMetrics, percentage bool) float64 {
value := float64(getFreeSwap(manager)) / 1048576
if percentage {
return 100.0 * (value / getTotalSwap(manager) / 1048576)
}
return value
}
func getActualUsedSwap(manager sigarMetrics, percentage bool) float64 {
value := float64(getUsedSwap(manager)) / 1048576
if percentage {
return 100.0 * (value / (getTotalSwap(manager) / 1048576))
}
return value
}
func getUptime(manager sigarMetrics) float64 {
value, err := manager.getUpTime()
if err != nil {
return 0.0
}
return value.Length
}