-
Notifications
You must be signed in to change notification settings - Fork 3
/
nfsdtop.linux
executable file
·2577 lines (2319 loc) · 58.5 KB
/
nfsdtop.linux
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
863
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
#!/bin/sh
# vi: set ft=sh noet ts=8 sw=8 :: Vi/ViM
############################################################ IDENT(1)
#
# $Title: Script to generate top-like statistics for nfsd I/O $
# $Copyright: 2020-2022 Devin Teske. All rights reserved. $
# $FrauBSD: nfsdtop/nfsdtop.linux 2022-08-11 17:39:04 -0700 freebsdfrau $
#
############################################################ INFORMATION
#
# In nfsdtop, a ``view'' is the user's choice between -c, -g, -s, or -u.
# For example, `-u' asks nfsdtop to display the ``user view'' where statistics
# displayed are on a per-user basis.
#
# The ``mode'' is the user's choice between viewing data input/output (this is
# the default) and metadata operations (-m). When presenting data in JSON (-j)
# use -M to get both data and metadata options.
#
# The code is broken down into:
# - Mode selection (user choice)
# - View selection (user choice)
# - Filter selection (user choice)
# - bpftrace execution
# - awk to process bpftrace output
#
# Code navigating Search Terms/ST:
# BPFTRACE Start of bpftrace
# FILTERS Data filters
# JSONDATA JSON data generation
# OPS Data operations (read vs write)
# POST bpftrace post-processor (awk)
# PRE Start of pre-processor (sh)
# SORTING Sort routines
# TRACEDATA bpftrace data generation and processing
# VIEWDATA View data generation
# VIEWS View processing
#
############################################################ DEFAULTS
DEFAULT_INTERVAL=2.0 # seconds
#
# Sorting choices (case-insensitive):
# total (in any mode; default sort column)
# read write in out (without `-m')
# good fail (with `-m')
#
DEFAULT_SORT=total
#
# User/Group map files
#
DEFAULT_PASSWD_MAP=.nfsd.passwd
DEFAULT_GROUP_MAP=.nfsd.group
#
# Network IP map file
#
DEFAULT_IP_MAP=.nfsd.hosts
############################################################ GLOBALS
VERSION='$Version: 8.0-alpha-4 $'
pgm="${0##*/}" # Program basename
#
# Global exit status
#
SUCCESS=0
FAILURE=1
#
# Command-line options
#
COLOR=1 # -a
DEBUG= # -d
DEBUGGER= # -D
FILTER_CLIENT= # -C ip
FILTER_FILE= # -F regex
FILTER_GROUP= # -G group
FILTER_SERVER= # -S ip
FILTER_USER= # -U user
GROUP_MAP="$DEFAULT_GROUP_MAP" # -P file
INTERVAL=$DEFAULT_INTERVAL # -i sec
IP_MAP="$DEFAULT_IP_MAP" # -I file
MODE=data # vs `meta' (-m) or `both' (-M)
NO_NAMES= # -n
NSAMPLES= # -N num
OUTPUT_JSON= # -j
PASSWD_MAP="$DEFAULT_PASSWD_MAP" # -p file
QUIET= # -q
RAW_VIEW= # -r
REDACT=${NFSDTOP_REDACT: 1} # -R
SHOW_BASIC= # -b
SORT=$DEFAULT_SORT # -k col
VIEW_CLIENT= # -c
VIEW_FILE= # -f
VIEW_GROUP= # -g
VIEW_SERVER= # -s
VIEW_USER= # -u (default)
WIDE_VIEW= # -w
#
# Miscellaneous
#
CB='}'
CONS=1
[ -t 1 ] || CONS= COLOR= # stdout is not a tty
DQ='"'
_FILTER_CLIENT=
_FILTER_GROUP=
_FILTER_SERVER=
_FILTER_USER=
INTERVAL_PROBE= # Calculated
INTERVAL_SECONDS= # Raw value for awk
MODE_DATA=
MODE_META=
NEED_CLIENT=
NEED_GROUP=
NEED_SERVER=
NEED_USER=
NFS3=1
NFS4=1
VIEW=
############################################################ FUNCTIONS
die()
{
local fmt="$1"
if [ "$fmt" ]; then
shift 1 # fmt
printf "%s: $fmt\n" "$pgm" "$@" >&2
fi
exit $FAILURE
}
usage()
{
local fmt="$1"
local optfmt="\t%-11s %s\n"
exec >&2
if [ "$fmt" ]; then
shift 1 # fmt
printf "%s: $fmt\n" "$pgm" "$@"
fi
printf "Usage: %s [OPTIONS]\n" "$pgm"
printf "Options:\n"
printf "$optfmt" "-a" "Always enable color."
printf "$optfmt" "-b" "Show basic counts instead of rates."
printf "$optfmt" "-C ip" "Client filter (IPv4 only)."
printf "$optfmt" "-c" "View read/write activity by client."
printf "$optfmt" "-D" "Enable debugger."
printf "$optfmt" "-d" "Debug. Print bpftrace script and exit."
printf "$optfmt" "-F regex" "File filter. Implies \`-f'."
printf "$optfmt" "-f" \
"View read/write activity by file. Implies \`-w'."
printf "$optfmt" "-G group" "Group filter (name or id)."
printf "$optfmt" "-g" "View read/write activity by group."
printf "$optfmt" "-h" "Print usage statement and exit."
printf "$optfmt" "-I file" "IP map file. Default \`$DEFAULT_IP_MAP'."
printf "$optfmt" "-i sec" \
"Set interval seconds. Default \`$DEFAULT_INTERVAL'."
printf "$optfmt" "-J" "Output most JSON data. Same as \`-cgjMsu'."
printf "$optfmt" "-j" "Output JSON formatted data."
printf "$optfmt" "-k col" "Sort column (default \`$DEFAULT_SORT')."
printf "$optfmt" "-M" "Show metadata statistics (requires \`-j')."
printf "$optfmt" "-m" "Only show metadata statistics."
printf "$optfmt" "-N num" "Perform num samples and exit."
printf "$optfmt" "-n" "Do not attempt to map uid/gid/ip to names."
printf "$optfmt" "-o" "Force non-console output."
printf "$optfmt" "-P file" \
"Group map file. Default \`$DEFAULT_GROUP_MAP'."
printf "$optfmt" "-p file" \
"User map file. Default \`$DEFAULT_PASSWD_MAP'."
printf "$optfmt" "-q" "Quiet. Hide informational messages."
printf "$optfmt" "-R" "Redact potentially sensitive information."
printf "$optfmt" "-r" "Raw view. Do not format output of bpftrace."
printf "$optfmt" "-S ip" "Server filtter (IPv4 only)."
printf "$optfmt" "-s" "View read/write activity by server."
printf "$optfmt" "-U user" "User filter (name or id)."
printf "$optfmt" "-u" "View read/write activity by user (default)."
printf "$optfmt" "-v" "Print version and exit."
printf "$optfmt" "-w" "Wide view. Maximize width of first column."
die
}
run_bpftrace()
{
if [ "$DEBUG" ]; then
cat
return
fi
bpftrace -B none /dev/stdin "$@"
}
isint()
{
local arg="${1#-}"
[ "${arg:-x}" = "${arg%[!0-9]*}" ]
}
isip()
{
local IFS=. noctets=0 octet
for octet in $1; do
[ "$octet" ] || return 2
isint "$octet" || return 3
[ $octet -ge 0 ] || return 4
[ $octet -gt 255 ] && return 5
noctets=$(( $noctets 1 ))
done
[ $noctets -eq 4 ]
}
checkip()
{
isip "$1"
case $? in
1) die "-C argument \`%s' too many dots" "$1" ;;
2) die "-C argument \`%s' missing octet" "$1" ;;
3) die "-C argument \`%s' bad octet" "$1" ;;
4) die "-C argument \`%s' negative octet" "$1" ;;
5) die "-C argument \`%s' too big octet" "$1" ;;
esac
}
atoi()
{
local __addr="$1" __var_to_set="$2" __num=0
if isip "$__addr"; then
local IFS=.
set -- $__addr
__num=$(( ($1 << 24) ($2 << 16) ($3 << 8) $4 ))
fi
eval $__var_to_set=$__num
}
#
# ST: CALLS
#
send_user()
{
local type="$1"
shift 1 # type
printf "%s|%s\n" "$type" "$*"
}
info() { send_user info "$*"; }
resize()
{
local size
if [ -e /dev/tty ]; then
size=$( { stty size < /dev/tty; } 2> /dev/null )
else
size=$( stty size 2> /dev/null )
fi
send_user resize "${size:-24 80}"
}
############################################################ MAIN
#
# Process command-line options
#
while getopts 34abC:cDdF:fG:ghi:Jjk:MmN:noP:p:qRrS:sU:uvw flag; do
case "$flag" in
3) NFS3=1 NFS4= ;;
4) NFS4=1 NFS3= ;;
a) COLOR=1 ;;
b) SHOW_BASIC=1 ;;
C) FILTER_CLIENT="$OPTARG" ;;
c) VIEW=CLIENT VIEW_CLIENT=1 ;;
D) DEBUGGER=1 RAW_VIEW=1 ;;
d) DEBUG=1 RAW_VIEW=1 ;;
F) VIEW=FILE VIEW_FILE=1 WIDE_VIEW=1 FILTER_FILE="$OPTARG" ;;
f) VIEW=FILE VIEW_FILE=1 WIDE_VIEW=1 ;;
G) FILTER_GROUP="$OPTARG" ;;
g) VIEW=GROUP VIEW_GROUP=1 ;;
i) INTERVAL="$OPTARG" ;;
J) VIEW=JSON OUTPUT_JSON=1 MODE=both \
VIEW_CLIENT=1 VIEW_GROUP=1 VIEW_SERVER=1 VIEW_USER=1 ;;
j) OUTPUT_JSON=1 ;;
k) SORT="$OPTARG" ;;
M) MODE=both ;;
m) MODE=meta ;;
N) [ "$OPTARG" ] || usage "-N option requires an argument" # NOTREACHED
NSAMPLES="$OPTARG" ;;
n) NO_NAMES=1 ;;
o) CONS= COLOR= ;;
P) GROUP_MAP="$OPTARG" ;;
p) PASSWD_MAP="$OPTARG" ;;
q) QUIET=1 ;;
R) REDACT=1 ;;
r) RAW_VIEW=1 ;;
S) FILTER_SERVER="$OPTARG" ;;
s) VIEW=SERVER VIEW_SERVER=1 ;;
U) FILTER_USER="$OPTARG" ;;
u) VIEW=USER VIEW_USER=1 ;;
v) VERSION="${VERSION#*: }"
echo "${VERSION% $}"
exit $SUCCESS ;;
w) WIDE_VIEW=1 ;;
*) usage # NOTREACHED
esac
done
shift $(( $OPTIND - 1 ))
#
# Process command-line arguments
#
[ $# -eq 0 ] || usage "Too many arguments" # NOTREACHED
#
# Prevent non-functional option combinations
#
if [ "$SHOW_BASIC" ]; then
[ "$VIEW" != "JSON" ] || die "-b cannot be combined with -J"
[ ! "$OUTPUT_JSON" ] || die "-b cannot be combined with -j"
fi
case "$MODE" in
both) [ "$OUTPUT_JSON" ] || die "-M requires -j or -J" ;;
esac
#
# Silently ignore previous view options unless JSON output
#
[ "$VIEW" ] || VIEW=USER VIEW_USER=1
if [ ! "$OUTPUT_JSON" ]; then
case "$VIEW" in # ST: VIEWS
CLIENT) VIEW_FILE= VIEW_GROUP= VIEW_SERVER= VIEW_USER= ;;
FILE) VIEW_CLIENT= VIEW_GROUP= VIEW_SERVER= VIEW_USER= ;;
GROUP) VIEW_CLIENT= VIEW_FILE= VIEW_SERVER= VIEW_USER= ;;
SERVER) VIEW_CLIENT= VIEW_FILE= VIEW_GROUP= VIEW_USER= ;;
USER) VIEW_CLIENT= VIEW_FILE= VIEW_GROUP= VIEW_SERVER= ;;
esac
fi
#
# Validate `-i sec' option
#
case "$INTERVAL" in
"") usage "missing -i argument" ;; # NOTREACHED
0) die "-i sec must be non-zero" ;;
*[!0-9.]*|*.*.*|.) die "-i sec must be a number" ;;
*.*)
INTERVAL_SECONDS=$INTERVAL
ms=$( echo "$INTERVAL * 1000" | bc )
ms="${ms%%.*}"
#
# If, after multiplying by 1000 to convert sec to msec,
# the leading [non-decimal] digit is either missing or zero,
# the input was too small to produce timing of at least 1 msec
#
case "$ms" in
""|0) die "-i sec must be at least 0.001" ;;
esac
INTERVAL_PROBE=interval:ms:$ms
;;
*)
INTERVAL_SECONDS=$INTERVAL
INTERVAL_PROBE=interval:s:$INTERVAL_SECONDS
esac
#
# Validate `-k col' option
# ST: SORTING
#
if [ "$MODE" = data ]; then
case "$SORT" in
"") usage "missing -k argument" ;; # NOTREACHED
[Tt][Oo][Tt][Aa][Ll]) SORT=total ;; # TOTAL
[Rr][Ee][Aa][Dd]|[Oo][Uu][Tt]) SORT=read ;; # READ(IN)
[Ww][Rr][Ii][Tt][Ee]|[Ii][Nn]) SORT=write ;; # WRITE(OUT)
*) die "-k col must be one of: total read write in out"
esac
elif [ "$MODE" = meta ]; then
case "$SORT" in
"") usage "missing -k argument" ;; # NOTREACHED
[Tt][Oo][Tt][Aa][Ll]) SORT=total ;; # TOTAL
[Gg][Oo][Oo][Dd]) SORT=good ;; # STAT(GOOD)
[Ff][Aa][Ii][Ll]) SORT=fail ;; # STAT(FAIL)
*) die "-k col must be one of: total good fail"
esac
elif [ "$MODE" = both ]; then
case "$SORT" in
"") usage "missing -k argument" ;; # NOTREACHED
[Tt][Oo][Tt][Aa][Ll]) SORT=total ;; # TOTAL
[Rr][Ee][Aa][Dd]|[Oo][Uu][Tt]) SORT=read ;; # READ(IN)
[Ww][Rr][Ii][Tt][Ee]|[Ii][Nn]) SORT=write ;; # WRITE(OUT)
[Gg][Oo][Oo][Dd]) SORT=good ;; # STAT(GOOD)
[Ff][Aa][Ii][Ll]) SORT=fail ;; # STAT(FAIL)
*) die "-k col must be one of: total read write in out good fail"
esac
fi
#
# Process `-m' and `-M' options
#
case "$MODE" in
both) MODE_DATA=1 MODE_META=1 ;;
data) MODE_DATA=1 MODE_META= ;;
meta) MODE_META=1 MODE_DATA= ;;
esac
#
# Validate `-N num' option
#
case "$NSAMPLES" in
0) die "-N num must be non-zero" ;;
*[!0-9]*) die "-N num must be a positive integer" ;;
esac
#
# Process `-G group'/`-U user' option
#
case "$FILTER_GROUP" in
"") : leave-empty ;;
*[!0-9]*) # Translate from name to GID
_FILTER_GROUP=$( awk \
-v sq="'" \
-v group_map="$GROUP_MAP" \
-v name="$FILTER_GROUP" \
'BEGIN {
delete name2gid
while (getline < group_map > 0) {
n = split($0, fields, /:/)
name2gid[fields[1]] = fields[3]
}
close(group_map)
if (name in name2gid) {
print name2gid[name]
exit 0
}
gsub(sq, "&\\\\&&", name)
cmd = sprintf("getent group %s%s%s", sq, name, sq)
cmd | getline group
close(cmd)
if (split(group, fields, /:/) >= 3) {
print fields[3]
exit 0
}
exit 1
}' 2> /dev/null ) || die "Unknown group %s" "$FILTER_GROUP"
FILTER_GROUP="$_FILTER_GROUP"
;;
esac
case "$FILTER_USER" in
"") : leave-empty ;;
*[!0-9]*) # Translate from name to UID
_FILTER_USER=$( awk \
-v sq="'" \
-v user_map="$PASSWD_MAP" \
-v name="$FILTER_USER" \
'BEGIN {
delete uid2name
while (getline < user_map > 0) {
n = split($0, fields, /:/)
name2uid[fields[1]] = fields[3]
}
close(user_map)
if (name in name2uid) {
print name2uid[name]
exit 0
}
gsub(sq, "&\\\\&&", name)
cmd = sprintf("getent passwd %s%s%s", sq, name, sq)
cmd | getline passwd
close(cmd)
if (split(passwd, fields, /:/) >= 3) {
print fields[3]
exit 0
}
exit 1
}' ) || die "Unknown user %s" "$FILTER_USER"
FILTER_USER="$_FILTER_USER"
;;
esac
#
# Process `-C ip'/`-S ip' option
#
case "$FILTER_CLIENT" in
"") : ok ;;
*[a-zA-Z]*)
_FILTER_CLIENT=$( awk \
-v sq="'" \
-v ip_map="$IP_MAP" \
-v host="$FILTER_CLIENT" \
'BEGIN {
delete host2ip
while (getline < ip_map > 0) {
if (/^[[:space:]]*(#|$)/) continue
n = split($0, fields, /[[:space:]] /)
host2ip[fields[2]] = fields[1]
}
close(ip_map)
if (host in host2ip) {
print host2ip[host]
exit 0
} else if (host !~ /\.$/ && (host ".") in host2ip) {
print host2ip[host "."]
exit 0
}
gsub(sq, "&\\\\&&", host)
cmd = sprintf("host -t A %s%s%s", sq, host, sq)
cmd | getline
close(cmd)
if ($NF !~ /NXDOMAIN/) {
print $NF
exit 0
}
exit 1
}' 2> /dev/null ) || die "Unknown host %s" "$FILTER_CLIENT"
FILTER_CLIENT="$_FILTER_CLIENT"
checkip "$FILTER_CLIENT" # NOTREACHED if non-ip
atoi $FILTER_CLIENT FILTER_CLIENT
;;
*)
checkip "$FILTER_CLIENT" # NOTREACHED if non-ip
atoi $FILTER_CLIENT FILTER_CLIENT
;;
esac
case "$FILTER_SERVER" in
"") : leave-empty ;;
*[a-zA-Z]*) # Translate from name to IP
_FILTER_SERVER=$( awk \
-v sq="'" \
-v ip_map="$IP_MAP" \
-v host="$FILTER_SERVER" \
'BEGIN {
delete host2ip
while (getline < ip_map > 0) {
if (/^[[:space:]]*(#|$)/) continue
n = split($0, fields, /[[:space:]] /)
host2ip[fields[2]] = fields[1]
}
close(ip_map)
if (host in host2ip) {
print host2ip[host]
exit 0
} else if (host !~ /\.$/ && (host ".") in host2ip) {
print host2ip[host "."]
exit 0
}
gsub(sq, "&\\\\&&", host)
cmd = sprintf("host -t A %s%s%s", sq, host, sq)
cmd | getline
close(cmd)
if ($NF !~ /NXDOMAIN/) {
print $NF
exit 0
}
exit 1
}' 2> /dev/null ) || die "Unknown host %s" "$FILTER_SERVER"
FILTER_SERVER="$_FILTER_SERVER"
checkip "$FILTER_SERVER" # NOTREACHED if non-ip
atoi $FILTER_SERVER FILTER_SERVER
;;
*)
checkip "$FILTER_SERVER" # NOTREACHED if non-ip
atoi $FILTER_SERVER FILTER_SERVER
;;
esac
#
# Collect only information we need
#
[ ! "$VIEW_CLIENT$FILTER_CLIENT" ] || NEED_CLIENT=1
[ ! "$VIEW_GROUP$FILTER_GROUP" ] || NEED_GROUP=1
[ ! "$VIEW_SERVER$FILTER_SERVER" ] || NEED_SERVER=1
[ ! "$VIEW_USER$FILTER_USER" ] || NEED_USER=1
#
# Get terminal size
#
size=$( resize )
size="${size#*|}"
if [ "$size" ]; then
cols="${size#*[$IFS]}"
rows="${size%%[$IFS]*}"
fi
case "$rows$cols" in
""|*[!0-9]*)
cols=80
rows=24
;;
esac
#
# Run script
# ST: PRE
#
{
trap resize WINCH # ST: SIGWINCH
#
# Start background bpftrace
# NB: M-x package-install [RET] dtrace-script-mode [RET]
# ST: BPFTRACE
#
run_bpftrace <<EOF &
${DEBUG: #!/usr/bin/bpftrace}
/* -*- mode: dtrace-script; tab-width: 4 -*- ;; Emacs
* vi: set ft=dtrace noet ts=4 sw=4 :: Vi/ViM
*/
////////////////////////////////////////////////// INCLUDES
#include <linux/sunrpc/svc.h>
${NFS4:
#include <linux/nfs4.h>
}
////////////////////////////////////////////////// INTERFACE
${NFS4:
struct clientid {
u32 cl_boot;
u32 cl_id;
$CB;
struct stateid_opaque {
struct clientid so_clid;
u32 so_id;
$CB;
struct stateid {
u32 si_generation;
struct stateid_opaque si_opaque;
$CB;
struct nfsd4_read {
struct stateid rd_stateid; /* request */
u64 rd_offset; /* request */
u32 rd_length; /* request */
int rd_vlen;
struct file *rd_filp;
bool rd_tmp_file;
struct svc_rqst *rd_rqstp; /* response */
struct svc_fh *rd_fhp; /* response */
$CB;
struct nfsd4_write {
struct stateid wr_stateid; /* request */
u64 wr_offset; /* request */
u32 wr_stable_how; /* request */
u32 wr_buflen; /* request */
struct kvec wr_head;
struct page ** wr_pagelist; /* request */
u32 wr_bytes_written; /* response */
u32 wr_how_written; /* response */
nfs4_verifier wr_verifier; /* response */
$CB;
}
////////////////////////////////////////////////// BEGIN
BEGIN
{
time("time|%s\n");
printf("init|"); // Initialize post-processor
printf("===\n"); // Clear screen and draw header
printf("info|Sampling data for ${INTERVAL}s (\`-i sec')...\n");
}
////////////////////////////////////////////////// STAT PROBE
/*
* stat(2, 3) probes
*/
${MODE_META:
kprobe:nfsd3_proc_getattr
{
\$rqstp = (struct svc_rqst *)arg0;
@nfsd_getattr_rqstp[tid] = \$rqstp;
${NEED_CLIENT:
\$data = (int64 *)\$rqstp->rq_addr.__data;
\$num = (int64)*\$data;
@nfsd_getattr_client[tid] = \$num >> 16 & 0xFFFFFFFF;
}
${NEED_SERVER:
\$data = (int64 *)\$rqstp->rq_daddr.__data;
\$num = (int64)*\$data;
@nfsd_getattr_server[tid] = \$num >> 16 & 0xFFFFFFFF;
}
$CB
${VIEW_FILE:
kprobe:nfsd_init_raparms
/@nfsd_getattr_rqstp[tid] != 0/
{
@nfsd_getattr_filep[tid] = (struct file *)arg0;
$CB
}
kretprobe:nfsd3_proc_getattr
/@nfsd_getattr_rqstp[tid] != 0/
{
\$rqstp = @nfsd_getattr_rqstp[tid];
${NEED_USER:
\$uid = \$rqstp->rq_cred.cr_uid.val;
}
${NEED_GROUP:
\$gid = \$rqstp->rq_cred.cr_gid.val;
}
\$retval = retval;
${NEED_CLIENT:
\$client_ip4 = @nfsd_getattr_client[tid];
\$client_ip4num = (uint32)(\$client_ip4 >> 24 |
(\$client_ip4 >> 16 & 0xFF) << 8 |
(\$client_ip4 >> 8 & 0xFF) << 16 |
(\$client_ip4 & 0xFF) << 24);
delete(@nfsd_getattr_client[tid]);
}
${NEED_SERVER:
\$server_ip4 = @nfsd_getattr_server[tid];
\$server_ip4num = (uint32)(\$server_ip4 >> 24 |
(\$server_ip4 >> 16 & 0xFF) << 8 |
(\$server_ip4 >> 8 & 0xFF) << 16 |
(\$server_ip4 & 0xFF) << 24);
delete(@nfsd_getattr_server[tid]);
}
delete(@nfsd_getattr_rqstp[tid]);
${VIEW_FILE:
\$file = @nfsd_getattr_filep[tid];
\$de = \$file->f_path.dentry;
\$n1 = \$de->d_name.name;
\$de = \$de->d_parent;
\$n2 = \$de->d_name.name;
delete(@nfsd_getattr_filep[tid]);
}
\$filtered = 0;
// ST: FILTERS
${FILTER_CLIENT:
if (\$client_ip4num != $FILTER_CLIENT) { \$filtered = 1; $CB
}
${FILTER_GROUP:
if (\$gid != $FILTER_GROUP) { \$filtered = 1; $CB
}
${FILTER_SERVER:
if (\$server_ip4num != $FILTER_SERVER) { \$filtered = 1; $CB
}
${FILTER_USER:
if (\$uid != $FILTER_USER) { \$filtered = 1; $CB
}
if (\$filtered == 0)
{
if (\$retval == 0) {
// ST: VIEWS TRACEDATA
${VIEW_CLIENT:
@stat_good_client[ntop(AF_INET, \$client_ip4)] = count();
}
${VIEW_FILE:
@stat_good_file[str(\$n2), str(\$n1)] = count();
}
${VIEW_GROUP:
@stat_good_group[\$gid] = count();
}
${VIEW_SERVER:
@stat_good_server[ntop(AF_INET, \$server_ip4)] = count();
}
${VIEW_USER:
@stat_good_user[\$uid] = count();
}
$CB
else
{
// ST: VIEWS TRACEDATA
${VIEW_CLIENT:
@stat_fail_client[ntop(AF_INET, \$client_ip4)] = count();
}
${VIEW_FILE:
@stat_fail_file[str(\$n2), str(\$n1)] = count();
}
${VIEW_GROUP:
@stat_fail_group[\$gid] = count();
}
${VIEW_SERVER:
@stat_fail_server[ntop(AF_INET, \$server_ip4)] = count();
}
${VIEW_USER:
@stat_fail_user[\$uid] = count();
}
$CB
$CB
$CB
}
////////////////////////////////////////////////// READ PROBES
/*
* read(2) probes
*/
${MODE_DATA: ${NFS3:
kprobe:nfsd_read
{
\$rqstp = (struct svc_rqst *)arg0;
@nfsd_read_rqstp[tid] = \$rqstp;
@nfsd_read_countp[tid] = arg5;
${NEED_CLIENT:
\$data = (int64 *)\$rqstp->rq_addr.__data;
\$num = (int64)*\$data;
@nfsd_read_client[tid] = \$num >> 16 & 0xFFFFFFFF;
}
${NEED_SERVER:
\$data = (int64 *)\$rqstp->rq_daddr.__data;
\$num = (int64)*\$data;
@nfsd_read_server[tid] = \$num >> 16 & 0xFFFFFFFF;
}
$CB
${VIEW_FILE:
kprobe:nfsd_init_raparms
/@nfsd_read_rqstp[tid] != 0/
{
@nfsd_read_filep[tid] = (struct file *)arg0;
$CB
}
kretprobe:nfsd_read
/@nfsd_read_rqstp[tid] != 0/
{
\$rqstp = @nfsd_read_rqstp[tid];
${NEED_USER:
\$uid = \$rqstp->rq_cred.cr_uid.val;
}
${NEED_GROUP:
\$gid = \$rqstp->rq_cred.cr_gid.val;
}
${NEED_CLIENT:
\$client_ip4 = @nfsd_read_client[tid];
\$client_ip4num = (uint32)(\$client_ip4 >> 24 |
(\$client_ip4 >> 16 & 0xFF) << 8 |
(\$client_ip4 >> 8 & 0xFF) << 16 |
(\$client_ip4 & 0xFF) << 24);
delete(@nfsd_read_client[tid]);
}
${NEED_SERVER:
\$server_ip4 = @nfsd_read_server[tid];
\$server_ip4num = (uint32)(\$server_ip4 >> 24 |
(\$server_ip4 >> 16 & 0xFF) << 8 |
(\$server_ip4 >> 8 & 0xFF) << 16 |
(\$server_ip4 & 0xFF) << 24);
delete(@nfsd_read_server[tid]);
}
delete(@nfsd_read_rqstp[tid]);
\$filtered = 0;
// ST: FILTERS
${FILTER_CLIENT:
if (\$client_ip4num != $FILTER_CLIENT) { \$filtered = 1; $CB
}
${FILTER_GROUP:
if (\$gid != $FILTER_GROUP) { \$filtered = 1; $CB
}
${FILTER_SERVER:
if (\$server_ip4num != $FILTER_SERVER) { \$filtered = 1; $CB
}
${FILTER_USER:
if (\$uid != $FILTER_USER) { \$filtered = 1; $CB
}
if (\$filtered == 0)
{
\$countp = @nfsd_read_countp[tid];
\$bytes = *\$countp;
// ST: VIEWS TRACEDATA
${VIEW_CLIENT:
@rd_client[ntop(AF_INET, \$client_ip4)] = sum(\$bytes);
}
${VIEW_FILE:
\$file = @nfsd_read_filep[tid];
\$de = \$file->f_path.dentry;
\$n1 = \$de->d_name.name;
\$de = \$de->d_parent;
\$n2 = \$de->d_name.name;
@rd_file[str(\$n2), str(\$n1)] = sum(\$bytes);
}
${VIEW_GROUP:
@rd_group[\$gid] = sum(\$bytes);
}
${VIEW_SERVER:
@rd_server[ntop(AF_INET, \$server_ip4)] = sum(\$bytes);
}
${VIEW_USER:
@rd_user[\$uid] = sum(\$bytes);
}
$CB
delete(@nfsd_read_countp[tid]);
${VIEW_FILE:
delete(@nfsd_read_filep[tid]);
}
$CB
}}
${MODE_DATA: ${NFS4:
kprobe:nfsd4_read
{
\$rqstp = (struct svc_rqst *)arg0;
@nfsd4_read_rqstp[tid] = \$rqstp;
\$nfsd4_read = (struct nfsd4_read *)arg2;
${VIEW_FILE:
@nfsd4_readp[tid] = \$nfsd4_read;
}
@nfsd4_read_count[tid] = \$nfsd4_read->rd_length;
${NEED_CLIENT:
\$data = (int64 *)\$rqstp->rq_addr.__data;
\$num = (int64)*\$data;
@nfsd4_read_client[tid] = \$num >> 16 & 0xFFFFFFFF;
}
${NEED_SERVER:
\$data = (int64 *)\$rqstp->rq_daddr.__data;
\$num = (int64)*\$data;
@nfsd4_read_server[tid] = \$num >> 16 & 0xFFFFFFFF;
}
$CB
kretprobe:nfsd4_read
/@nfsd4_read_rqstp[tid] != 0/
{
\$rqstp = @nfsd4_read_rqstp[tid];
${VIEW_FILE:
\$readp = @nfsd4_readp[tid];
}
${NEED_USER:
\$uid = \$rqstp->rq_cred.cr_uid.val;
}
${NEED_GROUP:
\$gid = \$rqstp->rq_cred.cr_gid.val;
}
${NEED_CLIENT:
\$client_ip4 = @nfsd4_read_client[tid];
\$client_ip4num = (uint32)(\$client_ip4 >> 24 |
(\$client_ip4 >> 16 & 0xFF) << 8 |
(\$client_ip4 >> 8 & 0xFF) << 16 |
(\$client_ip4 & 0xFF) << 24);
delete(@nfsd4_read_client[tid]);
}
${NEED_SERVER:
\$server_ip4 = @nfsd4_read_server[tid];
\$server_ip4num = (uint32)(\$server_ip4 >> 24 |
(\$server_ip4 >> 16 & 0xFF) << 8 |
(\$server_ip4 >> 8 & 0xFF) << 16 |
(\$server_ip4 & 0xFF) << 24);
delete(@nfsd4_read_server[tid]);
}
delete(@nfsd4_read_rqstp[tid]);
${VIEW_FILE:
delete(@nfsd4_readp[tid]);
}
\$filtered = 0;
// ST: FILTERS
${FILTER_CLIENT:
if (\$client_ip4num != $FILTER_CLIENT) { \$filtered = 1; $CB
}
${FILTER_GROUP:
if (\$gid != $FILTER_GROUP) { \$filtered = 1; $CB
}
${FILTER_SERVER:
if (\$server_ip4num != $FILTER_SERVER) { \$filtered =1; $CB
}
${FILTER_USER: