| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package ceph |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | _ "embed" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "net/http" |
| 11 | "sync" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 15 | "github.com/netdata/netdata/go/plugins/pkg/tlscfg" |
| 16 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 17 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 18 | ) |
| 19 | |
| 20 | //go:embed "config_schema.json" |
| 21 | var configSchema string |
| 22 | |
| 23 | func init() { |
| 24 | collectorapi.Register("ceph", collectorapi.Creator{ |
| 25 | JobConfigSchema: configSchema, |
| 26 | Defaults: collectorapi.Defaults{ |
| 27 | UpdateEvery: 10, |
| 28 | }, |
| 29 | Create: func() collectorapi.CollectorV1 { return New() }, |
| 30 | Config: func() any { return &Config{} }, |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | func New() *Collector { |
| 35 | return &Collector{ |
| 36 | Config: Config{ |
| 37 | HTTPConfig: web.HTTPConfig{ |
| 38 | RequestConfig: web.RequestConfig{ |
| 39 | URL: "https://127.0.0.1:8443", |
| 40 | }, |
| 41 | ClientConfig: web.ClientConfig{ |
| 42 | Timeout: confopt.Duration(time.Second * 2), |
| 43 | TLSConfig: tlscfg.TLSConfig{ |
| 44 | InsecureSkipVerify: true, |
| 45 | }, |
| 46 | }, |
| 47 | }, |
| 48 | }, |
| 49 | charts: &collectorapi.Charts{}, |
| 50 | seenPools: make(map[string]bool), |
| 51 | seenOsds: make(map[string]bool), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | type Config struct { |
| 56 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 57 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 58 | AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"` |
| 59 | web.HTTPConfig `yaml:",inline" json:""` |
| 60 | } |
| 61 | |
| 62 | type Collector struct { |
| 63 | collectorapi.Base |
| 64 | Config `yaml:",inline" json:""` |
| 65 | |
| 66 | charts *collectorapi.Charts |
| 67 | addClusterChartsOnce sync.Once |
| 68 | |
| 69 | httpClient *http.Client |
| 70 | |
| 71 | token string |
| 72 | |
| 73 | fsid string // a unique identifier for the cluster |
| 74 | |
| 75 | seenPools map[string]bool |
| 76 | seenOsds map[string]bool |
| 77 | } |
| 78 | |
| 79 | func (c *Collector) Configuration() any { |
| 80 | return c.Config |
| 81 | } |
| 82 | |
| 83 | func (c *Collector) Init(context.Context) error { |
| 84 | if err := c.validateConfig(); err != nil { |
| 85 | return fmt.Errorf("invalid config: %v", err) |
| 86 | } |
| 87 | |
| 88 | httpClient, err := web.NewHTTPClient(c.ClientConfig) |
| 89 | if err != nil { |
| 90 | return fmt.Errorf("create http client: %v", err) |
| 91 | } |
| 92 | c.httpClient = httpClient |
| 93 | |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | func (c *Collector) Check(context.Context) error { |
| 98 | mx, err := c.collect() |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | |
| 103 | if len(mx) == 0 { |
| 104 | return errors.New("no metrics collected") |
| 105 | } |
| 106 | |
| 107 | return nil |
| 108 | } |
| 109 | |
| 110 | func (c *Collector) Charts() *collectorapi.Charts { |
| 111 | return c.charts |
| 112 | } |
| 113 | |
| 114 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 115 | mx, err := c.collect() |
| 116 | if err != nil { |
| 117 | c.Error(err) |
| 118 | } |
| 119 | |
| 120 | if len(mx) == 0 { |
| 121 | return nil |
| 122 | } |
| 123 | |
| 124 | return mx |
| 125 | } |
| 126 | |
| 127 | func (c *Collector) Cleanup(context.Context) { |
| 128 | if c.httpClient != nil { |
| 129 | if err := c.authLogout(); err != nil { |
| 130 | c.Warningf("failed to logout: %v", err) |
| 131 | } |
| 132 | c.httpClient.CloseIdleConnections() |
| 133 | } |
| 134 | } |