2021-05-12 16:57:18 +02:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2021-11-25 15:11:39 +01:00
|
|
|
"encoding/json"
|
2021-10-04 15:23:43 +02:00
|
|
|
"fmt"
|
2021-05-12 16:57:18 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-01-25 15:37:43 +01:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2021-05-12 16:57:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const CPUSTATFILE = `/proc/stat`
|
|
|
|
|
2021-11-25 14:04:03 +01:00
|
|
|
type CpustatCollectorConfig struct {
|
2022-01-20 12:38:52 +01:00
|
|
|
ExcludeMetrics []string `json:"exclude_metrics,omitempty"`
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
|
2021-05-12 16:57:18 +02:00
|
|
|
type CpustatCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2021-11-25 14:04:03 +01:00
|
|
|
config CpustatCollectorConfig
|
2021-05-12 16:57:18 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *CpustatCollector) Init(config json.RawMessage) error {
|
2021-05-12 16:57:18 +02:00
|
|
|
m.name = "CpustatCollector"
|
|
|
|
m.setup()
|
2022-01-25 15:37:43 +01:00
|
|
|
m.meta = map[string]string{"source": m.name, "group": "CPU"}
|
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-25 14:04:03 +01:00
|
|
|
}
|
2021-10-04 15:23:43 +02:00
|
|
|
m.init = true
|
2021-05-14 19:21:16 +02:00
|
|
|
return nil
|
2021-05-12 16:57:18 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (c *CpustatCollector) parseStatLine(line string, cpu int, exclude []string, output chan lp.CCMetric) {
|
2021-05-12 17:45:41 +02:00
|
|
|
ls := strings.Fields(line)
|
2021-10-04 15:23:43 +02:00
|
|
|
matches := []string{"", "cpu_user", "cpu_nice", "cpu_system", "cpu_idle", "cpu_iowait", "cpu_irq", "cpu_softirq", "cpu_steal", "cpu_guest", "cpu_guest_nice"}
|
2021-11-25 15:11:39 +01:00
|
|
|
for _, ex := range exclude {
|
|
|
|
matches, _ = RemoveFromStringList(matches, ex)
|
|
|
|
}
|
2021-10-04 15:23:43 +02:00
|
|
|
|
|
|
|
var tags map[string]string
|
|
|
|
if cpu < 0 {
|
|
|
|
tags = map[string]string{"type": "node"}
|
|
|
|
} else {
|
|
|
|
tags = map[string]string{"type": "cpu", "type-id": fmt.Sprintf("%d", cpu)}
|
|
|
|
}
|
|
|
|
for i, m := range matches {
|
|
|
|
if len(m) > 0 {
|
|
|
|
x, err := strconv.ParseInt(ls[i], 0, 64)
|
|
|
|
if err == nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
y, err := lp.New(m, tags, c.meta, map[string]interface{}{"value": int(x)}, time.Now())
|
2021-10-04 15:23:43 +02:00
|
|
|
if err == nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
output <- y
|
2021-10-04 15:23:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 16:57:18 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *CpustatCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
2021-11-25 15:11:39 +01:00
|
|
|
if !m.init {
|
|
|
|
return
|
|
|
|
}
|
2021-05-12 16:57:18 +02:00
|
|
|
buffer, err := ioutil.ReadFile(string(CPUSTATFILE))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:45:41 +02:00
|
|
|
ll := strings.Split(string(buffer), "\n")
|
|
|
|
for _, line := range ll {
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ls := strings.Fields(line)
|
|
|
|
if strings.Compare(ls[0], "cpu") == 0 {
|
2022-01-25 15:37:43 +01:00
|
|
|
m.parseStatLine(line, -1, m.config.ExcludeMetrics, output)
|
2021-05-12 17:45:41 +02:00
|
|
|
} else if strings.HasPrefix(ls[0], "cpu") {
|
|
|
|
cpustr := strings.TrimLeft(ls[0], "cpu")
|
|
|
|
cpu, _ := strconv.Atoi(cpustr)
|
2022-01-25 15:37:43 +01:00
|
|
|
m.parseStatLine(line, cpu, m.config.ExcludeMetrics, output)
|
2021-05-12 17:45:41 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-12 16:57:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *CpustatCollector) Close() {
|
2021-10-04 15:47:03 +02:00
|
|
|
m.init = false
|
2021-05-12 16:57:18 +02:00
|
|
|
}
|