From 14bc29f76614782c23cd2799f72d89d99e2419e8 Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Wed, 12 May 2021 17:47:12 +0200 Subject: [PATCH] Add collector for the top5 processes in %cpu --- collectors/topprocsMetric.go | 39 ++++++++++++++++++++++++++++++++++++ config.json | 3 ++- metric-collector.go | 1 + 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 collectors/topprocsMetric.go diff --git a/collectors/topprocsMetric.go b/collectors/topprocsMetric.go new file mode 100644 index 0000000..16d0fbb --- /dev/null +++ b/collectors/topprocsMetric.go @@ -0,0 +1,39 @@ +package collectors + +import ( + "fmt" + "log" + "os/exec" + "strings" + "time" +) + +const NUM_PROCS = 5 + +type TopProcsCollector struct { + MetricCollector +} + +func (m *TopProcsCollector) Init() { + m.name = "TopProcsCollector" + m.setup() +} + +func (m *TopProcsCollector) Read(interval time.Duration) { + command := exec.Command("/usr/bin/ps", "-Ao", "comm", "--sort=-pcpu") + command.Wait() + stdout, err := command.Output() + if err != nil { + log.Print(m.name, err) + return + } + + lines := strings.Split(string(stdout), "\n") + for i := 1; i < NUM_PROCS+1; i++ { + m.node[fmt.Sprintf("topproc%d", i)] = lines[i] + } +} + +func (m *TopProcsCollector) Close() { + return +} diff --git a/config.json b/config.json index 2bd044e..969f62a 100644 --- a/config.json +++ b/config.json @@ -16,6 +16,7 @@ "netstat", "ibstat", "lustrestat", - "cpustat" + "cpustat", + "topprocs" ] } diff --git a/metric-collector.go b/metric-collector.go index aeaffd1..8cf8232 100644 --- a/metric-collector.go +++ b/metric-collector.go @@ -24,6 +24,7 @@ var Collectors = map[string]collectors.MetricGetter{ "ibstat": &collectors.InfinibandCollector{}, "lustrestat": &collectors.LustreCollector{}, "cpustat": &collectors.CpustatCollector{}, + "topprocs": &collectors.TopProcsCollector{}, } var Sinks = map[string]sinks.SinkFuncs{