master
go 108 lines 2.28 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package freeradius
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "fmt"
10 "time"
11
12 "github.com/netdata/netdata/go/plugins/pkg/confopt"
13 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
14 "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/freeradius/api"
15 )
16
17 //go:embed "config_schema.json"
18 var configSchema string
19
20 func init() {
21 collectorapi.Register("freeradius", collectorapi.Creator{
22 JobConfigSchema: configSchema,
23 Create: func() collectorapi.CollectorV1 { return New() },
24 Config: func() any { return &Config{} },
25 })
26 }
27
28 func New() *Collector {
29 return &Collector{
30 Config: Config{
31 Address: "127.0.0.1",
32 Port: 18121,
33 Secret: "adminsecret",
34 Timeout: confopt.Duration(time.Second),
35 },
36 }
37 }
38
39 type Config struct {
40 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
41 UpdateEvery int `yaml:"update_every" json:"update_every"`
42 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
43 Address string `yaml:"address" json:"address"`
44 Port int `yaml:"port" json:"port"`
45 Secret string `yaml:"secret" json:"secret"`
46 Timeout confopt.Duration `yaml:"timeout" json:"timeout"`
47 }
48
49 type (
50 Collector struct {
51 collectorapi.Base
52 Config `yaml:",inline" json:""`
53
54 client
55 }
56 client interface {
57 Status() (*api.Status, error)
58 }
59 )
60
61 func (c *Collector) Configuration() any {
62 return c.Config
63 }
64
65 func (c *Collector) Init(context.Context) error {
66 if err := c.validateConfig(); err != nil {
67 return fmt.Errorf("config validation: %v", err)
68 }
69
70 c.client = api.New(api.Config{
71 Address: c.Address,
72 Port: c.Port,
73 Secret: c.Secret,
74 Timeout: c.Timeout.Duration(),
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 if len(mx) == 0 {
86 return errors.New("no metrics collected")
87
88 }
89 return nil
90 }
91
92 func (c *Collector) Charts() *Charts {
93 return charts.Copy()
94 }
95
96 func (c *Collector) Collect(context.Context) map[string]int64 {
97 mx, err := c.collect()
98 if err != nil {
99 c.Error(err)
100 }
101
102 if len(mx) == 0 {
103 return nil
104 }
105 return mx
106 }
107
108 func (c *Collector) Cleanup(context.Context) {}