Skip to content

Commit 6141afa

Browse files
authored
Merge pull request #7 from stackhpc/bugfix/logs
Limit number of logs collected
2 parents 8a6aca3 + 7400129 commit 6141afa

File tree

9 files changed

+198
-103
lines changed

9 files changed

+198
-103
lines changed

README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,21 @@ problematic the collection of logs can be configured as follows:
3131
To disable log collection you can set:
3232

3333
```yaml
34-
collectlogs: false
34+
hosts:
35+
default:
36+
username: username
37+
password: password
38+
collectlogs: false
39+
```
40+
41+
or for a group:
42+
43+
```yaml
44+
groups:
45+
group1:
46+
username: username
47+
password: password
48+
collectlogs: false
3549
```
3650

3751
2) via the `collectlogs` query parameter
@@ -60,6 +74,47 @@ curl '127.0.0.1:9123/redfish?target=10.10.12.23&collectlogs=true'
6074

6175
The `collectlogs` query parameter can be included in Prometheus config.
6276

77+
78+
### Log collection count
79+
80+
To restrict the number of logs collected for all log services:
81+
82+
```
83+
hosts:
84+
default:
85+
username: username
86+
password: password
87+
collectlogs: true
88+
logcount:
89+
DEFAULT: 10
90+
```
91+
92+
For a particular log service, `Sel`:
93+
94+
```
95+
hosts:
96+
default:
97+
username: username
98+
password: password
99+
collectlogs: true
100+
logcount:
101+
Sel: 20
102+
```
103+
104+
Collect all logs for one service, `Sel` , but limit others:
105+
106+
```
107+
hosts:
108+
default:
109+
username: username
110+
password: password
111+
collectlogs: true
112+
logcount:
113+
DEFAULT: 10
114+
Sel: -1
115+
```
116+
117+
63118
## Building
64119

65120
To build the redfish_exporter executable run the command:

collector/chassis_collector.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"sync"
88

99
"github.com/apex/log"
10+
"github.com/jenningsloy318/redfish_exporter/common"
1011
"github.com/prometheus/client_golang/prometheus"
11-
"github.com/stmcginnis/gofish"
1212
"github.com/stmcginnis/gofish/redfish"
1313
)
1414

