2021-05-12 17:47:12 +02:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2021-11-25 15:11:39 +01:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2021-05-12 17:47:12 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-01-21 14:35:52 +01:00
|
|
|
|
|
|
|
lp "github.com/influxdata/line-protocol"
|
2021-05-12 17:47:12 +02:00
|
|
|
)
|
|
|
|
|
2021-11-25 14:04:03 +01:00
|
|
|
const MAX_NUM_PROCS = 10
|
2021-11-26 18:19:26 +01:00
|
|
|
const DEFAULT_NUM_PROCS = 2
|
2021-11-25 14:04:03 +01:00
|
|
|
|
|
|
|
type TopProcsCollectorConfig struct {
|
2021-11-25 15:11:39 +01:00
|
|
|
num_procs int `json:"num_procs"`
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2021-05-12 17:47:12 +02:00
|
|
|
|
|
|
|
type TopProcsCollector struct {
|
|
|
|
MetricCollector
|
2021-11-25 15:11:39 +01:00
|
|
|
tags map[string]string
|
2021-11-25 14:04:03 +01:00
|
|
|
config TopProcsCollectorConfig
|
2021-05-12 17:47:12 +02:00
|
|
|
}
|
|
|
|
|
2021-11-25 14:04:03 +01:00
|
|
|
func (m *TopProcsCollector) Init(config []byte) error {
|
2021-11-25 15:11:39 +01:00
|
|
|
var err error
|
2021-05-12 17:47:12 +02:00
|
|
|
m.name = "TopProcsCollector"
|
2021-10-04 15:23:43 +02:00
|
|
|
m.tags = map[string]string{"type": "node"}
|
2021-11-25 14:04:03 +01:00
|
|
|
if len(config) > 0 {
|
2021-11-25 15:11:39 +01:00
|
|
|
err = json.Unmarshal(config, &m.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-26 18:19:26 +01:00
|
|
|
} else {
|
2021-11-29 15:32:58 +01:00
|
|
|
m.config.num_procs = int(DEFAULT_NUM_PROCS)
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
if m.config.num_procs <= 0 || m.config.num_procs > MAX_NUM_PROCS {
|
2021-11-25 15:11:39 +01:00
|
|
|
return errors.New(fmt.Sprintf("num_procs option must be set in 'topprocs' config (range: 1-%d)", MAX_NUM_PROCS))
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2021-05-12 17:47:12 +02:00
|
|
|
m.setup()
|
2021-10-04 15:23:43 +02:00
|
|
|
command := exec.Command("ps", "-Ao", "comm", "--sort=-pcpu")
|
|
|
|
command.Wait()
|
2021-11-25 14:04:03 +01:00
|
|
|
_, err = command.Output()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("Failed to execute command")
|
2021-10-04 15:23:43 +02:00
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
m.init = true
|
2021-05-14 19:21:16 +02:00
|
|
|
return nil
|
2021-05-12 17:47:12 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 15:23:43 +02:00
|
|
|
func (m *TopProcsCollector) Read(interval time.Duration, out *[]lp.MutableMetric) {
|
2021-11-25 15:11:39 +01:00
|
|
|
if !m.init {
|
|
|
|
return
|
|
|
|
}
|
2021-05-18 15:14:37 +02:00
|
|
|
command := exec.Command("ps", "-Ao", "comm", "--sort=-pcpu")
|
2021-05-12 17:47:12 +02:00
|
|
|
command.Wait()
|
|
|
|
stdout, err := command.Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(m.name, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(string(stdout), "\n")
|
2021-11-25 14:04:03 +01:00
|
|
|
for i := 1; i < m.config.num_procs+1; i++ {
|
2021-10-04 15:23:43 +02:00
|
|
|
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 {
|
|
|
|
*out = append(*out, y)
|
|
|
|
}
|
2021-05-12 17:47:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TopProcsCollector) Close() {
|
2021-10-04 15:47:03 +02:00
|
|
|
m.init = false
|
2021-05-12 17:47:12 +02:00
|
|
|
}
|