cc-metric-collector/collectors/lustreMetric.go

107 lines
2.3 KiB
Go
Raw Normal View History

2021-03-25 16:52:28 +01:00
package collectors
import (
2021-11-25 15:11:39 +01:00
"encoding/json"
"errors"
2021-10-04 15:23:43 +02:00
lp "github.com/influxdata/line-protocol"
2021-03-25 16:52:28 +01:00
"io/ioutil"
"log"
"strconv"
"strings"
"time"
)
const LUSTREFILE = `/proc/fs/lustre/llite/lnec-XXXXXX/stats`
type LustreCollectorConfig struct {
2021-11-25 15:11:39 +01:00
procfiles []string `json:"procfiles"`
ExcludeMetrics []string `json:"exclude_metrics"`
}
2021-03-25 16:52:28 +01:00
type LustreCollector struct {
MetricCollector
2021-10-04 15:23:43 +02:00
tags map[string]string
matches map[string]map[string]int
devices []string
2021-11-25 15:11:39 +01:00
config LustreCollectorConfig
2021-03-25 16:52:28 +01:00
}
func (m *LustreCollector) Init(config []byte) error {
2021-11-25 15:11:39 +01:00
var err error
2021-03-25 17:47:08 +01:00
m.name = "LustreCollector"
if len(config) > 0 {
2021-11-25 15:11:39 +01:00
err = json.Unmarshal(config, &m.config)
if err != nil {
return err
}
}
2021-03-25 16:52:28 +01:00
m.setup()
2021-10-04 15:23:43 +02:00
m.tags = map[string]string{"type": "node"}
m.matches = map[string]map[string]int{"read_bytes": {"read_bytes": 6, "read_requests": 1},
"write_bytes": {"write_bytes": 6, "write_requests": 1},
"open": {"open": 1},
"close": {"close": 1},
"setattr": {"setattr": 1},
"getattr": {"getattr": 1},
"statfs": {"statfs": 1},
"inode_permission": {"inode_permission": 1}}
m.devices = make([]string, 0)
for _, p := range m.config.procfiles {
2021-11-25 15:11:39 +01:00
_, err := ioutil.ReadFile(p)
if err == nil {
m.devices = append(m.devices, p)
} else {
log.Print(err.Error())
continue
}
2021-10-04 15:23:43 +02:00
}
2021-11-25 15:11:39 +01:00
if len(m.devices) == 0 {
return errors.New("No metrics to collect")
}
m.init = true
return nil
2021-03-25 16:52:28 +01:00
}
2021-10-04 15:23:43 +02:00
func (m *LustreCollector) Read(interval time.Duration, out *[]lp.MutableMetric) {
2021-11-25 15:11:39 +01:00
if !m.init {
return
}
for _, p := range m.devices {
buffer, err := ioutil.ReadFile(p)
2021-03-25 16:52:28 +01:00
2021-11-25 15:11:39 +01:00
if err != nil {
log.Print(err)
return
}
2021-03-25 16:52:28 +01:00
2021-11-25 15:11:39 +01:00
for _, line := range strings.Split(string(buffer), "\n") {
lf := strings.Fields(line)
if len(lf) > 1 {
for match, fields := range m.matches {
if lf[0] == match {
for name, idx := range fields {
_, skip := stringArrayContains(m.config.ExcludeMetrics, name)
if skip {
continue
}
x, err := strconv.ParseInt(lf[idx], 0, 64)
if err == nil {
y, err := lp.New(name, m.tags, map[string]interface{}{"value": x}, time.Now())
if err == nil {
*out = append(*out, y)
}
}
}
}
}
}
}
2021-03-25 16:52:28 +01:00
}
}
func (m *LustreCollector) Close() {
2021-10-04 15:47:03 +02:00
m.init = false
2021-03-25 17:47:08 +01:00
return
2021-03-25 16:52:28 +01:00
}