forked from vetinari/go-ldappool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthcheck.go
78 lines (67 loc) · 1.83 KB
/
healthcheck.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
package ldappool
import (
"fmt"
hc "github.com/PennState/go-healthcheck/pkg/health"
"github.com/go-ldap/ldap/v3"
"time"
)
// Healthcheck ...
type Healthcheck struct {
Pool Pool
SearchDN string // DN of LDAP entry to search for
SearchTimeLimit int
Timeout time.Duration
}
// Check runs a gitlab client healthcheck
func (c Healthcheck) Check() ([]hc.Check, hc.Status) {
searchCheck := hc.Check{
Key: hc.Key{
ComponentName: "Search",
MeasurementName: "Result",
},
Output: "",
Time: time.Now().UTC(),
ComponentType: "component",
Links: map[string]string{"dn": c.SearchDN},
Status: hc.Pass,
}
poolSizeCheck := hc.Check{
Key: hc.Key{
ComponentName: "Pool",
MeasurementName: "Size",
},
Output: fmt.Sprintf("Pool size: %d", c.Pool.Len()),
Time: time.Now().UTC(),
ComponentType: "component",
Status: hc.Pass,
}
l, err := c.Pool.Get()
if err != nil {
searchCheck.Status = hc.Fail
searchCheck.Output = err.Error()
return []hc.Check{searchCheck, poolSizeCheck}, hc.Fail
}
defer l.Close()
l.SetTimeout(c.Timeout)
res, err := l.Search(&ldap.SearchRequest{
BaseDN: c.SearchDN,
Scope: ldap.ScopeBaseObject,
Attributes: []string{},
SizeLimit: 1,
TimeLimit: c.SearchTimeLimit,
Filter: "(objectclass=*)",
})
if err != nil {
l.MarkUnusable()
searchCheck.Status = hc.Fail
searchCheck.Output = err.Error()
return []hc.Check{searchCheck, poolSizeCheck}, hc.Fail
}
if len(res.Entries) != 1 {
searchCheck.Status = hc.Fail
searchCheck.Output = fmt.Sprintf("Search returned %d entries", len(res.Entries))
return []hc.Check{searchCheck, poolSizeCheck}, hc.Fail
}
searchCheck.Output = fmt.Sprintf("Found entry for %s", res.Entries[0].DN)
return []hc.Check{searchCheck, poolSizeCheck}, hc.Pass
}