/
libpodstats.go
65 lines (54 loc) · 1.5 KB
/
libpodstats.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
package main
import (
"context"
"fmt"
"time"
"collectd.org/api"
"collectd.org/plugin"
"github.com/infrawatch/collectd-libpod-stats/pkg/cgroups"
"github.com/infrawatch/collectd-libpod-stats/pkg/virt"
)
// LibpodStats gathers container resource usage stats from libpod cgroups
type LibpodStats struct {
handlers map[cgroups.ControlType]handler
}
//NewLibpodStats initialize new libpodstats plugins with handlers
//TODO: generate handlers from plugin config
func NewLibpodStats() *LibpodStats {
handlers := map[cgroups.ControlType]handler{}
handlers[cgroups.CPUAcctT] = &cpuHandler{}
handlers[cgroups.MemoryT] = &memoryHandler{}
return &LibpodStats{
handlers: handlers,
}
}
func (ls *LibpodStats) Read(ctx context.Context) error {
statMatrix, err := virt.ContainersStats(cgroups.CPUAcctT, cgroups.MemoryT)
if err != nil {
return err
}
for cLabel, metric := range statMatrix {
for controlType, stat := range metric {
vl := &api.ValueList{
Identifier: api.Identifier{
Plugin: "libpodstats",
PluginInstance: cLabel,
},
Time: time.Now(),
Interval: 10 * time.Second,
}
if _, found := ls.handlers[controlType]; !found {
return fmt.Errorf("unhandled cgroup type: %s", controlType.String())
}
ls.handlers[controlType].populateValueList(stat, vl)
if err := plugin.Write(ctx, vl); err != nil {
return fmt.Errorf("plugin.Write: %w", err)
}
}
}
return nil
}
func init() {
plugin.RegisterRead("libpodstats", NewLibpodStats())
}
func main() {}