2021-03-25 14:47:35 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2021-11-25 15:11:39 +01:00
|
|
|
"encoding/json"
|
2021-10-04 15:23:43 +02:00
|
|
|
lp "github.com/influxdata/line-protocol"
|
2021-03-25 14:47:35 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-03-25 17:47:08 +01:00
|
|
|
"time"
|
2021-03-25 14:47:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const LOADAVGFILE = `/proc/loadavg`
|
|
|
|
|
2021-11-25 14:04:03 +01:00
|
|
|
type LoadavgCollectorConfig struct {
|
2021-11-25 15:11:39 +01:00
|
|
|
ExcludeMetrics []string `json:"exclude_metrics, omitempty"`
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
|
2021-03-25 14:47:35 +01:00
|
|
|
type LoadavgCollector struct {
|
|
|
|
MetricCollector
|
2021-10-04 15:23:43 +02:00
|
|
|
tags map[string]string
|
|
|
|
load_matches []string
|
|
|
|
proc_matches []string
|
2021-11-25 15:11:39 +01:00
|
|
|
config LoadavgCollectorConfig
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|
|
|
|
|
2021-11-25 14:04:03 +01:00
|
|
|
func (m *LoadavgCollector) Init(config []byte) 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
|
|
|
}
|
2021-10-04 15:23:43 +02:00
|
|
|
m.tags = map[string]string{"type": "node"}
|
|
|
|
m.load_matches = []string{"load_one", "load_five", "load_fifteen"}
|
|
|
|
m.proc_matches = []string{"proc_run", "proc_total"}
|
|
|
|
m.init = true
|
2021-05-14 19:21:16 +02:00
|
|
|
return nil
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-04 15:23:43 +02:00
|
|
|
func (m *LoadavgCollector) Read(interval time.Duration, out *[]lp.MutableMetric) {
|
2021-11-25 15:11:39 +01:00
|
|
|
var skip bool
|
|
|
|
if !m.init {
|
|
|
|
return
|
|
|
|
}
|
2021-03-25 14:47:35 +01:00
|
|
|
buffer, err := ioutil.ReadFile(string(LOADAVGFILE))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
if err == nil {
|
2021-11-25 15:11:39 +01:00
|
|
|
_, skip = stringArrayContains(m.config.ExcludeMetrics, name)
|
2021-10-04 15:23:43 +02:00
|
|
|
y, err := lp.New(name, m.tags, map[string]interface{}{"value": float64(x)}, time.Now())
|
2021-11-25 14:04:03 +01:00
|
|
|
if err == nil && !skip {
|
2021-10-04 15:23:43 +02:00
|
|
|
*out = append(*out, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
x, err := strconv.ParseFloat(lv[i], 64)
|
|
|
|
if err == nil {
|
2021-11-25 15:11:39 +01:00
|
|
|
_, skip = stringArrayContains(m.config.ExcludeMetrics, name)
|
2021-10-04 15:23:43 +02:00
|
|
|
y, err := lp.New(name, m.tags, map[string]interface{}{"value": float64(x)}, time.Now())
|
2021-11-25 14:04:03 +01:00
|
|
|
if err == nil && !skip {
|
2021-10-04 15:23:43 +02:00
|
|
|
*out = append(*out, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 17:47:08 +01:00
|
|
|
return
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|