| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package supervisord |
| 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/pkg/web" |
| 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("supervisord", 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 | URL: "http://127.0.0.1:9001/RPC2", |
| 32 | ClientConfig: web.ClientConfig{ |
| 33 | Timeout: confopt.Duration(time.Second), |
| 34 | }, |
| 35 | }, |
| 36 | |
| 37 | charts: summaryCharts.Copy(), |
| 38 | cache: make(map[string]map[string]bool), |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | type Config struct { |
| 43 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 44 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 45 | AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"` |
| 46 | URL string `yaml:"url" json:"url"` |
| 47 | web.ClientConfig `yaml:",inline" json:""` |
| 48 | } |
| 49 | |
| 50 | type Collector struct { |
| 51 | collectorapi.Base |
| 52 | Config `yaml:",inline" json:""` |
| 53 | |
| 54 | charts *collectorapi.Charts |
| 55 | |
| 56 | client supervisorClient |
| 57 | |
| 58 | cache map[string]map[string]bool // map[group][procName]collected |
| 59 | } |
| 60 | |
| 61 | func (c *Collector) Configuration() any { |
| 62 | return c.Config |
| 63 | } |
| 64 | |
| 65 | func (c *Collector) Init(context.Context) error { |
| 66 | err := c.verifyConfig() |
| 67 | if err != nil { |
| 68 | return fmt.Errorf("verify config: %v", err) |
| 69 | } |
| 70 | |
| 71 | client, err := c.initSupervisorClient() |
| 72 | if err != nil { |
| 73 | return fmt.Errorf("init supervisord client: %v", err) |
| 74 | } |
| 75 | c.client = client |
| 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 | return nil |
| 89 | } |
| 90 | |
| 91 | func (c *Collector) Charts() *collectorapi.Charts { |
| 92 | return c.charts |
| 93 | } |
| 94 | |
| 95 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 96 | ms, err := c.collect() |
| 97 | if err != nil { |
| 98 | c.Error(err) |
| 99 | } |
| 100 | |
| 101 | if len(ms) == 0 { |
| 102 | return nil |
| 103 | } |
| 104 | return ms |
| 105 | } |
| 106 | |
| 107 | func (c *Collector) Cleanup(context.Context) { |
| 108 | if c.client != nil { |
| 109 | c.client.closeIdleConnections() |
| 110 | } |
| 111 | } |