-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnetbox.go
618 lines (494 loc) · 14.8 KB
/
netbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//
// Copyright (c) 2023 Cloudflare, Inc.
//
// Licensed under Apache 2.0 license found in the LICENSE file
// or at http://www.apache.org/licenses/LICENSE-2.0
//
package netbox
import (
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
dbModel "github.com/cloudflare/octopus/pkg/connector/netbox/model"
nbUtils "github.com/cloudflare/octopus/pkg/connector/netbox/utils"
"github.com/cloudflare/octopus/pkg/model"
"github.com/cloudflare/octopus/proto/octopus"
bnet "github.com/bio-routing/bio-rd/net"
log "github.com/sirupsen/logrus"
)
const (
connectorName = "Netbox"
updateInterval = time.Minute * 2
)
type NetboxConnector struct {
connectorMu sync.RWMutex
client NetboxClientI
loadDuration time.Duration
loadTime time.Time
refreshErrorCount atomic.Uint64
devices map[int64]*dbModel.DcimDevice
interfaces map[int64]*dbModel.DcimInterface
ipAddresses []*dbModel.IpamIpaddress
cables []*dbModel.DcimCable
prefixes []*dbModel.IpamPrefix
circuits map[int64]*dbModel.CircuitsCircuit
circuitTerminations map[int64]*dbModel.CircuitsCircuittermination
frontPorts map[int64]*dbModel.DcimFrontport
rearPorts map[int64]*dbModel.DcimRearport
}
type NetboxClientI interface {
Connect() error
GetDBHost() string
GetDevices() ([]*dbModel.DcimDevice, error)
GetInterfaces() (map[int64]*dbModel.DcimInterface, error)
GetIPAddresses() ([]*dbModel.IpamIpaddress, error)
GetCables() ([]*dbModel.DcimCable, error)
GetPrefixes() ([]*dbModel.IpamPrefix, error)
GetDcimInterfaceTypeID() int32
GetCircuitsCircuitterminationTypeID() int32
GetDcimFrontPortTypeID() int32
GetDcimRearPortTypeID() int32
GetCircuits() ([]*dbModel.CircuitsCircuit, error)
GetCircuitTerminations() ([]*dbModel.CircuitsCircuittermination, error)
GetFrontPorts() ([]*dbModel.DcimFrontport, error)
GetRearPorts() ([]*dbModel.DcimRearport, error)
}
func NewConnector(host string, port uint, user string, password string, dbName string, useTLS bool, caCertPath string, logDBQueries bool) *NetboxConnector {
dbParams := dbParams{
host: host,
port: port,
dBname: dbName,
user: user,
password: password,
useTLS: useTLS,
caCertPath: caCertPath,
logDBQueries: logDBQueries,
}
return newNetboxConnectorWithClient(&NetboxClient{
db: newDB(dbParams),
})
}
func newNetboxConnectorWithClient(apiClient NetboxClientI) *NetboxConnector {
return &NetboxConnector{
client: apiClient,
}
}
func (n *NetboxConnector) InitialLoad() error {
return n.update()
}
func (n *NetboxConnector) Healthy() bool {
n.connectorMu.RLock()
defer n.connectorMu.RUnlock()
return n._healthy()
}
func (n *NetboxConnector) _healthy() bool {
return len(n.devices) > 0
}
func (n *NetboxConnector) GetLoadDuration() time.Duration {
return n.loadDuration
}
func (n *NetboxConnector) GetLoadTime() time.Time {
return n.loadTime
}
func (n *NetboxConnector) GetName() string {
return connectorName
}
func (n *NetboxConnector) GetUpdateErrorCount() uint64 {
return n.refreshErrorCount.Load()
}
func (n *NetboxConnector) EnrichTopology(t *model.Topology) error {
n.connectorMu.RLock()
defer n.connectorMu.RUnlock()
if !n._healthy() {
return fmt.Errorf("%s not healthy", connectorName)
}
return n._enrichTopology(t)
}
func (n *NetboxConnector) _enrichTopology(t *model.Topology) error {
err := n.addDevices(t)
if err != nil {
return fmt.Errorf("failed to enrich devices: %v", err)
}
err = n.addInterfaces(t)
if err != nil {
return fmt.Errorf("failed to enrich interfaces: %v", err)
}
err = n.addInterfaceUnits(t)
if err != nil {
return fmt.Errorf("failed to enrich interface units: %v", err)
}
err = n.addIPAddresses(t)
if err != nil {
return fmt.Errorf("failed to enrich IP addresses: %v", err)
}
err = n.addCables(t)
if err != nil {
return fmt.Errorf("failed to enrich cables: %v", err)
}
err = n.addCircuits(t)
if err != nil {
return fmt.Errorf("failed to enrich circuits: %v", err)
}
err = n.addRearPorts(t)
if err != nil {
return fmt.Errorf("failed to enrich rear ports: %v", err)
}
err = n.addFrontPorts(t)
if err != nil {
return fmt.Errorf("failed to enrich front ports: %v", err)
}
err = n.addPrefixes(t)
if err != nil {
return fmt.Errorf("failed to add prefixes: %v", err)
}
return nil
}
func (n *NetboxConnector) addDevices(t *model.Topology) error {
for _, d := range n.devices {
topoDev := t.AddDeviceIfNotExists(d.Name)
s := t.AddSiteIfNotExists(d.Site.Name)
topoDev.Site = s
topoDev.Role = d.DeviceRole.Slug
topoDev.DeviceType = d.DeviceType.Slug
md, err := nbUtils.GetMetaDataFromTags(d.Tags)
if err != nil {
return fmt.Errorf("unable to get meta data: %v", err)
}
topoDev.MetaData = md
}
return nil
}
func (n *NetboxConnector) addInterfaces(t *model.Topology) error {
for _, nbIfa := range n.interfaces {
if nbIfa.Parent != nil {
continue
}
d := t.GetDevice(nbIfa.Device.Name)
if d == nil {
return fmt.Errorf("can not find device %q", nbIfa.Device.Name)
}
ifa := d.AddInterfaceItNotExists(nbIfa.Name)
t.DevicesByInterfaceID[nbIfa.ID] = d
t.Interfaces[nbIfa.ID] = ifa
md, err := nbUtils.GetMetaDataFromTags(nbIfa.Tags)
if err != nil {
return fmt.Errorf("unable to get meta data: %v", err)
}
ifa.MetaData = md
ifa.Type = nbIfa.Type
if nbIfa.LAG != nil {
ifa.LAGMemberOf = nbIfa.LAG.Name
}
}
return nil
}
func (n *NetboxConnector) addInterfaceUnits(t *model.Topology) error {
for _, nbIfa := range n.interfaces {
if nbIfa.Parent == nil {
continue
}
if !strings.Contains(nbIfa.Name, ".") {
continue
}
unitStr := strings.TrimPrefix(nbIfa.Name, nbIfa.Parent.Name+".")
d := t.GetDevice(nbIfa.Device.Name)
if d == nil {
return fmt.Errorf("can not find device %q", nbIfa.Device.Name)
}
ifa := d.GetInterface(nbIfa.Parent.Name)
if ifa == nil {
return fmt.Errorf("can not find interface %s:%s", nbIfa.Device.Name, nbIfa.Parent.Name)
}
vlanTag, err := nbUtils.ParseUnitStr(unitStr)
if err != nil {
// In this case the interface name is not following the convention `<parent_interface_name>.<unit>` (e.g., ending with ".").
// So we ignore it.
log.Warnf("unable to convert unit %q (id=%d) (interface %q) to int for %s:%s. Ignoring.", unitStr, nbIfa.ID, nbIfa.Name, nbIfa.Device.Name, nbIfa.Parent.Name)
}
u := ifa.AddUnitIfNotExists(vlanTag)
t.DevicesByInterfaceID[nbIfa.ID] = d
md, err := nbUtils.GetMetaDataFromTags(nbIfa.Tags)
if err != nil {
return fmt.Errorf("unable to get meta data: %v", err)
}
u.MetaData = md
}
return nil
}
func (n *NetboxConnector) addIPAddresses(t *model.Topology) error {
for _, nbIP := range n.ipAddresses {
if nbIP.AssignedObjectID == 0 || nbIP.AssignedObjectTypeID != n.client.GetDcimInterfaceTypeID() {
continue
}
dcimIfa, exists := n.interfaces[nbIP.AssignedObjectID]
if !exists {
continue
}
ifaID := nbIP.AssignedObjectID
if dcimIfa.ParentID != 0 {
ifaID = dcimIfa.ParentID
}
ifa := t.Interfaces[ifaID]
if ifa == nil {
return fmt.Errorf("interface with id %d not found", ifaID)
}
_, vt, err := nbUtils.GetInterfaceAndVLANTag(dcimIfa.Name)
if err != nil {
return fmt.Errorf("unable to extract interface name and unit from %q: %v", dcimIfa.Name, err)
}
pfx, err := bnet.PrefixFromString(nbUtils.SanitizeIPAddress(nbIP.Address))
if err != nil {
return fmt.Errorf("failed to parse IP %q: %v", nbIP.Address, err)
}
ip := model.NewIP(*pfx)
nbUtils.GetCustomFieldData(ip.MetaData, nbIP.CustomFieldData)
u := ifa.AddUnitIfNotExists(vt)
if pfx.Addr().IsIPv4() {
u.IPv4Addresses = append(u.IPv4Addresses, ip)
} else {
u.IPv6Addresses = append(u.IPv6Addresses, ip)
}
}
return nil
}
func (n *NetboxConnector) addPrefixes(t *model.Topology) error {
for _, p := range n.prefixes {
pfx, err := bnet.PrefixFromString(p.Prefix)
if err != nil {
return fmt.Errorf("failed to parse Prefix %q: %v", p.Prefix, err)
}
md, err := nbUtils.GetMetaDataFromTags(p.Tags)
if err != nil {
return fmt.Errorf("failed to get Tags for Prefix %q: %v", p.Prefix, err)
}
oPfx := model.NewPrefix(*pfx)
oPfx.MetaData = md
t.Prefixes[p.ID] = oPfx
}
return nil
}
func (n *NetboxConnector) getCableEnd(terminationType int32, terminationID int64, t *model.Topology) (*model.CableEnd, error) {
ce := model.CableEnd{}
switch terminationType {
case n.client.GetDcimInterfaceTypeID():
ce.EndpointType = octopus.CableEndpointType_CABLE_ENDPOINT_TYPE_INTERFACE
ifa, exists := t.Interfaces[terminationID]
if !exists {
return nil, fmt.Errorf("unable to find interface %d", terminationID)
}
dev, exists := t.DevicesByInterfaceID[terminationID]
if !exists {
return nil, fmt.Errorf("unable to find device by interface id %d", terminationID)
}
ce.DeviceName = dev.Name
ce.EndpointName = ifa.Name
case n.client.GetCircuitsCircuitterminationTypeID():
ce.EndpointType = octopus.CableEndpointType_CABLE_ENDPOINT_TYPE_CIRCUIT_TERMINATION
cktTerm := n.circuitTerminations[terminationID]
if cktTerm == nil {
return nil, fmt.Errorf("unable to find circuit termination %d", terminationID)
}
ckt := n.circuits[cktTerm.CircuitID]
if ckt == nil {
return nil, fmt.Errorf("unable to find circuit %d", cktTerm.ID)
}
ce.DeviceName = ckt.Cid
if ckt.TerminationAID == terminationID {
ce.EndpointName = "A"
}
if ckt.TerminationZID == terminationID {
ce.EndpointName = "Z"
}
case n.client.GetDcimFrontPortTypeID():
ce.EndpointType = octopus.CableEndpointType_CABLE_ENDPOINT_TYPE_FRONT_PORT
fp := n.frontPorts[terminationID]
if fp == nil {
return nil, fmt.Errorf("unable to find front port %d", terminationID)
}
d := n.devices[fp.DeviceID]
if d == nil {
return nil, fmt.Errorf("unable to find device with id %d", fp.DeviceID)
}
ce.DeviceName = d.Name
ce.EndpointName = fp.Name
case n.client.GetDcimRearPortTypeID():
ce.EndpointType = octopus.CableEndpointType_CABLE_ENDPOINT_TYPE_REAR_PORT
rp := n.rearPorts[terminationID]
if rp == nil {
return nil, fmt.Errorf("unable to find rear port %d", terminationID)
}
d := n.devices[rp.DeviceID]
if d == nil {
return nil, fmt.Errorf("unable to find device with id %d", rp.DeviceID)
}
ce.DeviceName = d.Name
ce.EndpointName = rp.Name
default:
// consoleport, consoleserverport, powerport, or poweroutlet
return nil, fmt.Errorf("don't know what to do with cable termination ID %d (type %d)", terminationID, terminationType)
}
return &ce, nil
}
func (n *NetboxConnector) addCables(t *model.Topology) error {
for _, c := range n.cables {
// For now, all terminations should have exactly 2 ends.
if len(c.Terminations) != 2 {
continue
}
termination_a := c.Terminations[0]
termination_b := c.Terminations[1]
if termination_a.TerminationID == 0 || termination_b.TerminationID == 0 {
continue
}
AEnd, err := n.getCableEnd(termination_a.TerminationTypeID, termination_a.TerminationID, t)
if err != nil {
continue
}
BEnd, err := n.getCableEnd(termination_b.TerminationTypeID, termination_b.TerminationID, t)
if err != nil {
continue
}
cable := model.Cable{
AEnd: *AEnd,
BEnd: *BEnd,
}
t.Cables[cable.String()] = &cable
}
return nil
}
func (n *NetboxConnector) addCircuits(t *model.Topology) error {
for _, c := range n.circuits {
t.Circuits[c.Cid] = model.NewCircuit(c.Cid, c.Provider.Slug, c.Type.Slug, c.Status)
}
return nil
}
func (n *NetboxConnector) addRearPorts(t *model.Topology) error {
for _, rp := range n.rearPorts {
nbDev := n.devices[rp.DeviceID]
if nbDev == nil {
return fmt.Errorf("device %d not found", rp.DeviceID)
}
d := t.GetDevice(nbDev.Name)
if d == nil {
return fmt.Errorf("can not find device %q", nbDev.Name)
}
d.RearPorts[rp.Name] = &model.RearPort{
Name: rp.Name,
Positions: rp.Positions,
}
}
return nil
}
func (n *NetboxConnector) addFrontPorts(t *model.Topology) error {
for _, fp := range n.frontPorts {
nbDev := n.devices[fp.DeviceID]
if nbDev == nil {
return fmt.Errorf("device %d not found", fp.DeviceID)
}
d := t.GetDevice(nbDev.Name)
if d == nil {
return fmt.Errorf("can not find device %q", nbDev.Name)
}
rpName := ""
rp := n.rearPorts[fp.RearPortID]
if rp != nil {
rpName = rp.Name
}
d.FrontPorts[fp.Name] = &model.FrontPort{
Name: fp.Name,
RearPort: rpName,
RearPortPosition: uint32(fp.RearPortPosition),
}
}
return nil
}
func (n *NetboxConnector) StartRefreshRoutine() {
go n.refreshRoutine()
}
func (n *NetboxConnector) refreshRoutine() {
ticker := time.NewTicker(updateInterval)
for {
err := n.update()
if err != nil {
n.refreshErrorCount.Add(1)
log.Errorf("Failed to refresh Netbox data: %v", err)
} else {
log.Infof("Successfully refreshed Netbox data from %q", n.client.GetDBHost())
}
<-ticker.C
}
}
func (n *NetboxConnector) update() error {
startTime := time.Now()
err := n.client.Connect()
if err != nil {
return fmt.Errorf("failed to connect: %v", err)
}
devices, err := n.client.GetDevices()
if err != nil {
return fmt.Errorf("unable to get devices: %v", err)
}
interfaces, err := n.client.GetInterfaces()
if err != nil {
return fmt.Errorf("unable to get interfaces: %v", err)
}
ips, err := n.client.GetIPAddresses()
if err != nil {
return fmt.Errorf("unable to get IP addresses: %v", err)
}
cables, err := n.client.GetCables()
if err != nil {
return fmt.Errorf("unable to get cables: %v", err)
}
prefixes, err := n.client.GetPrefixes()
if err != nil {
return fmt.Errorf("unable to get prefixes: %v", err)
}
circuits, err := n.client.GetCircuits()
if err != nil {
return fmt.Errorf("unable to get circuits: %v", err)
}
cts, err := n.client.GetCircuitTerminations()
if err != nil {
return fmt.Errorf("unable to get circuit terminations: %v", err)
}
fps, err := n.client.GetFrontPorts()
if err != nil {
return fmt.Errorf("unable to get front ports: %v", err)
}
rps, err := n.client.GetRearPorts()
if err != nil {
return fmt.Errorf("unable to get rear ports: %v", err)
}
n.connectorMu.Lock()
defer n.connectorMu.Unlock()
n.devices = make(map[int64]*dbModel.DcimDevice)
for _, d := range devices {
n.devices[d.ID] = d
}
n.interfaces = interfaces
n.ipAddresses = ips
n.cables = cables
n.prefixes = prefixes
n.circuits = make(map[int64]*dbModel.CircuitsCircuit)
for _, ckt := range circuits {
n.circuits[ckt.ID] = ckt
}
n.circuitTerminations = make(map[int64]*dbModel.CircuitsCircuittermination)
for _, ct := range cts {
n.circuitTerminations[ct.ID] = ct
}
n.frontPorts = make(map[int64]*dbModel.DcimFrontport)
for _, fp := range fps {
n.frontPorts[fp.ID] = fp
}
n.rearPorts = make(map[int64]*dbModel.DcimRearport)
for _, rp := range rps {
n.rearPorts[rp.ID] = rp
}
n.loadDuration = time.Since(startTime)
n.loadTime = time.Now()
return nil
}