Add option to LustreCollector to call lctl with sudo

This commit is contained in:
Thomas Roehl 2022-03-03 13:02:00 +01:00
parent c61b8d2877
commit 276c00442a

View File

@ -22,6 +22,7 @@ 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"` SendAllMetrics bool `json:"send_all_metrics"`
Sudo bool `json:"use_sudo"`
} }
type LustreCollector struct { type LustreCollector struct {
@ -31,11 +32,17 @@ type LustreCollector struct {
stats map[string]map[string]int64 stats map[string]map[string]int64
config LustreCollectorConfig config LustreCollectorConfig
lctl string lctl string
sudoCmd string
} }
func (m *LustreCollector) getDeviceDataCommand(device string) []string { func (m *LustreCollector) getDeviceDataCommand(device string) []string {
var command *exec.Cmd
statsfile := fmt.Sprintf("llite.%s.stats", device) 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() command.Wait()
stdout, _ := command.Output() stdout, _ := command.Output()
return strings.Split(string(stdout), "\n") return strings.Split(string(stdout), "\n")
@ -136,6 +143,12 @@ func (m *LustreCollector) Init(config json.RawMessage) error {
} }
} }
m.lctl = p m.lctl = p
if m.config.Sudo {
p, err := exec.LookPath("sudo")
if err != nil {
m.sudoCmd = p
}
}
devices := m.getDevices() devices := m.getDevices()
if len(devices) == 0 { if len(devices) == 0 {