2021-03-25 14:47:35 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2021-11-25 15:11:39 +01:00
|
|
|
"encoding/json"
|
2022-02-04 16:11:56 +01:00
|
|
|
"fmt"
|
2021-03-25 14:47:35 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-03-25 17:47:08 +01:00
|
|
|
"time"
|
2022-02-04 16:11:56 +01:00
|
|
|
|
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
|
2022-01-25 15:37:43 +01:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2021-03-25 14:47:35 +01:00
|
|
|
)
|
|
|
|
|
2022-02-04 16:11:56 +01:00
|
|
|
//
|
|
|
|
// LoadavgCollector collects:
|
|
|
|
// * load average of last 1, 5 & 15 minutes
|
|
|
|
// * number of processes currently runnable
|
|
|
|
// * total number of processes in system
|
|
|
|
//
|
|
|
|
// See: https://www.kernel.org/doc/html/latest/filesystems/proc.html
|
|
|
|
//
|
|
|
|
const LOADAVGFILE = "/proc/loadavg"
|
2021-03-25 14:47:35 +01:00
|
|
|
|
|
|
|
type LoadavgCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2021-10-04 15:23:43 +02:00
|
|
|
tags map[string]string
|
|
|
|
load_matches []string
|
2022-02-04 19:22:31 +01:00
|
|
|
load_skips []bool
|
2021-10-04 15:23:43 +02:00
|
|
|
proc_matches []string
|
2022-02-04 19:22:31 +01:00
|
|
|
proc_skips []bool
|
|
|
|
config struct {
|
|
|
|
ExcludeMetrics []string `json:"exclude_metrics,omitempty"`
|
|
|
|
}
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *LoadavgCollector) Init(config json.RawMessage) error {
|
2021-03-25 17:47:08 +01:00
|
|
|
m.name = "LoadavgCollector"
|
2021-03-25 14:47:35 +01:00
|
|
|
m.setup()
|
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
|
|
|
}
|
2022-02-04 16:11:56 +01:00
|
|
|
m.meta = map[string]string{
|
|
|
|
"source": m.name,
|
|
|
|
"group": "LOAD"}
|
2021-10-04 15:23:43 +02:00
|
|
|
m.tags = map[string]string{"type": "node"}
|
2022-02-04 16:11:56 +01:00
|
|
|
m.load_matches = []string{
|
|
|
|
"load_one",
|
|
|
|
"load_five",
|
|
|
|
"load_fifteen"}
|
2022-02-04 19:22:31 +01:00
|
|
|
m.load_skips = make([]bool, len(m.load_matches))
|
2022-02-04 16:11:56 +01:00
|
|
|
m.proc_matches = []string{
|
|
|
|
"proc_run",
|
|
|
|
"proc_total"}
|
2022-02-04 19:22:31 +01:00
|
|
|
m.proc_skips = make([]bool, len(m.proc_matches))
|
|
|
|
|
|
|
|
for i, name := range m.load_matches {
|
|
|
|
_, m.load_skips[i] = stringArrayContains(m.config.ExcludeMetrics, name)
|
|
|
|
}
|
|
|
|
for i, name := range m.proc_matches {
|
|
|
|
_, m.proc_skips[i] = stringArrayContains(m.config.ExcludeMetrics, name)
|
|
|
|
}
|
2021-10-04 15:23:43 +02:00
|
|
|
m.init = true
|
2021-05-14 19:21:16 +02:00
|
|
|
return nil
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *LoadavgCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
2021-11-25 15:11:39 +01:00
|
|
|
if !m.init {
|
|
|
|
return
|
|
|
|
}
|
2022-02-04 16:11:56 +01:00
|
|
|
buffer, err := ioutil.ReadFile(LOADAVGFILE)
|
2021-03-25 14:47:35 +01:00
|
|
|
if err != nil {
|
2022-02-04 16:11:56 +01:00
|
|
|
if err != nil {
|
|
|
|
cclog.ComponentError(
|
|
|
|
m.name,
|
|
|
|
fmt.Sprintf("Read(): Failed to read file '%s': %v", LOADAVGFILE, err))
|
|
|
|
}
|
2021-03-25 14:47:35 +01:00
|
|
|
return
|
|
|
|
}
|
2022-02-04 16:11:56 +01:00
|
|
|
now := time.Now()
|
2021-03-25 14:47:35 +01:00
|
|
|
|
2022-02-04 16:11:56 +01:00
|
|
|
// Load metrics
|
2021-03-25 14:47:35 +01:00
|
|
|
ls := strings.Split(string(buffer), ` `)
|
2021-10-04 15:23:43 +02:00
|
|
|
for i, name := range m.load_matches {
|
|
|
|
x, err := strconv.ParseFloat(ls[i], 64)
|
2022-02-04 16:11:56 +01:00
|
|
|
if err != nil {
|
|
|
|
cclog.ComponentError(
|
|
|
|
m.name,
|
|
|
|
fmt.Sprintf("Read(): Failed to convert '%s' to float64: %v", ls[i], err))
|
|
|
|
continue
|
|
|
|
}
|
2022-02-04 19:22:31 +01:00
|
|
|
if m.load_skips[i] {
|
|
|
|
continue
|
|
|
|
}
|
2022-02-04 16:11:56 +01:00
|
|
|
y, err := lp.New(name, m.tags, m.meta, map[string]interface{}{"value": x}, now)
|
2022-02-04 19:22:31 +01:00
|
|
|
if err == nil {
|
2022-02-04 16:11:56 +01:00
|
|
|
output <- y
|
2021-10-04 15:23:43 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-04 16:11:56 +01:00
|
|
|
|
|
|
|
// Process metrics
|
2021-05-12 18:08:43 +02:00
|
|
|
lv := strings.Split(ls[3], `/`)
|
2021-10-04 15:23:43 +02:00
|
|
|
for i, name := range m.proc_matches {
|
2022-02-04 19:22:31 +01:00
|
|
|
x, err := strconv.ParseInt(lv[i], 10, 64)
|
2022-02-04 16:11:56 +01:00
|
|
|
if err != nil {
|
|
|
|
cclog.ComponentError(
|
|
|
|
m.name,
|
|
|
|
fmt.Sprintf("Read(): Failed to convert '%s' to float64: %v", lv[i], err))
|
|
|
|
continue
|
2021-10-04 15:23:43 +02:00
|
|
|
}
|
2022-02-04 19:22:31 +01:00
|
|
|
if m.proc_skips[i] {
|
|
|
|
continue
|
|
|
|
}
|
2022-02-04 16:11:56 +01:00
|
|
|
y, err := lp.New(name, m.tags, m.meta, map[string]interface{}{"value": x}, now)
|
2022-02-04 19:22:31 +01:00
|
|
|
if err == nil {
|
2022-02-04 16:11:56 +01:00
|
|
|
output <- y
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:23:43 +02:00
|
|
|
}
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *LoadavgCollector) Close() {
|
2021-10-04 15:47:03 +02:00
|
|
|
m.init = false
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|