mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2025-07-31 08:56:06 +02:00
Collector-specific configuration. LIKWID collector derives metrics itself, Run once CLI option
This commit is contained in:
@@ -7,29 +7,50 @@ import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
const NUM_PROCS = 5
|
||||
const MAX_NUM_PROCS = 10
|
||||
|
||||
type TopProcsCollectorConfig struct {
|
||||
num_procs int `json:"num_procs"`
|
||||
}
|
||||
|
||||
type TopProcsCollector struct {
|
||||
MetricCollector
|
||||
tags map[string]string
|
||||
config TopProcsCollectorConfig
|
||||
}
|
||||
|
||||
func (m *TopProcsCollector) Init() error {
|
||||
func (m *TopProcsCollector) Init(config []byte) error {
|
||||
var err error
|
||||
m.name = "TopProcsCollector"
|
||||
m.tags = map[string]string{"type": "node"}
|
||||
if len(config) > 0 {
|
||||
err = json.Unmarshal(config, &m.config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if m.config.num_procs <= 0 || m.config.num_procs > MAX_NUM_PROCS {
|
||||
return errors.New(fmt.Sprintf("num_procs option must be set in 'topprocs' config (range: 1-%d)", MAX_NUM_PROCS))
|
||||
}
|
||||
m.setup()
|
||||
command := exec.Command("ps", "-Ao", "comm", "--sort=-pcpu")
|
||||
command.Wait()
|
||||
_, err := command.Output()
|
||||
if err == nil {
|
||||
m.init = true
|
||||
_, err = command.Output()
|
||||
if err != nil {
|
||||
return errors.New("Failed to execute command")
|
||||
}
|
||||
m.init = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TopProcsCollector) Read(interval time.Duration, out *[]lp.MutableMetric) {
|
||||
if !m.init {
|
||||
return
|
||||
}
|
||||
command := exec.Command("ps", "-Ao", "comm", "--sort=-pcpu")
|
||||
command.Wait()
|
||||
stdout, err := command.Output()
|
||||
@@ -39,7 +60,7 @@ func (m *TopProcsCollector) Read(interval time.Duration, out *[]lp.MutableMetric
|
||||
}
|
||||
|
||||
lines := strings.Split(string(stdout), "\n")
|
||||
for i := 1; i < NUM_PROCS+1; i++ {
|
||||
for i := 1; i < m.config.num_procs+1; i++ {
|
||||
name := fmt.Sprintf("topproc%d", i)
|
||||
y, err := lp.New(name, m.tags, map[string]interface{}{"value": string(lines[i])}, time.Now())
|
||||
if err == nil {
|
||||
|
Reference in New Issue
Block a user