Fix for LustreCollector. Check for root user

This commit is contained in:
Thomas Roehl 2022-02-07 13:27:35 +01:00
parent c313055570
commit 52458ce5a1

View File

@ -5,10 +5,12 @@ import (
"errors" "errors"
"fmt" "fmt"
"os/exec" "os/exec"
"os/user"
"strconv" "strconv"
"strings" "strings"
"time" "time"
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
) )
@ -19,17 +21,26 @@ const LCTL_OPTION = `get_param`
type LustreCollectorConfig struct { type LustreCollectorConfig struct {
LCtlCommand string `json:"lctl_command"` LCtlCommand string `json:"lctl_command"`
ExcludeMetrics []string `json:"exclude_metrics"` ExcludeMetrics []string `json:"exclude_metrics"`
SendAllMetrics bool `json:"send_all_metrics"`
} }
type LustreCollector struct { type LustreCollector struct {
metricCollector metricCollector
tags map[string]string tags map[string]string
matches map[string]map[string]int matches map[string]map[string]int
devices []string stats map[string]map[string]int64
config LustreCollectorConfig config LustreCollectorConfig
lctl string lctl string
} }
func (m *LustreCollector) getDeviceDataCommand(device string) []string {
statsfile := fmt.Sprintf("llite.%s.stats", device)
command := exec.Command(m.lctl, LCTL_OPTION, statsfile)
command.Wait()
stdout, _ := command.Output()
return strings.Split(string(stdout), "\n")
}
func (m *LustreCollector) getDevices() []string { func (m *LustreCollector) getDevices() []string {
devices := make([]string, 0) devices := make([]string, 0)
@ -44,13 +55,9 @@ func (m *LustreCollector) getDevices() []string {
// devices = append(devices, pathlist[4]) // devices = append(devices, pathlist[4])
// } // }
command := exec.Command(m.lctl, LCTL_OPTION, "llite.*.stats") data := m.getDeviceDataCommand("*")
command.Wait()
stdout, err := command.Output() for _, line := range data {
if err != nil {
return devices
}
for _, line := range strings.Split(string(stdout), "\n") {
if strings.HasPrefix(line, "llite") { if strings.HasPrefix(line, "llite") {
linefields := strings.Split(line, ".") linefields := strings.Split(line, ".")
if len(linefields) > 2 { if len(linefields) > 2 {
@ -73,14 +80,6 @@ func (m *LustreCollector) getDevices() []string {
// return strings.Split(string(buffer), "\n") // return strings.Split(string(buffer), "\n")
// } // }
func (m *LustreCollector) getDeviceDataCommand(device string) []string {
statsfile := fmt.Sprintf("llite.%s.stats", device)
command := exec.Command(m.lctl, LCTL_OPTION, statsfile)
command.Wait()
stdout, _ := command.Output()
return strings.Split(string(stdout), "\n")
}
func (m *LustreCollector) Init(config json.RawMessage) error { func (m *LustreCollector) Init(config json.RawMessage) error {
var err error var err error
m.name = "LustreCollector" m.name = "LustreCollector"
@ -93,14 +92,42 @@ func (m *LustreCollector) Init(config json.RawMessage) error {
m.setup() m.setup()
m.tags = map[string]string{"type": "node"} m.tags = map[string]string{"type": "node"}
m.meta = map[string]string{"source": m.name, "group": "Lustre"} m.meta = map[string]string{"source": m.name, "group": "Lustre"}
m.matches = map[string]map[string]int{"read_bytes": {"read_bytes": 6, "read_requests": 1}, defmatches := map[string]map[string]int{
"write_bytes": {"write_bytes": 6, "write_requests": 1}, "read_bytes": {"lustre_read_bytes": 6, "lustre_read_requests": 1},
"open": {"open": 1}, "write_bytes": {"lustre_write_bytes": 6, "lustre_write_requests": 1},
"close": {"close": 1}, "open": {"lustre_open": 1},
"setattr": {"setattr": 1}, "close": {"lustre_close": 1},
"getattr": {"getattr": 1}, "setattr": {"lustre_setattr": 1},
"statfs": {"statfs": 1}, "getattr": {"lustre_getattr": 1},
"inode_permission": {"inode_permission": 1}} "statfs": {"lustre_statfs": 1},
"inode_permission": {"lustre_inode_permission": 1}}
// Lustre file system statistics can only be queried by user root
user, err := user.Current()
if err != nil {
cclog.ComponentError(m.name, "Failed to get current user:", err.Error())
return err
}
if user.Uid != "0" {
cclog.ComponentError(m.name, "Lustre file system statistics can only be queried by user root:", err.Error())
return err
}
m.matches = make(map[string]map[string]int)
for lineprefix, names := range defmatches {
for metricname, offset := range names {
_, skip := stringArrayContains(m.config.ExcludeMetrics, metricname)
if skip {
continue
}
if _, prefixExist := m.matches[lineprefix]; !prefixExist {
m.matches[lineprefix] = make(map[string]int)
}
if _, metricExist := m.matches[lineprefix][metricname]; !metricExist {
m.matches[lineprefix][metricname] = offset
}
}
}
p, err := exec.LookPath(m.config.LCtlCommand) p, err := exec.LookPath(m.config.LCtlCommand)
if err != nil { if err != nil {
p, err = exec.LookPath(LCTL_CMD) p, err = exec.LookPath(LCTL_CMD)
@ -110,10 +137,19 @@ func (m *LustreCollector) Init(config json.RawMessage) error {
} }
m.lctl = p m.lctl = p
m.devices = m.getDevices() devices := m.getDevices()
if len(m.devices) == 0 { if len(devices) == 0 {
return errors.New("no metrics to collect") return errors.New("no metrics to collect")
} }
m.stats = make(map[string]map[string]int64)
for _, d := range devices {
m.stats[d] = make(map[string]int64)
for _, names := range m.matches {
for metricname := range names {
m.stats[d][metricname] = 0
}
}
}
m.init = true m.init = true
return nil return nil
} }
@ -122,33 +158,52 @@ func (m *LustreCollector) Read(interval time.Duration, output chan lp.CCMetric)
if !m.init { if !m.init {
return return
} }
for _, device := range m.devices { for device, devData := range m.stats {
stats := m.getDeviceDataCommand(device) stats := m.getDeviceDataCommand(device)
processed := []string{}
for _, line := range stats { for _, line := range stats {
lf := strings.Fields(line) lf := strings.Fields(line)
if len(lf) > 1 { if len(lf) > 1 {
for match, fields := range m.matches { if fields, ok := m.matches[lf[0]]; ok {
if lf[0] == match {
for name, idx := range fields { for name, idx := range fields {
_, skip := stringArrayContains(m.config.ExcludeMetrics, name) x, err := strconv.ParseInt(lf[idx], 0, 64)
if skip { if err != nil {
continue continue
} }
x, err := strconv.ParseInt(lf[idx], 0, 64) value := x - devData[name]
if err == nil { devData[name] = x
y, err := lp.New(name, m.tags, m.meta, map[string]interface{}{"value": x}, time.Now()) if value < 0 {
value = 0
}
y, err := lp.New(name, m.tags, m.meta, map[string]interface{}{"value": value}, time.Now())
if err == nil { if err == nil {
y.AddTag("device", device)
if strings.Contains(name, "byte") { if strings.Contains(name, "byte") {
y.AddMeta("unit", "Byte") y.AddMeta("unit", "Byte")
} }
output <- y output <- y
if m.config.SendAllMetrics {
processed = append(processed, name)
} }
} }
} }
} }
} }
} }
if m.config.SendAllMetrics {
for name := range devData {
if _, done := stringArrayContains(processed, name); !done {
y, err := lp.New(name, m.tags, m.meta, map[string]interface{}{"value": 0}, time.Now())
if err == nil {
y.AddTag("device", device)
if strings.Contains(name, "byte") {
y.AddMeta("unit", "Byte")
}
output <- y
}
}
}
} }
} }
} }