Add collector for the top5 processes in %cpu

This commit is contained in:
Thomas Roehl
2021-05-12 17:47:12 +02:00
parent b84dc08cd6
commit 14bc29f766
3 changed files with 42 additions and 1 deletions

View File

@@ -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
}