master
go 102 lines 2.16 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package phpfpm
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 )
16
17 //go:embed "config_schema.json"
18 var configSchema string
19
20 func init() {
21 collectorapi.Register("phpfpm", collectorapi.Creator{
22 JobConfigSchema: configSchema,
23 Create: func() collectorapi.CollectorV1 { return New() },
24 Config: func() any { return &Config{} },
25 })
26 }
27
28 func New() *Collector {
29 return &Collector{
30 Config: Config{
31 HTTPConfig: web.HTTPConfig{
32 RequestConfig: web.RequestConfig{
33 URL: "http://127.0.0.1/status?full&json",
34 },
35 ClientConfig: web.ClientConfig{
36 Timeout: confopt.Duration(time.Second),
37 },
38 },
39 FcgiPath: "/status",
40 },
41 }
42 }
43
44 type Config struct {
45 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
46 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
47 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
48 web.HTTPConfig `yaml:",inline" json:""`
49 Socket string `yaml:"socket,omitempty" json:"socket"`
50 Address string `yaml:"address,omitempty" json:"address"`
51 FcgiPath string `yaml:"fcgi_path,omitempty" json:"fcgi_path"`
52 }
53
54 type Collector struct {
55 collectorapi.Base
56 Config `yaml:",inline" json:""`
57
58 client client
59 }
60
61 func (c *Collector) Configuration() any {
62 return c.Config
63 }
64
65 func (c *Collector) Init(context.Context) error {
66 cli, err := c.initClient()
67 if err != nil {
68 return fmt.Errorf("init client: %v", err)
69 }
70 c.client = cli
71
72 return nil
73 }
74
75 func (c *Collector) Check(context.Context) error {
76 mx, err := c.collect()
77 if err != nil {
78 return err
79 }
80 if len(mx) == 0 {
81 return errors.New("no metrics collected")
82 }
83 return nil
84 }
85
86 func (c *Collector) Charts() *Charts {
87 return charts.Copy()
88 }
89
90 func (c *Collector) Collect(context.Context) map[string]int64 {
91 mx, err := c.collect()
92 if err != nil {
93 c.Error(err)
94 }
95
96 if len(mx) == 0 {
97 return nil
98 }
99 return mx
100 }
101
102 func (c *Collector) Cleanup(context.Context) {}