@cryptotaxi247 / netdata-1 / commits / 7ed4ea779

go.d sd local-listeners fix extractComm (#17763)

Ilya Mashchenko committed May 28, 2024 at 13:32 UTC 7ed4ea779c1f5de969154d6d3a0f697c1901058e
2 files changed +12 -4
src/go/collectors/go.d.plugin/agent/discovery/sd/discoverer/netlisteners/netlisteners.go
+3 -4
@@ -308,11 +308,10 @@ func (e *localListenersExec) discover(ctx context.Context) ([]byte, error) {
308 }
309
310 func extractComm(cmdLine string) string {
311 - i := strings.IndexByte(cmdLine, ' ')
312 - if i <= 0 {
313 - return strings.TrimSuffix(cmdLine, ":")
311 + if i := strings.IndexByte(cmdLine, ' '); i != -1 {
312 + cmdLine = cmdLine[:i]
313 }
315 - _, comm := filepath.Split(cmdLine[:i])
314 + _, comm := filepath.Split(cmdLine)
315 return strings.TrimSuffix(comm, ":")
316 }
317
src/go/collectors/go.d.plugin/agent/discovery/sd/discoverer/netlisteners/netlisteners_test.go
+9
@@ -13,6 +13,7 @@ func TestDiscoverer_Discover(t *testing.T) {
13 tests := map[string]discoverySim{
14 "add listeners": {
15 listenersCli: func(cli listenersCli, interval, expiry time.Duration) {
16 + cli.addListener("UDP|127.0.0.1|323|/usr/sbin/chronyd")
17 cli.addListener("UDP6|::1|8125|/opt/netdata/usr/sbin/netdata -P /run/netdata/netdata.pid -D")
18 cli.addListener("TCP6|::1|8125|/opt/netdata/usr/sbin/netdata -P /run/netdata/netdata.pid -D")
19 cli.addListener("TCP6|::|8125|/opt/netdata/usr/sbin/netdata -P /run/netdata/netdata.pid -D")
@@ -31,6 +32,14 @@ func TestDiscoverer_Discover(t *testing.T) {
32 provider: "sd:net_listeners",
33 source: "discoverer=net_listeners,host=localhost",
34 targets: []model.Target{
35 + withHash(&target{
36 + Protocol: "UDP",
37 + IPAddress: "127.0.0.1",
38 + Port: "323",
39 + Address: "127.0.0.1:323",
40 + Comm: "chronyd",
41 + Cmdline: "/usr/sbin/chronyd",
42 + }),
43 withHash(&target{
44 Protocol: "TCP",
45 IPAddress: "127.0.0.1",