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