2021-03-25 16:52:28 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2021-11-25 15:11:39 +01:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-02-03 22:05:16 +01:00
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2022-02-07 13:27:35 +01:00
|
|
|
"os/user"
|
2021-03-25 16:52:28 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-02-03 22:05:16 +01:00
|
|
|
|
2022-02-07 13:27:35 +01:00
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
|
2022-01-25 15:37:43 +01:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2021-03-25 16:52:28 +01:00
|
|
|
)
|
|
|
|
|
2022-02-03 22:05:16 +01:00
|
|
|
const LUSTRE_SYSFS = `/sys/fs/lustre`
|
|
|
|
const LCTL_CMD = `lctl`
|
|
|
|
const LCTL_OPTION = `get_param`
|
2021-03-25 16:52:28 +01:00
|
|
|
|
2021-11-25 14:04:03 +01:00
|
|
|
type LustreCollectorConfig struct {
|
2022-02-03 22:05:16 +01:00
|
|
|
LCtlCommand string `json:"lctl_command"`
|
2021-11-25 15:11:39 +01:00
|
|
|
ExcludeMetrics []string `json:"exclude_metrics"`
|
2022-02-07 13:27:35 +01:00
|
|
|
SendAllMetrics bool `json:"send_all_metrics"`
|
2022-03-03 13:02:00 +01:00
|
|
|
Sudo bool `json:"use_sudo"`
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
|
2021-03-25 16:52:28 +01:00
|
|
|
type LustreCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2021-10-04 15:23:43 +02:00
|
|
|
tags map[string]string
|
|
|
|
matches map[string]map[string]int
|
2022-02-07 13:27:35 +01:00
|
|
|
stats map[string]map[string]int64
|
2021-11-25 15:11:39 +01:00
|
|
|
config LustreCollectorConfig
|
2022-02-03 22:05:16 +01:00
|
|
|
lctl string
|
2022-03-03 13:02:00 +01:00
|
|
|
sudoCmd string
|
2022-02-03 22:05:16 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 13:27:35 +01:00
|
|
|
func (m *LustreCollector) getDeviceDataCommand(device string) []string {
|
2022-03-03 13:02:00 +01:00
|
|
|
var command *exec.Cmd
|
2022-02-07 13:27:35 +01:00
|
|
|
statsfile := fmt.Sprintf("llite.%s.stats", device)
|
2022-03-03 13:02:00 +01:00
|
|
|
if m.config.Sudo {
|
|
|
|
command = exec.Command(m.sudoCmd, m.lctl, LCTL_OPTION, statsfile)
|
|
|
|
} else {
|
|
|
|
command = exec.Command(m.lctl, LCTL_OPTION, statsfile)
|
|
|
|
}
|
2022-02-07 13:27:35 +01:00
|
|
|
command.Wait()
|
|
|
|
stdout, _ := command.Output()
|
|
|
|
return strings.Split(string(stdout), "\n")
|
|
|
|
}
|
|
|
|
|
2022-02-03 22:05:16 +01:00
|
|
|
func (m *LustreCollector) getDevices() []string {
|
|
|
|
devices := make([]string, 0)
|
|
|
|
|
|
|
|
// //Version reading devices from sysfs
|
|
|
|
// globPattern := filepath.Join(LUSTRE_SYSFS, "llite/*/stats")
|
|
|
|
// files, err := filepath.Glob(globPattern)
|
|
|
|
// if err != nil {
|
|
|
|
// return devices
|
|
|
|
// }
|
|
|
|
// for _, f := range files {
|
|
|
|
// pathlist := strings.Split(f, "/")
|
|
|
|
// devices = append(devices, pathlist[4])
|
|
|
|
// }
|
|
|
|
|
2022-02-07 13:27:35 +01:00
|
|
|
data := m.getDeviceDataCommand("*")
|
|
|
|
|
|
|
|
for _, line := range data {
|
2022-02-03 22:05:16 +01:00
|
|
|
if strings.HasPrefix(line, "llite") {
|
|
|
|
linefields := strings.Split(line, ".")
|
|
|
|
if len(linefields) > 2 {
|
|
|
|
devices = append(devices, linefields[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return devices
|
|
|
|
}
|
|
|
|
|
|
|
|
// //Version reading the stats data of a device from sysfs
|
|
|
|
// func (m *LustreCollector) getDeviceDataSysfs(device string) []string {
|
|
|
|
// llitedir := filepath.Join(LUSTRE_SYSFS, "llite")
|
|
|
|
// devdir := filepath.Join(llitedir, device)
|
|
|
|
// statsfile := filepath.Join(devdir, "stats")
|
|
|
|
// buffer, err := ioutil.ReadFile(statsfile)
|
|
|
|
// if err != nil {
|
|
|
|
// return make([]string, 0)
|
|
|
|
// }
|
|
|
|
// return strings.Split(string(buffer), "\n")
|
|
|
|
// }
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *LustreCollector) 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 = "LustreCollector"
|
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-03-25 16:52:28 +01:00
|
|
|
m.setup()
|
2021-10-04 15:23:43 +02:00
|
|
|
m.tags = map[string]string{"type": "node"}
|
2022-01-25 15:37:43 +01:00
|
|
|
m.meta = map[string]string{"source": m.name, "group": "Lustre"}
|
2022-02-07 13:27:35 +01:00
|
|
|
defmatches := map[string]map[string]int{
|
|
|
|
"read_bytes": {"lustre_read_bytes": 6, "lustre_read_requests": 1},
|
|
|
|
"write_bytes": {"lustre_write_bytes": 6, "lustre_write_requests": 1},
|
|
|
|
"open": {"lustre_open": 1},
|
|
|
|
"close": {"lustre_close": 1},
|
|
|
|
"setattr": {"lustre_setattr": 1},
|
|
|
|
"getattr": {"lustre_getattr": 1},
|
|
|
|
"statfs": {"lustre_statfs": 1},
|
|
|
|
"inode_permission": {"lustre_inode_permission": 1}}
|
|
|
|
|
|
|
|
// Lustre file system statistics can only be queried by user root
|
2022-03-04 11:32:10 +01:00
|
|
|
if !m.config.Sudo {
|
|
|
|
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")
|
|
|
|
return err
|
|
|
|
}
|
2022-02-07 13:27:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-03 22:05:16 +01:00
|
|
|
p, err := exec.LookPath(m.config.LCtlCommand)
|
|
|
|
if err != nil {
|
|
|
|
p, err = exec.LookPath(LCTL_CMD)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
2021-10-04 15:23:43 +02:00
|
|
|
}
|
2022-02-03 22:05:16 +01:00
|
|
|
m.lctl = p
|
2022-03-03 13:02:00 +01:00
|
|
|
if m.config.Sudo {
|
|
|
|
p, err := exec.LookPath("sudo")
|
|
|
|
if err != nil {
|
|
|
|
m.sudoCmd = p
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 15:11:39 +01:00
|
|
|
|
2022-02-07 13:27:35 +01:00
|
|
|
devices := m.getDevices()
|
|
|
|
if len(devices) == 0 {
|
2022-02-03 22:05:16 +01:00
|
|
|
return errors.New("no metrics to collect")
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2022-02-07 13:27:35 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
m.init = true
|
|
|
|
return nil
|
2021-03-25 16:52:28 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *LustreCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
2021-11-25 15:11:39 +01:00
|
|
|
if !m.init {
|
|
|
|
return
|
|
|
|
}
|
2022-02-07 13:27:35 +01:00
|
|
|
for device, devData := range m.stats {
|
2022-02-03 22:05:16 +01:00
|
|
|
stats := m.getDeviceDataCommand(device)
|
2022-02-07 13:27:35 +01:00
|
|
|
processed := []string{}
|
2021-03-25 16:52:28 +01:00
|
|
|
|
2022-02-03 22:05:16 +01:00
|
|
|
for _, line := range stats {
|
2021-11-25 15:11:39 +01:00
|
|
|
lf := strings.Fields(line)
|
|
|
|
if len(lf) > 1 {
|
2022-02-07 13:27:35 +01:00
|
|
|
if fields, ok := m.matches[lf[0]]; ok {
|
|
|
|
for name, idx := range fields {
|
|
|
|
x, err := strconv.ParseInt(lf[idx], 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
value := x - devData[name]
|
|
|
|
devData[name] = x
|
|
|
|
if value < 0 {
|
|
|
|
value = 0
|
|
|
|
}
|
|
|
|
y, err := lp.New(name, m.tags, m.meta, map[string]interface{}{"value": value}, time.Now())
|
|
|
|
if err == nil {
|
|
|
|
y.AddTag("device", device)
|
|
|
|
if strings.Contains(name, "byte") {
|
|
|
|
y.AddMeta("unit", "Byte")
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
2022-02-07 13:27:35 +01:00
|
|
|
output <- y
|
|
|
|
if m.config.SendAllMetrics {
|
|
|
|
processed = append(processed, name)
|
2021-11-25 15:11:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 13:27:35 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 16:52:28 +01:00
|
|
|
}
|