| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package phpdaemon |
| 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("phpdaemon", collectorapi.Creator{ |
| 24 | JobConfigSchema: configSchema, |
| 25 | Create: func() collectorapi.CollectorV1 { return New() }, |
| 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:8509/FullStatus", |
| 36 | }, |
| 37 | ClientConfig: web.ClientConfig{ |
| 38 | Timeout: confopt.Duration(time.Second), |
| 39 | }, |
| 40 | }, |
| 41 | }, |
| 42 | charts: charts.Copy(), |
| 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 Collector struct { |
| 54 | collectorapi.Base |
| 55 | Config `yaml:",inline" json:""` |
| 56 | |
| 57 | charts *Charts |
| 58 | once sync.Once |
| 59 | |
| 60 | httpClient *http.Client |
| 61 | } |
| 62 | |
| 63 | func (c *Collector) Configuration() any { |
| 64 | return c.Config |
| 65 | } |
| 66 | |
| 67 | func (c *Collector) Init(context.Context) error { |
| 68 | if c.URL == "" { |
| 69 | return errors.New("phpDaemon URL is required but not set") |
| 70 | } |
| 71 | |
| 72 | httpClient, err := web.NewHTTPClient(c.ClientConfig) |
| 73 | if err != nil { |
| 74 | return fmt.Errorf("failed to initialize http client: %w", err) |
| 75 | } |
| 76 | c.httpClient = httpClient |
| 77 | |
| 78 | c.Debugf("using URL %s", c.URL) |
| 79 | c.Debugf("using timeout: %s", c.Timeout) |
| 80 | |
| 81 | return nil |
| 82 | } |
| 83 | |
| 84 | func (c *Collector) Check(context.Context) error { |
| 85 | mx, err := c.collect() |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | |
| 90 | if len(mx) == 0 { |
| 91 | return errors.New("no metrics collected") |
| 92 | } |
| 93 | |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | func (c *Collector) Charts() *Charts { |
| 98 | return c.charts |
| 99 | } |
| 100 | |
| 101 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 102 | mx, err := c.collect() |
| 103 | if err != nil { |
| 104 | c.Error(err) |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | return mx |
| 109 | } |
| 110 | |
| 111 | func (c *Collector) Cleanup(context.Context) { |
| 112 | if c.httpClient != nil { |
| 113 | c.httpClient.CloseIdleConnections() |
| 114 | } |
| 115 | } |