-
Notifications
You must be signed in to change notification settings - Fork 7
/
connectedness.slax
609 lines (583 loc) · 29.3 KB
/
connectedness.slax
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
/* connectedness.slax
* Version 0.1
* 11 February 2016
*
* ben.dale@gmail.com
*
* Measure the connectedness of a node
*/
version 1.1;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
ns my = "http://www.example.com/myfunctions";
import "../import/junos.xsl";
var $arguments = {
<argument> {
<name> "mode";
<description> "Can be info (default) or execute - determines whether the script makes configuration changes";
}
}
param $mode = "info";
var $router = jcs:open();
match / {
<op-script-results> {
if ($mode = "info") {
<output> jcs:printf("%s", "Informational mode" );
call established-bgp-peers( $min-peer-count = "0" );
call ospf-marker-route( $marker-route = "11.0.0.1/32" );
call isis-marker-route( $marker-route = "11.0.0.1/32" );
call full-ospf-neighbours( $area = "0", $min-ospf-neighbours = "4" );
call bgp-as-marker-route( $marker-route = "103.252.114.0/23", $as-number = "13414" );
call active-prefixes-installed( $route-table = "inet.0", $min-active-routes = "5" );
call active-next-hops( $prefix = "11.0.0.0/24", $route-table = "inet.0", $min-next-hops = "3" );
call min-prefixes-received( $neighbour-ip = "1.1.1.1", $min-recevied-routes = "5" );
}
else if ($mode = "execute") {
<output> jcs:printf("%s", "Execute mode" );
/* call overload-isis(); */
/* call overload-isis( $instance = "BEN" ); */
/* call overload-ospf(); */
/* call overload-ospf( $instance = "BEN" ); */
/* call bgp-apply-group( $bgp-group = "BEN", $apply-group-name = "BGP-REJECT-ALL"); */
/* call bgp-apply-group( $instance = "BEN", $bgp-group = "BEN", $apply-group-name = "BGP-INSTANCE-REJECT-ALL"); */
/* call remove-overload-isis(); */
/* call remove-overload-isis( $instance = "BEN" ); */
/* call remove-overload-ospf(); */
/* call remove-overload-ospf( $instance = "BEN" ); */
/* call remove-bgp-apply-group( $bgp-group = "BEN", $apply-group-name = "BGP-REJECT-ALL"); */
/* call remove-bgp-apply-group( $instance = "BEN", $bgp-group = "BEN", $apply-group-name = "BGP-INSTANCE-REJECT-ALL"); */
}
}
var $close-results = jcs:close($router);
}
/* --== Tests ==-- */
template min-prefixes-received( $neighbour-ip, $min-recevied-routes ) {
/* Confirm there are at least $min-active-routes prefixes being received and accepted from BGP peer $neighbour-ip */
var $bgp-neighbour = <get-bgp-neighbor-information> {
<neighbor-address> $neighbour-ip;
}
var $bgp-neighbour-result = jcs:execute($router, $bgp-neighbour);
for-each ($bgp-neighbour-result/bgp-peer) {
if ((sum(bgp-rib/accepted-prefix-count)) < $min-received-routes ){
<output> jcs:printf("%s %d %s %d %s %s", "FAIL: Only receiving", sum(bgp-rib/accepted-prefix-count), "of", $min-received-routes, "prefixes from peer", $neighbour-ip );
}
else {
<output> jcs:printf("%s %d %s %d %s %s", "PASS: Receiving", sum(bgp-rib/accepted-prefix-count), "of", $min-received-routes, "prefixes from peer", $neighbour-ip );
}
}
}
template established-bgp-peers( $min-peer-count ) {
/* Confirm there are at least $min-peer-count BGP neighbours in "Established" state */
var $bgp-summary = <get-bgp-summary-information>;
var $bgp-summary-result = jcs:execute($router, $bgp-summary);
if ($bgp-summary-result/peer-count - $bgp-summary-result/down-peer-count < $min-peer-count) {
<output> jcs:printf("%s %d %s %d", "FAIL: Available BGP Peers:", $bgp-summary-result/peer-count - $bgp-summary-result/down-peer-count, "of", $bgp-summary-result/peer-count );
}
else {
<output> jcs:printf("%s %d %s %d", "PASS: Available BGP Peers:", $bgp-summary-result/peer-count - $bgp-summary-result/down-peer-count, "of", $bgp-summary-result/peer-count );
}
}
template ospf-marker-route ( $marker-route ) {
/* Confirm prefix $marker-route is an active OSPF route inet.0 */
var $ospf-routes = <get-route-information> {
<destination> $marker-route;
<protocol> ospf;
}
var $ospf-route-result = jcs:execute($router, $ospf-routes);
if ($ospf-route-result/route-table/rt/rt-entry/active-tag == "*") {
<output> jcs:printf("%s %s %s", "PASS: OSPF marker route", $marker-route, "active");
}
else {
<output> jcs:printf("%s %s %s", "FAIL: OSPF marker route", $marker-route, "is inactive");
}
}
template isis-marker-route ( $marker-route ) {
/* Confirm prefix $marker-route is an active ISIS route inet.0 */
var $isis-routes = <get-route-information> {
<destination> $marker-route;
<protocol> isis;
}
var $isis-route-result = jcs:execute($router, $isis-routes);
if ($isis-route-result/route-table/rt/rt-entry/active-tag == "*") {
<output> jcs:printf("%s %s %s", "PASS: ISIS marker route", $marker-route, "active");
}
else {
<output> jcs:printf("%s %s %s", "FAIL: ISIS marker route", $marker-route, "is inactive");
}
}
template full-ospf-neighbours( $area, $min-ospf-neighbours ) {
/* Confirm OSPF area $area has at least $min-ospf-neighbours in "Full" state */
/* Note that in broadcast domains, only DR and BDR will have Full adjacency states with all neighbours */
var $ospf-neighbours = <get-ospf-neighbor-information> {
<area> $area;
}
var $ospf-neighbours-results = jcs:execute($router, $ospf-neighbours);
mvar $full-ospf-neighbour-count = 0;
for-each ($ospf-neighbours-results/ospf-neighbour) {
if (ospf-neighbor-state == "Full") {
set $full-ospf-neighbour-count = $full-ospf-neighbour-count + 1;
}
}
if ($full-ospf-neighbour-count < $min-ospf-neighbours) {
<output> jcs:printf("%s %d %s %d", "FAIL: Full OSPF neighbours:", $full-ospf-neighbour-count, "out of", $min-ospf-neighbours);
}
else {
<output> jcs:printf("%s %d %s %d", "PASS: Full OSPF neighbours:", $full-ospf-neighbour-count, "out of", $min-ospf-neighbours);
}
}
template bgp-as-marker-route ( $marker-route, $as-number ) {
/* Confirm prefix $marker-route from AS $as-path is an active BGP route in inet.0 */
var $bgp-as-route = <get-route-information> {
<destination> $marker-route;
<protocol> bgp;
<aspath-regex> $as-number _ ".*";
}
var $bgp-as-route-result = jcs:execute($router, $bgp-as-route);
if ($bgp-as-route-result/route-table/rt/rt-entry/active-tag == "*") {
<output> jcs:printf("%s %s %s%s", "PASS: BGP marker route", $marker-route, "active from AS", $as-number);
}
else {
<output> jcs:printf("%s %s %s%s", "FAIL: BGP marker route", $marker-route, "inactive from AS", $as-number);
}
}
template active-prefixes-installed( $route-table, $min-active-routes ) {
/* Confirm there are at least $min-active-routes in $route-table table*/
var $active-table-prefixes = <get-route-information> {
<table> $route-table;
}
var $active-table-prefixes-result = jcs:execute($router, $active-table-prefixes);
if ($active-table-prefixes-result/route-table/active-route-count < $min-active-routes) {
<output> jcs:printf("%s %d %s %s %s %d %s", "FAIL:", $active-table-prefixes-result/route-table/active-route-count, "active prefixes in", $route-table, "-", $min-active-routes,"required");
}
else {
<output> jcs:printf("%s %d %s %s %s %d %s", "PASS:", $active-table-prefixes-result/route-table/active-route-count, "active prefixes in", $route-table, "-", $min-active-routes,"required");
}
}
template active-next-hops( $prefix, $route-table, $min-next-hops ) {
/* Confirm that prefix $prefix in $route-table table has at least $min-next-hops active next-hops available for ECMP */
var $active-next-hops = <get-route-information> {
<table> $route-table;
<destination> $prefix;
<active-path>;
<exact>;
}
var $active-next-hops-result = jcs:execute($router, $active-next-hops);
mvar $active-nexthop-count = 0;
for-each ($active-next-hops-result/route-table/rt/rt-entry/nh) {
set $active-nexthop-count = $active-nexthop-count + 1;
}
if ($active-nexthop-count >= $min-next-hops ) {
<output> jcs:printf("%s %s %s %d %s %d %s", "PASS: Prefix", $prefix, "has", $active-nexthop-count, "out of", $min-next-hops, "active next-hops");
}
else {
<output> jcs:printf("%s %s %s %d %s %d %s", "FAIL: Prefix", $prefix, "has only", $active-nexthop-count, "out of", $min-next-hops, "active next-hops");
}
}
/* --== Actions ==-- */
template overload-ospf( $instance = "master" ) {
/* Overload OSPF in $instance instance */
/* this might be better if we do the various checks, then end up with a single config which we commit outside the if / else section */
/* would need a configuration variable with template-wide scope though. Perhaps even a merge of the overload and remove-overload */
if ($instance == "master") {
var $config-rpc = <get-configuration database="committed"> {
<configuration> {
<protocols> {
<ospf>;
}
}
}
var $config = jcs:invoke( $config-rpc );
var $ospf-overload = $config/protocols/ospf/overload;
if (jcs:empty( $ospf-overload )) {
<output> jcs:printf("%s %s", "Overloading OSPF in instance", $instance );
var $overload-config = <configuration> {
<protocols> {
<ospf> {
<overload> {
}
}
}
}
var $results := { call jcs:load-configuration( $action = "merge", $connection = $router, $configuration = $overload-config ); }
if( $results //xnm:error ) {
<output> message;
}
else {
<output> jcs:printf("%s %s", "OSPF already overloaded in instance", $instance );
}
}
}
else {
var $config-rpc = <get-configuration database="committed"> {
<configuration> {
<routing-instances> {
<instance> {
<name> $instance;
<protocols> {
<ospf>;
}
}
}
}
}
var $config = jcs:invoke( $config-rpc );
var $ospf-overload = $config/routing-instances/instance[name==$instance]/protocols/ospf/overload;
if (jcs:empty( $ospf-overload )) {
var $overload-config = <configuration> {
<routing-instances> {
<instance> {
<name> $instance;
<protocols> {
<ospf> {
<overload> {
}
}
}
}
}
}
var $results := { call jcs:load-configuration( $action = "merge", $connection = $router, $configuration = $overload-config ); }
if( $results //xnm:error ) {
<output> message;
}
}
else {
<output> jcs:printf("%s %s", "OSPF already overloaded in instance", $instance );
}
}
}
template remove-overload-ospf( $instance = "master" ) {
/* Remove overload from OSPF in $instance instance */
if ($instance == "master") {
var $config-rpc = <get-configuration database="committed"> {
<configuration> {
<protocols> {
<ospf>;
}
}
}
var $config = jcs:invoke( $config-rpc );
var $ospf-overload = $config/protocols/ospf/overload;
if (jcs:empty( $ospf-overload )) {
<output> jcs:printf("%s %s", "Overload already removed from OSPF in instance", $instance );
}
else if ( $ospf-overload ) {
<output> jcs:printf("%s %s", "Removing Overload from OSPF in instance", $instance );
var $overload-config = <change> {
<protocols> {
<ospf> {
<overload delete="delete"> {
}
}
}
}
var $results := { call jcs:load-configuration( $action = "merge", $connection = $router, $configuration = $overload-config ); }
if( $results //xnm:error ) {
<output> message;
}
}
}
else {
var $config-rpc = <get-configuration database="committed"> {
<configuration> {
<routing-instances> {
<instance> {
<name> $instance;
<protocols> {
<ospf>;
}
}
}
}
}
var $config = jcs:invoke( $config-rpc );
var $ospf-overload = $config/routing-instances/instance[name==$instance]/protocols/ospf/overload;
if (jcs:empty( $ospf-overload )) {
<output> jcs:printf("%s %s", "Overload already removed from OSPF in instance", $instance );
}
else if ( $ospf-overload ) {
<output> jcs:printf("%s %s", "Removing Overload from OSPF in instance", $instance );
var $overload-config = <change> {
<routing-instances> {
<instance> {
<name> $instance;
<protocols> {
<ospf> {
<overload delete="delete"> {
}
}
}
}
}
}
var $results := { call jcs:load-configuration( $action = "merge", $connection = $router, $configuration = $overload-config ); }
if( $results //xnm:error ) {
<output> message;
}
}
}
}
template overload-isis ( $instance = "master" ) {
/* Overload ISIS in $instance instance */
if ($instance == "master") {
var $config-rpc = <get-configuration database="committed"> {
<configuration> {
<protocols> {
<isis>;
}
}
}
var $config = jcs:invoke( $config-rpc );
var $isis-overload = $config/protocols/isis/overload;
if (jcs:empty( $isis-overload )) {
<output> jcs:printf("%s %s", "Overloading ISIS in instance", $instance );
var $overload-config = <configuration> {
<protocols> {
<isis> {
<overload> {
}
}
}
}
var $results := { call jcs:load-configuration( $action = "merge", $connection = $router, $configuration = $overload-config ); }
if( $results //xnm:error ) {
<output> message;
}
}
else {
<output> jcs:printf("%s %s", "ISIS already overloaded in instance", $instance );
}
}
else {
var $config-rpc = <get-configuration database="committed"> {
<configuration> {
<routing-instances> {
<instance> {
<name> $instance;
<protocols> {
<isis>;
}
}
}
}
}
var $config = jcs:invoke( $config-rpc );
var $isis-overload = $config/routing-instances/instance[name==$instance]/protocols/isis/overload;
if (jcs:empty( $isis-overload )) {
<output> jcs:printf("%s %s", "Overloading ISIS in instance", $instance );
var $overload-config = <configuration> {
<routing-instances> {
<instance> {
<name> $instance;
<protocols> {
<isis> {
<overload> {
}
}
}
}
}
}
var $results := { call jcs:load-configuration( $action = "merge", $connection = $router, $configuration = $overload-config ); }
if( $results //xnm:error ) {
<output> message;
}
}
else {
<output> jcs:printf("%s %s", "ISIS already overloaded in instance", $instance );
}
}
}
template remove-overload-isis ( $instance = "master" ) {
/* Remove overload from ISIS in $instance instance */
if ($instance == "master") {
var $config-rpc = <get-configuration database="committed"> {
<configuration> {
<protocols> {
<isis>;
}
}
}
var $config = jcs:invoke( $config-rpc );
var $isis-overload = $config/protocols/isis/overload;
if (jcs:empty( $isis-overload )) {
<output> jcs:printf("%s %s", "Overload already removed from ISIS in instance", $instance );
}
else if ( $isis-overlaod ) {
var $overload-config = <change> {
<protocols> {
<isis> {
<overload delete="delete"> {
}
}
}
}
var $results := { call jcs:load-configuration( $action = "merge", $connection = $router, $configuration = $overload-config ); }
if( $results //xnm:error ) {
<output> message;
}
}
}
else {
var $config-rpc = <get-configuration database="committed"> {
<configuration> {
<routing-instances> {
<instance> {
<name> $instance;
<protocols> {
<isis>;
}
}
}
}
}
var $config = jcs:invoke( $config-rpc );
var $isis-overload = $config/routing-instances/instance[name==$instance]/protocols/isis/overload;
if (jcs:empty( $isis-overload )) {
<output> jcs:printf("%s %s", "Overload already removed from ISIS in instance", $instance );
}
else if ( $isis-overlaod ) {
var $overload-config = <change> {
<routing-instances> {
<instance> {
<name> $instance;
<protocols> {
<isis> {
<overload delete="delete"> {
}
}
}
}
}
}
var $results := { call jcs:load-configuration( $action = "merge", $connection = $router, $configuration = $overload-config ); }
if( $results //xnm:error ) {
<output> message;
}
}
}
}
template bgp-apply-group( $instance = "master", $bgp-group, $apply-group-name) {
/* Commit existing apply-group $apply-group-name to BGP group $bgp-group in $instance routing-instance */
if ($instance == "master") {
var $config-rpc = <get-configuration database="committed"> {
<configuration> {
<protocols> {
<bgp> {
<group> {
<name> $bgp-group;
}
}
}
}
}
var $config = jcs:invoke( $config-rpc );
var $bgp-apply-group = $config/protocols/bgp/group[name==$bgp-group][apply-group==$apply-group-name];
if ( $bgp-apply-group ) {
<output> jcs:printf("%s %s %s %s %s %s", "Apply group", $apply-group-name, "already applied to BGP group", $bgp-group, "in instance", $instance );
}
else if ( jcs:empty( $bgp-apply-group )) {
<output> jcs:printf("%s %s %s %s %s %s", "Activating apply-group", $apply-group-name, "on BGP group", $bgp-group, "in routing-instance", $instance);
var $apply-group-config = <configuration> {
<protocols> {
<bgp> {
<group> {
<name> $bgp-group;
<apply-groups> $apply-group-name;
}
}
}
}
var $results := { call jcs:load-configuration( $action = "merge", $connection = $router, $configuration = $apply-group-config ); }
if( $results //xnm:error ) {
<output> message;
}
}
}
else {
var $config-rpc = <get-configuration database="committed"> {
<configuration> {
<routing-instances> {
<instance> {
<name> $instance;
<protocols> {
<bgp> {
<group> {
<name> $bgp-group;
}
}
}
}
}
}
}
var $config = jcs:invoke( $config-rpc );
var $bgp-apply-group = $config/routing-instances/instance[name==$instance]/protocols/bgp/group[name==$bgp-group][apply-group==$apply-group-name];
if ( $bgp-apply-group ) {
<output> jcs:printf("%s %s %s %s %s %s", "Apply group", $apply-group-name, "already applied to BGP group", $bgp-group, "in instance", $instance );
}
else if ( jcs:empty( $bgp-apply-group )) {
var $apply-group-config = <configuration> {
<routing-instances> {
<instance> {
<name> $instance;
<protocols> {
<bgp> {
<group> {
<name> $bgp-group;
<apply-groups> $apply-group-name;
}
}
}
}
}
}
var $results := { call jcs:load-configuration( $action = "merge", $connection = $router, $configuration = $apply-group-config ); }
if( $results //xnm:error ) {
<output> message;
}
}
}
}
template remove-bgp-apply-group( $instance = "master", $bgp-group, $apply-group-name) {
/* Remove existing apply-group $apply-group-name from BGP group $bgp-group in $instance routing-instance */
<output> jcs:printf("%s %s %s %s %s %s", "Deactivating apply-group", $apply-group-name, "on BGP group", $bgp-group, "in routing-instance", $instance);
if ($instance == "master") {
var $apply-group-config = <configuration> {
<protocols> {
<bgp> {
<group> {
<name> $bgp-group;
<apply-groups delete="delete"> $apply-group-name;
}
}
}
}
var $results := { call jcs:load-configuration( $action = "merge", $connection = $router, $configuration = $apply-group-config ); }
if( $results //xnm:error ) {
<output> message;
}
}
else {
var $apply-group-config = <configuration> {
<routing-instances> {
<instance> {
<name> $instance;
<protocols> {
<bgp> {
<group> {
<name> $bgp-group;
<apply-groups delete="delete"> $apply-group-name;
}
}
}
}
}
}
var $results := { call jcs:load-configuration( $action = "merge", $connection = $router, $configuration = $apply-group-config ); }
if( $results //xnm:error ) {
<output> message;
}
}
}