master
go 97 lines 1.87 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 //go:build linux
4
5 package logind
6
7 import (
8 "context"
9 _ "embed"
10 "errors"
11 "time"
12
13 "github.com/netdata/netdata/go/plugins/pkg/confopt"
14 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
15 )
16
17 //go:embed "config_schema.json"
18 var configSchema string
19
20 func init() {
21 collectorapi.Register("logind", collectorapi.Creator{
22 JobConfigSchema: configSchema,
23 Defaults: collectorapi.Defaults{
24 Priority: 59999, // copied from the python collector
25 },
26 Create: func() collectorapi.CollectorV1 { return New() },
27 Config: func() any { return &Config{} },
28 })
29 }
30
31 func New() *Collector {
32 return &Collector{
33 Config: Config{
34 Timeout: confopt.Duration(time.Second),
35 },
36 newLogindConn: func(cfg Config) (logindConnection, error) {
37 return newLogindConnection(cfg.Timeout.Duration())
38 },
39 charts: charts.Copy(),
40 }
41 }
42
43 type Config struct {
44 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
45 Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"`
46 }
47
48 type Collector struct {
49 collectorapi.Base
50 Config `yaml:",inline" json:""`
51
52 charts *collectorapi.Charts
53
54 conn logindConnection
55 newLogindConn func(config Config) (logindConnection, error)
56 }
57
58 func (c *Collector) Configuration() any {
59 return c.Config
60 }
61
62 func (c *Collector) Init(context.Context) error {
63 return nil
64 }
65
66 func (c *Collector) Check(context.Context) error {
67 mx, err := c.collect()
68 if err != nil {
69 return err
70 }
71 if len(mx) == 0 {
72 return errors.New("no metrics collected")
73 }
74 return nil
75 }
76
77 func (c *Collector) Charts() *collectorapi.Charts {
78 return c.charts
79 }
80
81 func (c *Collector) Collect(context.Context) map[string]int64 {
82 mx, err := c.collect()
83 if err != nil {
84 c.Error(err)
85 }
86
87 if len(mx) == 0 {
88 return nil
89 }
90 return mx
91 }
92
93 func (c *Collector) Cleanup(context.Context) {
94 if c.conn != nil {
95 c.conn.Close()
96 }
97 }