forked from monicasarbu/topbeat
/
topbeat.go
434 lines (345 loc) · 8.89 KB
/
topbeat.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package main
import (
"errors"
"math"
"regexp"
"strconv"
"time"
"github.com/elastic/gosigar"
"github.com/elastic/libbeat/beat"
"github.com/elastic/libbeat/cfgfile"
"github.com/elastic/libbeat/common"
"github.com/elastic/libbeat/logp"
"github.com/elastic/libbeat/publisher"
)
type ProcsMap map[int]*Process
type Topbeat struct {
period time.Duration
procs []string
procsMap ProcsMap
lastCpuTimes *CpuTimes
lastCpuTimesList []CpuTimes
TbConfig ConfigSettings
events publisher.Client
sysStats bool
procStats bool
fsStats bool
done chan struct{}
}
func (tb *Topbeat) Config(b *beat.Beat) error {
err := cfgfile.Read(&tb.TbConfig, "")
if err != nil {
logp.Err("Error reading configuration file: %v", err)
return err
}
if tb.TbConfig.Input.Period != nil {
tb.period = time.Duration(*tb.TbConfig.Input.Period) * time.Second
} else {
tb.period = 10 * time.Second
}
if tb.TbConfig.Input.Procs != nil {
tb.procs = *tb.TbConfig.Input.Procs
} else {
tb.procs = []string{".*"} //all processes
}
if tb.TbConfig.Input.Stats.System != nil {
tb.sysStats = *tb.TbConfig.Input.Stats.System
} else {
tb.sysStats = true
}
if tb.TbConfig.Input.Stats.Proc != nil {
tb.procStats = *tb.TbConfig.Input.Stats.Proc
} else {
tb.procStats = true
}
if tb.TbConfig.Input.Stats.Filesystem != nil {
tb.fsStats = *tb.TbConfig.Input.Stats.Filesystem
} else {
tb.fsStats = true
}
if !tb.sysStats && !tb.procStats && !tb.fsStats {
return errors.New("Invalid statistics configuration")
}
logp.Debug("topbeat", "Init toppbeat")
logp.Debug("topbeat", "Follow processes %q\n", tb.procs)
logp.Debug("topbeat", "Period %v\n", tb.period)
logp.Debug("topbeat", "System statistics %t\n", tb.sysStats)
logp.Debug("topbeat", "Process statistics %t\n", tb.procStats)
logp.Debug("topbeat", "File system statistics %t\n", tb.fsStats)
return nil
}
func (tb *Topbeat) Setup(b *beat.Beat) error {
tb.events = b.Events
tb.done = make(chan struct{})
return nil
}
func (t *Topbeat) Run(b *beat.Beat) error {
var err error
t.initProcStats()
ticker := time.NewTicker(t.period)
defer ticker.Stop()
for {
select {
case <-t.done:
return nil
case <-ticker.C:
}
timerStart := time.Now()
if t.sysStats {
err = t.exportSystemStats()
if err != nil {
logp.Err("Error reading system stats: %v", err)
break
}
}
if t.procStats {
err = t.exportProcStats()
if err != nil {
logp.Err("Error reading proc stats: %v", err)
break
}
}
if t.fsStats {
err = t.exportFileSystemStats()
if err != nil {
logp.Err("Error reading fs stats: %v", err)
break
}
}
timerEnd := time.Now()
duration := timerEnd.Sub(timerStart)
if duration.Nanoseconds() > t.period.Nanoseconds() {
logp.Warn("Ignoring tick(s) due to processing taking longer than one period")
}
}
return err
}
func (tb *Topbeat) Cleanup(b *beat.Beat) error {
return nil
}
func (t *Topbeat) Stop() {
close(t.done)
}
func (t *Topbeat) initProcStats() {
t.procsMap = make(ProcsMap)
if len(t.procs) == 0 {
return
}
pids, err := Pids()
if err != nil {
logp.Warn("Getting the list of pids: %v", err)
}
for _, pid := range pids {
process, err := GetProcess(pid)
if err != nil {
logp.Debug("topbeat", "Skip process %d: %v", pid, err)
continue
}
t.procsMap[process.Pid] = process
}
}
func (t *Topbeat) exportProcStats() error {
if len(t.procs) == 0 {
return nil
}
pids, err := Pids()
if err != nil {
logp.Warn("Getting the list of pids: %v", err)
return err
}
newProcs := make(ProcsMap, len(pids))
for _, pid := range pids {
process, err := GetProcess(pid)
if err != nil {
logp.Debug("topbeat", "Skip process %d: %v", pid, err)
continue
}
if t.MatchProcess(process.Name) {
t.addProcCpuPercentage(process)
t.addProcMemPercentage(process, 0 /*read total mem usage */)
newProcs[process.Pid] = process
event := common.MapStr{
"@timestamp": common.Time(time.Now()),
"type": "proc",
"proc": common.MapStr{
"pid": process.Pid,
"ppid": process.Ppid,
"name": process.Name,
"state": process.State,
"mem": process.Mem,
"cpu": process.Cpu,
},
}
t.events.PublishEvent(event)
}
}
t.procsMap = newProcs
return nil
}
func (t *Topbeat) exportSystemStats() error {
load_stat, err := GetSystemLoad()
if err != nil {
logp.Warn("Getting load statistics: %v", err)
return err
}
cpu_stat, err := GetCpuTimes()
if err != nil {
logp.Warn("Getting cpu times: %v", err)
return err
}
t.addCpuPercentage(cpu_stat)
cpu_core_stat, err := GetCpuTimesList()
if err != nil {
logp.Warn("Getting cpu core times: %v", err)
return err
}
t.addCpuPercentageList(cpu_core_stat)
mem_stat, err := GetMemory()
if err != nil {
logp.Warn("Getting memory details: %v", err)
return err
}
t.addMemPercentage(mem_stat)
swap_stat, err := GetSwap()
if err != nil {
logp.Warn("Getting swap details: %v", err)
return err
}
t.addMemPercentage(swap_stat)
event := common.MapStr{
"@timestamp": common.Time(time.Now()),
"type": "system",
"load": load_stat,
"cpu": cpu_stat,
"mem": mem_stat,
"swap": swap_stat,
}
for coreNumber, core := range cpu_core_stat {
event["cpu"+strconv.Itoa(coreNumber)] = core
}
t.events.PublishEvent(event)
return nil
}
func (t *Topbeat) exportFileSystemStats() error {
fss, err := GetFileSystemList()
if err != nil {
logp.Warn("Getting filesystem list: %v", err)
return err
}
t.events.PublishEvents(collectFileSystemStats(fss))
return nil
}
func collectFileSystemStats(fss []sigar.FileSystem) []common.MapStr {
events := make([]common.MapStr, 0, len(fss))
for _, fs := range fss {
fsStat, err := GetFileSystemStat(fs)
if err != nil {
logp.Debug("topbeat", "Skip filesystem %d: %v", fsStat, err)
continue
}
addFileSystemUsedPercentage(fsStat)
event := common.MapStr{
"@timestamp": common.Time(time.Now()),
"type": "filesystem",
"fs": fsStat,
}
events = append(events, event)
}
return events
}
func (t *Topbeat) MatchProcess(name string) bool {
for _, reg := range t.procs {
matched, _ := regexp.MatchString(reg, name)
if matched {
return true
}
}
return false
}
func (t *Topbeat) addMemPercentage(m *MemStat) {
if m.Total == 0 {
return
}
perc := float64(m.Used) / float64(m.Total)
m.UsedPercent = Round(perc, .5, 2)
actual_perc := float64(m.ActualUsed) / float64(m.Total)
m.ActualUsedPercent = Round(actual_perc, .5, 2)
}
func addFileSystemUsedPercentage(f *FileSystemStat) {
if f.Total == 0 {
return
}
perc := float64(f.Used) / float64(f.Total)
f.UsedPercent = Round(perc, .5, 2)
}
func (t *Topbeat) addCpuPercentage(t2 *CpuTimes) {
t1 := t.lastCpuTimes
if t1 != nil && t2 != nil {
all_delta := t2.sum() - t1.sum()
calculate := func(field2 uint64, field1 uint64) float64 {
perc := 0.0
delta := field2 - field1
perc = float64(delta) / float64(all_delta)
return Round(perc, .5, 2)
}
t2.UserPercent = calculate(t2.User, t1.User)
t2.SystemPercent = calculate(t2.System, t1.System)
}
t.lastCpuTimes = t2
}
func (t *Topbeat) addCpuPercentageList(t2 []CpuTimes) {
t1 := t.lastCpuTimesList
if t1 != nil && t2 != nil && len(t1) == len(t2) {
calculate := func(field2 uint64, field1 uint64, all_delta uint64) float64 {
perc := 0.0
delta := field2 - field1
perc = float64(delta) / float64(all_delta)
return Round(perc, .5, 2)
}
for i := 0; i < len(t1); i++ {
all_delta := t2[i].sum() - t1[i].sum()
t2[i].UserPercent = calculate(t2[i].User, t1[i].User, all_delta)
t2[i].SystemPercent = calculate(t2[i].System, t1[i].System, all_delta)
}
}
t.lastCpuTimesList = t2
}
func (t *Topbeat) addProcMemPercentage(proc *Process, total_phymem uint64) {
// in unit tests, total_phymem is set to a value greater than zero
if total_phymem == 0 {
mem_stat, err := GetMemory()
if err != nil {
logp.Warn("Getting memory details: %v", err)
return
}
total_phymem = mem_stat.Total
}
perc := (float64(proc.Mem.Rss) / float64(total_phymem))
proc.Mem.RssPercent = Round(perc, .5, 2)
}
func (t *Topbeat) addProcCpuPercentage(proc *Process) {
oproc, ok := t.procsMap[proc.Pid]
if ok {
delta_proc := (proc.Cpu.User - oproc.Cpu.User) + (proc.Cpu.System - oproc.Cpu.System)
delta_time := proc.ctime.Sub(oproc.ctime).Nanoseconds() / 1e6 // in milliseconds
perc := float64(delta_proc) / float64(delta_time)
t.procsMap[proc.Pid] = proc
proc.Cpu.UserPercent = Round(perc, .5, 2)
}
}
func Round(val float64, roundOn float64, places int) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return
}
func (t *CpuTimes) sum() uint64 {
return t.User + t.Nice + t.System + t.Idle + t.IOWait + t.Irq + t.SoftIrq + t.Steal
}