forked from tomcz/openldap_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.go
192 lines (171 loc) · 4.58 KB
/
scraper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package openldap_exporter
import (
"fmt"
"log"
"strconv"
"crypto/tls"
"github.com/hashicorp/go-multierror"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/ldap.v2"
)
const (
baseDN = "cn=Monitor"
opsBaseDN = "cn=Operations,cn=Monitor"
monitorCounterObject = "monitorCounterObject"
monitorCounter = "monitorCounter"
monitoredObject = "monitoredObject"
monitoredInfo = "monitoredInfo"
monitorOperation = "monitorOperation"
monitorOpCompleted = "monitorOpCompleted"
)
type query struct {
baseDN string
searchFilter string
searchAttr string
metric *prometheus.GaugeVec
}
var (
monitoredObjectGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: "openldap",
Name: "monitored_object",
Help: baseDN + " " + objectClass(monitoredObject) + " " + monitoredInfo,
},
[]string{"dn"},
)
monitorCounterObjectGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: "openldap",
Name: "monitor_counter_object",
Help: baseDN + " " + objectClass(monitorCounterObject) + " " + monitorCounter,
},
[]string{"dn"},
)
monitorOperationGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: "openldap",
Name: "monitor_operation",
Help: opsBaseDN + " " + objectClass(monitorOperation) + " " + monitorOpCompleted,
},
[]string{"dn"},
)
scrapeCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: "openldap",
Name: "scrape",
Help: "successful vs unsuccessful ldap scrape attempts",
},
[]string{"result"},
)
queries = []query{
{
baseDN: baseDN,
searchFilter: objectClass(monitoredObject),
searchAttr: monitoredInfo,
metric: monitoredObjectGauge,
},
{
baseDN: baseDN,
searchFilter: objectClass(monitorCounterObject),
searchAttr: monitorCounter,
metric: monitorCounterObjectGauge,
},
{
baseDN: opsBaseDN,
searchFilter: objectClass(monitorOperation),
searchAttr: monitorOpCompleted,
metric: monitorOperationGauge,
},
}
)
func init() {
prometheus.MustRegister(
monitoredObjectGauge,
monitorCounterObjectGauge,
monitorOperationGauge,
scrapeCounter,
)
}
func objectClass(name string) string {
return fmt.Sprintf("(objectClass=%v)", name)
}
func ScrapeMetrics(ldapAddr, ldapUser, ldapPass string) {
if err := scrapeAll(ldapAddr, ldapUser, ldapPass); err != nil {
scrapeCounter.WithLabelValues("fail").Inc()
log.Println("scrape failed, error is:", err)
} else {
scrapeCounter.WithLabelValues("ok").Inc()
}
}
func ScrapeSecureMetrics(ldapsAddr, ldapUser, ldapPass string, tlsConfig *tls.Config) {
if err := scrapeSecureAll(ldapsAddr, ldapUser, ldapPass, tlsConfig); err != nil {
scrapeCounter.WithLabelValues("fail").Inc()
log.Println("scrape failed, error is:", err)
} else {
scrapeCounter.WithLabelValues("ok").Inc()
}
}
func scrapeAll(ldapAddr, ldapUser, ldapPass string) error {
l, err := ldap.Dial("tcp", ldapAddr)
if err != nil {
return err
}
defer l.Close()
if ldapUser != "" && ldapPass != "" {
err = l.Bind(ldapUser, ldapPass)
if err != nil {
return err
}
}
var errs error
for _, q := range queries {
if err := scrapeQuery(l, &q); err != nil {
errs = multierror.Append(errs, err)
}
}
return errs
}
func scrapeSecureAll(ldapsAddr, ldapUser, ldapPass string, tlsConfig *tls.Config) error {
l, err := ldap.DialTLS("tcp", ldapsAddr, tlsConfig)
if err != nil {
return err
}
defer l.Close()
if ldapUser != "" && ldapPass != "" {
err = l.Bind(ldapUser, ldapPass)
if err != nil {
return err
}
}
var errs error
for _, q := range queries {
if err := scrapeQuery(l, &q); err != nil {
errs = multierror.Append(errs, err)
}
}
return errs
}
func scrapeQuery(l *ldap.Conn, q *query) error {
req := ldap.NewSearchRequest(
q.baseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
q.searchFilter, []string{"dn", q.searchAttr}, nil,
)
sr, err := l.Search(req)
if err != nil {
return err
}
for _, entry := range sr.Entries {
val := entry.GetAttributeValue(q.searchAttr)
if val == "" {
// not every entry will have this attribute
continue
}
num, err := strconv.ParseFloat(val, 64)
if err != nil {
// some of these attributes are not numbers
continue
}
q.metric.WithLabelValues(entry.DN).Set(num)
}
return nil
}