master
go 120 lines 2.35 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package nginxvts
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "fmt"
10 "net/http"
11 "time"
12
13 "github.com/netdata/netdata/go/plugins/pkg/confopt"
14 "github.com/netdata/netdata/go/plugins/pkg/web"
15 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
16 )
17
18 //go:embed "config_schema.json"
19 var configSchema string
20
21 func init() {
22 collectorapi.Register("nginxvts", collectorapi.Creator{
23 JobConfigSchema: configSchema,
24 Defaults: collectorapi.Defaults{
25 UpdateEvery: 1,
26 },
27 Create: func() collectorapi.CollectorV1 { return New() },
28 Config: func() any { return &Config{} },
29 })
30 }
31
32 func New() *Collector {
33 return &Collector{
34 Config: Config{
35 HTTPConfig: web.HTTPConfig{
36 RequestConfig: web.RequestConfig{
37 URL: "http://localhost/status/format/json",
38 },
39 ClientConfig: web.ClientConfig{
40 Timeout: confopt.Duration(time.Second),
41 },
42 },
43 },
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
63 func (c *Collector) Configuration() any {
64 return c.Config
65 }
66
67 func (c *Collector) Cleanup(context.Context) {
68 if c.httpClient == nil {
69 return
70 }
71 c.httpClient.CloseIdleConnections()
72 }
73
74 func (c *Collector) Init(context.Context) error {
75 err := c.validateConfig()
76 if err != nil {
77 return fmt.Errorf("config: %v", err)
78 }
79
80 httpClient, err := c.initHTTPClient()
81 if err != nil {
82 return fmt.Errorf("init HTTP client: %v", err)
83 }
84 c.httpClient = httpClient
85
86 charts, err := c.initCharts()
87 if err != nil {
88 return fmt.Errorf("init charts: %v", err)
89 }
90 c.charts = charts
91
92 return nil
93 }
94
95 func (c *Collector) Check(context.Context) error {
96 mx, err := c.collect()
97 if err != nil {
98 return err
99 }
100 if len(mx) == 0 {
101 return errors.New("no metrics collected")
102 }
103 return nil
104 }
105
106 func (c *Collector) Charts() *collectorapi.Charts {
107 return c.charts
108 }
109
110 func (c *Collector) Collect(context.Context) map[string]int64 {
111 mx, err := c.collect()
112 if err != nil {
113 c.Error(err)
114 return nil
115 }
116 if len(mx) == 0 {
117 return nil
118 }
119 return mx
120 }