| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package apache |
| 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/web" |
| 16 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 17 | ) |
| 18 | |
| 19 | //go:embed "config_schema.json" |
| 20 | var configSchema string |
| 21 | |
| 22 | func init() { |
| 23 | collectorapi.Register("apache", collectorapi.Creator{ |
| 24 | Create: func() collectorapi.CollectorV1 { return New() }, |
| 25 | JobConfigSchema: configSchema, |
| 26 | Config: func() any { return &Config{} }, |
| 27 | }) |
| 28 | } |
| 29 | |
| 30 | func New() *Collector { |
| 31 | return &Collector{ |
| 32 | Config: Config{ |
| 33 | HTTPConfig: web.HTTPConfig{ |
| 34 | RequestConfig: web.RequestConfig{ |
| 35 | URL: "http://127.0.0.1/server-status?auto", |
| 36 | }, |
| 37 | ClientConfig: web.ClientConfig{ |
| 38 | Timeout: confopt.Duration(time.Second), |
| 39 | }, |
| 40 | }, |
| 41 | }, |
| 42 | charts: &collectorapi.Charts{}, |
| 43 | once: &sync.Once{}, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | type Config struct { |
| 48 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 49 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 50 | AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"` |
| 51 | web.HTTPConfig `yaml:",inline" json:""` |
| 52 | } |
| 53 | |
| 54 | type Collector struct { |
| 55 | collectorapi.Base |
| 56 | Config `yaml:",inline" json:""` |
| 57 | |
| 58 | charts *collectorapi.Charts |
| 59 | |
| 60 | httpClient *http.Client |
| 61 | |
| 62 | once *sync.Once |
| 63 | } |
| 64 | |
| 65 | func (c *Collector) Configuration() any { |
| 66 | return c.Config |
| 67 | } |
| 68 | |
| 69 | func (c *Collector) Init(context.Context) error { |
| 70 | if err := c.validateConfig(); err != nil { |
| 71 | return fmt.Errorf("config validation: %v", err) |
| 72 | } |
| 73 | |
| 74 | httpClient, err := c.initHTTPClient() |
| 75 | if err != nil { |
| 76 | return fmt.Errorf("init HTTP client: %v", err) |
| 77 | } |
| 78 | c.httpClient = httpClient |
| 79 | |
| 80 | c.Debugf("using URL %s", c.URL) |
| 81 | c.Debugf("using timeout: %s", c.Timeout) |
| 82 | |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | func (c *Collector) Check(context.Context) error { |
| 87 | mx, err := c.collect() |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | if len(mx) == 0 { |
| 92 | return errors.New("no metrics collected") |
| 93 | |
| 94 | } |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | func (c *Collector) Charts() *collectorapi.Charts { |
| 99 | return c.charts |
| 100 | } |
| 101 | |
| 102 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 103 | mx, err := c.collect() |
| 104 | if err != nil { |
| 105 | c.Error(err) |
| 106 | } |
| 107 | |
| 108 | if len(mx) == 0 { |
| 109 | return nil |
| 110 | } |
| 111 | return mx |
| 112 | } |
| 113 | |
| 114 | func (c *Collector) Cleanup(context.Context) { |
| 115 | if c.httpClient != nil { |
| 116 | c.httpClient.CloseIdleConnections() |
| 117 | } |
| 118 | } |