From 534775f13a9140b8f3bd573e9c39dd5dd49f0780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ch=C3=A1bek?= Date: Wed, 20 May 2020 14:14:43 +0200 Subject: [PATCH] Add new metric config_lan_mode --- collector.go | 42 ++++++++++++++++++++++++++++++++++++++++++ config.go | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/collector.go b/collector.go index a557e77..1ccdd51 100644 --- a/collector.go +++ b/collector.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "encoding/csv" "encoding/hex" + "errors" "fmt" "math" "os" @@ -189,6 +190,13 @@ var ( nil, nil, ) + + lanModeDesc = prometheus.NewDesc( + prometheus.BuildFQName(namespace, "config", "lan_mode"), + "Returns configured LAN mode.", + nil, + nil, + ) ) func pipeName() string { @@ -404,6 +412,7 @@ func (c collector) Describe(ch chan<- *prometheus.Desc) { ch <- selFreeSpaceDesc ch <- upDesc ch <- durationDesc + ch <- lanModeDesc } func collectTypedSensor(ch chan<- prometheus.Metric, desc, stateDesc *prometheus.Desc, state float64, data sensorData) { @@ -491,6 +500,37 @@ func collectMonitoring(ch chan<- prometheus.Metric, target ipmiTarget) (int, err return 1, nil } +func collectSmLanMode(ch chan<- prometheus.Metric, target ipmiTarget) (int, error) { + output, err := freeipmiOutput("ipmi-raw", target, "0x0", "0x30", "0x70", "0x0c", "0") + if err != nil { + log.Errorf("Failed to collect sm-lan-mode data from %s: %s", targetName(target.host), err) + return 0, err + } + + strOutput := strings.Trim(string(output), " \r\n") + if !strings.HasPrefix(strOutput, "rcvd: ") { + log.Errorf("Unexpected output of ipmi-raw from %s: %s", targetName(target.host), strOutput) + return 0, errors.New("unexpected output") + } + + octects := strings.Split(strOutput[6:], " ") + if len(octects) != 3 { + log.Errorf("Unexpected number of octects of ipmi-raw from %s: %+v", targetName(target.host), octects) + return 0, errors.New("unexpected number of octects") + } + + switch octects[2] { + case "00", "01", "02": + value, _ := strconv.Atoi(octects[2]) + ch <- prometheus.MustNewConstMetric(lanModeDesc, prometheus.GaugeValue, float64(value)) + default: + log.Errorf("Unexpected lan mode status (ipmi-raw) from %s: %+v", targetName(target.host), octects[2]) + return 0, errors.New("unexpected lan mode status") + } + + return 1, nil +} + func collectDCMI(ch chan<- prometheus.Metric, target ipmiTarget) (int, error) { output, err := ipmiDCMIOutput(target) if err != nil { @@ -617,6 +657,8 @@ func (c collector) Collect(ch chan<- prometheus.Metric) { switch collector { case "ipmi": up, _ = collectMonitoring(ch, target) + case "sm-lan-mode": + up, _ = collectSmLanMode(ch, target) case "dcmi": up, _ = collectDCMI(ch, target) case "bmc": diff --git a/config.go b/config.go index 09c4afd..e7fbc71 100644 --- a/config.go +++ b/config.go @@ -79,7 +79,7 @@ func (s *IPMIConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { return err } for _, c := range s.Collectors { - if !(c == "ipmi" || c == "dcmi" || c == "bmc" || c == "chassis" || c == "sel") { + if !(c == "ipmi" || c == "sm-lan-mode" || c == "dcmi" || c == "bmc" || c == "chassis" || c == "sel") { return fmt.Errorf("unknown collector name: %s", c) } }