master
go 117 lines 2.44 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package openldap
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "time"
10
11 "github.com/netdata/netdata/go/plugins/pkg/confopt"
12 "github.com/netdata/netdata/go/plugins/pkg/tlscfg"
13 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
14 )
15
16 //go:embed "config_schema.json"
17 var configSchema string
18
19 func init() {
20 collectorapi.Register("openldap", collectorapi.Creator{
21 JobConfigSchema: configSchema,
22 Defaults: collectorapi.Defaults{
23 UpdateEvery: 1,
24 },
25 Create: func() collectorapi.CollectorV1 { return New() },
26 Config: func() any { return &Config{} },
27 })
28 }
29
30 func New() *Collector {
31 return &Collector{
32 Config: Config{
33 URL: "ldap://127.0.0.1:389",
34 Timeout: confopt.Duration(time.Second * 2),
35 },
36
37 newConn: newLdapConn,
38
39 charts: charts.Copy(),
40 }
41
42 }
43
44 type Config struct {
45 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
46 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
47 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
48 URL string `yaml:"url" json:"url"`
49 Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"`
50 Username string `yaml:"username" json:"username"`
51 Password string `yaml:"password" json:"password"`
52 tlscfg.TLSConfig `yaml:",inline" json:""`
53 }
54
55 type Collector struct {
56 collectorapi.Base
57 Config `yaml:",inline" json:""`
58
59 charts *collectorapi.Charts
60
61 conn ldapConn
62 newConn func(Config) ldapConn
63 }
64
65 func (c *Collector) Configuration() any {
66 return c.Config
67 }
68
69 func (c *Collector) Init(context.Context) error {
70 if c.URL == "" {
71 return errors.New("empty LDAP server url")
72 }
73 if c.Username == "" {
74 return errors.New("empty LDAP username")
75 }
76
77 return nil
78 }
79
80 func (c *Collector) Check(context.Context) error {
81 mx, err := c.collect()
82 if err != nil {
83 return err
84 }
85
86 if len(mx) == 0 {
87 return errors.New("no metrics collected")
88 }
89
90 return nil
91 }
92
93 func (c *Collector) Charts() *collectorapi.Charts {
94 return c.charts
95 }
96
97 func (c *Collector) Collect(context.Context) map[string]int64 {
98 mx, err := c.collect()
99 if err != nil {
100 c.Error(err)
101 }
102
103 if len(mx) == 0 {
104 return nil
105 }
106
107 return mx
108 }
109
110 func (c *Collector) Cleanup(context.Context) {
111 if c.conn != nil {
112 if err := c.conn.disconnect(); err != nil {
113 c.Warningf("error disconnecting ldap client: %v", err)
114 }
115 c.conn = nil
116 }
117 }