forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_summary.go
97 lines (85 loc) · 2.47 KB
/
process_summary.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
// +build darwin freebsd linux windows
package process_summary
import (
"github.com/pkg/errors"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/metric/system/process"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
sigar "github.com/elastic/gosigar"
)
// init registers the MetricSet with the central registry.
// The New method will be called after the setup of the module and before starting to fetch data
func init() {
if err := mb.Registry.AddMetricSet("system", "process_summary", New, parse.EmptyHostParser); err != nil {
panic(err)
}
}
// MetricSet type defines all fields of the MetricSet
// As a minimum it must inherit the mb.BaseMetricSet fields, but can be extended with
// additional entries. These variables can be used to persist data or configuration between
// multiple fetch calls.
type MetricSet struct {
mb.BaseMetricSet
}
// New create a new instance of the MetricSet
// Part of new is also setting up the configuration by processing additional
// configuration entries if needed.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return &MetricSet{
BaseMetricSet: base,
}, nil
}
// Fetch methods implements the data gathering and data conversion to the right format
// It returns the event which is then forward to the output. In case of an error, a
// descriptive error must be returned.
func (m *MetricSet) Fetch() (common.MapStr, error) {
pids, err := process.Pids()
if err != nil {
return nil, errors.Wrap(err, "failed to fetch the list of PIDs")
}
var summary struct {
sleeping int
running int
idle int
stopped int
zombie int
unknown int
}
for _, pid := range pids {
state := sigar.ProcState{}
err = state.Get(pid)
if err != nil {
summary.unknown += 1
continue
}
switch byte(state.State) {
case 'S':
summary.sleeping++
case 'R':
summary.running++
case 'D':
summary.idle++
case 'T':
summary.stopped++
case 'Z':
summary.zombie++
default:
logp.Err("Unknown state <%v> for process with pid %d", state.State, pid)
summary.unknown++
}
}
event := common.MapStr{
"total": len(pids),
"sleeping": summary.sleeping,
"running": summary.running,
"idle": summary.idle,
"stopped": summary.stopped,
"zombie": summary.zombie,
"unknown": summary.unknown,
}
// change the name space to use . instead of _
event[mb.NamespaceKey] = "process.summary"
return event, nil
}