master
go 104 lines 2.15 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package boinc
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "time"
10
11 "github.com/netdata/netdata/go/plugins/logger"
12 "github.com/netdata/netdata/go/plugins/pkg/confopt"
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("boinc", collectorapi.Creator{
21 JobConfigSchema: configSchema,
22 Create: func() collectorapi.CollectorV1 { return New() },
23 Config: func() any { return &Config{} },
24 })
25 }
26
27 func New() *Collector {
28 return &Collector{
29 Config: Config{
30 Address: "127.0.0.1:31416",
31 Timeout: confopt.Duration(time.Second * 1),
32 },
33 newConn: newBoincConn,
34 charts: charts.Copy(),
35 }
36 }
37
38 type Config struct {
39 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
40 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
41 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
42 Address string `yaml:"address" json:"address"`
43 Timeout confopt.Duration `yaml:"timeout" json:"timeout"`
44 Password string `yaml:"password" json:"password"`
45 }
46
47 type Collector struct {
48 collectorapi.Base
49 Config `yaml:",inline" json:""`
50
51 charts *collectorapi.Charts
52
53 newConn func(Config, *logger.Logger) boincConn
54 conn boincConn
55 }
56
57 func (c *Collector) Configuration() any {
58 return c.Config
59 }
60
61 func (c *Collector) Init(context.Context) error {
62 if c.Address == "" {
63 return errors.New("config: 'address' not set")
64 }
65
66 return nil
67 }
68
69 func (c *Collector) Check(context.Context) error {
70 mx, err := c.collect()
71 if err != nil {
72 return err
73 }
74
75 if len(mx) == 0 {
76 return errors.New("no metrics collected")
77 }
78
79 return nil
80 }
81
82 func (c *Collector) Charts() *collectorapi.Charts {
83 return c.charts
84 }
85
86 func (c *Collector) Collect(context.Context) map[string]int64 {
87 mx, err := c.collect()
88 if err != nil {
89 c.Error(err)
90 }
91
92 if len(mx) == 0 {
93 return nil
94 }
95
96 return mx
97 }
98
99 func (c *Collector) Cleanup(context.Context) {
100 if c.conn != nil {
101 c.conn.disconnect()
102 c.conn = nil
103 }
104 }