| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package openldap |
| 4 | |
| 5 | import ( |
| 6 | "strconv" |
| 7 | |
| 8 | "github.com/go-ldap/ldap/v3" |
| 9 | ) |
| 10 | |
| 11 | const ( |
| 12 | attrMonitorCounter = "monitorCounter" |
| 13 | ) |
| 14 | |
| 15 | func (c *Collector) collectMonitorCounters(mx map[string]int64) error { |
| 16 | req := newLdapMonitorCountersSearchRequest() |
| 17 | |
| 18 | dnMetricMap := map[string]string{ |
| 19 | "cn=Current,cn=Connections,cn=Monitor": "current_connections", |
| 20 | "cn=Total,cn=Connections,cn=Monitor": "total_connections", |
| 21 | "cn=Bytes,cn=Statistics,cn=Monitor": "bytes_sent", |
| 22 | "cn=Referrals,cn=Statistics,cn=Monitor": "referrals_sent", |
| 23 | "cn=Entries,cn=Statistics,cn=Monitor": "entries_sent", |
| 24 | "cn=Write,cn=Waiters,cn=Monitor": "write_waiters", |
| 25 | "cn=Read,cn=Waiters,cn=Monitor": "read_waiters", |
| 26 | } |
| 27 | |
| 28 | return c.doSearchRequest(req, func(entry *ldap.Entry) { |
| 29 | metric := dnMetricMap[entry.DN] |
| 30 | if metric == "" { |
| 31 | c.Debugf("skipping entry '%s'", entry.DN) |
| 32 | return |
| 33 | } |
| 34 | |
| 35 | s := entry.GetAttributeValue(attrMonitorCounter) |
| 36 | if s == "" { |
| 37 | c.Debugf("entry '%s' does not have attribute '%s'", entry.DN, attrMonitorCounter) |
| 38 | return |
| 39 | } |
| 40 | |
| 41 | v, err := strconv.ParseInt(s, 10, 64) |
| 42 | if err != nil { |
| 43 | c.Debugf("failed to parse entry '%s' value '%s': %v", entry.DN, s, err) |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | mx[metric] = v |
| 48 | }) |
| 49 | } |
| 50 | |
| 51 | func newLdapMonitorCountersSearchRequest() *ldap.SearchRequest { |
| 52 | return ldap.NewSearchRequest( |
| 53 | "cn=Monitor", |
| 54 | ldap.ScopeWholeSubtree, |
| 55 | ldap.NeverDerefAliases, |
| 56 | 0, |
| 57 | 0, |
| 58 | false, |
| 59 | "(objectclass=monitorCounterObject)", |
| 60 | []string{attrMonitorCounter}, |
| 61 | nil, |
| 62 | ) |
| 63 | } |