@@ -33,9 +33,8 @@ var (
3333

3434
// ChassisCollector implements the prometheus.Collector.
3535
type ChassisCollector struct {
36-
redfishClient *gofish.APIClient
36+
Ctx *common.CollectionContext
3737
metrics map[string]Metric
38-
collectLogs bool
3938
collectorScrapeStatus *prometheus.GaugeVec
4039
Log *log.Entry
4140
}
@@ -91,13 +90,12 @@ func createChassisMetricMap() map[string]Metric {
9190
}
9291

9392
// NewChassisCollector returns a collector that collecting chassis statistics
94-
func NewChassisCollector(redfishClient *gofish.APIClient, collectLogs bool, logger *log.Entry) *ChassisCollector {
93+
func NewChassisCollector(ctx *common.CollectionContext, logger *log.Entry) *ChassisCollector {
9594
// get service from redfish client
9695

9796
return &ChassisCollector{
98-
redfishClient: redfishClient,
99-
metrics: chassisMetrics,
100-
collectLogs: collectLogs,
97+
metrics: chassisMetrics,
98+
Ctx: ctx,
10199
Log: logger.WithFields(log.Fields{
102100
"collector": "ChassisCollector",
103101
}),
@@ -124,8 +122,8 @@ func (c *ChassisCollector) Describe(ch chan<- *prometheus.Desc) {
124122
// Collect implemented prometheus.Collector
125123
func (c *ChassisCollector) Collect(ch chan<- prometheus.Metric) {
126124
collectorLogContext := c.Log
127-
collectLogs := c.collectLogs
128-
service := c.redfishClient.Service
125+
collectLogs := c.Ctx.CollectLogs
126+
service := c.Ctx.RedfishClient.Service
129127

130128
// get a list of chassis from service
131129
if chassises, err := service.Chassis(); err != nil {
@@ -252,7 +250,7 @@ func (c *ChassisCollector) Collect(ch chan<- prometheus.Metric) {
252250
wg6.Add(len(logServices))
253251

254252
for _, logService := range logServices {
255-
if err = parseLogService(ch, chassisMetrics, ChassisSubsystem, chassisID, logService, wg6); err != nil {
253+
if err = parseLogService(ch, chassisMetrics, c.Ctx, chassisLogContext, ChassisSubsystem, chassisID, logService, wg6); err != nil {
256254
chassisLogContext.WithField("operation", "chassis.LogServices()").WithError(err).Error("error getting log entries from log service")
257255
}
258256
}

collector/common_collector.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"fmt"
55
"sync"
66

7+
"github.com/apex/log"
8+
redfish_common "github.com/jenningsloy318/redfish_exporter/common"
79
"github.com/prometheus/client_golang/prometheus"
10+
"github.com/stmcginnis/gofish/common"
811
"github.com/stmcginnis/gofish/redfish"
912
)
1013

@@ -33,7 +36,7 @@ func addToMetricMap(metricMap map[string]Metric, subsystem, name, help string, v
3336
}
3437
}
3538

36-
func parseLogService(ch chan<- prometheus.Metric, metrics map[string]Metric, subsystem, collectorID string, logService *redfish.LogService, wg *sync.WaitGroup) (err error) {
39+
func parseLogService(ch chan<- prometheus.Metric, metrics map[string]Metric, ctx *redfish_common.CollectionContext, logger *log.Entry, subsystem, collectorID string, logService *redfish.LogService, wg *sync.WaitGroup) (err error) {
3740
defer wg.Done()
3841
logServiceName := logService.Name
3942
logServiceID := logService.ID
@@ -50,8 +53,24 @@ func parseLogService(ch chan<- prometheus.Metric, metrics map[string]Metric, sub
5053
if logServiceHealthStateValue, ok := parseCommonStatusHealth(logServiceHealthState); ok {
5154
ch <- prometheus.MustNewConstMetric(metrics[fmt.Sprintf("%s_%s", subsystem, "log_service_health_state")].desc, prometheus.GaugeValue, logServiceHealthStateValue, logServiceLabelValues...)
5255
}
56+
var (
57+
logEntries []*redfish.LogEntry
58+
)
59+
logCount, ok := ctx.LogCount[logServiceID]
60+
if !ok {
61+
logCount, ok = ctx.LogCount["DEFAULT"]
62+
if !ok {
63+
logCount = -1
64+
}
65+
}
66+
67+
logger.WithField("operation", "parseLogService").Info(fmt.Sprintf("logServiceID=%s, logcount=%d", logServiceID, logCount))
5368

54-
logEntries, err := logService.Entries()
69+
if logCount > 0 {
70+
logEntries, err = logService.FilteredEntries(common.WithTop(logCount))
71+
} else {
72+
logEntries, err = logService.Entries()
73+
}
5574
if err != nil {
5675
return
5776
}
@@ -65,7 +84,6 @@ func parseLogService(ch chan<- prometheus.Metric, metrics map[string]Metric, sub
6584
continue
6685
}
6786
go parseLogEntry(ch, metrics[fmt.Sprintf("%s_%s", subsystem, "log_entry_severity_state")].desc, collectorID, logServiceName, logServiceID, logEntry, wg2)
68-
6987
processed[logEntry.MessageID] = true
7088
}
7189
return

collector/manager_collector.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"sync"
66

77
"github.com/apex/log"
8+
"github.com/jenningsloy318/redfish_exporter/common"
89
"github.com/prometheus/client_golang/prometheus"
9-
"github.com/stmcginnis/gofish"
1010
)
1111

1212
// ManagerSubmanager is the manager subsystem
@@ -22,9 +22,8 @@ var (
2222

2323
// ManagerCollector implements the prometheus.Collector.
2424
type ManagerCollector struct {
25-
redfishClient *gofish.APIClient
25+
Ctx *common.CollectionContext
2626
metrics map[string]Metric
27-
collectLogs bool
2827
collectorScrapeStatus *prometheus.GaugeVec
2928
Log *log.Entry
3029
}
@@ -43,11 +42,10 @@ func createManagerMetricMap() map[string]Metric {
4342
}
4443

4544
// NewManagerCollector returns a collector that collecting memory statistics
46-
func NewManagerCollector(redfishClient *gofish.APIClient, collectLogs bool, logger *log.Entry) *ManagerCollector {
45+
func NewManagerCollector(ctx *common.CollectionContext, logger *log.Entry) *ManagerCollector {
4746
return &ManagerCollector{
48-
redfishClient: redfishClient,
49-
metrics: managerMetrics,
50-
collectLogs: collectLogs,
47+
metrics: managerMetrics,
48+
Ctx: ctx,
5149
Log: logger.WithFields(log.Fields{
5250
"collector": "ManagerCollector",
5351
}),
@@ -74,9 +72,9 @@ func (m *ManagerCollector) Describe(ch chan<- *prometheus.Desc) {
7472
// Collect implemented prometheus.Collector
7573
func (m *ManagerCollector) Collect(ch chan<- prometheus.Metric) {
7674
collectorLogContext := m.Log
77-
collectLogs := m.collectLogs
75+
collectLogs := m.Ctx.CollectLogs
7876
//get service
79-
service := m.redfishClient.Service
77+
service := m.Ctx.RedfishClient.Service
8078

8179
// get a list of managers from service
8280
if managers, err := service.Managers(); err != nil {
@@ -118,7 +116,7 @@ func (m *ManagerCollector) Collect(ch chan<- prometheus.Metric) {
118116
wg.Add(len(logServices))
119117

120118
for _, logService := range logServices {
121-
if err = parseLogService(ch, managerMetrics, ManagerSubmanager, ManagerID, logService, wg); err != nil {
119+
if err = parseLogService(ch, managerMetrics, m.Ctx, managerLogContext, ManagerSubmanager, ManagerID, logService, wg); err != nil {
122120
managerLogContext.WithField("operation", "manager.LogServices()").WithError(err).Error("error getting log entries from log service")
123121
}
124122
}

collector/redfish_collector.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package collector
22

33
import (
44
"bytes"
5-
"fmt"
65
"sync"
76
"time"
87

9-
"github.com/apex/log"
8+
alog "github.com/apex/log"
9+
"github.com/jenningsloy318/redfish_exporter/common"
1010
"github.com/prometheus/client_golang/prometheus"
1111
gofish "github.com/stmcginnis/gofish"
1212
gofishcommon "github.com/stmcginnis/gofish/common"
@@ -40,22 +40,16 @@ type RedfishCollector struct {
4040
}
4141

4242
// NewRedfishCollector return RedfishCollector
43-
func NewRedfishCollector(host string, username string, password string, collectLogs bool, logger *log.Entry) *RedfishCollector {
43+
func NewRedfishCollector(ctx *common.CollectionContext, logger *alog.Entry) *RedfishCollector {
4444
var collectors map[string]prometheus.Collector
45-
collectorLogCtx := logger
46-
redfishClient, err := newRedfishClient(host, username, password)
47-
if err != nil {
48-
collectorLogCtx.WithError(err).Error("error creating redfish client")
49-
} else {
50-
chassisCollector := NewChassisCollector(redfishClient, collectLogs, collectorLogCtx)
51-
systemCollector := NewSystemCollector(redfishClient, collectLogs, collectorLogCtx)
52-
managerCollector := NewManagerCollector(redfishClient, collectLogs, collectorLogCtx)
45+
chassisCollector := NewChassisCollector(ctx, logger)
46+
systemCollector := NewSystemCollector(ctx, logger)
47+
managerCollector := NewManagerCollector(ctx, logger)
5348

54-
collectors = map[string]prometheus.Collector{"chassis": chassisCollector, "system": systemCollector, "manager": managerCollector}
55-
}
49+
collectors = map[string]prometheus.Collector{"chassis": chassisCollector, "system": systemCollector, "manager": managerCollector}
5650

5751
return &RedfishCollector{
58-
redfishClient: redfishClient,
52+
redfishClient: ctx.RedfishClient,
5953
collectors: collectors,
6054
redfishUp: prometheus.NewGauge(
6155
prometheus.GaugeOpts{
@@ -101,23 +95,6 @@ func (r *RedfishCollector) Collect(ch chan<- prometheus.Metric) {
10195
ch <- prometheus.MustNewConstMetric(totalScrapeDurationDesc, prometheus.GaugeValue, time.Since(scrapeTime).Seconds())
10296
}
10397

104-
func newRedfishClient(host string, username string, password string) (*gofish.APIClient, error) {
105-
106-
url := fmt.Sprintf("https://%s", host)
107-
108-
config := gofish.ClientConfig{
109-
Endpoint: url,
110-
Username: username,
111-
Password: password,
112-
Insecure: true,
113-
}
114-
redfishClient, err := gofish.Connect(config)
115-
if err != nil {
116-
return nil, err
117-
}
118-
return redfishClient, nil
119-
}
120-
12198
func parseCommonStatusHealth(status gofishcommon.Health) (float64, bool) {
12299
if bytes.Equal([]byte(status), []byte("OK")) {
123100
return float64(1), true

collector/system_collector.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"sync"
66

77
"github.com/apex/log"
8+
"github.com/jenningsloy318/redfish_exporter/common"
89
"github.com/prometheus/client_golang/prometheus"
9-
"github.com/stmcginnis/gofish"
1010
"github.com/stmcginnis/gofish/redfish"
1111
)
1212

@@ -33,9 +33,8 @@ var (
3333

3434
// SystemCollector implements the prometheus.Collector.
3535
type SystemCollector struct {
36-
redfishClient *gofish.APIClient
37-
metrics map[string]Metric
38-
collectLogs bool
36+
Ctx *common.CollectionContext
37+
metrics map[string]Metric
3938
prometheus.Collector
4039
collectorScrapeStatus *prometheus.GaugeVec
4140
Log *log.Entry
@@ -101,11 +100,10 @@ func createSystemMetricMap() map[string]Metric {
101100
}
102101

103102
// NewSystemCollector returns a collector that collecting memory statistics
104-
func NewSystemCollector(redfishClient *gofish.APIClient, collectLogs bool, logger *log.Entry) *SystemCollector {
103+
func NewSystemCollector(ctx *common.CollectionContext, logger *log.Entry) *SystemCollector {
105104
return &SystemCollector{
106-
redfishClient: redfishClient,
107-
metrics: systemMetrics,
108-
collectLogs: collectLogs,
105+
metrics: systemMetrics,
106+
Ctx: ctx,
109107
Log: logger.WithFields(log.Fields{
110108
"collector": "SystemCollector",
111109
}),
@@ -131,9 +129,9 @@ func (s *SystemCollector) Describe(ch chan<- *prometheus.Desc) {
131129
// Collect implements prometheus.Collector.
132130
func (s *SystemCollector) Collect(ch chan<- prometheus.Metric) {
133131
collectorLogContext := s.Log
134-
collectLogs := s.collectLogs
132+
collectLogs := s.Ctx.CollectLogs
135133
//get service
136-
service := s.redfishClient.Service
134+
service := s.Ctx.RedfishClient.Service
137135

138136
// get a list of systems from service
139137
if systems, err := service.Systems(); err != nil {
@@ -403,7 +401,7 @@ func (s *SystemCollector) Collect(ch chan<- prometheus.Metric) {
403401
wg10.Add(len(logServices))
404402

405403
for _, logService := range logServices {
406-
if err = parseLogService(ch, systemMetrics, SystemSubsystem, SystemID, logService, wg10); err != nil {
404+
if err = parseLogService(ch, systemMetrics, s.Ctx, systemLogContext, SystemSubsystem, SystemID, logService, wg10); err != nil {
407405
systemLogContext.WithField("operation", "system.LogServices()").WithError(err).Error("error getting log entries from log service")
408406
}
409407
}

0 commit comments

Comments
 (0)