-
Notifications
You must be signed in to change notification settings - Fork 3
/
cass_top
executable file
·1340 lines (1059 loc) · 36 KB
/
cass_top
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
#!/bin/bash
# Program: cass_top
# Usage: MOCK=[1] cass_top [connection_host=localhost] [keyspace=all] [one-character option=none]
# License: Copyright 2014 by James Briggs, San Jose, California, USA. Released under Apache 2 software license
# Github: https://github.com/jamesbriggs/cassandra-top/blob/master/cass_top
# Date: 2014 09 16
# Env: bash on linux or Mac OS X, vt100/ANSI terminal types
# Programming Notes:
# - the bash "continue" statement is used to jump back to the "while true" read loop throughout this program, even from nested subroutine calls. it works, except when it doesn't.
# - if you see the error message "nodetool: line nnn: [: XXX unary operator expected" it means multiple nodetool args were double-quoted together, confusing nodetool
# - avoid calling nodetool casually: 'nodetool status' can take seconds.
# - it's ugly-looking, but string pasting can be used to one-line read formatting like this: read -e -p $'\n'"What is the DC? "
# - bash -x cass_top is surprisingly useful for tracing, especially if you insert exit immediately after the code you're debugging
# - summary of bash read options: http://ss64.com/bash/read.html
# - how to use bashdb without terminating on scripted breakpoints because of pty conflicts:
# http://sourceforge.net/p/bashdb/mailman/message/20998494/ (in ~/.bashdbinit add "source /dev/pts/NN", where NN is your shell's pty from the ps command)
# Security Notes:
# - set the full pathname for nodetool
# - the only envariables used are HOME TERM USER JMX_USERNAME JMX_PASSWORD, though nodetool could use others like JAVA_HOME and JRE_HOME and
# "requires the same environment as cassandra itself, namely the same classpath (including log4j.properties), and a valid storage-conf property."
version="0.7"
# set -e # not happy with select and continue?
set -o pipefail
set -u
### start of user-configurable settings ###
# bash read timeout feature (delay between nodetool status calls)
timeout=1
# path to find nodetool commmand. please set it to a full pathname to improve security and performance.
nodetool_cmd="nodetool"
# path to find mock_nodetool commmand if MOCK is set and you need it for testing, otherwise leave it empty.
mock_nodetool_cmd="./mock_nodetool"
# in case we're running under bashdb, $0 will be set to this string
program_debugger="bashdb"
# where to log nodetool commands (to disable logging, use empty string). See the log_it() function for how to customize logging.
logfile="$HOME/cass_top.log"
# flag to confirm before running nodetool commands
flag_confirm_nodetool_operations=1 # 1=confirm, 0=do not confirm
# general menu message strings
MSG_CANCEL="CANCEL"
MSG_ALL="ALL"
MSG_DONE="DONE"
# characters to show screen is often updating
sprite_state_on="_"
sprite_state_off=" "
### end of of user-configurable settings ###
shopt -s nocasematch # case-insensitive regex matches
#shopt extdebug
mockmode=${MOCK:-}
if [[ -n $mockmode ]] && [[ -n $mock_nodetool_cmd ]]; then
nodetool_cmd="$mock_nodetool_cmd"
fi
# some variables that are globablly used
# some constants
RE_HEX="0-9a-f"
RE_UUID="[$RE_HEX]{8}-[$RE_HEX]{4}-[$RE_HEX]{4}-[$RE_HEX]{4}-[$RE_HEX]{12}"
RE_STATUS_LINE="[UD][NLJM] "
DT_FMT="%F %T"
# ANSI colors
COLOR_BLACK_NORMAL="\e[0;30m"
COLOR_RED_NORMAL="\e[0;31m"
COLOR_GREEN_NORMAL="\e[0;32m"
COLOR_BLUE_NORMAL="\e[0;34m"
COLOR_BLACK_BOLD="\e[1;30m"
COLOR_RED_BOLD="\e[1;31m"
COLOR_GREEN_BOLD="\e[1;32m"
COLOR_BLUE_BOLD="\e[1;34m"
COLOR_RESET="\e[0m"
HELP_COLOR_SAFE=$COLOR_GREEN_NORMAL
HELP_COLOR_BOLD=$COLOR_BLACK_BOLD
ERROR_COLOR=$COLOR_RED_NORMAL
# select menu meta options
N_ALL=1
N_CANCEL=0
LOG_START="S"
LOG_FINISH="F"
# globally cache nodetool status and cfstats output to improve performance
hosts_cache="" # cache hosts output of nodetool status
keyspaces_cache="" # cache output of nodetool cfstats
# avoid these global variables unless you understand them
cf=""
in=""
keyspace=""
op=""
yn=""
c_host=${1:-"localhost"}
c_keyspace=${2:-""}
c_option=${3:-""}
first_run=1
n_views=0
program=${0##*/} # extract basename from the full pathname ($0)
if [ $program == "$program_debugger" ]; then # quiet -v option
_Dbg_set_trace_init=1
fi
msg_usage="$program $version usage: $program [connection_host=localhost] [keyspace=all] [one character option=none]"
show_usage() {
echo $msg_usage
}
cleanup() {
echo
return $?
}
control_c()
# run if user hits control-c
{
cleanup
return $?
}
# trap keyboard interrupt (control-c)
# trap control_c SIGINT
select_colors_normal() {
DISPLAY_COLOR_GREEN=$COLOR_GREEN_NORMAL
DISPLAY_COLOR_RED=$COLOR_RED_NORMAL
DISPLAY_COLOR_BLUE=$COLOR_BLUE_NORMAL
}
select_colors_bold() {
DISPLAY_COLOR_GREEN=$COLOR_GREEN_BOLD
DISPLAY_COLOR_RED=$COLOR_RED_BOLD
DISPLAY_COLOR_BLUE=$COLOR_BLUE_BOLD
}
select_colors_normal
clear_screen() {
clear
# echo -n -e '\0033\0143'
}
clear_stdin() {
read -t 1 -n 10000 discard_me # this is a bashism to clear stdin (before asking for the real user input), though adding a 1 second pause
}
c_debug() {
s=$1
echo "debug: $s"
c_press_any_key
}
c_error() {
msg=$1
echo -e "\n${ERROR_COLOR}error: $msg${COLOR_RESET}"
}
c_press_any_key() {
clear_stdin
echo
read -s -n 1 -p "Press any key to continue, Ctrl+C to quit... " anykey
echo # acknowledge that user supplied input
}
validate_base() {
err_msg=$1
re=$2
s=$3
allow_empty=$4
on_error_exit=$5
if [ -z "$s" ]; then
[ "$allow_empty" -ne 1 ] && continue
else
if ! [[ $s =~ $re ]] ; then
c_error "$err_msg"
if [ "$on_error_exit" -eq 1 ]; then
exit 1
else
c_press_any_key
continue
fi
fi
fi
}
validate() {
type=$1
s=$2
allow_empty=$3
on_error_exit=$4
if [ "$type" = "dc" ]; then
validate_base "not a valid DC" "^[a-z0-9_-]+$" "$s" "$allow_empty" "$on_error_exit"
elif [ "$type" = "dclist" ]; then
validate_base "not a valid DC list" "^[a-z0-9_,-]+$" "$s" "$allow_empty" "$on_error_exit"
elif [ "$type" = "float" ]; then
validate_base "not a valid float" "^[0-9.]+$" "$s" "$allow_empty" "$on_error_exit"
elif [ "$type" = "host" ]; then
validate_base "not a valid hostname, domainname or IP address" "^[a-z0-9_.:-]+$" "$s" "$allow_empty" "$on_error_exit"
elif [ "$type" = "cf" ] | [ "$type" = "keyspace" ]; then
validate_base "not a valid column family name" "^[a-z][a-z0-9_]{0,31}$" "$s" "$allow_empty" "$on_error_exit"
elif [ "$type" = "integer" ]; then
validate_base "not a valid integer" "^-?[0-9]{1,20}$" "$s" "$allow_empty" "$on_error_exit"
elif [ "$type" = "java_domain" ]; then # eg. org.apache.cassandra.service.StorageProxy
validate_base "not a valid java domain" "^[a-z0-9.]+$" "$s" "$allow_empty" "$on_error_exit"
elif [ "$type" = "key" ] || [ "$type" = "token" ]; then
validate_base "not a valid token" "^-?$RE_HEX+$" "$s" "$allow_empty" "$on_error_exit"
elif [ "$type" = "loglevel" ]; then
validate_base "not a valid logging level" "^(TRACE|DEBUG|INFO|WARN|ERROR|FATAL)$" "$s" "$allow_empty" "$on_error_exit"
elif [ "$type" = "number" ]; then
validate_base "not a valid number" "^[0-9]{1,20}$" "$s" "$allow_empty" "$on_error_exit"
elif [ "$type" = "probability" ]; then
validate_base "not a valid probability" "^[01]\.[0-9.]{3}$" "$s" "$allow_empty" "$on_error_exit"
elif [ "$type" = "uuid" ]; then
validate_base "not a valid uuid" "$RE_UUID" "$s" "$allow_empty" "$on_error_exit"
else
echo "error: invalid type '$type'"
exit 4
fi
}
append_auth() {
cmd=$1
# append authentication credentials if set in cassandra-env.sh
if env | grep -q ^JMX_USERNAME=
then
cmd="$cmd -u $JMX_USERNAME -pw $JMX_PASSWORD"
fi
echo -n "$cmd" # bash doesn't have a real function return capability, so echo the return value for capture by the caller
}
do_bp() {
bp_msg=${1:-}
if [ $program == "$program_debugger" ]; then # see programming notes above to understand how to make this work without terminating because of of pty conflicts
echo -e "\ninfo: $bp_msg\n"
_Dbg_debugger
fi
}
do_colorize() {
s=${1:-}
pre=""
post=$COLOR_RESET
out=""
# get banner
if [[ $s =~ (.*Rack) ]]; then
match=${BASH_REMATCH[1]}
out=$match
s=${s#*$match}
fi
re="($RE_STATUS_LINE"'[^
]*)'
while [[ $s =~ $re ]]; do
out="$out\n"
match=${BASH_REMATCH[1]}
s=${s#*$match}
if [ "${match:0:2}" = "UN" ]; then
pre=$DISPLAY_COLOR_GREEN
elif [ "${match:0:2}" = "DN" ]; then
pre=$DISPLAY_COLOR_RED
else
pre=$DISPLAY_COLOR_BLUE
fi
out="$out$pre$match$post"
done
}
# deprecated function
do_colorize_old() {
s=$1
re="^($RE_STATUS_LINE.*)"
pre=""
post=$COLOR_RESET
printf %s "$s" | while IFS= read -r line || [ -n "$line" ]; do
# we are in a subshell, so variable settings will be forgotten. either use echo or write to a file for persistence
if [[ $line =~ $re ]]; then
match=${BASH_REMATCH[0]}
if [ "${match:0:3}" = "UN " ]; then
pre=$DISPLAY_COLOR_GREEN
elif [ "${match:0:3}" = "DN " ]; then
pre=$DISPLAY_COLOR_RED
else
pre=$DISPLAY_COLOR_BLUE
fi
l=${line/$match/$pre$match$post}
echo "$l"
else
echo "$line"
fi
done
}
# parse_var_regex_slow takes 4x longer than parse_var with read
parse_var_new() {
s=${1:-}
re=${2:-}
if [ -z "$re" ]; then
echo "error: empty regex"
exit 9
fi
# match everything before a newline, then an optional newline. we need to do it this way because all matches in bash are greedy
re_line='([^
]*)(
?)'
while [[ -n "$s" ]] && [[ $s =~ $re_line ]]; do # need to remove newline here though
s=${s#*${BASH_REMATCH[1]}${BASH_REMATCH[2]}}
[[ ${BASH_REMATCH[1]} =~ $re ]] && echo -n "${BASH_REMATCH[1]} "
done
}
parse_var() {
s=$1
re=$2
printf %s "$s" | while IFS= read -r line || [ -n "$line" ]
# we are in a subshell, so variable settings will be forgotten. either use echo or write to a file for persistence
do
if [[ $line =~ $re ]] ; then
echo -n "${BASH_REMATCH[1]} "
fi
done
}
get_hosts() {
s=$1
re="$RE_STATUS_LINE([^ ]+) "
parse_var "$s" "$re"
}
get_nodes() {
s=$1
re="$RE_STATUS_LINE.* ($RE_UUID)"
parse_var "$s" "$re"
}
get_keyspaces() {
s=`$nodetool_full cfstats`
re='Keyspace: (.*)'
parse_var "$s" "$re"
}
get_cfs() {
keyspace=$1
s=`$nodetool_full cfstats $keyspace`
re='Table: (.*)'
parse_var "$s" "$re"
}
validate_cmd_line() {
if [ "$c_host" = "-h" ]; then
show_usage
exit 1
fi
validate "host" "$c_host" 0 1
# when nodetool connects to c_host, it will do the final name resolution and auth validations
# prepend connection host
nodetool_full="$nodetool_cmd -h $c_host"
nodetool_full=$(append_auth "$nodetool_full")
# use string functions to extract Cassandra version number
str=`$nodetool_full version 2>&1`
if [[ $str =~ error ]] || [[ $str =~ Failed ]] || [[ $str =~ resolve ]]; then
echo "error: cannot connect to '$c_host'"
exit 1
fi
c_version=${str##* }
if [ -n "$c_keyspace" ]; then
keyspaces_cache=$(get_keyspaces)
c_keyspace_tmp=" $c_keyspace "
if [[ " $keyspaces_cache " != *$c_keyspace_tmp* ]]; then
echo "error: unknown keyspace '$c_keyspace'"
show_usage
exit 2
fi
fi
if [ ${#c_option} -gt 1 ]; then
echo "error: unknown command"
show_usage
exit 3
elif [ "$c_option" = "q" ]; then
exit
elif [ "$c_option" = "2" ]; then
select_colors_bold
in=""
else
in=$c_option
fi
}
validate_cmd_line
do_status() {
s=$1
if [ "$s" -eq 0 ]; then
echo -e "info: nodetool was successful!"
else
c_error "nodetool was not successful, exit status was $s"
fi
}
show_banner() {
c_keyspace_tmp=""
[ -n "$c_keyspace" ] && c_keyspace_tmp="/$c_keyspace"
echo -e "$program $version (c) James Briggs $(date), connected to /$c_host$c_keyspace_tmp\n$c_info"
}
show_help() {
clear_screen
banner=$(show_banner)
echo -e "$banner
${HELP_COLOR_BOLD}Help for Interactive Commands ${HELP_COLOR_SAFE}(Items in green are safe, or reporting-only commands)${COLOR_RESET}
${HELP_COLOR_BOLD}Cluster-wide Commands${COLOR_RESET}
${HELP_COLOR_SAFE}C nodetool cfhistograms keyspace column family |
cfstats [keyspace].[column family] |
describecluster |
getendpoints <keyspace> <cf> <key> |
getsstables <keyspace> <cf> <key> |
rangekeysample | tpstats
R nodetool ring [keyspace] |
describering [keyspace]
G nodetool getstreamthroughput | getcompactionthroughput | gossipinfo${COLOR_RESET}
f nodetool flush [keyspace] [column family ...]
r nodetool -h issue_host repair [-par] [-local] [-dc DC1,DC2 ...] keyspace [column family ...] [-st token] [-et token]
U nodetool rebuild [DC] |
reloadtriggers |
setcachecapacity <key-cache-capacity> <row-cache-capacity> |
setcachekeystosave <key-cache-keys-to-save> <row-cache-keys-to-save> |
setcompactionthroughput [MB] |
setLoggingLevel [logger] [loglevel] |
setstreamthroughput [MB] |
settraceprobability [0.000:1.000]
${HELP_COLOR_BOLD}Node-specific Commands (target_host is a specific Cassandra node)${COLOR_RESET}
B nodetool -h target_host clearsnapshot | snapshot
d nodetool -h target_host drain
j nodetool -h target_host join
c nodetool -h target_host compact [keyspace] [column family ...]
k nodetool -h target_host cleanup [keyspace] [column family ...]
f nodetool -h target_host refresh <keyspace> [column family]
i nodetool -h target_host rebuild_index [keyspace] [column family] [index ...] IndexNameExample: Standard3.IdxName,Standard3.IdxName1
I nodetool -h target_host invalidatekeycache | invalidaterowcache
m nodetool -h target_host move <token>
p nodetool -h target_host stop <compaction_type>
s nodetool -h target_host scrub [skip] [keyspace] [column family ...]
${HELP_COLOR_SAFE}S nodetool -h target_host compactionhistory | compactionstats | info |
getLoggingLevels | netstats | proxyhistograms |
statusbinary | statusthrift | version${COLOR_RESET}
u nodetool -h target_host upgradesstables [keyspace] [column family ...]
x nodetool -h target_host removenode status|force|<ID>
Z nodetool -h target_host decommission |
enableautocompaction [keyspace] [column family ...] |
disableautocompaction [keyspace] [column family ...] |
enablebackup | disablebackup |
enablebinary | disablebinary |
enablegossip | disablegossip |
enablehandoff | disablehandoff |
enablethrift | disablethrift |
pausehandoff | resumehandoff |
resetlocalschema | truncatehints [endpoint ...]
${HELP_COLOR_BOLD}Miscellaneous Commands${COLOR_RESET}
${HELP_COLOR_SAFE}h help 1 light colors 2 bold colors q quit${COLOR_RESET}"
c_press_any_key
continue
}
msg_confirm() {
sure=""
[ "$flag_confirm_nodetool_operations" -eq 0 ] && return 1
c_confirm "Are you sure?"
if [ "$yn" -eq 1 ]; then
echo -n ""
else
c_error "You chose to cancel operation."
continue
fi
}
tidy_spaces() {
s=${1:-}
### bashism to trim spaces: parse string to an array, then squash array back to a string to trim and single-space
# declare -a ary=($s)
# echo -n "${ary[@]:+${ary[@]}}" # bashism to make set -u happy
echo -n $s # we want the side-effect of trimming spaces, so do not quote
}
msg_running() {
msg=$1
echo -e "\nReady to run '$msg'"
}
log_it() {
cmd=$1
stage=$2
status=${3:-0}
dt=$4
[ -z "$dt" ] && dt="$(date +"$DT_FMT")"
if [ -n "$logfile" ]; then
# you can customize the logfile name here in case you're running multiple sessions, or to log by cluster name. example:
# s=${c_name//[[:blank:]]/} # use cluster name, but remove spaces first
# logfile="$HOME/cass_top-$s.log"
s="$dt $$ $USER $stage $status '$cmd'"
echo "$s" >> "${logfile}"
echo "$s" >> "${logfile}.out"
fi
}
run_it() {
action=$1
nodeid=${2:-""}
action=$(tidy_spaces "$action")
if [ -z "$nodeid" ]; then
cmd_nopw="$nodetool_cmd -h $c_host $action"
cmd="$nodetool_full $action"
else
cmd_nopw="$nodetool_cmd -h $nodeid $action"
cmd=$(append_auth "$nodetool_cmd")
cmd="$cmd -h $nodeid $action"
fi
msg_running "$cmd_nopw"
msg_confirm
dt="$(date +"$DT_FMT")"
log_it "$cmd_nopw" "$LOG_START" "0" "$dt"
echo -e "\n$dt Start\n"
if [ -z "$logfile" ]; then
$cmd
status=$?
else
# If you want nodetool output logged, pick one of the following exec options that works on your bash:
#
# Option 1 (subshell):
# ( $cmd ) 2>&1 | tee -a ${logfile}.out
# status=${PIPESTATUS[0]}
# Option 2 (named pipe):
$cmd > >(tee -a ${logfile}.out) 2>&1
status=${PIPESTATUS[0]}
# Option 3 (backticks):
# out=`$cmd 2>&1`
# status=${PIPESTATUS[0]}
# echo "$out" >>${logfile}.out
# echo "$out"
fi
do_status $status
dt="$(date +"$DT_FMT")"
log_it "$cmd_nopw" "$LOG_FINISH" "$status" "$dt"
echo -e "\n$dt End"
c_press_any_key
continue
}
do_exec() {
option=$1
[ -z "$option" ] && continue
run_it "$option"
}
get_keyspace() {
op=$1
all=${2:-""}
extra=${3:-""}
keys_remaining=${4:-""}
keys="$keyspaces_cache"
[ -n "$keys_remaining" ] && keys="$keys_remaining"
echo -e "\nWhich keyspaces do you want to $op?"
keyspace=$(c_select "$keys" "$all" "$extra")
if [ "$keyspace" = "$MSG_ALL" ]; then
keyspace=""
elif [ "$keyspace" = "$MSG_CANCEL" ]; then
continue
fi
}
get_multiple_keyspaces() {
op=$1
all=${2:-""}
type="keyspace"
items_remaining="$keyspaces_cache"
out=""
get_multiple_items_with_recursion $op $all $type
keyspace="$out"
}
get_cf() {
op=$1
all=${2:-""}
extra=${3:-""}
cfs_remaining=${4:-""}
if [ -n "$cfs_remaining" ]; then
cfs="$cfs_remaining"
else
cfs=$(get_cfs "$keyspace")
fi
echo -e "\nWhich column family do you want to $op?"
cf=$(c_select "$cfs" $all $extra)
if [ "$cf" = "$MSG_ALL" ]; then
cf=""
elif [ "$cf" = "$MSG_CANCEL" ]; then
continue
fi
}
get_multiple_cf() {
op=$1
all=${2:-""}
type="cf"
cfs=$(get_cfs "$keyspace")
items_remaining=" $cfs "
out=""
get_multiple_items_with_recursion $op $all $type
cf="$out"
}
get_multiple_items_with_recursion() {
op=$1
all=$2
type=$3
items_remaining=$(tidy_spaces "$items_remaining")
if [ -n "$items_remaining" ]; then
items_remaining=" $items_remaining "
if [ "$type" = "cf" ]; then
get_cf "$op" "$N_CANCEL" "$MSG_DONE" "$items_remaining"
item=$cf
elif [ "$type" = "keyspace" ]; then
get_keyspace "$op" "$N_CANCEL" "$MSG_DONE" "$items_remaining"
item=$keyspace
else
c_error "invalid type '$type'"
exit 6
fi
if [ "$item" = "$MSG_CANCEL" ]; then
continue # unwind many stack levels
elif [ "$item" = "$MSG_DONE" ]; then
items="$out" # unwind recursion stack once
else
out="$out $item"
if [ -n "$items_remaining" ]; then
items_remaining=${items_remaining// $item / }
get_multiple_items_with_recursion $op $all $type # we cannot use a do loop or continue will not work, and there's no goto in bash, so use recursion as a last resort
fi
fi
fi
}
do_flush_exec() {
keyspace=""
cf=""
get_keyspace "flush" "$N_ALL"
[ -n "$keyspace" ] && get_multiple_cf "flush" "$N_ALL"
run_it "flush $keyspace $cf"
}
do_misc_B() {
file=""
filename=""
keyspace=""
cf=""
echo "Choose a node snapshot command: "
op=$(c_select "clearsnapshot snapshot")
[ "$op" = "$MSG_CANCEL" ] && continue
c_confirm "Do you want to enter a snapshot filename?"
if [ "$yn" -eq 1]; then
echo
read -e -p "Which snapshot filename? ["$MSG_CANCEL"]: " filename
if [ -z "$filename" ]; then
continue
fi
filename="-f $filename"
fi
get_multiple_keyspaces "$op" "$N_CANCEL"
# if [ "$op" = "snapshot" ]; then
# not sure why nodetool snapshot accepts multiple keyspaces plus a single cf
# get_cf "$op" "$N_CANCEL"
# [ -n "$cf" ] && cf="-cf $cf"
# fi
run_it "$op $filename $keyspace $cf"
}
do_misc_C() {
echo "Choose a cluster-wide reporting command: "
op=$(c_select "cfhistograms cfstats describecluster getendpoints getsstables rangekeysample tpstats")
[ "$op" = "$MSG_CANCEL" ] && continue
keyspace=""
cf=""
key=""
if [ "$op" = "cfstats" ]; then
get_keyspace "$op" "$N_ALL"
[ -n "$keyspace" ] && get_cf "$op" "$N_ALL"
[ -n "$cf" ] && cf=".$cf"
run_it "cfstats $keyspace$cf"
elif [ "$op" = "cfhistograms" ]; then
get_keyspace "$op" "$N_CANCEL"
get_cf "$op" "$N_CANCEL"
run_it "cfhistograms $keyspace $cf"
elif [ "$op" = "getendpoints" ] || [ "$op" = "getsstables" ]; then
get_keyspace "$op" "$N_CANCEL"
get_cf "$op" "$N_CANCEL"
read -e -p "Which key do you want $op?" key
validate "key" $key 0 1
run_it "$op $keyspace $cf $key"
else
run_it "$op"
fi
}
select_node() {
s=$1
echo -e "\nChoose a host node to $s: "
nodeid=""
nodeid=$(c_select "$hosts_cache")
[ "$nodeid" = "$MSG_CANCEL" ] && continue
}
do_stop_exec() {
nodeid=""
cl=""
select_node "stop"
echo "Choose compaction level [$MSG_CANCEL]: "
cl=$(c_select "COMPACTION VALIDATION CLEANUP SCRUB INDEX_BUILD")
[ "$cl" = "$MSG_CANCEL" ] && continue
run_it "stop $cl" "$nodeid"
}
do_move_exec() {
nodeid=""
token=""
select_node "move token"
read -e -p "What is the token to move? [$MSG_CANCEL]: " token
validate "integer" "$token" 0 1
[ "${token:0:1}" = "-" ] && token="\\\\$token"
run_it "move $token" "$nodeid"
}
do_compact_exec() {
nodeid=""
keyspace=""
cf=""
select_node "compact"
get_keyspace "compact" "$N_ALL"
[ -n "$keyspace" ] && get_multiple_cf "compact" "$N_ALL"
run_it "compact $keyspace $cf" "$nodeid"
}
do_refresh_exec() {
nodeid=""
keyspace=""
cf=""
select_node "refresh"
get_keyspace "refresh" "$N_CANCEL"
get_cf "refresh" "$N_ALL"
run_it "refresh $keyspace $cf" "$nodeid"
}
c_confirm() {
msg=$1
echo
clear_stdin
read -n 1 -e -p "$msg [yes/NO/cancel]: " yn # also could do select "cancel yes no" to force user to type return
# echo # this would be correct, but then all the read -e "\n..." need to be changed too
if [[ $yn =~ ^[Yy]$ ]]; then
yn=1
elif [[ $yn =~ ^[Cc]$ ]]; then
continue
else
yn=0
fi
}
do_repair_exec() {
nodeid=""
keyspace=""
cf=""
par=""
dc=""
st=""
et=""
select_node "issue repair (this node's DC will be the default local DC for this repair)"
c_confirm "All nodes in the repair DC/DCs must be up. Are all nodes up?"
if [ "$yn" -ne 1 ]; then
c_error "All nodes in the repair DC/DCs must be up."
c_press_any_key
continue
fi
c_confirm "Do you want to repair in parallel?"
[ "$yn" -eq 1 ] && par="-par"
echo -e "\nDo you want the repair to be in the local DC, or other DCs?"
dc=$(c_select "LOCAL DCs")
if [ "$dc" = "$MSG_CANCEL" ]; then
continue
elif [ "$dc" = "LOCAL" ]; then
dc="-local"
elif [ "$dc" = "DCs" ]; then
echo
read -e -p "What is the list of DCs (separated by commas)? [$MSG_CANCEL]: " dc
[ -z "$dc" ] || [ "$dc" = "$MSG_CANCEL" ] && continue
validate "dclist" "$dc" 0 1
[ -n "$dc" ] && dc="-dc $dc"
fi
c_confirm "Do you want to enter a start token?"
if [ "$yn" -eq 1 ]; then
echo
read -e -p "What is the start token? [$MSG_CANCEL]: " st
[ -z "$st" ] && continue
validate "token" $st 0 1
st="-st $st"
fi
c_confirm "Do you want to enter an end token?"
if [ "$yn" -eq 1 ]; then
echo
read -e -p "What is the end token? [$MSG_CANCEL]: " et
[ -z "$et" ] && continue
validate "token" $et 0 1
et="-et $et"
fi
get_keyspace "repair" "$N_CANCEL"
[ -n "$keyspace" ] && get_multiple_cf "repair" "$N_ALL"
run_it "repair $par $dc $keyspace $cf $st $et" "$nodeid"
}
do_upgradesstables_exec() {
nodeid=""
keyspace=""
cf=""
select_node "upgradesstables"
get_keyspace "upgradesstables" "$N_ALL"
[ -n "$keyspace" ] && get_multiple_cf "upgradesstables" "$N_ALL"
[ -z "$keyspace" ] && keyspace="-a"
run_it "upgradesstables $keyspace $cf" "$nodeid"
}
join_hosts_and_nodes() {
skip_host=$1
declare -a ahosts=($hosts_cache)
declare -a anodes=($nodes_cache)
# join host names and node uuids into one string "host(uuid) ..." for use in a select menu
n=0
for h in ${ahosts[*]}; do
if [ "$h" != "$skip_host" ]; then # removenode restricts the connection host from being removed
ids="$ids $host(${anodes[$n]})"
fi
let n++
done
echo $ids # return value to caller for capture
}
do_removenode_exec() {
nodeid=""
cmd=""
ids=$(join_hosts_and_nodes "$c_host")
echo "Choose a node (it must use vnodes, and after doing decommission) to remove with the removenode command: "
nodeid=$(c_select "$ids")
[ "$nodeid" = "$MSG_CANCEL" ] && continue