-
Notifications
You must be signed in to change notification settings - Fork 1
/
scraper.go
252 lines (239 loc) · 6.16 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package opendj_exporter
import (
"context"
"fmt"
"strconv"
"time"
ldap "github.com/go-ldap/ldap/v3"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
const (
namespace = "opendj"
)
var (
baseMonitor = "cn=monitor"
administrationConnectorGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "administration_connector",
Help: "Administration connector",
},
[]string{"attr"},
)
backendGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "userRoot_backend",
Help: "userRoot backend",
},
[]string{"attr"},
)
memoryGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "jvm_memory_usage",
Help: "JVM memory usage",
},
[]string{"attr"},
)
ldapGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "ldap_connection_handler",
Help: "LDAP connection handler metrics",
},
[]string{"attr"},
)
ldapsGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "ldaps_connection_handler",
Help: "LDAPS connection handler metrics",
},
[]string{"attr"},
)
databaseGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "userRoot_je_database",
Help: "userRoot JE database",
},
[]string{"attr"},
)
administrationStatisticsGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "administration_connector_statistics",
Help: "Administration connector statistics",
},
[]string{"attr"},
)
ldapHandlerGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "ldap_connection_handler_statistics",
Help: "LDAP connection handler statistics",
},
[]string{"attr"},
)
ldapsHandlerGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "ldaps_connection_handler_statistics",
Help: "LDAPS connection handler statistics",
},
[]string{"attr"},
)
queries []*query
)
type Scraper struct {
Addr string
User string
Pass string
Tick time.Duration
log log.FieldLogger
Conn *ldap.Conn
LdapListenAddr string
LdapsListenAddr string
AdministrationConnector string
LdapPort int
LdapsPort int
AdministrationPort int
}
type query struct {
baseDN string
metric *prometheus.GaugeVec
setData func([]*ldap.Entry, *query)
}
func getBaseDN(cn string) string {
return fmt.Sprintf("cn=%s,%s", cn, baseMonitor)
}
func buildQueries(s *Scraper) {
queries = append(queries, &query{
baseDN: getBaseDN("userRoot JE Database"),
metric: databaseGauge,
setData: setValue,
})
queries = append(queries, &query{
baseDN: getBaseDN("JVM Memory Usage"),
metric: memoryGauge,
setData: setValue,
})
queries = append(queries, &query{
baseDN: getBaseDN("userRoot backend"),
metric: backendGauge,
setData: setValue,
})
builtCn := fmt.Sprintf("Administration Connector %s port %d", s.AdministrationConnector, s.AdministrationPort)
queries = append(queries, &query{
baseDN: getBaseDN(builtCn),
metric: administrationConnectorGauge,
setData: setValue,
})
builtCn = fmt.Sprintf("LDAP Connection Handler %s port %d", s.LdapListenAddr, s.LdapPort)
queries = append(queries, &query{
baseDN: getBaseDN(builtCn),
metric: ldapGauge,
setData: setValue,
})
builtCn = fmt.Sprintf("LDAPS Connection Handler %s port %d", s.LdapsListenAddr, s.LdapsPort)
queries = append(queries, &query{
baseDN: getBaseDN(builtCn),
metric: ldapsGauge,
setData: setValue,
})
builtCn = fmt.Sprintf("Administration Connector %s port %d Statistics", s.AdministrationConnector, s.AdministrationPort)
queries = append(queries, &query{
baseDN: getBaseDN(builtCn),
metric: administrationStatisticsGauge,
setData: setValue,
})
builtCn = fmt.Sprintf("LDAP Connection Handler %s port %d Statistics", s.LdapListenAddr, s.LdapPort)
queries = append(queries, &query{
baseDN: getBaseDN(builtCn),
metric: ldapHandlerGauge,
setData: setValue,
})
builtCn = fmt.Sprintf("LDAPS Connection Handler %s port %d Statistics", s.LdapsListenAddr, s.LdapsPort)
queries = append(queries, &query{
baseDN: getBaseDN(builtCn),
metric: ldapsHandlerGauge,
setData: setValue,
})
}
func setValue(entries []*ldap.Entry, q *query) {
for _, entry := range entries {
for _, attribute := range entry.Attributes {
attrName := attribute.Name
attrVal := attribute.Values[0]
if n, err := strconv.ParseFloat(attrVal, 64); err == nil {
q.metric.WithLabelValues(attrName).Set(n)
}
}
}
}
func init() {
prometheus.MustRegister(
databaseGauge,
memoryGauge,
backendGauge,
ldapsGauge,
ldapGauge,
administrationConnectorGauge,
administrationStatisticsGauge,
ldapsHandlerGauge,
ldapHandlerGauge,
)
}
func (s *Scraper) Start(ctx context.Context) {
var err error
s.log = log.WithField("component", "scraper")
buildQueries(s)
address := fmt.Sprintf("tcp://%s", s.Addr)
s.log.WithField("addr", address).Info("starting monitor loop")
s.Conn, err = ldap.Dial("tcp", s.Addr)
if err != nil {
s.log.WithError(err).Error("dial failed")
return
}
ticker := time.NewTicker(s.Tick)
defer ticker.Stop()
defer s.Conn.Close()
for {
select {
case <-ticker.C:
s.scrape()
case <-ctx.Done():
return
}
}
}
func (s *Scraper) scrape() {
var err error
conn := s.Conn
if s.User != "" && s.Pass != "" {
err = conn.Bind(s.User, s.Pass)
if err != nil {
s.log.WithError(err).Error("bind failed")
return
}
}
for _, q := range queries {
if err = scrapeQuery(conn, q); err != nil {
s.log.WithError(err).WithField("name", q.baseDN).Warn("query failed")
}
}
}
func scrapeQuery(conn *ldap.Conn, q *query) error {
req := ldap.NewSearchRequest(
q.baseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(objectclass=*)", []string{"*"}, nil,
)
sr, err := conn.Search(req)
if err != nil {
return err
}
q.setData(sr.Entries, q)
return nil
}