From 10fa1a447510c8f40b905c4ff2a69dc4c2b29a2b Mon Sep 17 00:00:00 2001 From: Matt Anson Date: Wed, 7 Aug 2024 12:06:19 +0100 Subject: [PATCH] WIP: Add config option to not collect logs --- collector/chassis_collector.go | 4 ++-- collector/manager_collector.go | 2 +- collector/redfish_collector.go | 8 ++++---- collector/system_collector.go | 12 +++++++++--- config.go | 18 ++++++++++++++---- config.yml.example | 1 + main.go | 2 +- 7 files changed, 32 insertions(+), 15 deletions(-) diff --git a/collector/chassis_collector.go b/collector/chassis_collector.go index 5392958..1d006b6 100755 --- a/collector/chassis_collector.go +++ b/collector/chassis_collector.go @@ -90,7 +90,7 @@ func createChassisMetricMap() map[string]Metric { } // NewChassisCollector returns a collector that collecting chassis statistics -func NewChassisCollector(redfishClient *gofish.APIClient, logger *log.Entry) *ChassisCollector { +func NewChassisCollector(redfishClient *gofish.APIClient, collectLogs bool, logger *log.Entry) *ChassisCollector { // get service from redfish client return &ChassisCollector{ @@ -267,7 +267,7 @@ func parseChassisTemperature(ch chan<- prometheus.Metric, chassisID string, chas chassisTemperatureStatus := chassisTemperature.Status chassisTemperatureLabelvalues := []string{"temperature", chassisID, chassisTemperatureSensorName, chassisTemperatureSensorID} - chassisTemperatureStatusHealth :=chassisTemperatureStatus.Health + chassisTemperatureStatusHealth := chassisTemperatureStatus.Health if chassisTemperatureStatusHealthValue, ok := parseCommonStatusHealth(chassisTemperatureStatusHealth); ok { ch <- prometheus.MustNewConstMetric(chassisMetrics["chassis_temperature_sensor_health"].desc, prometheus.GaugeValue, chassisTemperatureStatusHealthValue, chassisTemperatureLabelvalues...) } diff --git a/collector/manager_collector.go b/collector/manager_collector.go index 4717811..2de0fc6 100755 --- a/collector/manager_collector.go +++ b/collector/manager_collector.go @@ -42,7 +42,7 @@ func createManagerMetricMap() map[string]Metric { } // NewManagerCollector returns a collector that collecting memory statistics -func NewManagerCollector(redfishClient *gofish.APIClient, logger *log.Entry) *ManagerCollector { +func NewManagerCollector(redfishClient *gofish.APIClient, collectLogs bool, logger *log.Entry) *ManagerCollector { return &ManagerCollector{ redfishClient: redfishClient, metrics: managerMetrics, diff --git a/collector/redfish_collector.go b/collector/redfish_collector.go index 0dc5f9c..cd7dfed 100755 --- a/collector/redfish_collector.go +++ b/collector/redfish_collector.go @@ -40,16 +40,16 @@ type RedfishCollector struct { } // NewRedfishCollector return RedfishCollector -func NewRedfishCollector(host string, username string, password string, logger *log.Entry) *RedfishCollector { +func NewRedfishCollector(host string, username string, password string, collectLogs bool, logger *log.Entry) *RedfishCollector { var collectors map[string]prometheus.Collector collectorLogCtx := logger redfishClient, err := newRedfishClient(host, username, password) if err != nil { collectorLogCtx.WithError(err).Error("error creating redfish client") } else { - chassisCollector := NewChassisCollector(redfishClient, collectorLogCtx) - systemCollector := NewSystemCollector(redfishClient, collectorLogCtx) - managerCollector := NewManagerCollector(redfishClient, collectorLogCtx) + chassisCollector := NewChassisCollector(redfishClient, collectLogs, collectorLogCtx) + systemCollector := NewSystemCollector(redfishClient, collectLogs, collectorLogCtx) + managerCollector := NewManagerCollector(redfishClient, collectLogs, collectorLogCtx) collectors = map[string]prometheus.Collector{"chassis": chassisCollector, "system": systemCollector, "manager": managerCollector} } diff --git a/collector/system_collector.go b/collector/system_collector.go index c2a1f26..ea0e4ab 100755 --- a/collector/system_collector.go +++ b/collector/system_collector.go @@ -35,6 +35,7 @@ var ( type SystemCollector struct { redfishClient *gofish.APIClient metrics map[string]Metric + collectLogs bool prometheus.Collector collectorScrapeStatus *prometheus.GaugeVec Log *log.Entry @@ -100,10 +101,11 @@ func createSystemMetricMap() map[string]Metric { } // NewSystemCollector returns a collector that collecting memory statistics -func NewSystemCollector(redfishClient *gofish.APIClient, logger *log.Entry) *SystemCollector { +func NewSystemCollector(redfishClient *gofish.APIClient, collectLogs bool, logger *log.Entry) *SystemCollector { return &SystemCollector{ redfishClient: redfishClient, metrics: systemMetrics, + collectLogs: collectLogs, Log: logger.WithFields(log.Fields{ "collector": "SystemCollector", }), @@ -129,6 +131,7 @@ func (s *SystemCollector) Describe(ch chan<- *prometheus.Desc) { // Collect implements prometheus.Collector. func (s *SystemCollector) Collect(ch chan<- prometheus.Metric) { collectorLogContext := s.Log + collectLogs := s.collectLogs //get service service := s.redfishClient.Service @@ -177,7 +180,7 @@ func (s *SystemCollector) Collect(ch chan<- prometheus.Metric) { if systemTotalMemoryHealthStateValue, ok := parseCommonStatusHealth(systemTotalMemoryHealthState); ok { ch <- prometheus.MustNewConstMetric(s.metrics["system_total_memory_health_state"].desc, prometheus.GaugeValue, systemTotalMemoryHealthStateValue, systemLabelValues...) } - + // get system OdataID //systemOdataID := system.ODataID @@ -357,7 +360,10 @@ func (s *SystemCollector) Collect(ch chan<- prometheus.Metric) { } // process log services - logServices, err := system.LogServices() + logServices := nil + if collectLogs { + logServices, err := system.LogServices() + } if err != nil { systemLogContext.WithField("operation", "system.LogServices()").WithError(err).Error("error getting log services from system") } else if logServices == nil { diff --git a/config.go b/config.go index ea56835..adf0e44 100755 --- a/config.go +++ b/config.go @@ -9,9 +9,10 @@ import ( ) type Config struct { - Hosts map[string]HostConfig `yaml:"hosts"` - Groups map[string]HostConfig `yaml:"groups"` - Loglevel string `yaml:"loglevel"` + Hosts map[string]HostConfig `yaml:"hosts"` + Groups map[string]HostConfig `yaml:"groups"` + Loglevel string `yaml:"loglevel"` + Collectlogs *bool `yaml:"collectlogs,omitempty"` } type SafeConfig struct { @@ -71,7 +72,7 @@ func (sc *SafeConfig) HostConfigForGroup(group string) (*HostConfig, error) { return &HostConfig{}, fmt.Errorf("no credentials found for group %s", group) } -func (sc *SafeConfig) AppLogLevel() (string) { +func (sc *SafeConfig) AppLogLevel() string { sc.Lock() defer sc.Unlock() logLevel := sc.C.Loglevel @@ -80,3 +81,12 @@ func (sc *SafeConfig) AppLogLevel() (string) { } return "info" } + +func (sc *SafeConfig) CollectLogs() bool { + sc.Lock() + defer sc.Unlock() + if sc.C.Collectlogs == nil { + return true + } + return *sc.C.Collectlogs +} diff --git a/config.yml.example b/config.yml.example index 7ae7693..13d69ba 100644 --- a/config.yml.example +++ b/config.yml.example @@ -11,3 +11,4 @@ groups: password: group1_pass # loglevel can be one of "debug", "info", "warn", "error", or "fatal" # loglevel: info +collectlogs: true diff --git a/main.go b/main.go index b6b6c28..f62cb55 100755 --- a/main.go +++ b/main.go @@ -118,7 +118,7 @@ func metricsHandler() http.HandlerFunc { } } - collector := collector.NewRedfishCollector(target, hostConfig.Username, hostConfig.Password, targetLoggerCtx) + collector := collector.NewRedfishCollector(target, hostConfig.Username, hostConfig.Password, sc.CollectLogs(), targetLoggerCtx) registry.MustRegister(collector) gatherers := prometheus.Gatherers{ prometheus.DefaultGatherer,