github.com/netdata/go.d.plugin@v0.58.1/modules/hdfs/hdfs.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package hdfs 4 5 import ( 6 _ "embed" 7 "errors" 8 "strings" 9 "time" 10 11 "github.com/netdata/go.d.plugin/pkg/web" 12 13 "github.com/netdata/go.d.plugin/agent/module" 14 ) 15 16 //go:embed "config_schema.json" 17 var configSchema string 18 19 func init() { 20 module.Register("hdfs", module.Creator{ 21 JobConfigSchema: configSchema, 22 Create: func() module.Module { return New() }, 23 }) 24 } 25 26 // New creates HDFS with default values. 27 func New() *HDFS { 28 config := Config{ 29 HTTP: web.HTTP{ 30 Request: web.Request{ 31 URL: "http://127.0.0.1:50070/jmx", 32 }, 33 Client: web.Client{ 34 Timeout: web.Duration{Duration: time.Second}}, 35 }, 36 } 37 38 return &HDFS{ 39 Config: config, 40 } 41 } 42 43 type nodeType string 44 45 const ( 46 dataNodeType nodeType = "DataNode" 47 nameNodeType nodeType = "NameNode" 48 ) 49 50 // Config is the HDFS module configuration. 51 type Config struct { 52 web.HTTP `yaml:",inline"` 53 } 54 55 // HDFS HDFS module. 56 type HDFS struct { 57 module.Base 58 Config `yaml:",inline"` 59 60 nodeType 61 client *client 62 } 63 64 // Cleanup makes cleanup. 65 func (HDFS) Cleanup() {} 66 67 func (h HDFS) createClient() (*client, error) { 68 httpClient, err := web.NewHTTPClient(h.Client) 69 if err != nil { 70 return nil, err 71 } 72 73 return newClient(httpClient, h.Request), nil 74 } 75 76 func (h HDFS) determineNodeType() (nodeType, error) { 77 var raw rawJMX 78 err := h.client.doOKWithDecodeJSON(&raw) 79 if err != nil { 80 return "", err 81 } 82 83 if raw.isEmpty() { 84 return "", errors.New("empty response") 85 } 86 87 jvm := raw.findJvm() 88 if jvm == nil { 89 return "", errors.New("couldn't find jvm in response") 90 } 91 92 v, ok := jvm["tag.ProcessName"] 93 if !ok { 94 return "", errors.New("couldn't find process name in JvmMetrics") 95 } 96 97 t := nodeType(strings.Trim(string(v), "\"")) 98 if t == nameNodeType || t == dataNodeType { 99 return t, nil 100 } 101 return "", errors.New("unknown node type") 102 } 103 104 // Init makes initialization. 105 func (h *HDFS) Init() bool { 106 cl, err := h.createClient() 107 if err != nil { 108 h.Errorf("error on creating client : %v", err) 109 return false 110 } 111 h.client = cl 112 113 return true 114 } 115 116 // Check makes check. 117 func (h *HDFS) Check() bool { 118 t, err := h.determineNodeType() 119 if err != nil { 120 h.Errorf("error on node type determination : %v", err) 121 return false 122 } 123 h.nodeType = t 124 125 return len(h.Collect()) > 0 126 } 127 128 // Charts returns Charts. 129 func (h HDFS) Charts() *Charts { 130 switch h.nodeType { 131 default: 132 return nil 133 case nameNodeType: 134 return nameNodeCharts() 135 case dataNodeType: 136 return dataNodeCharts() 137 } 138 } 139 140 // Collect collects metrics. 141 func (h *HDFS) Collect() map[string]int64 { 142 mx, err := h.collect() 143 144 if err != nil { 145 h.Error(err) 146 } 147 148 if len(mx) == 0 { 149 return nil 150 } 151 152 return mx 153 }