2021-03-25 14:47:35 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2021-11-25 15:11:39 +01:00
|
|
|
"encoding/json"
|
2021-03-25 14:47:35 +01:00
|
|
|
"errors"
|
2021-10-04 15:23:43 +02:00
|
|
|
"fmt"
|
2021-03-25 14:47:35 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
const MEMSTATFILE = `/proc/meminfo`
|
|
|
|
|
2021-11-25 14:04:03 +01:00
|
|
|
type MemstatCollectorConfig struct {
|
2021-11-25 15:11:39 +01:00
|
|
|
ExcludeMetrics []string `json:"exclude_metrics"`
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
|
2021-03-25 14:47:35 +01:00
|
|
|
type MemstatCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2021-10-04 15:23:43 +02:00
|
|
|
stats map[string]int64
|
|
|
|
tags map[string]string
|
|
|
|
matches map[string]string
|
2021-11-25 15:11:39 +01:00
|
|
|
config MemstatCollectorConfig
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *MemstatCollector) Init(config json.RawMessage) error {
|
2021-11-25 15:11:39 +01:00
|
|
|
var err error
|
2021-03-25 17:47:08 +01:00
|
|
|
m.name = "MemstatCollector"
|
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-01-25 15:37:43 +01:00
|
|
|
m.meta = map[string]string{"source": m.name, "group": "Memory", "unit": "kByte"}
|
2021-10-04 15:23:43 +02:00
|
|
|
m.stats = make(map[string]int64)
|
2021-11-25 14:04:03 +01:00
|
|
|
m.matches = make(map[string]string)
|
2021-10-04 15:23:43 +02:00
|
|
|
m.tags = map[string]string{"type": "node"}
|
2021-11-25 14:04:03 +01:00
|
|
|
matches := map[string]string{`MemTotal`: "mem_total",
|
2021-10-04 15:23:43 +02:00
|
|
|
"SwapTotal": "swap_total",
|
|
|
|
"SReclaimable": "mem_sreclaimable",
|
|
|
|
"Slab": "mem_slab",
|
|
|
|
"MemFree": "mem_free",
|
|
|
|
"Buffers": "mem_buffers",
|
|
|
|
"Cached": "mem_cached",
|
|
|
|
"MemAvailable": "mem_available",
|
|
|
|
"SwapFree": "swap_free"}
|
2021-11-25 14:04:03 +01:00
|
|
|
for k, v := range matches {
|
2021-11-25 15:11:39 +01:00
|
|
|
_, skip := stringArrayContains(m.config.ExcludeMetrics, k)
|
|
|
|
if !skip {
|
|
|
|
m.matches[k] = v
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
if len(m.matches) == 0 {
|
2021-11-25 15:11:39 +01:00
|
|
|
return errors.New("No metrics to collect")
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2021-03-25 14:47:35 +01:00
|
|
|
m.setup()
|
2021-11-25 14:04:03 +01:00
|
|
|
_, err = ioutil.ReadFile(string(MEMSTATFILE))
|
2021-10-04 15:23:43 +02:00
|
|
|
if err == nil {
|
2021-10-04 15:47:03 +02:00
|
|
|
m.init = true
|
|
|
|
}
|
2021-10-08 13:27:15 +02:00
|
|
|
return err
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *MemstatCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
2021-11-25 15:11:39 +01:00
|
|
|
if !m.init {
|
|
|
|
return
|
|
|
|
}
|
2021-03-25 14:47:35 +01:00
|
|
|
|
2021-11-25 14:04:03 +01:00
|
|
|
buffer, err := ioutil.ReadFile(string(MEMSTATFILE))
|
2021-03-25 14:47:35 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ll := strings.Split(string(buffer), "\n")
|
|
|
|
for _, line := range ll {
|
|
|
|
ls := strings.Split(line, `:`)
|
|
|
|
if len(ls) > 1 {
|
|
|
|
lv := strings.Fields(ls[1])
|
2021-10-04 15:23:43 +02:00
|
|
|
m.stats[ls[0]], err = strconv.ParseInt(lv[0], 0, 64)
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:23:43 +02:00
|
|
|
if _, exists := m.stats[`MemTotal`]; !exists {
|
2021-03-25 14:47:35 +01:00
|
|
|
err = errors.New("Parse error")
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:23:43 +02:00
|
|
|
for match, name := range m.matches {
|
|
|
|
if _, exists := m.stats[match]; !exists {
|
|
|
|
err = errors.New(fmt.Sprintf("Parse error for %s : %s", match, name))
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
2022-01-25 15:37:43 +01:00
|
|
|
y, err := lp.New(name, m.tags, m.meta, map[string]interface{}{"value": int(float64(m.stats[match]) * 1.0e-3)}, 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 18:09:15 +02:00
|
|
|
|
2021-10-04 15:23:43 +02:00
|
|
|
if _, free := m.stats[`MemFree`]; free {
|
|
|
|
if _, buffers := m.stats[`Buffers`]; buffers {
|
|
|
|
if _, cached := m.stats[`Cached`]; cached {
|
|
|
|
memUsed := m.stats[`MemTotal`] - (m.stats[`MemFree`] + m.stats[`Buffers`] + m.stats[`Cached`])
|
2021-11-25 14:04:03 +01:00
|
|
|
_, skip := stringArrayContains(m.config.ExcludeMetrics, "mem_used")
|
2022-01-25 15:37:43 +01:00
|
|
|
y, err := lp.New("mem_used", m.tags, m.meta, map[string]interface{}{"value": int(float64(memUsed) * 1.0e-3)}, time.Now())
|
2021-11-25 14:04:03 +01:00
|
|
|
if err == nil && !skip {
|
2022-01-25 15:37:43 +01:00
|
|
|
output <- y
|
2021-10-04 15:23:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, found := m.stats[`MemShared`]; found {
|
2021-11-25 15:11:39 +01:00
|
|
|
_, skip := stringArrayContains(m.config.ExcludeMetrics, "mem_shared")
|
2022-01-25 15:37:43 +01:00
|
|
|
y, err := lp.New("mem_shared", m.tags, m.meta, map[string]interface{}{"value": int(float64(m.stats[`MemShared`]) * 1.0e-3)}, time.Now())
|
2021-11-25 14:04:03 +01:00
|
|
|
if err == nil && !skip {
|
2022-01-25 15:37:43 +01:00
|
|
|
output <- y
|
2021-10-04 15:23:43 +02:00
|
|
|
}
|
2021-05-12 18:09:15 +02:00
|
|
|
}
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemstatCollector) Close() {
|
2021-10-04 15:47:03 +02:00
|
|
|
m.init = false
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|