| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package scaleio |
| 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 | "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/scaleio/client" |
| 16 | ) |
| 17 | |
| 18 | //go:embed "config_schema.json" |
| 19 | var configSchema string |
| 20 | |
| 21 | func init() { |
| 22 | collectorapi.Register("scaleio", 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: "https://127.0.0.1", |
| 35 | }, |
| 36 | ClientConfig: web.ClientConfig{ |
| 37 | Timeout: confopt.Duration(time.Second), |
| 38 | }, |
| 39 | }, |
| 40 | }, |
| 41 | charts: systemCharts.Copy(), |
| 42 | charted: make(map[string]bool), |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | type Config struct { |
| 47 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 48 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 49 | AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"` |
| 50 | web.HTTPConfig `yaml:",inline" json:""` |
| 51 | } |
| 52 | |
| 53 | type ( |
| 54 | Collector struct { |
| 55 | collectorapi.Base |
| 56 | Config `yaml:",inline" json:""` |
| 57 | |
| 58 | charts *collectorapi.Charts |
| 59 | |
| 60 | client *client.Client |
| 61 | |
| 62 | discovered instances |
| 63 | charted map[string]bool |
| 64 | lastDiscoveryOK bool |
| 65 | runs int |
| 66 | } |
| 67 | instances struct { |
| 68 | sdc map[string]client.Sdc |
| 69 | pool map[string]client.StoragePool |
| 70 | } |
| 71 | ) |
| 72 | |
| 73 | func (c *Collector) Configuration() any { |
| 74 | return c.Config |
| 75 | } |
| 76 | |
| 77 | func (c *Collector) Init(context.Context) error { |
| 78 | if c.Username == "" || c.Password == "" { |
| 79 | return errors.New("config: username and password aren't set") |
| 80 | } |
| 81 | |
| 82 | cli, err := client.New(c.ClientConfig, c.RequestConfig) |
| 83 | if err != nil { |
| 84 | return fmt.Errorf("error on creating ScaleIO client: %v", err) |
| 85 | } |
| 86 | c.client = cli |
| 87 | |
| 88 | c.Debugf("using URL %s", c.URL) |
| 89 | c.Debugf("using timeout: %s", c.Timeout) |
| 90 | |
| 91 | return nil |
| 92 | } |
| 93 | |
| 94 | func (c *Collector) Check(context.Context) error { |
| 95 | if err := c.client.Login(); err != nil { |
| 96 | return err |
| 97 | } |
| 98 | mx, err := c.collect() |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | if len(mx) == 0 { |
| 103 | return errors.New("no metrics collected") |
| 104 | } |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | func (c *Collector) Charts() *collectorapi.Charts { |
| 109 | return c.charts |
| 110 | } |
| 111 | |
| 112 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 113 | mx, err := c.collect() |
| 114 | if err != nil { |
| 115 | c.Error(err) |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | if len(mx) == 0 { |
| 120 | return nil |
| 121 | } |
| 122 | return mx |
| 123 | } |
| 124 | |
| 125 | func (c *Collector) Cleanup(context.Context) { |
| 126 | if c.client == nil { |
| 127 | return |
| 128 | } |
| 129 | _ = c.client.Logout() |
| 130 | } |