go.d/hddtemp: connect and read (#18619)
Ilya Mashchenko committed
Sep 25, 2024 at 17:12 UTC
732acca7187d8aee3942b2a3114207a95b237374
4 files changed
+42
-81
src/go/plugin/go.d/modules/hddtemp/client.go
+24
-17
@@ -3,42 +3,49 @@
3
package hddtemp
4
5
import (
6
+ "time"
7
+
8
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/socket"
9
)
10
9
-func newHddTempConn(conf Config) hddtempConn {
10
- return &hddtempClient{conn: socket.New(socket.Config{
11
- Address: conf.Address,
12
- ConnectTimeout: conf.Timeout.Duration(),
13
- ReadTimeout: conf.Timeout.Duration(),
14
- WriteTimeout: conf.Timeout.Duration(),
15
- })}
11
+type hddtempConn interface {
12
+ queryHddTemp() (string, error)
13
}
14
18
-type hddtempClient struct {
19
- conn socket.Client
20
-}
21
-
22
-func (c *hddtempClient) connect() error {
23
- return c.conn.Connect()
15
+func newHddTempConn(conf Config) hddtempConn {
16
+ return &hddtempClient{
17
+ address: conf.Address,
18
+ timeout: conf.Timeout.Duration(),
19
+ }
20
}
21
26
-func (c *hddtempClient) disconnect() {
27
- _ = c.conn.Disconnect()
22
+type hddtempClient struct {
23
+ address string
24
+ timeout time.Duration
25
}
26
27
func (c *hddtempClient) queryHddTemp() (string, error) {
28
var i int
29
var s string
33
- err := c.conn.Command("", func(bytes []byte) bool {
30
+
31
+ cfg := socket.Config{
32
+ Address: c.address,
33
+ ConnectTimeout: c.timeout,
34
+ ReadTimeout: c.timeout,
35
+ WriteTimeout: c.timeout,
36
+ }
37
+
38
+ err := socket.ConnectAndRead(cfg, func(bs []byte) bool {
39
if i++; i > 1 {
40
return false
41
}
37
- s = string(bytes)
42
+ s = string(bs)
43
return true
44
+
45
})
46
if err != nil {
47
return "", err
48
}
49
+
50
return s, nil
51
}
src/go/plugin/go.d/modules/hddtemp/collect.go
+1
-9
@@ -17,15 +17,7 @@ type diskStats struct {
17
}
18
19
func (h *HddTemp) collect() (map[string]int64, error) {
20
- conn := h.newHddTempConn(h.Config)
21
-
22
- if err := conn.connect(); err != nil {
23
- return nil, err
24
- }
25
-
26
- defer conn.disconnect()
27
-
28
- msg, err := conn.queryHddTemp()
20
+ msg, err := h.conn.queryHddTemp()
21
if err != nil {
22
return nil, err
23
}
src/go/plugin/go.d/modules/hddtemp/hddtemp.go
+13
-20
@@ -28,10 +28,9 @@ func New() *HddTemp {
28
Address: "127.0.0.1:7634",
29
Timeout: confopt.Duration(time.Second * 1),
30
},
31
- newHddTempConn: newHddTempConn,
32
- charts: &module.Charts{},
33
- disks: make(map[string]bool),
34
- disksTemp: make(map[string]bool),
31
+ charts: &module.Charts{},
32
+ disks: make(map[string]bool),
33
+ disksTemp: make(map[string]bool),
34
}
35
}
36
@@ -41,25 +40,17 @@ type Config struct {
40
Timeout confopt.Duration `yaml:"timeout" json:"timeout"`
41
}
42
44
-type (
45
- HddTemp struct {
46
- module.Base
47
- Config `yaml:",inline" json:""`
43
+type HddTemp struct {
44
+ module.Base
45
+ Config `yaml:",inline" json:""`
46
49
- charts *module.Charts
47
+ charts *module.Charts
48
51
- newHddTempConn func(Config) hddtempConn
49
+ conn hddtempConn
50
53
- disks map[string]bool
54
- disksTemp map[string]bool
55
- }
56
-
57
- hddtempConn interface {
58
- connect() error
59
- disconnect()
60
- queryHddTemp() (string, error)
61
- }
62
-)
51
+ disks map[string]bool
52
+ disksTemp map[string]bool
53
+}
54
55
func (h *HddTemp) Configuration() any {
56
return h.Config
@@ -71,6 +62,8 @@ func (h *HddTemp) Init() error {
62
return errors.New("address not set")
63
}
64
65
+ h.conn = newHddTempConn(h.Config)
66
+
67
return nil
68
}
69
src/go/plugin/go.d/modules/hddtemp/hddtemp_test.go
+4
-35
@@ -82,7 +82,7 @@ func TestHddTemp_Cleanup(t *testing.T) {
82
"after check": {
83
prepare: func() *HddTemp {
84
hdd := New()
85
- hdd.newHddTempConn = func(config Config) hddtempConn { return prepareMockAllDisksOk() }
85
+ hdd.conn = prepareMockAllDisksOk()
86
_ = hdd.Check()
87
return hdd
88
},
@@ -90,7 +90,7 @@ func TestHddTemp_Cleanup(t *testing.T) {
90
"after collect": {
91
prepare: func() *HddTemp {
92
hdd := New()
93
- hdd.newHddTempConn = func(config Config) hddtempConn { return prepareMockAllDisksOk() }
93
+ hdd.conn = prepareMockAllDisksOk()
94
_ = hdd.Collect()
95
return hdd
96
},
@@ -123,10 +123,6 @@ func TestHddTemp_Check(t *testing.T) {
123
wantFail: false,
124
prepareMock: prepareMockAllDisksSleep,
125
},
126
- "err on connect": {
127
- wantFail: true,
128
- prepareMock: prepareMockErrOnConnect,
129
- },
126
"unexpected response": {
127
wantFail: true,
128
prepareMock: prepareMockUnexpectedResponse,
@@ -140,8 +136,7 @@ func TestHddTemp_Check(t *testing.T) {
136
for name, test := range tests {
137
t.Run(name, func(t *testing.T) {
138
hdd := New()
143
- mock := test.prepareMock()
144
- hdd.newHddTempConn = func(config Config) hddtempConn { return mock }
139
+ hdd.conn = test.prepareMock()
140
141
if test.wantFail {
142
assert.Error(t, hdd.Check())
@@ -219,10 +214,6 @@ func TestHddTemp_Collect(t *testing.T) {
214
"disk_ata-WDC_WD10EARS-00Y5B1_WD-WCAV5R693922_temp_sensor_status_unk": 0,
215
},
216
},
222
- "err on connect": {
223
- prepareMock: prepareMockErrOnConnect,
224
- wantDisconnect: false,
225
- },
217
"unexpected response": {
218
prepareMock: prepareMockUnexpectedResponse,
219
wantDisconnect: true,
@@ -236,8 +227,7 @@ func TestHddTemp_Collect(t *testing.T) {
227
for name, test := range tests {
228
t.Run(name, func(t *testing.T) {
229
hdd := New()
239
- mock := test.prepareMock()
240
- hdd.newHddTempConn = func(config Config) hddtempConn { return mock }
230
+ hdd.conn = test.prepareMock()
231
232
mx := hdd.Collect()
233
@@ -245,8 +235,6 @@ func TestHddTemp_Collect(t *testing.T) {
235
236
assert.Len(t, *hdd.Charts(), test.wantCharts, "wantCharts")
237
248
- assert.Equal(t, test.wantDisconnect, mock.disconnectCalled, "disconnectCalled")
249
-
238
module.TestMetricsHasAllChartsDims(t, hdd.Charts(), mx)
239
})
240
}
@@ -264,12 +252,6 @@ func prepareMockAllDisksSleep() *mockHddTempConn {
252
}
253
}
254
267
-func prepareMockErrOnConnect() *mockHddTempConn {
268
- return &mockHddTempConn{
269
- errOnConnect: true,
270
- }
271
-}
272
-
255
func prepareMockUnexpectedResponse() *mockHddTempConn {
256
return &mockHddTempConn{
257
hddTempLine: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
@@ -283,21 +265,8 @@ func prepareMockEmptyResponse() *mockHddTempConn {
265
}
266
267
type mockHddTempConn struct {
286
- errOnConnect bool
268
errOnQueryHddTemp bool
269
hddTempLine string
289
- disconnectCalled bool
290
-}
291
-
292
-func (m *mockHddTempConn) connect() error {
293
- if m.errOnConnect {
294
- return errors.New("mock.connect() error")
295
- }
296
- return nil
297
-}
298
-
299
-func (m *mockHddTempConn) disconnect() {
300
- m.disconnectCalled = true
270
}
271
272
func (m *mockHddTempConn) queryHddTemp() (string, error) {