@@ -87,20 +87,46 @@ func Discover(ctx context.Context, log logger.ContextL, pollDuration time.Durati
87
87
}
88
88
defer mdb .Close ()
89
89
90
+ ignoreMap := map [string ]bool {}
91
+ for _ , ip := range conf .Disco .IgnoreList {
92
+ ignoreMap [ip ] = true
93
+ }
94
+
95
+ foundDevices := map [string ]* kt.SnmpDeviceConfig {}
90
96
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 )
92
105
if err != nil {
93
106
return nil , err
94
107
}
95
- return nil , nil
96
108
}
97
109
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
+ }
101
116
}
102
117
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 {
104
130
for _ , ipr := range conf .Disco .Cidrs {
105
131
_ , _ , err := net .ParseCIDR (ipr )
106
132
if err != nil {
@@ -121,11 +147,11 @@ func Discover(ctx context.Context, log logger.ContextL, pollDuration time.Durati
121
147
timeout := time .Millisecond * time .Duration (conf .Global .TimeoutMS )
122
148
scanner := scan .NewDeviceScanner (targetIterator , timeout )
123
149
if err := scanner .Start (); err != nil {
124
- return nil , err
150
+ return err
125
151
}
126
152
results , err := scanner .Scan (ctx , conf .Disco .Ports )
127
153
if err != nil {
128
- return nil , err
154
+ return err
129
155
}
130
156
var wg sync.WaitGroup
131
157
var mux sync.RWMutex
@@ -145,22 +171,7 @@ func Discover(ctx context.Context, log logger.ContextL, pollDuration time.Durati
145
171
log .Infof ("Checked %d ips in %v (from start: %v)" , len (results ), time .Now ().Sub (st ), time .Now ().Sub (stb ))
146
172
}
147
173
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
164
175
}
165
176
166
177
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 ) {
0 commit comments