Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Commit

Permalink
add counters for bind & dial ldap op success
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcz committed Aug 13, 2022
1 parent 286817a commit 675f29b
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ var (
},
[]string{"dn"},
)
bindCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: "openldap",
Name: "bind",
Help: "successful vs unsuccessful ldap bind attempts",
},
[]string{"result"},
)
dialCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: "openldap",
Name: "dial",
Help: "successful vs unsuccessful ldap dial attempts",
},
[]string{"result"},
)
scrapeCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: "openldap",
Expand Down Expand Up @@ -116,6 +132,8 @@ func init() {
monitorOperationGauge,
monitorReplicationGauge,
scrapeCounter,
bindCounter,
dialCounter,
)
}

Expand Down Expand Up @@ -213,45 +231,41 @@ func (s *Scraper) Start(ctx context.Context) {
for {
select {
case <-ticker.C:
s.runOnce()
s.scrape()
case <-ctx.Done():
return
}
}
}

func (s *Scraper) runOnce() {
result := "fail"
if s.scrape() {
result = "ok"
}
scrapeCounter.WithLabelValues(result).Inc()
}

func (s *Scraper) scrape() bool {
func (s *Scraper) scrape() {
conn, err := ldap.Dial(s.Net, s.Addr)
if err != nil {
s.log.WithError(err).Error("dial failed")
return false
dialCounter.WithLabelValues("fail").Inc()
return
}
dialCounter.WithLabelValues("ok").Inc()
defer conn.Close()

if s.User != "" && s.Pass != "" {
err = conn.Bind(s.User, s.Pass)
if err != nil {
s.log.WithError(err).Error("bind failed")
return false
bindCounter.WithLabelValues("fail").Inc()
return
}
bindCounter.WithLabelValues("fail").Inc()
}

ret := true
scrapeRes := "ok"
for _, q := range queries {
if err := scrapeQuery(conn, q); err != nil {
if err = scrapeQuery(conn, q); err != nil {
s.log.WithError(err).WithField("filter", q.searchFilter).Warn("query failed")
ret = false
scrapeRes = "fail"
}
}
return ret
scrapeCounter.WithLabelValues(scrapeRes).Inc()
}

func scrapeQuery(conn *ldap.Conn, q *query) error {
Expand Down

0 comments on commit 675f29b

Please sign in to comment.