Skip to content

Commit 1a149a3

Browse files
committed
Mostly a wip
1 parent 21ba9f1 commit 1a149a3

File tree

3 files changed

+86
-29
lines changed

3 files changed

+86
-29
lines changed

pkg/inputs/snmp/disco.go

+35-24
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,46 @@ func Discover(ctx context.Context, log logger.ContextL, pollDuration time.Durati
8787
}
8888
defer mdb.Close()
8989

90+
ignoreMap := map[string]bool{}
91+
for _, ip := range conf.Disco.IgnoreList {
92+
ignoreMap[ip] = true
93+
}
94+
95+
foundDevices := map[string]*kt.SnmpDeviceConfig{}
9096
if conf.Disco.NetboxAPIHost != "" {
91-
err = getDevicesFromNetbox(ctx, conf, log)
97+
log.Infof("Discovering devices from Netbox with tag %s", conf.Disco.NetboxTag)
98+
err = getDevicesFromNetbox(ctx, ctl, foundDevices, mdb, conf, kentikDevices, log, ignoreMap)
99+
if err != nil {
100+
return nil, err
101+
}
102+
} else {
103+
log.Infof("Discovering devices from network scan")
104+
err := runScanCheckDisco(ctx, ctl, foundDevices, mdb, conf, kentikDevices, log, ignoreMap)
92105
if err != nil {
93106
return nil, err
94107
}
95-
return nil, nil
96108
}
97109

