-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtctree
5568 lines (4426 loc) · 202 KB
/
tctree
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/sh
## Copyright (c) 2024-present Alex Tsvetkov
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
##
## The handles of qdiscs consist of two parts, a major number and a minor number.
## It is habitual to name the root qdisc '1:', which is equal to '1:0'.
## The minor number of a qdisc is always 0.
## Classes need to have the same major number as their parent.
##
## Classes residing under a qdisc share their qdisc major number, but each have a separate minor number
## called a 'classid' that has no relation to their parent classes, only to their parent qdisc.
##
## Major number 0 is allowed but it's reserved for internal use.
##
## Moreover even multiple qdiscs with major number 0 may exist:
##
## qdisc mq 0: root
## Sent 995827135 bytes 1112122 pkt (dropped 0, overlimits 0 requeues 37)
## backlog 0b 0p requeues 37
## qdisc pfifo_fast 0: parent :1 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
## Sent 995827135 bytes 1112122 pkt (dropped 0, overlimits 0 requeues 37)
## backlog 0b 0p requeues 37
##
## class mq :1 root
## Sent 995827561 bytes 1112123 pkt (dropped 0, overlimits 0 requeues 37)
## backlog 0b 0p requeues 37
## class mq :2 root
## Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0)
## backlog 0b 0p requeues 0
##
## Some tc constants:
## #define TC_H_MAJ_MASK (0xFFFF0000U)
## #define TC_H_MIN_MASK (0x0000FFFFU)
## #define TC_H_MAJ(h) ((h)&TC_H_MAJ_MASK)
## #define TC_H_MIN(h) ((h)&TC_H_MIN_MASK)
## #define TC_H_MAKE(maj,min) (((maj)&TC_H_MAJ_MASK)|((min)&TC_H_MIN_MASK))
## #define TC_H_ROOT (0xFFFFFFFFU)
## #define TC_H_INGRESS (0xFFFFFFF1U)
##
## Interface names in Linux allow all characters except: "/", <space>, NULL.
##
# When any command fails, the shell immediately shall exit,
# as if by executing the exit special built-in utility with no arguments.
# Note: this affects `[ ... ] && ...` within functions. Prefer `[ ... ] || ...` instead.
# Note: calling any command/fn with `|| ...` "switches off" this setting.
# This is why explicit `return $?` are used everywhere.
set -e
# When the shell tries to expand an unset parameter other than the '@' and '*' special parameters,
# it shall write a message to standard error and the expansion shall fail.
set -u
TCTREE_VERSION="0.30.0"
# Subshell `$()` strips _all_ trailing newline characters
TCTREE_NL="$(printf "\nX")"
TCTREE_NL="${TCTREE_NL%"X"}"
TCTREE_DEFAULT_TEMPLATE='{type} {handle} {kind}'\
'[% if sent_bytes > 0 %] {sent_bytes|traffic} {sent_packets} pkt[% endif %]{nl}'\
'[% if raw_options != "" %][[[{raw_options}]|oneline({__indent_length}, ...)]]{nl}[% endif %]'\
'dropped: {dropped} overlimits: {overlimits} requeues: {requeues} backlog: {backlog} qlen: {qlen}{nl}'
TCTREE_DEFAULT_TEMPLATE_QDISC="$TCTREE_DEFAULT_TEMPLATE"
TCTREE_DEFAULT_TEMPLATE_CLASS="$TCTREE_DEFAULT_TEMPLATE"
TCTREE_DEFAULT_HEADER_WATCH="Node Est. rate Sent Dropped Overlim"
TCTREE_DEFAULT_TEMPLATE_WATCH=''\
'[[[{type} {handle} {kind}]|maxlen(30)|pad_end(30)|cut_end({__indent_length})]]'\
'[[[[% if diff:sent_packets > 0 %]{rate:sent_bytes|decmul(8,1)|traffic_rate}[% endif %]]|maxlen(11, ..)|pad_start(12)]]'\
'[[[[% if sent_bytes > 0 %]{sent_bytes|traffic}[% endif %]]|maxlen(11, ..)|pad_start(12)]]'\
'[[[[% if diff:dropped > 0 %]+{diff:dropped}/[% endif %]{dropped}]|maxlen(9, ..)|pad_start(10)]]'\
'[[[[% if diff:overlimits > 0 %]+{diff:overlimits}/[% endif %]{overlimits}]|maxlen(9, ..)|pad_start(10)]]'
TCTREE_DEFAULT_TEMPLATE_QDISC_WATCH="$TCTREE_DEFAULT_TEMPLATE_WATCH"
TCTREE_DEFAULT_TEMPLATE_CLASS_WATCH="$TCTREE_DEFAULT_TEMPLATE_WATCH"
ERR_COMMAND_NOT_SUPPORTED=10
ERR_VAR_NOT_EXISTS=11
ERR_NUMBER_INVALID=20
ERR_NUMBER_NOT_INT=21
ERR_NUMBER_INVALID_HEX=22
ERR_SPACESEP_LIST_NOT_EXISTS=40
ERR_PARSE_UNEXPECTED_FORMAT=50
ERR_PARSE_UNCLOSED_PARENTHESIS=51
ERR_INTERFACE_INVALID_NUMBER=60
ERR_INTERFACE_ROOT_QDISC_NOT_SET=61
ERR_NODE_INVALID_HANDLE=70
ERR_NODE_PROPERTY_NOT_EXISTS=71
ERR_NODE_NS_BY_ID_NOT_EXISTS=72
ERR_RESOLVE_PROP_NOT_FOUND=80
ERR_EXPR_PROP_NOT_FOUND=90
ERR_EXPR_EXPLICIT_PROPERTY_NOT_FOUND=91
ERR_EXPR_INSUFFICIENT_DATA=92
ERR_EXPR_PROP_INVALID_FORMAT=93
ERR_EXPR_TRANSFORMER_FAILED=94
g_benchmark=""
g_debug=""
g_trace=""
g_no_target_titles=""
g_custom_template_set=""
g_hide_common_count=0
g_hide_qdiscs_count=0
g_hide_classes_count=0
g_indent_guide_length=3
g_indent_guide_vert="|"
g_indent_guide_vert_chars=""
g_indent_guide_vert_padded=""
g_indent_guide_vert_padded_chars=""
g_indent_guide_branch="+-"
g_indent_guide_branch_chars=""
g_indent_guide_branch_padded=""
g_indent_guide_branch_padded_chars=""
g_indent_guide_branch_sibling="+-"
g_indent_guide_branch_sibling_chars=""
g_indent_guide_branch_sibling_padded=""
g_indent_guide_branch_sibling_padded_chars=""
g_indent_guide_space=" "
g_indent_guide_space_chars=""
g_indent_guide_space_padded=""
g_indent_guide_space_padded_chars=""
g_watch_mode=""
g_watch_interval_ms=1000
g_watch_clear_cmd="clear"
g_snapshots_capacity=2
g_actual_snapshot=1
g_snapshots_filled=""
g_targets=""
g_targets_count=0
g_interfaces_count=0
g_term_columns=""
g_date_supported=""
g_sleep_supported=""
g_tput_supported=""
##!
##!
##!
usage() {
sg_is_full="${1-0}"
sg_basename="$(basename "$0")"
while IFS="" read -r sg_line || [ -n "$sg_line" ]; do
printf "%s\n" "${sg_line#" "}"
done <<EOT
Usage: $sg_basename [options] [<...targets>]
Target is an interface and optionally a handle: <interface>[/<handle>].
If handle is not specified then interface's root egress and ingress qdiscs are used.
If no target is specified then all network interfaces are discovered and used as targets.
Watch mode:
-w, --watch - periodically refresh data.
-i, --interval <number> - refresh data interval in seconds (default: 1).
--snapshots <number> - number of data snapshots to be collected (default: 2).
--watch-clear-cmd <command> - command to clear the terminal (default: clear).
Other options:
--no-target-titles - do not display target titles before the data.
--header-template <template> - generate and display something before data rows.
--debug - output debug information.
--benchmark - profile the script and show performance details.
--trace - output more debug info and show every executed command (set -x).
--version - display version and exit.
--help - show this help message.
--help-full - show extended help including details on templating and filtering.
EOT
[ "$sg_is_full" = "1" ] || return 0
while IFS="" read -r sg_line || [ -n "$sg_line" ]; do
printf "%s\n" "${sg_line#" "}"
done <<EOT
Nodes filtering:
--hide <condition> - hide any nodes (and their subtrees) that match the condition.
--hide-qdisc <condition> - hide qdisc nodes (and their subtrees) that match the condition.
--hide-class <condition> - hide class nodes (and their subtrees) that match the condition.
Nodes templating:
--template <template> - generate text of any nodes from this template.
--template[-<kind>] <template> - override default template with this kind-specific (e.g. --template-htb).
--template-qdisc <template> - generate text of qdisc nodes from this template.
--template-qdisc[-<kind>] <template> - override default template with this kind-specific (e.g. --template-qdisc-htb).
--template-class <template> - generate text of class nodes from this template.
--template-class[-<kind>] <template> - override default template with this kind-specific (e.g. --template-class-htb).
Indent guides:
--indent-guide-length <number> - number of characters for each level (default: 3).
--indent-guide-vert <string> - (default: \"|\").
--indent-guide-vert-chars <number> - (default: vert-string length in bytes).
--indent-guide-branch <string> - (default: \"+-\").
--indent-guide-branch-chars <number> - (default: branch-string length in bytes).
--indent-guide-branch-sibling <string> - (default: \"+-\").
--indent-guide-branch-sibling-chars <number> - (default: branch-sibling-string length in bytes).
--indent-guide-space <string> - (default: \" \").
--indent-guide-space-chars <number> - (default: space-string length in bytes).
## Templating and filtering
Interface properties:
name, root_qdisc_id, fetch_time.
Node properties:
type, kind, handle, parent, parent_major, parent_minor, parent_id, raw_options, sent_bytes, sent_packets, dropped, overlimits, requeues, backlog, qlen.
HTB:
qdisc: htb_refcnt, htb_r2q, htb_default, htb_direct_packets_stat, htb_ver, htb_direct_qlen.
class: htb_prio, htb_quantum, htb_rate, htb_overhead, htb_ceil, htb_linklayer, htb_level, htb_burst, htb_burst_mpu, htb_cburst, htb_cburst_mpu, htb_lended, htb_borrowed, htb_giants, htb_tokens, htb_ctokens.
fq_codel:
qdisc: fq_codel_limit, fq_codel_flows, fq_codel_quantum, fq_codel_target, fq_codel_interval, fq_codel_memory_limit, fq_codel_drop_batch, fq_codel_ecn, fq_codel_maxpacket, fq_codel_drop_overlimit, fq_codel_new_flow_count, fq_codel_ecn_mark, fq_codel_ce_mark, fq_codel_memory_used, fq_codel_drop_overmemory, fq_codel_new_flows_len, fq_codel_old_flows_len.
class: deficit, fq_codel_count, fq_codel_lastcount, fq_codel_ldelay, fq_codel_dropping, fq_codel_drop_next.
Transformers:
<value>|transformer[|transformer([arg1[,arg2[,<...>]]])]
Transformer is a function that takes any value, transforms it and returns the new value.
Multiple transformers are piped and executed from the left to the right.
Transformer functions may take optional comma-separated arguments enclosed in parentheses.
Supported transformers (and their aliases):
length (len)
maxlength (maxlen)
repeat (str_repeat)
trim
trim_start (trim_left, left_trim)
trim_end (trim_right, right_trim)
pad_start (pad_left, left_pad)
pad_end (pad_right, right_pad)
cut_start (cut_left, left_cut)
cut_end (cut_right, right_cut)
oneline
mul (decmul, muldec)
floor
traffic
traffic_rate
traffic_rate_bps
Examples:
2|mul(4) # 16
120|mul(30)|traffic # 3.5 KiB
"Hello"|repeat(3) # HelloHelloHello
"Hello"|repeat(3)|maxlen(10)|pad_end(12, !) # HelloHello!!
Expression syntax:
Expressions return some optionally transformed value.
They are used in conditions and templates.
<number>[|<...transformers>]
<string>[|<...transformers>]
[<func>:]<property>[|<...transformers>]
[<func>:]parent.<property>[|<...transformers>]
[<func>:]interface.<property>[|<...transformers>]
<number> - integer or decimal.
<string> - quoted string literal.
<property> - node/parent/interface property.
<func> - aggregate function to use in watch-mode.
Supported functions:
rate
diff
Examples:
sent_bytes|mul(8) # sent data in bits
rate:sent_packets # packets per second (watch mode, estimated)
diff:sent_packets # number of new packets from previous update (watch mode)
Condition syntax:
Conditions are evaluated and return a boolean value (true/1 or false/0).
They are used in filters (if evaluates to true then filter is applied) and templates.
[!]<property>
check for node property existence/inexistence.
[!]parent.<property>
check for parent node property existence/inexistence.
[!]interface.<property>
check for interface property existence/inexistence.
<expr> <operator> <expr>
supported operators: =, !=, <, <=, >=, >.
<condition1> [&& <condition2> [&& <...>]]
multiple conditions: true if and only if all conditions are true
Examples:
htb_prio # true if node contains this property
!htb_overhead # true if node doesn't contain this property
sent_packets > 0
kind = "fq_codel"
rate:sent_bytes|mul(8) > 500000 # true if node's rate exceeds 500 kbit/s
diff:dropped > 0 # true if there were drops from previous update
Template syntax:
Templates are used to generate node's text.
Indent guides are formed independently of node templates.
Besides plain texts templates support the following 3 constructs:
Expressions:
{<expr>}
They are usually used to output node's properties (raw or transformed).
Special expressions:
{nl} - new line
{__indent_length} - length of the node's indent in characters
Conditions:
[% if <condition> %]<...>[% endif %]
[% if <condition> %]<...>[% else %]<...>[% endif %]
Conditional output. Nested conditions are not supported.
Blocks:
[[[<...>]|<...transformers>]]
Blocks are usually used to apply transformers to a group of expressions.
Evaluating order: conditions, expressions, blocks.
Examples:
{type} {handle} # qdisc 1:
{handle} bits sent: {sent_bytes|mul(8)} # 1: bits sent: 16384
[% if sent_bytes > 0 %]Total {sent_bytes} bytes sent[% endif %]
[% if sent_packets > 0 %]Some packets were seen[% else %]No packets yet[% endif %]
[% if rate:sent_bytes|mul(8) > 10000 %]More than 10 kbit/s right now![% endif %]
[[[{type} {handle} {rate:sent_bytes|traffic_rate_bps}]|pad_end(30)]]
Examples:
# All interfaces
$sg_basename
# Only specified interfaces
$sg_basename net0 wwan0
# Only specified node and its subtree
$sg_basename net0/1:1
# Watch mode with refreshing data every 3 seconds
$sg_basename -w -i 3
# Hide nodes without traffic
$sg_basename net0 --hide 'sent_bytes = 0'
# All net0 nodes except the one with handle 1:10 (including its subtree)
$sg_basename net0 --hide "handle = '1:10'"
# This may be seen as alias to \`--hide 'type = "class" && kind = "fq_codel"'\`
$sg_basename net0 --hide-class 'kind = "fq_codel"'
# HTB-tree without fq_codel subtrees
$sg_basename net0 --hide "parent.kind = 'htb' && kind = 'fq_codel'"
# Do not show interface's ingress qdisc (if it exists)
$sg_basename net0 --hide-qdisc 'kind = "ingress"'
# Example of compact output
$sg_basename net0 --template "{type} {handle} {kind} sent: {sent_bytes|traffic}"
EOT
}
##!
##!
##!
main() {
g_targets=""
m_is_dashdash=""
while [ $# -gt 0 ]; do
m_arg="$1"
shift 1
if [ "$m_is_dashdash" = "1" ]; then
g_targets="${g_targets}${g_targets:+ }$m_arg"
continue
fi
case "$m_arg" in
--template-qdisc)
# shellcheck disable=SC2034 # appears unused
g_template_qdisc="$1"
shift 1
g_custom_template_set="1"
;;
--template-qdisc-*)
var_set "g_template_qdisc_${m_arg#"--template-qdisc-"}" "$1"
shift 1
g_custom_template_set="1"
;;
--template-class)
# shellcheck disable=SC2034 # appears unused
g_template_class="$1"
shift 1
g_custom_template_set="1"
;;
--template-class-*)
var_set "g_template_class_${m_arg#"--template-class-"}" "$1"
shift 1
g_custom_template_set="1"
;;
--template)
g_template_common="$1"
shift 1
g_custom_template_set="1"
;;
--template-*)
var_set "g_template_common_${m_arg#"--template-"}" "$1"
shift 1
g_custom_template_set="1"
;;
--hide)
g_hide_common_count=$((g_hide_common_count + 1))
var_set "g_hide_common${g_hide_common_count}" "$1"
shift 1
;;
--hide-qdisc)
g_hide_qdiscs_count=$((g_hide_qdiscs_count + 1))
var_set "g_hide_qdiscs${g_hide_qdiscs_count}" "$1"
shift 1
;;
--hide-class)
g_hide_classes_count=$((g_hide_classes_count + 1))
var_set "g_hide_classes${g_hide_classes_count}" "$1"
shift 1
;;
-w | --watch)
g_watch_mode="1"
;;
-i | --interval)
m_interval_ms=""
decimal_mul "m_interval_ms" "$1" 1000 0 || return $?
g_watch_interval_ms="$m_interval_ms"
shift 1
;;
--watch-clear-cmd)
g_watch_clear_cmd="$1"
shift 1
;;
--snapshots)
g_snapshots_capacity="$1"
shift 1
;;
--indent-guide-len | --indent-guides-length)
g_indent_guide_length="$1"
shift 1
;;
--indent-guide-vert | --indent-guides-vert)
g_indent_guide_vert="$1"
shift 1
;;
--indent-guide-vert-chars | --indent-guides-vert-chars)
g_indent_guide_vert_chars="$1"
shift 1
;;
--indent-guide-branch | --indent-guides-branch)
g_indent_guide_branch="$1"
shift 1
;;
--indent-guide-branch-chars | --indent-guides-branch-chars)
g_indent_guide_branch_chars="$1"
shift 1
;;
--indent-guide-branch-sibling | --indent-guides-branch-sibling)
g_indent_guide_branch_sibling="$1"
shift 1
;;
--indent-guide-branch-sibling-chars | --indent-guides-branch-sibling-chars)
g_indent_guide_branch_sibling_chars="$1"
shift 1
;;
--indent-guide-space | --indent-guides-space)
g_indent_guide_space="$1"
shift 1
;;
--indent-guide-space-chars | --indent-guides-space-chars)
g_indent_guide_space_chars="$1"
shift 1
;;
--no-target-title | --no-target-titles)
g_no_target_titles="1"
;;
--header-template | --headers-template)
g_header_template="$1"
shift 1
;;
--benchmark)
g_benchmark="1"
;;
--debug)
g_debug="1"
;;
--trace)
g_trace="1"
;;
-h | --help)
usage || return $?
return 0
;;
--help-full)
usage 1 || return $?
return 0
;;
--version)
printf "tctree %s\n" "$TCTREE_VERSION"
exit 0
;;
--)
m_is_dashdash="1"
;;
-*)
exit_err "Unknown option: $m_arg"
;;
*)
g_targets="${g_targets}${g_targets:+ }$m_arg"
;;
esac
done
if [ "$g_trace" = "1" ]; then
set -x
fi
#
# Check commands for their existence
#
fncmd_exists "tc" || exit_err "Command 'tc' is required to be installed"
g_date_supported="1"
fncmd_exists "date" || g_date_supported="0"
g_sleep_supported="1"
fncmd_exists "sleep" || g_sleep_supported="0"
g_tput_supported="1"
fncmd_exists "tput" || g_tput_supported="0"
#
# Align/format indent guide strings
#
m_indent_spaces=""
[ "$g_indent_guide_vert_chars" != "" ] || g_indent_guide_vert_chars="${#g_indent_guide_vert}"
[ "$g_indent_guide_branch_chars" != "" ] || g_indent_guide_branch_chars="${#g_indent_guide_branch}"
[ "$g_indent_guide_branch_sibling_chars" != "" ] || g_indent_guide_branch_sibling_chars="${#g_indent_guide_branch_sibling}"
[ "$g_indent_guide_space_chars" != "" ] || g_indent_guide_space_chars="${#g_indent_guide_space}"
m_pad_times=$(((g_indent_guide_length - g_indent_guide_vert_chars) / g_indent_guide_space_chars))
str_repeat "m_indent_spaces" "$g_indent_guide_space" "$m_pad_times" || return $?
g_indent_guide_vert_padded="${g_indent_guide_vert}${m_indent_spaces}"
g_indent_guide_vert_padded_chars=$((g_indent_guide_vert_chars + g_indent_guide_space_chars * m_pad_times))
[ "$g_indent_guide_vert_padded_chars" = "$g_indent_guide_length" ] ||
exit_err "indent guides: vert+space chars number '$g_indent_guide_vert_padded_chars' does not match '$g_indent_guide_length'"
m_pad_times=$(((g_indent_guide_length - g_indent_guide_branch_chars) / g_indent_guide_space_chars))
str_repeat "m_indent_spaces" "$g_indent_guide_space" "$m_pad_times" || return $?
g_indent_guide_branch_padded="${g_indent_guide_branch}${m_indent_spaces}"
g_indent_guide_branch_padded_chars=$((g_indent_guide_branch_chars + g_indent_guide_space_chars * m_pad_times))
[ "$g_indent_guide_branch_padded_chars" = "$g_indent_guide_length" ] ||
exit_err "indent guides: branch+space chars number '$g_indent_guide_branch_padded_chars' does not match '$g_indent_guide_length'"
m_pad_times=$(((g_indent_guide_length - g_indent_guide_branch_sibling_chars) / g_indent_guide_space_chars))
str_repeat "m_indent_spaces" "$g_indent_guide_space" "$m_pad_times" || return $?
g_indent_guide_branch_sibling_padded="${g_indent_guide_branch_sibling}${m_indent_spaces}"
g_indent_guide_branch_sibling_padded_chars=$((g_indent_guide_branch_sibling_chars + g_indent_guide_space_chars * m_pad_times))
[ "$g_indent_guide_branch_sibling_padded_chars" = "$g_indent_guide_length" ] ||
exit_err "indent guides: branch_sibling+space chars number '$g_indent_guide_branch_sibling_padded_chars' does not match '$g_indent_guide_length'"
m_pad_times=$((g_indent_guide_length / g_indent_guide_space_chars))
g_indent_guide_space_padded=""
str_repeat "g_indent_guide_space_padded" "$g_indent_guide_space" "$m_pad_times" || return $?
g_indent_guide_space_padded_chars=$((g_indent_guide_space_chars * m_pad_times))
[ "$g_indent_guide_space_padded_chars" = "$g_indent_guide_length" ] ||
exit_err "indent guides: space chars number '$g_indent_guide_space_padded_chars' does not match '$g_indent_guide_length'"
#
# If no target was specified then use all system's interfaces
#
if [ "$g_targets" = "" ]; then
m_net_ifaces="$(ls /sys/class/net)"
for m_iface in $m_net_ifaces; do
[ "$m_iface" != "lo" ] || continue
g_targets="${g_targets}${g_targets:+ }$m_iface"
done
if [ "$g_targets" = "" ]; then
exit_err "No targets were specified and no network interfaces were found"
fi
fi
g_targets_count=0
for m_target in $g_targets; do
g_targets_count=$((g_targets_count + 1))
if [ "${m_target#*"/"}" != "$m_target" ]; then
m_iface="${m_target%%"/"*}"
m_node="${m_target#*"/"}"
else
m_iface="$m_target"
m_node=""
fi
var_set "g_targets${g_targets_count}_title" "$m_target"
var_set "g_targets${g_targets_count}_interface" "$m_iface"
var_set "g_targets${g_targets_count}_node" "$m_node"
# Add interface to the internal registry and assign its number
get_interface_number "g_targets${g_targets_count}_interface_number" "$m_iface" || return $?
done
if [ "$g_watch_mode" = "1" ]; then
g_actual_snapshot=0
while :; do
g_actual_snapshot=$((g_actual_snapshot + 1))
if [ "$g_actual_snapshot" -gt "$g_snapshots_capacity" ]; then
g_actual_snapshot=1
g_snapshots_filled=1
fi
m_watch_iter_start_time=""
time_ms "m_watch_iter_start_time" || m_watch_iter_start_time=0
clean_snapshot "$g_actual_snapshot" || return $?
initialize_snapshot "$g_actual_snapshot" || return $?
fetch_tc_output "$g_actual_snapshot" || return $?
process_tc_output "$g_actual_snapshot" || return $?
m_rendered=""
render_targets "m_rendered" "$g_actual_snapshot" || return $?
if [ "$g_watch_clear_cmd" != "" ]; then
$g_watch_clear_cmd
fi
printf "%s" "$m_rendered"
if [ "$g_sleep_supported" = "1" ]; then
m_watch_iter_end_time=""
time_ms "m_watch_iter_end_time" || m_watch_iter_end_time=0
m_watch_iter_diff_time=0
[ "$m_watch_iter_end_time" -lt "$m_watch_iter_start_time" ] ||
m_watch_iter_diff_time="$((m_watch_iter_end_time - m_watch_iter_start_time))"
if [ "$m_watch_iter_diff_time" != "0" ] && [ "$m_watch_iter_diff_time" -lt "$g_watch_interval_ms" ]; then
m_sleep_secs=""
decimal_div "m_sleep_secs" "$((g_watch_interval_ms - m_watch_iter_diff_time))" 1000 3 || return $?
sleep "$m_sleep_secs"
fi
fi
done
else
initialize_snapshot "$g_actual_snapshot" || return $?
fetch_tc_output "$g_actual_snapshot" || return $?
process_tc_output "$g_actual_snapshot" || return $?
m_rendered=""
render_targets "m_rendered" "$g_actual_snapshot" || return $?
printf "%s" "$m_rendered"
fi
}
##!
##! @param $1 {number} - snapshot number
##!
clean_snapshot() {
[ "${1-}" != "" ] || exit_err "clean_snapshot: empty argument 1"
clnsnp_iface_num=0
while [ "$clnsnp_iface_num" -lt "$g_interfaces_count" ]; do
clnsnp_iface_num=$((clnsnp_iface_num + 1))
clean_interface_data "$1" "$clnsnp_iface_num" || return $?
done
}
##!
##! @param $1 {number} - snapshot number
##!
initialize_snapshot() {
[ "${1-}" != "" ] || exit_err "initialize_snapshot: empty argument 1"
ntlsnp_iface_num=0
while [ "$ntlsnp_iface_num" -lt "$g_interfaces_count" ]; do
ntlsnp_iface_num=$((ntlsnp_iface_num + 1))
initialize_interface "$1" "$ntlsnp_iface_num" || return $?
done
}
##!
##! @param $1 {number} - snapshot number
##!
fetch_tc_output() {
[ "${1-}" != "" ] || exit_err "fetch_tc_output: empty argument 1"
ftctpt_iface_num=0
while [ "$ftctpt_iface_num" -lt "$g_interfaces_count" ]; do
ftctpt_iface_num=$((ftctpt_iface_num + 1))
fetch_interface_tc_output "$1" "$ftctpt_iface_num" || return $?
done
}
##!
##! @param $1 {number} - snapshot number
##!
process_tc_output() {
[ "${1-}" != "" ] || exit_err "process_tc_output: empty argument 1"
prctpt_iface_num=0
while [ "$prctpt_iface_num" -lt "$g_interfaces_count" ]; do
prctpt_iface_num=$((prctpt_iface_num + 1))
parse_interface_tc_output "$1" "$prctpt_iface_num" || return $?
build_interface_tree "$1" "$prctpt_iface_num" || return $?
done
}
##!
##! @param $1 {identifier} - result out variable name
##! @param $2 {number} - snapshot number
##!
render_targets() {
[ "${1-}" != "" ] || exit_err "render_targets: empty argument 1"
[ "${2-}" != "" ] || exit_err "render_targets: empty argument 2"
if [ "$g_tput_supported" = "1" ]; then
g_term_columns="$(tput cols)"
fi
rdrgts_header=""
if var_exists "g_header_template"; then
rdrgts_header="$g_header_template"
elif [ "$g_watch_mode" = "1" ] && [ "$g_custom_template_set" != "1" ]; then
rdrgts_header="$TCTREE_DEFAULT_HEADER_WATCH"
fi
rdrgts_result=""
rdrgts_target_num=0
while [ "$rdrgts_target_num" -lt "$g_targets_count" ]; do
rdrgts_target_num=$((rdrgts_target_num + 1))
rdrgts_title=""
var_set_expanded "rdrgts_title" "g_targets${rdrgts_target_num}_title" || return $?
rdrgts_iface_num=""
var_set_expanded "rdrgts_iface_num" "g_targets${rdrgts_target_num}_interface_number" || return $?
rdrgts_node=""
var_set_expanded "rdrgts_node" "g_targets${rdrgts_target_num}_node" || return $?
rdrgts_show_title="1"
[ "$g_no_target_titles" != "1" ] || rdrgts_show_title=""
if [ "$rdrgts_show_title" = "1" ]; then
[ "$rdrgts_target_num" -lt 2 ] || rdrgts_result="${rdrgts_result}$TCTREE_NL"
if fncmd_exists "format_target_title"; then
rdrgts_title_formatted=""
format_target_title "rdrgts_title_formatted" "$rdrgts_title" || return $?
else
rdrgts_title_formatted=""
str_repeat "rdrgts_title_formatted" "=" "${#rdrgts_title}" || return $?
rdrgts_title_formatted="${rdrgts_title}$TCTREE_NL${rdrgts_title_formatted}$TCTREE_NL"
fi
if [ "$rdrgts_title_formatted" != "" ]; then
rdrgts_result="${rdrgts_result}${rdrgts_title_formatted%"$TCTREE_NL"}$TCTREE_NL"
fi
fi
rdrgts_rendered_node=""
rdrgts_rendered_status=0
render_interface_node "rdrgts_rendered_node" "$2" "$rdrgts_iface_num" "$rdrgts_node" ||
rdrgts_rendered_status=$?
if [ "$rdrgts_rendered_status" != "0" ]; then
rdrgts_rendered_node="- Cannot render the target -$TCTREE_NL Error code: $rdrgts_rendered_status"
else
str_trim "rdrgts_rendered_node" "$rdrgts_rendered_node" "$TCTREE_NL" || return $?
if [ "$rdrgts_rendered_node" != "" ]; then
if [ "$rdrgts_header" != "" ]; then
rdrgts_result="${rdrgts_result}${rdrgts_header%"$TCTREE_NL"}$TCTREE_NL"
fi
else
rdrgts_rendered_node="- No data -"
fi
fi
rdrgts_result="${rdrgts_result}${rdrgts_rendered_node}$TCTREE_NL"
done
var_set "$1" "$rdrgts_result"
}
#
# FETCH/PARSE/PROCESS
# ==============================================================================
##!
##! @param $1 {number} - snapshot number
##! @param $2 {number} - interface number
##!
clean_interface_data() {
[ "${1-}" != "" ] || exit_err "clean_interface_data: empty argument 1"
[ "${2-}" != "" ] || exit_err "clean_interface_data: empty argument 2"
clnfdt_snapshot_num="$1"
clnfdt_iface_num="$2"
clnfdt_iface_name=""
[ "$g_debug" != "1" ] || get_interface_name "clnfdt_iface_name" "$clnfdt_iface_num"
[ "$g_debug" != "1" ] || log "Cleaning interface data: $clnfdt_iface_name ($clnfdt_iface_num)"
clnfdt_iface_namespace=""
make_interface_namespace "clnfdt_iface_namespace" "$clnfdt_snapshot_num" "$clnfdt_iface_num" || return $?
[ "$g_debug" != "1" ] || log "Interface namespace: $clnfdt_iface_namespace"
clnfdt_iface_variables=""
clnfdt_iface_variables="${clnfdt_iface_variables} name"
clnfdt_iface_variables="${clnfdt_iface_variables} fetch_time"
clnfdt_iface_variables="${clnfdt_iface_variables} fetch_failed"
clnfdt_iface_variables="${clnfdt_iface_variables} qdisc_output"
clnfdt_iface_variables="${clnfdt_iface_variables} qdiscs_count"
clnfdt_iface_variables="${clnfdt_iface_variables} qdiscs_ids"
clnfdt_iface_variables="${clnfdt_iface_variables} class_output"
clnfdt_iface_variables="${clnfdt_iface_variables} classes_count"
clnfdt_iface_variables="${clnfdt_iface_variables} classes_ids"
clnfdt_iface_variables="${clnfdt_iface_variables} root_qdisc_id"
clnfdt_iface_variables="${clnfdt_iface_variables} has_ingress"
clnfdt_iface_variables="${clnfdt_iface_variables} node_ns_ids"
for clnfdt_var in $clnfdt_iface_variables; do
unset "${clnfdt_iface_namespace}${clnfdt_var}" || :
done
init_interface_properties "$clnfdt_iface_namespace" || :
}
##!
##! @param $1 {number} - snapshot number
##! @param $2 {number} - interface number
##!
initialize_interface() {
[ "${1-}" != "" ] || exit_err "initialize_interface: empty argument 1"
[ "${2-}" != "" ] || exit_err "initialize_interface: empty argument 2"
ntlnrf_snapshot_num="$1"
ntlnrf_iface_num="$2"
ntlnrf_iface_name=""
[ "$g_debug" != "1" ] || get_interface_name "ntlnrf_iface_name" "$ntlnrf_iface_num" || return $?
[ "$g_debug" != "1" ] || log "Initializing interface: $ntlnrf_iface_name ($ntlnrf_iface_num)"
ntlnrf_iface_namespace=""
make_interface_namespace "ntlnrf_iface_namespace" "$ntlnrf_snapshot_num" "$ntlnrf_iface_num" || return $?
[ "$g_debug" != "1" ] || log "Interface namespace: $ntlnrf_iface_namespace"
init_interface_properties "$ntlnrf_iface_namespace" || return $?
init_node_namespaces_by_id "$ntlnrf_iface_namespace" || return $?
}
##!
##! @param $1 {number} - snapshot number
##! @param $2 {number} - interface number
##!
fetch_interface_tc_output() {
[ "${1-}" != "" ] || exit_err "fetch_interface_tc_output: empty argument 1"
[ "${2-}" != "" ] || exit_err "fetch_interface_tc_output: empty argument 2"
fntctp_snapshot_num="$1"
fntctp_iface_num="$2"
[ "$g_benchmark" != "1" ] || tmr_fetch_iface_total="$(print_time_ms)"
fntctp_iface_name=""
get_interface_name "fntctp_iface_name" "$fntctp_iface_num" || return $?
[ "$g_debug" != "1" ] || log "Fetching interface tc-output: $fntctp_iface_name ($fntctp_iface_num)"
[ "$fntctp_iface_name" != "" ] ||
exit_err "fetch_interface_tc_output: unexpected empty interface name" # unreachable
fntctp_iface_namespace=""
make_interface_namespace "fntctp_iface_namespace" "$fntctp_snapshot_num" "$fntctp_iface_num" || return $?
[ "$g_debug" != "1" ] || log "Interface namespace: $fntctp_iface_namespace"
fntctp_is_error=""
fntctp_time=""
time_ms "fntctp_time" || fntctp_time=0
[ "$g_benchmark" != "1" ] || tmr_tc_output="$(print_time_ms)"
fntctp_qdisc_output="$(tc -s -d qdisc show dev "$fntctp_iface_name")" || fntctp_is_error="1"
fntctp_class_output="$(tc -s -d class show dev "$fntctp_iface_name")" || fntctp_is_error="1"
[ "$g_benchmark" != "1" ] || log "Fetched tc output: $(($(print_time_ms) - tmr_tc_output)) ms"
[ "$g_debug" != "1" ] || log "$fntctp_qdisc_output"
[ "$g_debug" != "1" ] || log "$fntctp_class_output"
if [ "$fntctp_is_error" = "1" ]; then
[ "$g_debug" != "1" ] || log "Fetch failed: clearing outputs"
fntctp_qdisc_output=""
fntctp_class_output=""
fi
var_set "${fntctp_iface_namespace}qdisc_output" "$fntctp_qdisc_output"
var_set "${fntctp_iface_namespace}class_output" "$fntctp_class_output"
var_set "${fntctp_iface_namespace}fetch_failed" "$fntctp_is_error"
var_set "${fntctp_iface_namespace}fetch_time" "$fntctp_time"
register_interface_property "$fntctp_iface_namespace" "fetch_time" || return $?
[ "$g_benchmark" != "1" ] || log "[BLOCK] FETCH INTERFACE: $(($(print_time_ms) - tmr_fetch_iface_total)) ms"
}
##!
##! TODO: Parse with `awk` if available.
##! Shell is not perfect for text parsing and it takes ~35% of the total execution time.
##!
##! @param $1 {number} - snapshot number
##! @param $2 {number} - interface number
##!
parse_interface_tc_output() {
[ "${1-}" != "" ] || exit_err "parse_interface_tc_output: empty argument 1"
[ "${2-}" != "" ] || exit_err "parse_interface_tc_output: empty argument 2"
prfctp_snapshot_num="$1"
prfctp_iface_num="$2"
[ "$g_benchmark" != "1" ] || tmr_parse_iface_total="$(print_time_ms)"
prfctp_iface_name=""
get_interface_name "prfctp_iface_name" "$prfctp_iface_num" || return $?
[ "$g_debug" != "1" ] || log "Parsing interface: $prfctp_iface_name ($prfctp_iface_num)"
[ "$prfctp_iface_name" != "" ] ||