forked from scottdware/go-junos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.go
413 lines (355 loc) · 15 KB
/
views.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
package junos
import (
"encoding/xml"
"errors"
"strings"
"github.com/Juniper/go-netconf/netconf"
)
// ArpTable contains the ARP table on the device.
type ArpTable struct {
Count int `xml:"arp-entry-count"`
Entries []ArpEntry `xml:"arp-table-entry"`
}
// ArpEntry holds each individual ARP entry.
type ArpEntry struct {
MACAddress string `xml:"mac-address"`
IPAddress string `xml:"ip-address"`
Interface string `xml:"interface-name"`
}
// RoutingTable contains every routing table on the device.
type RoutingTable struct {
RouteTables []RouteTable `xml:"route-table"`
}
// RouteTable holds all the route information for each table.
type RouteTable struct {
Name string `xml:"table-name"`
TotalRoutes int `xml:"total-route-count"`
ActiveRoutes int `xml:"active-route-count"`
HolddownRoutes int `xml:"holddown-route-count"`
HiddenRoutes int `xml:"hidden-routes"`
Entries []Route `xml:"rt"`
}
// Route holds information about each individual route.
type Route struct {
Destination string `xml:"rt-destination"`
Active string `xml:"rt-entry>active-tag"`
Protocol string `xml:"rt-entry>protocol-name"`
Preference int `xml:"rt-entry>preference"`
Age string `xml:"rt-entry>age"`
NextHop string `xml:"rt-entry>nh>to,omitempty"`
NextHopInterface string `xml:"rt-entry>nh>via,omitempty"`
NextHopTable string `xml:"rt-entry>nh>nh-table,omitempty"`
NextHopLocalInterface string `xml:"rt-entry>nh>nh-local-interface,omitempty"`
}
// Interfaces contains information about every interface on the device.
type Interfaces struct {
Entries []PhysicalInterface `xml:"physical-interface"`
}
// PhysicalInterface contains information about each individual physical interface.
type PhysicalInterface struct {
Name string `xml:"name"`
AdminStatus string `xml:"admin-status"`
OperStatus string `xml:"oper-status"`
LocalIndex int `xml:"local-index"`
SNMPIndex int `xml:"snmp-index"`
LinkLevelType string `xml:"link-level-type"`
MTU string `xml:"mtu"`
LinkMode string `xml:"link-mode"`
Speed string `xml:"speed"`
FlowControl string `xml:"if-flow-control"`
AutoNegotiation string `xml:"if-auto-negotiation"`
HardwarePhysicalAddress string `xml:"hardware-physical-address"`
Flapped string `xml:"interface-flapped"`
InputBps int `xml:"traffic-statistics>input-bps"`
InputPps int `xml:"traffic-statistics>input-pps"`
OutputBps int `xml:"traffic-statistics>output-bps"`
OutputPps int `xml:"traffic-statistics>output-pps"`
LogicalInterfaces []LogicalInterface `xml:"logical-interface"`
}
// LogicalInterface contains information about the logical interfaces tied to a physical interface.
type LogicalInterface struct {
Name string `xml:"name"`
MTU string `xml:"address-family>mtu"`
IPAddress string `xml:"address-family>interface-address>ifa-local"`
LocalIndex int `xml:"local-index"`
SNMPIndex int `xml:"snmp-index"`
Encapsulation string `xml:"encapsulation"`
LAGInputPackets int `xml:"lag-traffic-statistics>lag-bundle>input-packets"`
LAGInputPps int `xml:"lag-traffic-statistics>lag-bundle>input-pps"`
LAGInputBytes int `xml:"lag-traffic-statistics>lag-bundle>input-bytes"`
LAGInputBps int `xml:"lag-traffic-statistics>lag-bundle>input-bps"`
LAGOutputPackets int `xml:"lag-traffic-statistics>lag-bundle>output-packets"`
LAGOutputPps int `xml:"lag-traffic-statistics>lag-bundle>output-pps"`
LAGOutputBytes int `xml:"lag-traffic-statistics>lag-bundle>output-bytes"`
LAGOutputBps int `xml:"lag-traffic-statistics>lag-bundle>output-bps"`
ZoneName string `xml:"logical-interface-zone-name"`
InputPackets int `xml:"traffic-statistics>input-packets"`
OutputPackets int `xml:"traffic-statistics>output-packets"`
AddressFamily string `xml:"address-family>address-family-name"`
AggregatedEthernet string `xml:"address-family>ae-bundle-name,omitempty"`
}
// Vlans contains all of the VLAN information on the device.
type Vlans struct {
Entries []Vlan `xml:"l2ng-l2ald-vlan-instance-group"`
}
// Vlan contains information about each individual VLAN.
type Vlan struct {
Name string `xml:"l2ng-l2rtb-vlan-name"`
Tag int `xml:"l2ng-l2rtb-vlan-tag"`
MemberInterfaces []string `xml:"l2ng-l2rtb-vlan-member>l2ng-l2rtb-vlan-member-interface"`
}
// EthernetSwitchingTable contains the ethernet-switching table on the device.
type EthernetSwitchingTable struct {
Entries []L2MACEntry `xml:"l2ng-l2ald-mac-entry-vlan"`
}
// L2MACEntry contains information about every MAC address on each VLAN.
type L2MACEntry struct {
GlobalMACCount int `xml:"mac-count-global"`
LearnedMACCount int `xml:"learnt-mac-count"`
RoutingInstance string `xml:"l2ng-l2-mac-routing-instance"`
VlanID int `xml:"l2ng-l2-vlan-id"`
MACEntries []MACEntry `xml:"l2ng-mac-entry"`
}
// MACEntry contains information about each individual MAC address. Flags are: S - static MAC, D - dynamic MAC,
// L - locally learned, P - persistent static, SE - statistics enabled, NM - non configured MAC, R - remote PE MAC,
// O - ovsdb MAC.
type MACEntry struct {
VlanName string `xml:"l2ng-l2-mac-vlan-name"`
MACAddress string `xml:"l2ng-l2-mac-address"`
Age string `xml:"l2ng-l2-mac-age"`
Flags string `xml:"l2ng-l2-mac-flags"`
LogicalInterface string `xml:"l2ng-l2-mac-logical-interface"`
}
// HardwareInventory contains all the hardware information about the device.
type HardwareInventory struct {
Chassis []Chassis `xml:"chassis"`
}
type srxHardwareInventory struct {
Chassis []Chassis `xml:"multi-routing-engine-item>chassis-inventory>chassis"`
}
// Chassis contains all of the hardware information for each chassis, such as a clustered pair of SRX's or a
// virtual-chassis configuration.
type Chassis struct {
Name string `xml:"name"`
SerialNumber string `xml:"serial-number"`
Description string `xml:"description"`
Modules []Module `xml:"chassis-module"`
}
// Module contains information about each individual module.
type Module struct {
Name string `xml:"name"`
Version string `xml:"version,omitempty"`
PartNumber string `xml:"part-number"`
SerialNumber string `xml:"serial-number"`
Description string `xml:"description"`
CLEICode string `xml:"clei-code"`
ModuleNumber string `xml:"module-number"`
SubModules []SubModule `xml:"chassis-sub-module"`
}
// SubModule contains information about each individual sub-module.
type SubModule struct {
Name string `xml:"name"`
Version string `xml:"version,omitempty"`
PartNumber string `xml:"part-number"`
SerialNumber string `xml:"serial-number"`
Description string `xml:"description"`
CLEICode string `xml:"clei-code"`
ModuleNumber string `xml:"module-number"`
SubSubModules []SubSubModule `xml:"chassis-sub-sub-module"`
}
// SubSubModule contains information about each sub-sub module, such as SFP's.
type SubSubModule struct {
Name string `xml:"name"`
Version string `xml:"version,omitempty"`
PartNumber string `xml:"part-number"`
SerialNumber string `xml:"serial-number"`
Description string `xml:"description"`
SubSubSubModules []SubSubSubModule `xml:"chassis-sub-sub-sub-module"`
}
// SubSubSubModule contains information about each sub-sub-sub module, such as SFP's on a
// PIC, which is tied to a MIC on an MX.
type SubSubSubModule struct {
Name string `xml:"name"`
Version string `xml:"version,omitempty"`
PartNumber string `xml:"part-number"`
SerialNumber string `xml:"serial-number"`
Description string `xml:"description"`
}
// VirtualChassis contains information regarding the virtual-chassis setup for the device.
type VirtualChassis struct {
PreProvisionedVCID string `xml:"preprovisioned-virtual-chassis-information>virtual-chassis-id"`
PreProvisionedVCMode string `xml:"preprovisioned-virtual-chassis-information>virtual-chassis-mode"`
Members []VCMember `xml:"member-list>member"`
}
// VCMember contains information about each individual virtual-chassis member.
type VCMember struct {
Status string `xml:"member-status"`
ID int `xml:"member-id"`
FPCSlot string `xml:"fpc-slot"`
SerialNumber string `xml:"member-serial-number"`
Model string `xml:"member-model"`
Priority int `xml:"member-priority"`
MixedMode string `xml:"member-mixed-mode"`
RouteMode string `xml:"member-route-mode"`
Role string `xml:"member-role"`
Neighbors []VCMemberNeighbor `xml:"neighbor-list>neighbor"`
}
// VCMemberNeighbor contains information about each virtual-chassis member neighbor.
type VCMemberNeighbor struct {
ID int `xml:"neighbor-id"`
Interface string `xml:"neighbor-interface"`
}
// BGPTable contains information about every BGP peer configured on the device.
type BGPTable struct {
TotalGroups int `xml:"group-count"`
TotalPeers int `xml:"peer-count"`
DownPeers int `xml:"down-peer-count"`
Entries []BGPPeer `xml:"bgp-peer"`
}
// BGPPeer contains information about each individual BGP peer.
type BGPPeer struct {
Address string `xml:"peer-address"`
ASN int `xml:"peer-as"`
InputMessages int `xml:"input-messages"`
OutputMessages int `xml:"output-messages"`
QueuedRoutes int `xml:"route-queue-count"`
Flaps int `xml:"flap-count"`
ElapsedTime string `xml:"elapsed-time"`
State string `xml:"peer-state"`
RoutingTable string `xml:"bgp-rib>name"`
ActivePrefixes int `xml:"bgp-rib>active-prefix-count"`
ReceivedPrefixes int `xml:"bgp-rib>received-prefix-count"`
AcceptedPrefixes int `xml:"bgp-rib>accepted-prefix-count"`
SuppressedPrefixes int `xml:"bgp-rib>suppressed-prefix-count"`
}
// Views contains the information for the specific views. Note that some views aren't available for specific
// hardware platforms, such as the "VirtualChassis" view on an SRX.
type Views struct {
Arp ArpTable
Route RoutingTable
Interface Interfaces
Vlan Vlans
EthernetSwitch EthernetSwitchingTable
Inventory HardwareInventory
VirtualChassis VirtualChassis
BGP BGPTable
}
var (
viewCategories = map[string]string{
"arp": "<get-arp-table-information><no-resolve/></get-arp-table-information>",
"route": "<get-route-information/>",
"interface": "<get-interface-information/>",
"vlan": "<get-vlan-information/>",
"ethernetswitch": "<get-ethernet-switching-table-information/>",
"inventory": "<get-chassis-inventory/>",
"virtualchassis": "<get-virtual-chassis-information/>",
"bgp": "<get-bgp-summary-information/>",
}
)
func validatePlatform(j *Junos, v string) error {
switch v {
case "ethernetswitch":
if strings.Contains(j.Platform[0].Model, "SRX") || strings.Contains(j.Platform[0].Model, "MX") {
return errors.New("ethernet-switching information is not available on this platform")
}
case "virtualchassis":
if strings.Contains(j.Platform[0].Model, "SRX") || strings.Contains(j.Platform[0].Model, "MX") {
return errors.New("virtual-chassis information is not available on this platform")
}
}
return nil
}
// Views gathers information on the device given the "view" specified. These views can be interrated/looped over to view the
// data (i.e. ARP table entries, interface details/statistics, routing tables, etc.). Supported views are:
// arp, route, interface, vlan, ethernetswitch, inventory.
func (j *Junos) Views(view string) (*Views, error) {
var results Views
if strings.Contains(j.Platform[0].Model, "SRX") || strings.Contains(j.Platform[0].Model, "MX") {
err := validatePlatform(j, view)
if err != nil {
return nil, err
}
}
reply, err := j.Session.Exec(netconf.RawMethod(viewCategories[view]))
if err != nil {
return nil, err
}
if reply.Errors != nil {
for _, m := range reply.Errors {
return nil, errors.New(m.Message)
}
}
if reply.Data == "" {
return nil, errors.New("no output available - please check the syntax of your command")
}
switch view {
case "arp":
var arpTable ArpTable
formatted := strings.Replace(reply.Data, "\n", "", -1)
if err := xml.Unmarshal([]byte(formatted), &arpTable); err != nil {
return nil, err
}
results.Arp = arpTable
case "route":
var routingTable RoutingTable
formatted := strings.Replace(reply.Data, "\n", "", -1)
if err := xml.Unmarshal([]byte(formatted), &routingTable); err != nil {
return nil, err
}
results.Route = routingTable
case "interface":
var ints Interfaces
formatted := strings.Replace(reply.Data, "\n", "", -1)
if err := xml.Unmarshal([]byte(formatted), &ints); err != nil {
return nil, err
}
results.Interface = ints
case "vlan":
var vlan Vlans
formatted := strings.Replace(reply.Data, "\n", "", -1)
if err := xml.Unmarshal([]byte(formatted), &vlan); err != nil {
return nil, err
}
results.Vlan = vlan
case "ethernetswitch":
var ethtable EthernetSwitchingTable
formatted := strings.Replace(reply.Data, "\n", "", -1)
if err := xml.Unmarshal([]byte(formatted), ðtable); err != nil {
return nil, err
}
results.EthernetSwitch = ethtable
case "inventory":
var inventory HardwareInventory
formatted := strings.Replace(reply.Data, "\n", "", -1)
if strings.Contains(reply.Data, "multi-routing-engine-results") {
var srxinventory srxHardwareInventory
if err := xml.Unmarshal([]byte(formatted), &srxinventory); err != nil {
return nil, err
}
for _, c := range srxinventory.Chassis {
inventory.Chassis = append(inventory.Chassis, c)
}
results.Inventory = inventory
} else {
if err := xml.Unmarshal([]byte(formatted), &inventory); err != nil {
return nil, err
}
results.Inventory = inventory
}
case "virtualchassis":
var vc VirtualChassis
formatted := strings.Replace(reply.Data, "\n", "", -1)
if err := xml.Unmarshal([]byte(formatted), &vc); err != nil {
return nil, err
}
results.VirtualChassis = vc
case "bgp":
var bgpTable BGPTable
formatted := strings.Replace(reply.Data, "\n", "", -1)
if err := xml.Unmarshal([]byte(formatted), &bgpTable); err != nil {
return nil, err
}
results.BGP = bgpTable
}
return &results, nil
}