master
go 124 lines 2.39 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package hdfs
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("hdfs", collectorapi.Creator{
23 JobConfigSchema: configSchema,
24 Create: func() collectorapi.CollectorV1 { return New() },
25 Config: func() any { return &Config{} },
26 })
27 }
28
29 func New() *Collector {
30 config := Config{
31 HTTPConfig: web.HTTPConfig{
32 RequestConfig: web.RequestConfig{
33 URL: "http://127.0.0.1:9870/jmx",
34 },
35 ClientConfig: web.ClientConfig{
36 Timeout: confopt.Duration(time.Second),
37 },
38 },
39 }
40
41 return &Collector{
42 Config: config,
43 }
44 }
45
46 type Config struct {
47 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
48 UpdateEvery int `yaml:"update_every" 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 httpClient *http.Client
58
59 nodeType string
60 }
61
62 func (c *Collector) Configuration() any {
63 return c.Config
64 }
65
66 func (c *Collector) Init(context.Context) error {
67 if c.URL == "" {
68 return errors.New("URL is required but not set")
69 }
70
71 httpClient, err := web.NewHTTPClient(c.ClientConfig)
72 if err != nil {
73 return fmt.Errorf("failed to create HTTP client: %v", err)
74 }
75 c.httpClient = httpClient
76
77 return nil
78 }
79
80 func (c *Collector) Check(context.Context) error {
81 typ, err := c.determineNodeType()
82 if err != nil {
83 return fmt.Errorf("error on node type determination : %v", err)
84 }
85 c.nodeType = typ
86
87 mx, err := c.collect()
88 if err != nil {
89 return err
90 }
91
92 if len(mx) == 0 {
93 return errors.New("no metrics collected")
94 }
95
96 return nil
97 }
98
99 func (c *Collector) Charts() *Charts {
100 switch c.nodeType {
101 default:
102 return nil
103 case nameNodeType:
104 return nameNodeCharts()
105 case dataNodeType:
106 return dataNodeCharts()
107 }
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
117 return mx
118 }
119
120 func (c *Collector) Cleanup(context.Context) {
121 if c.httpClient != nil {
122 c.httpClient.CloseIdleConnections()
123 }
124 }