cc-metric-collector/collectors/lustreMetric.go

71 lines
1.6 KiB
Go
Raw Normal View History

2021-03-25 16:52:28 +01:00
package collectors
import (
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 LustreCollector struct {
MetricCollector
2021-10-04 15:23:43 +02:00
tags map[string]string
matches map[string]map[string]int
2021-03-25 16:52:28 +01:00
}
func (m *LustreCollector) Init() error {
2021-03-25 17:47:08 +01:00
m.name = "LustreCollector"
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}}
_, err := ioutil.ReadFile(string(LUSTREFILE))
2021-10-04 15:23:43 +02:00
if err == nil {
2021-10-04 15:47:03 +02:00
m.init = true
2021-10-04 15:23:43 +02:00
}
return err
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-03-25 16:52:28 +01:00
buffer, err := ioutil.ReadFile(string(LUSTREFILE))
if err != nil {
log.Print(err)
return
}
for _, line := range strings.Split(string(buffer), "\n") {
lf := strings.Fields(line)
if len(lf) > 1 {
2021-10-04 15:23:43 +02:00
for match, fields := range m.matches {
if lf[0] == match {
for name, idx := range fields {
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 17:47:08 +01:00
}
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
}