| 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 | attrMonitorOpInitiated = "monitorOpInitiated" |
| 13 | attrMonitorOpCompleted = "monitorOpCompleted" |
| 14 | ) |
| 15 | |
| 16 | func (c *Collector) collectOperations(mx map[string]int64) error { |
| 17 | req := newLdapOperationsSearchRequest() |
| 18 | |
| 19 | dnMetricMap := map[string]string{ |
| 20 | "cn=Bind,cn=Operations,cn=Monitor": "bind_operations", |
| 21 | "cn=Unbind,cn=Operations,cn=Monitor": "unbind_operations", |
| 22 | "cn=Add,cn=Operations,cn=Monitor": "add_operations", |
| 23 | "cn=Delete,cn=Operations,cn=Monitor": "delete_operations", |
| 24 | "cn=Modify,cn=Operations,cn=Monitor": "modify_operations", |
| 25 | "cn=Compare,cn=Operations,cn=Monitor": "compare_operations", |
| 26 | "cn=Search,cn=Operations,cn=Monitor": "search_operations", |
| 27 | } |
| 28 | |
| 29 | return c.doSearchRequest(req, func(entry *ldap.Entry) { |
| 30 | metric := dnMetricMap[entry.DN] |
| 31 | if metric == "" { |
| 32 | c.Debugf("skipping entry '%s'", entry.DN) |
| 33 | return |
| 34 | } |
| 35 | |
| 36 | attrs := map[string]string{ |
| 37 | "initiated": attrMonitorOpInitiated, |
| 38 | "completed": attrMonitorOpCompleted, |
| 39 | } |
| 40 | |
| 41 | for prefix, attr := range attrs { |
| 42 | s := entry.GetAttributeValue(attr) |
| 43 | if s == "" { |
| 44 | c.Debugf("entry '%s' does not have attribute '%s'", entry.DN, attr) |
| 45 | continue |
| 46 | } |
| 47 | v, err := strconv.ParseInt(s, 10, 64) |
| 48 | if err != nil { |
| 49 | c.Debugf("failed to parse entry '%s' value '%s': %v", entry.DN, s, err) |
| 50 | continue |
| 51 | } |
| 52 | |
| 53 | mx[prefix+"_"+metric] = v |
| 54 | mx[prefix+"_operations"] += v |
| 55 | } |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | func newLdapOperationsSearchRequest() *ldap.SearchRequest { |
| 60 | return ldap.NewSearchRequest( |
| 61 | "cn=Operations,cn=Monitor", |
| 62 | ldap.ScopeWholeSubtree, |
| 63 | ldap.NeverDerefAliases, |
| 64 | 0, |
| 65 | 0, |
| 66 | false, |
| 67 | "(objectclass=monitorOperation)", |
| 68 | []string{attrMonitorOpInitiated, attrMonitorOpCompleted}, |
| 69 | nil, |
| 70 | ) |
| 71 | } |