master
go 63 lines 1.42 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package snmpsd
4
5 import (
6 "fmt"
7 "sync"
8
9 "github.com/netdata/netdata/go/plugins/plugin/agent/discovery/sd/model"
10 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/snmputils"
11 )
12
13 func targetSource(sub subnet) string { return fmt.Sprintf("discoverer=snmp,network=%s", subKey(sub)) }
14
15 func newTargetGroup(sub subnet) *targetGroup {
16 return &targetGroup{
17 provider: "sd:snmpdiscoverer",
18 source: targetSource(sub),
19 }
20 }
21
22 type targetGroup struct {
23 provider string
24 source string
25 mux sync.Mutex
26 targets []model.Target
27 }
28
29 func (g *targetGroup) Provider() string { return g.provider }
30 func (g *targetGroup) Source() string { return g.source }
31 func (g *targetGroup) Targets() []model.Target { return g.targets }
32
33 func (g *targetGroup) addTarget(tg model.Target) {
34 g.mux.Lock()
35 defer g.mux.Unlock()
36 g.targets = append(g.targets, tg)
37 }
38
39 func newTarget(ip string, cred CredentialConfig, si snmputils.SysInfo) *target {
40 tg := &target{
41 IPAddress: ip,
42 Credential: cred,
43 SysInfo: si,
44 }
45
46 tg.hash, _ = model.CalcHash(tg)
47
48 return tg
49 }
50
51 type (
52 target struct {
53 model.Base `hash:"ignore"`
54 hash uint64
55
56 IPAddress string
57 Credential CredentialConfig `hash:"ignore"`
58 SysInfo snmputils.SysInfo `hash:"ignore"`
59 }
60 )
61
62 func (t *target) TUID() string { return fmt.Sprintf("snmp_%s_%d", t.IPAddress, t.hash) }
63 func (t *target) Hash() uint64 { return t.hash }