98-
ignoreMap := map[string]bool{}
99-
for _, ip := range conf.Disco.IgnoreList {
100-
ignoreMap[ip] = true
110+
var stats *SnmpDiscoDeviceStat
111+
if conf.Disco.AddDevices {
112+
stats, err = addDevices(ctx, foundDevices, snmpFile, conf, false, log, pollDuration)
113+
if err != nil {
114+
return nil, err
115+
}
101116
}
102117

103-
foundDevices := map[string]*kt.SnmpDeviceConfig{}
118+
// Phone home if we have one of these set up.
119+
if confMgr != nil && (stats.added > 0 || stats.replaced > 0) {
120+
confMgr.DeviceDiscovery(conf.Devices)
121+
}
122+
123+
time.Sleep(2 * time.Second) // Give logs time to get sent back.
124+
125+
return stats, nil
126+
}
127+
128+
func runScanCheckDisco(ctx context.Context, ctl chan bool, foundDevices map[string]*kt.SnmpDeviceConfig,
129+
mdb *mibs.MibDB, conf *kt.SnmpConfig, kentikDevices map[string]string, log logger.ContextL, ignoreMap map[string]bool) error {
104130
for _, ipr := range conf.Disco.Cidrs {
105131
_, _, err := net.ParseCIDR(ipr)
106132
if err != nil {
@@ -121,11 +147,11 @@ func Discover(ctx context.Context, log logger.ContextL, pollDuration time.Durati
121147
timeout := time.Millisecond * time.Duration(conf.Global.TimeoutMS)
122148
scanner := scan.NewDeviceScanner(targetIterator, timeout)
123149
if err := scanner.Start(); err != nil {
124-
return nil, err
150+
return err
125151
}
126152
results, err := scanner.Scan(ctx, conf.Disco.Ports)
127153
if err != nil {
128-
return nil, err
154+
return err
129155
}
130156
var wg sync.WaitGroup
131157
var mux sync.RWMutex
@@ -145,22 +171,7 @@ func Discover(ctx context.Context, log logger.ContextL, pollDuration time.Durati
145171
log.Infof("Checked %d ips in %v (from start: %v)", len(results), time.Now().Sub(st), time.Now().Sub(stb))
146172
}
147173

148-
var stats *SnmpDiscoDeviceStat
149-
if conf.Disco.AddDevices {
150-
stats, err = addDevices(ctx, foundDevices, snmpFile, conf, false, log, pollDuration)
151-
if err != nil {
152-
return nil, err
153-
}
154-
}
155-
156-
// Phone home if we have one of these set up.
157-
if confMgr != nil && (stats.added > 0 || stats.replaced > 0) {
158-
confMgr.DeviceDiscovery(conf.Devices)
159-
}
160-
161-
time.Sleep(2 * time.Second) // Give logs time to get sent back.
162-
163-
return stats, nil
174+
return nil
164175
}
165176

166177
func RunDiscoOnTimer(ctx context.Context, c chan os.Signal, log logger.ContextL, pollTimeMin int, checkNow bool, cfg *ktranslate.SNMPInputConfig, apic *api.KentikApi, confMgr config.ConfigManager) {

pkg/inputs/snmp/netbox.go

+50-5
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,73 @@ Use netbox API to pull down list of devices to target. Get all devices matching
55
*/
66
import (
77
"context"
8+
"fmt"
9+
"net"
10+
"net/netip"
11+
"sync"
12+
"time"
813

914
"github.com/kentik/ktranslate/pkg/eggs/logger"
15+
"github.com/kentik/ktranslate/pkg/inputs/snmp/mibs"
1016
"github.com/kentik/ktranslate/pkg/kt"
1117

18+
"github.com/liamg/furious/scan"
1219
"github.com/netbox-community/go-netbox/v4"
1320
)
1421

15-
func getDevicesFromNetbox(ctx context.Context, conf *kt.SnmpConfig, log logger.ContextL) error {
22+
func getDevicesFromNetbox(ctx context.Context, ctl chan bool, foundDevices map[string]*kt.SnmpDeviceConfig,
23+
mdb *mibs.MibDB, conf *kt.SnmpConfig, kentikDevices map[string]string, log logger.ContextL, ignoreMap map[string]bool) error {
1624
c := netbox.NewAPIClientFor(conf.Disco.NetboxAPIHost, conf.Disco.NetboxAPIToken)
1725

26+
limit := int32(0) // This implies we see all. Do we need to implement paging?
1827
res, _, err := c.DcimAPI.
1928
DcimDevicesList(ctx).
20-
Status([]string{"active"}).
21-
Limit(10).
29+
Status([]string{"active"}). // Only look at active devices.
30+
Limit(limit).
2231
Execute()
2332

2433
if err != nil {
2534
return err
2635
}
2736

28-
log.Infof("%v", res.Results)
37+
timeout := time.Millisecond * time.Duration(conf.Global.TimeoutMS)
38+
stb := time.Now()
39+
results := make([]scan.Result, 0)
40+
for _, res := range res.Results {
41+
hasTag := false
42+
for _, tag := range res.Tags {
43+
if tag.Display == conf.Disco.NetboxTag {
44+
hasTag = true
45+
continue
46+
}
47+
}
2948

30-
return nil
49+
if hasTag && res.PrimaryIp.IsSet() {
50+
addr := res.PrimaryIp.Get().GetAddress()
51+
ipv, err := netip.ParsePrefix(addr)
52+
if err != nil {
53+
log.Infof("Skipping %s with bad PrimaryIP: %v", res.Display, addr)
54+
} else {
55+
results = append(results, scan.Result{Name: res.Display, Manufacturer: res.DeviceType.GetDisplay(), Host: net.ParseIP(ipv.Addr().String())})
56+
}
57+
}
58+
59+
}
3160

61+
var wg sync.WaitGroup
62+
var mux sync.RWMutex
63+
st := time.Now()
64+
log.Infof("Starting to check %d ips from netbox", len(results))
65+
for i, result := range results {
66+
if ignoreMap[result.Host.String()] { // If we have marked this ip as to be ignored, don't do anything more with it.
67+
continue
68+
}
69+
wg.Add(1)
70+
posit := fmt.Sprintf("%d/%d)", i+1, len(results))
71+
go doubleCheckHost(result, timeout, ctl, &mux, &wg, foundDevices, mdb, conf, posit, kentikDevices, log)
72+
}
73+
wg.Wait()
74+
log.Infof("Checked %d ips in %v (from start: %v)", len(results), time.Now().Sub(st), time.Now().Sub(stb))
75+
76+
return nil
3277
}

pkg/kt/snmp.go

+1
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ type SnmpDiscoConfig struct {
260260
NoUseBulkWalkAll bool `yaml:"no_use_bulkwalkall"`
261261
NetboxAPIHost string `yaml:"netbox_host"`
262262
NetboxAPIToken string `yaml:"netbox_token"`
263+
NetboxTag string `yaml:"netbox_tag"`
263264
}
264265

265266
type ProviderMap struct {

0 commit comments

Comments
 (0)