Skip to content

Commit 41a78ee

Browse files
committed
Watch replication with multiple nodes
1 parent 9b113b2 commit 41a78ee

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

cmd/openldap_exporter/main.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,14 @@ func main() {
105105
},
106106
}
107107
app := &cli.App{
108-
Name: "openldap_exporter",
109-
Usage: "Export OpenLDAP metrics to Prometheus",
110-
Before: altsrc.InitInputSourceWithContext(flags, optionalYamlSourceFunc(config)),
111-
Version: exporter.GetVersion(),
112-
HideHelpCommand: true,
113-
Flags: flags,
114-
Action: runMain,
108+
Name: "openldap_exporter",
109+
Usage: "Export OpenLDAP metrics to Prometheus",
110+
Before: altsrc.InitInputSourceWithContext(flags, optionalYamlSourceFunc(config)),
111+
Version: exporter.GetVersion(),
112+
HideHelpCommand: true,
113+
Flags: flags,
114+
Action: runMain,
115+
SliceFlagSeparator: ";", // default ',' is already used inside ldap identifiers
115116
}
116117
if err := app.Run(os.Args); err != nil {
117118
log.WithError(err).Fatal("service failed")

scraper.go

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -172,38 +172,41 @@ func setValue(entries []*ldap.Entry, q *query) {
172172
}
173173
}
174174

175+
// parse value for replication and handle multiple values for contextCSN attribute
175176
func setReplicationValue(entries []*ldap.Entry, q *query) {
176177
for _, entry := range entries {
177-
val := entry.GetAttributeValue(q.searchAttr)
178-
if val == "" {
179-
// not every entry will have this attribute
180-
continue
181-
}
182-
fields := log.Fields{
183-
"filter": q.searchFilter,
184-
"attr": q.searchAttr,
185-
"value": val,
186-
}
187-
valueBuffer := strings.Split(val, "#")
188-
gt, err := time.Parse("20060102150405.999999Z", valueBuffer[0])
189-
if err != nil {
190-
log.WithFields(fields).WithError(err).Warn("unexpected gt value")
191-
continue
192-
}
193-
count, err := strconv.ParseFloat(valueBuffer[1], 64)
194-
if err != nil {
195-
log.WithFields(fields).WithError(err).Warn("unexpected count value")
196-
continue
197-
}
198-
sid := valueBuffer[2]
199-
mod, err := strconv.ParseFloat(valueBuffer[3], 64)
200-
if err != nil {
201-
log.WithFields(fields).WithError(err).Warn("unexpected mod value")
202-
continue
178+
attributeValues := entry.GetAttributeValues(q.searchAttr) // with multiple node on replications, attribute can have multiple value
179+
for _, context := range attributeValues {
180+
if context == "" {
181+
// not every entry will have this attribute
182+
continue
183+
}
184+
fields := log.Fields{
185+
"filter": q.searchFilter,
186+
"attr": q.searchAttr,
187+
"value": context,
188+
}
189+
valueBuffer := strings.Split(context, "#")
190+
gt, err := time.Parse("20060102150405.999999Z", valueBuffer[0])
191+
if err != nil {
192+
log.WithFields(fields).WithError(err).Warn("unexpected gt value")
193+
continue
194+
}
195+
count, err := strconv.ParseFloat(valueBuffer[1], 64)
196+
if err != nil {
197+
log.WithFields(fields).WithError(err).Warn("unexpected count value")
198+
continue
199+
}
200+
sid := valueBuffer[2]
201+
mod, err := strconv.ParseFloat(valueBuffer[3], 64)
202+
if err != nil {
203+
log.WithFields(fields).WithError(err).Warn("unexpected mod value")
204+
continue
205+
}
206+
q.metric.WithLabelValues(sid, "gt").Set(float64(gt.Unix()))
207+
q.metric.WithLabelValues(sid, "count").Set(count)
208+
q.metric.WithLabelValues(sid, "mod").Set(mod)
203209
}
204-
q.metric.WithLabelValues(sid, "gt").Set(float64(gt.Unix()))
205-
q.metric.WithLabelValues(sid, "count").Set(count)
206-
q.metric.WithLabelValues(sid, "mod").Set(mod)
207210
}
208211
}
209212

0 commit comments

Comments
 (0)