From 276c00442a18fc5685dff03be8149a89b87f4e41 Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Thu, 3 Mar 2022 13:02:00 +0100 Subject: [PATCH] Add option to LustreCollector to call lctl with sudo --- collectors/lustreMetric.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/collectors/lustreMetric.go b/collectors/lustreMetric.go index 6d6fe26..f98d746 100644 --- a/collectors/lustreMetric.go +++ b/collectors/lustreMetric.go @@ -22,6 +22,7 @@ type LustreCollectorConfig struct { LCtlCommand string `json:"lctl_command"` ExcludeMetrics []string `json:"exclude_metrics"` SendAllMetrics bool `json:"send_all_metrics"` + Sudo bool `json:"use_sudo"` } type LustreCollector struct { @@ -31,11 +32,17 @@ type LustreCollector struct { stats map[string]map[string]int64 config LustreCollectorConfig lctl string + sudoCmd string } func (m *LustreCollector) getDeviceDataCommand(device string) []string { + var command *exec.Cmd statsfile := fmt.Sprintf("llite.%s.stats", device) - command := exec.Command(m.lctl, LCTL_OPTION, statsfile) + if m.config.Sudo { + command = exec.Command(m.sudoCmd, m.lctl, LCTL_OPTION, statsfile) + } else { + command = exec.Command(m.lctl, LCTL_OPTION, statsfile) + } command.Wait() stdout, _ := command.Output() return strings.Split(string(stdout), "\n") @@ -136,6 +143,12 @@ func (m *LustreCollector) Init(config json.RawMessage) error { } } m.lctl = p + if m.config.Sudo { + p, err := exec.LookPath("sudo") + if err != nil { + m.sudoCmd = p + } + } devices := m.getDevices() if len(devices) == 0 {