ceph.go 2.4 KB

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