-
Notifications
You must be signed in to change notification settings - Fork 7
/
es_funcs.bash
4347 lines (3788 loc) · 195 KB
/
es_funcs.bash
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
### es wrapper cmd inventory
declare -A escmd
escmd[l]="./esl"
escmd[p]="./esp"
escmd[c]="./esc"
### kb wrapper cmd inventory
declare -A kbcmd
kbcmd[l]="./kbl"
kbcmd[p]="./kbp"
kbcmd[c]="./kbc"
### es data node naming conventions
nodeBaseName="rdu-es-data-0"
declare -A esnode
esnode[l]="lab-${nodeBaseName}"
esnode[p]="${nodeBaseName}"
esnode[c]="instance-0000000"
### zenoss wrapper cmd inventory
declare -A zencmd
zencmd[l]="./zsl"
zencmd[p]="./zsp"
filename="es_funcs.bash"
#################################################
### Tips
#################################################
# watch -x bash -c ". es_funcs.bash; show_recovery p"
#################################################
### References
#################################################
# - https://dzone.com/articles/23-useful-elasticsearch-example-queries
#################################################
### Globals
#################################################
uname="$(uname)"
# sed
[[ "$uname" == 'Darwin' ]] && sedCmd=gsed || sedCmd=sed
# gpaste
[[ "$uname" == 'Darwin' ]] && pasteCmd=gpaste || pasteCmd=paste
# gdate
[[ "$uname" == 'Darwin' ]] && dateCmd=gdate || dateCmd=date
# ggrep
[[ "$uname" == 'Darwin' ]] && grepCmd=ggrep || grepCmd=grep
#################################################
### Functions
#################################################
#0-----------------------------------------------
# helper funcs
##-----------------------------------------------
gen_README () {
# generate contents of README.md
cat \
<($sedCmd -n '0,/^$ ▶ escli_ls$/p' README.md) \
<(escli_ls) \
<($sedCmd -n '/^show_template_ilm_idx_alias_details$/,/^You can also get that list/p' README.md | grep -v '^show_template_ilm_idx_alias_details$') \
<(grep -B1 '^$ ▶ escli_lsl$' README.md) \
<(escli_lsl) \
<($sedCmd -n '/^show_template_ilm_idx_alias_details[ ]\+#/,$p' README.md | $sedCmd -n '4,$p')
}
cmp_README () {
# sdiff new README.md vs. existing README.md
sdiff <(gen_README) README.md | less
}
mk_README () {
# save new README.md over existing README.md
gen_README | tee README.md.new
cp -f README.md.new README.md
rm -f README.md.new
}
gen_EXAMPLES () {
# generate content of EXAMPLES.md
./gen_EXAMPLES.bash
}
cmp_EXAMPLES () {
# sdiff new EXAMPLES.md vs. existing EXAMPLES.md
sdiff <(gen_EXAMPLES) EXAMPLES.md | less
}
mk_EXAMPLES () {
# save new EXAMPLES.md over existing EXAMPLES.md
gen_EXAMPLES | tee EXAMPLES.md.new
cp -f EXAMPLES.md.new EXAMPLES.md
rm -f EXAMPLES.md.new
}
#1-----------------------------------------------
# date & math funcs
##-----------------------------------------------
calc_date () {
# print UTC date X "days | days ago"
local english_days="$1"
[[ -z ${FUNCNAME[1]} ]] && caller=${FUNCNAME[0]} || caller=${FUNCNAME[1]}
[[ $english_days != *days* ]] \
&& printf "\nUSAGE: ${caller} [ 'X days ago' | 'X days' ]\n\n" \
&& return 1
${dateCmd} -u --date="$english_days" +%Y.%m.%d
}
calc_hour () {
# print UTC date X "hours | hours ago"
local english_hours="$1"
[[ -z ${FUNCNAME[1]} ]] && caller=${FUNCNAME[0]} || caller=${FUNCNAME[1]}
[[ $english_hours != *hours* ]] \
&& printf "\nUSAGE: ${caller} [ 'X hours ago' | 'X hours' ]\n\n" \
&& return 1
${dateCmd} --utc --iso-8601=sec --date="$english_hours"
}
calc_date_1daybefore () {
# print UTC date 1 day before given date (YYYY-mm-dd)
local date="$1"
[[ -z ${FUNCNAME[1]} ]] && caller=${FUNCNAME[0]} || caller=${FUNCNAME[1]}
[[ ! $date =~ [0-9]{4}-[0-9]{2}-[0-9]{2} ]] \
&& printf "\nUSAGE: ${caller} [YYYY-mm-dd]\n\n" \
&& return 1
${dateCmd} -u --date="$date -1 days" +%Y.%m.%d
}
calc_date_1dayafter () {
# print UTC date 1 day after given date (YYYY-mm-dd)
local date="$1"
[[ -z ${FUNCNAME[1]} ]] && caller=${FUNCNAME[0]} || caller=${FUNCNAME[1]}
[[ ! $date =~ [0-9]{4}-[0-9]{2}-[0-9]{2} ]] \
&& printf "\nUSAGE: ${caller} [YYYY-mm-dd]\n\n" \
&& return 1
${dateCmd} -u --date="$date +1 days" +%Y.%m.%d
}
calc_millis_date () {
# convert UTC millis date to human format
local millisTime="$1"
[[ -z ${FUNCNAME[1]} ]] && caller=${FUNCNAME[0]} || caller=${FUNCNAME[1]}
[[ ! $millisTime =~ [0-9]{5,} ]] \
&& printf "\nUSAGE: ${caller} [ '<millis time>' ]\n\n" \
&& return 1
echo ""
printf "%s: %s\n" "UTC" "$(date -u -r "$(( ($millisTime + 500) / 1000 ))")"
printf "%s: %s\n" "$(date +"%Z")" "$(date -r "$(( ($millisTime + 500) / 1000 ))")"
echo ""
}
julian_day () {
# calculate julian day based on a YYYYmmdd
[[ -z ${FUNCNAME[1]} ]] && caller=${FUNCNAME[0]} || caller=${FUNCNAME[1]}
[[ ! $1 =~ [0-9]{8} ]] \
&& printf "\nUSAGE: ${caller} YYYYmmdd\n\n" \
&& return 1
local year="${1:0:4}"
local month="$(printf "%g" "${1:4:2}")"
local day="$(printf "%g" "${1:6:2}")"
#DEBUG# echo "$year - $month - $day"
local julianday=$((day - 32075 + 1461 * (year + 4800 - (14 - month) / 12) / 4 + 367 * \
(month - 2 + ((14 - month) / 12) * 12) / 12 - 3 * \
((year + 4900 - (14 - month) / 12) / 100) / 4))
echo "$julianday"
# REFS:
# - https://stackoverflow.com/questions/43317428/bash-how-to-get-current-julian-day-number
# - https://en.wikipedia.org/wiki/Julian_day
}
ceiling_divide () {
# ceiling divide 2 numbers
[[ -z ${FUNCNAME[1]} ]] && caller=${FUNCNAME[0]} || caller=${FUNCNAME[1]}
[[ ! $1 =~ [0-9]{1,} ]] && [[ ! $2 =~ [0-9]{1,} ]] \
&& printf "\nUSAGE: ${caller} <numerator> <denominator>\n\n" \
&& return 1
ceiling_result=$(echo "($1 + $2 - 1)/$2" | bc)
echo "$ceiling_result"
}
#2-----------------------------------------------
# usage funcs
##-----------------------------------------------
escli_ls () {
# list function names
awk '/\(\)/ {print $1}' ${filename} | grep -vE "usage_chk|grep"
}
escli_lsl () {
# list function names + desc.
while read line; do
if [[ $line =~ ^#[0-9]+-- ]]; then
printf "\n"
grep --color=never -A2 "^${line}" "${filename}"
else
grep --color=never -A1 "^${line} () {" "${filename}" | sed 's/ ().*//' | \
paste - - | pr -t -e43
fi
done < <(awk '/^[0-9a-zA-Z_-]+ \(\) {|^#[0-9]+--/ {print $1}' "${filename}" | grep -v usage_chk)
printf "\n\n"
}
usage_chk1 () {
# usage msg for cmds w/ 1 arg
local env="$1"
[[ $env =~ [lpc] ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c]\n\n" && return 1
}
usage_chk2 () {
# usage msg for cmds w/ 2 arg (where 2nd arg. is a node suffix)
local env="$1"
local node="$2"
[[ $env =~ [lpc] && $node =~ 1[a-z] ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <node suffix--[1a|1b|1c|1d...]>\n$(list_node_name_suffixes_usage_helper $env)\n\n" \
&& return 1
}
usage_chk3 () {
# usage msg for cmds w/ 2 arg (where 2nd arg. is a index pattern)
local env="$1"
local idxArg="$2"
[[ $env =~ [lpc] && $idxArg != '' ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <idx pattern>\n\n" \
&& return 1
}
usage_chk4 () {
# usage msg for cmds w/ 4 arg (<shard name> <shard num> <from/to node suf.>)
local env="$1"
local shardName="$2"
local shardNum="$3"
local nodeCode="$4"
[[ $env =~ [lpc] && $shardName != '' \
&& $shardNum != '' \
&& $nodeCode =~ 1[a-z] || $nodeCode =~ [0-9]{3} ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <shard name> <shard num> <from/to node>\n$(list_node_name_suffixes_usage_helper $env)\n\n" \
&& return 1
}
usage_chk5 () {
# usage msg for cmds w/ 5 arg (<shard name> <shard num> <from node suf.> <to node suf.>)
local env="$1"
local shardName="$2"
local shardNum="$3"
local fromCode="$4"
local toCode="$5"
[[ $env =~ [lpc] && $shardName != '' \
&& $shardNum != '' \
&& $fromCode =~ 1[a-z] \
&& $toCode =~ 1[a-z] ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <shard name> <shard num> <from node> <to node>\n$(list_node_name_suffixes_usage_helper $env)\n\n" \
&& return 1
}
usage_chk6 () {
# usage msg for cmds w/ 4 arg (<k8s namespace> <start time> <end time>)
local env="$1"
local idxArg="$2"
local namespace="$3"
local sTime="$4"
local eTime="$5"
MSG1=$(cat <<-EOM
------------------------------------------------------------------------------------------------------
Example
=======
$ del_docs_k8s_ns_range l filebeat-* big-dipper-perf 2019-07-11T11:57:20.968Z 2019-07-12T04:26:38.757Z
{"task":"vudQxvnfSQuxMtdkq8ZTUQ:844209600"}
------------------------------------------------------------------------------------------------------
Source: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
EOM
)
[[ $env =~ [lpc] && $idxArg != '' \
&& $namespace != '' \
&& $sTime != '' \
&& $sTime =~ [0-9]{4}-[0-9]{2}-[0-9]{2}T && $sTime =~ [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z \
&& $eTime != '' \
&& $eTime =~ [0-9]{4}-[0-9]{2}-[0-9]{2}T && $eTime =~ [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z ]] \
&& return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <idx pattern> <k8s namespace> <start time> <end time>\n\n\n" \
&& printf " * TIME FORMAT: 2019-07-10T00:00:00.000Z\n\n" \
&& printf " * INDX FORMAT:\n -- %s\n -- %s\n -- %s\n\n%s\n\n\n" \
"filebeat-*" \
"-or- filebeat-6.5.1-2019.07.04,filebeat-6.5.1-2019.07.05,...." \
"-or- filebeat-*-2019.07*" \
"$MSG1" \
&& return 1
}
usage_chk7 () {
# usage msg for cmds w/ 3 arg (where 2nd arg. is a index pattern, and 3rd is a integer)
local env="$1"
local idxArg="$2"
local repNum="$3"
[[ $env =~ [lpc] && $idxArg != '' && ( $repNum =~ ^[0-9]{1,2}$ ) ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <idx pattern> <shard replica count>\n\n" \
&& return 1
}
usage_chk8 () {
# usage msg for cmds w/ 3 arg (where 2nd arg. is a index pattern, and 3rd is a integer)
local env="$1"
local idxArg="$2"
local retNum="$3"
[[ $env =~ [lpc] && $idxArg != '' && ( $retNum =~ ^[0-9]{1,2}$ ) ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <idx base type> <days to retain>\n\n" \
&& printf " * idx base type: [filebeat|metricbeat|packetbeat|etc.]\n" \
&& printf " * days to retain: [30|60|90|etc.]\n\n\n" \
&& return 1
}
usage_chk9 () {
# usage msg for cmds w/ 2 arg (where 2nd arg. is a integer - size in MB)
local env="$1"
local size="$2"
[[ $env =~ [lpc] && ( $size =~ ^[0-9]{2,4}$ && $size -gt 39 && $size -lt 2001 ) ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <size in megabytes>\n\n" \
&& printf " * size in megabytes: [40|100|250|500|2000|etc.]\n\n" \
&& printf " NOTE: ...minimum is 40, the max. 2000!...\n\n\n" \
&& return 1
}
usage_chk10 () {
# usage msg for cmds w/ 2 arg (where 2nd arg. is a integer (days))
local env="$1"
local days="$2"
[[ $env =~ [lpc] && ( $days =~ ^[0-9]{1,2}$ && $days -gt 0 && $days -lt 91 ) ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <days>\n\n" \
&& printf " * days: [1|5|30|45|90]\n\n" \
&& printf " NOTE: ...minimum is 1, the max. 90!...\n\n\n" \
&& return 1
}
usage_chk11 () {
# usage msg for cmds w/ 3 arg (where 2nd arg. is a index pattern, and 3rd is a integer or `-1`)
local env="$1"
local idxArg="$2"
local numShards="$3"
[[ $env =~ [lpc] && $idxArg != '' && ( $numShards =~ ^[-]*[0-9]{1,2}$ ) ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <idx pattern> <num shards>\n\n" \
&& return 1
}
usage_chk12 () {
# usage msg for cmds w/ 3 arg (where 2nd arg. is a index pattern, and 3rd is a field name)
local env="$1"
local idxArg="$2"
local field="$3"
# NOTE: Be careful where you put the `-` in the `$field` test. Should be at end, anywhere else
# and bash may interpret characters in a range
[[ $env =~ [lpc] && $idxArg != '' && ( $field =~ ^[[email protected]]+ ) ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <idx pattern> <field name>\n\n" \
&& return 1
}
usage_chk13 () {
# usage msg for cmds w/ 3 arg (where 2nd arg. is a index pattern, and 3rd is a integer)
local env="$1"
local idxArg="$2"
local maxNum="$3"
[[ $env =~ [lpc] && $idxArg != '' && ( $maxNum =~ ^[0-9]{1,3}$ ) ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <idx base type> <max number>\n\n" \
&& return 1
}
usage_chk14 () {
# usage msg for cmds w/ 2 arg (where 2nd arg. is a policy name)
local env="$1"
local policyArg="$2"
[[ $env =~ [lpc] && $policyArg != '' ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <policy name>\n\n" \
&& return 1
}
usage_chk15 () {
# usage msg for cmds w/ 2 arg (where 2nd arg. is a integer b/w 2-10)
local env="$1"
local num="$2"
[[ $env =~ [lpc] ]] && (( num >= 2 && num <= 10 )) && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <number>\n\n" \
&& printf " NOTE: ...minimum is 2, the max. 10!...\n\n\n" \
&& return 1
}
usage_chk16 () {
# usage msg for cmds w/ 3 arg (where 2nd arg. is a index, and 3rd is a zero-padded integer)
local env="$1"
local idxArg="$2"
local ilmSuffix="$3"
[[ $env =~ [lpc] && $idxArg != '' && ( $ilmSuffix =~ ^[0-9]{6}$ ) ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <index> <index suffix>\n\n" \
&& printf " * index: [filebeat-ilm-6.5.1|filebeat-ilm-7.6.2|...]\n\n" \
&& printf " * index suffix: [000001|000002|...]\n\n" \
&& return 1
}
usage_chk17 () {
# usage msg for cmds w/ 2 arg (where 2nd arg. is a index name)
local env="$1"
local idxArg="$2"
[[ $env =~ [lpc] && $idxArg != '' ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <idx name>\n\n" \
&& printf " * index: [filebeat-60d-6.5.1-2020.10.04-000089|filebeat-60d-6.5.1-2020.10.04-000140|...]\n\n" \
&& return 1
}
usage_chk18 () {
# usage msg for cmds w/ 2 arg (where 2nd arg. is a alias)
local env="$1"
local aliasArg="$2"
[[ $env =~ [lpc] && $aliasArg != '' ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <alias>\n\n" \
&& return 1
}
usage_chk19 () {
# usage msg for cmds w/ 2 arg (where 2nd arg. is a integer (days))
local env="$1"
local days="$2"
[[ $env =~ [lpc] && ( $days =~ ^[0-9]{1,3}$ && $days -gt 0 && $days -lt 366 ) ]] && return 0 || \
printf "\nUSAGE: ${FUNCNAME[1]} [l|p|c] <days>\n\n" \
&& printf " * days: [1|5|30|45|90|365]\n\n" \
&& printf " NOTE: ...minimum is 1, the max. 365!...\n\n\n" \
&& return 1
}
list_node_name_suffixes_usage_helper () {
# List node name suffixes (01a, 01b, etc.)
local env="$1"
if [[ ! -z $env ]]; then
list_node_name_suffixes $env
fi
}
#3-----------------------------------------------
# help funcs
##-----------------------------------------------
help_cat () {
# print help for _cat API call
local env="$1"
usage_chk1 "$env" || return 1
${escmd[$env]} GET '_cat'
}
help_indices () {
# print help for _cat/indices API call
local env="$1"
usage_chk1 "$env" || return 1
${escmd[$env]} GET '_cat/indices?pretty&v&help' | less
}
#4-----------------------------------------------
# node funcs
##-----------------------------------------------
list_nodes () {
# list ES nodes along w/ a list of data node suffixes for use by other cmds.
local env="$1"
usage_chk1 "$env" || return 1
#output=$(${escmd[$env]} GET '_cat/nodes?v&pretty')
output=$(${escmd[$env]} GET '_cat/nodes?v&h=ip,heap.percent,ram.percent,cpu,load_1m,load_5m,load_15m,node.role,master,name,disk.total,disk.used,disk.avail,disk.used_percent&s=name:asc')
dnodes=$(echo "${output}" | awk '/data|di.*instance/ { print $10 }' | sed 's/.*-00*//' | sort | paste -s -d"," -)
printf "\n%s\n\n" "${output}"
printf "valid data node suffixes: %s\n\n" "${dnodes}"
}
list_nodes_storage () {
# list ES nodes HDD usage
local env="$1"
usage_chk1 "$env" || return 1
output=$(${escmd[$env]} GET '_cat/nodes?v&h=ip,node.role,master,name,disk.total,disk.used,disk.avail,disk.used_percent&s=disk.used_percent:desc')
dnodes=$(echo "${output}" | awk '/data|di.*instance/ { print $4 }' | sed 's/.*-00*//' | sort | paste -s -d"," -)
printf "\n%s\n\n" "${output}"
printf "valid data node suffixes: %s\n" "${dnodes}"
printf "total data nodes: %s\n\n" "$(echo "$dnodes" | awk -F, '{print NF}')"
}
list_nodes_zenoss_alarms () {
# list ES node HDD usage alarms in Zenoss
local env="$1"
usage_chk1 "$env" || return 1
ips=$(${escmd[$env]} GET '_cat/nodes?h=ip,node.role,name' | awk '$2~/d/ {print $1" "$3}')
printf "\n"
printf "%-20s%-32s%-32s\n" "server" "zenoss alarm #1" "zenoss alarm #2"
printf "%-20s%-32s%-32s\n" "======" "===============" "==============="
set -- $ips
(
while [ ! -z "$1" ]; do
printf "%-20s%-s\n" \
"${2}" \
"$(${zencmd[$env]} $1 | jq ".result.data[].maxval" | paste - -)"
shift 2
done
) | sort -k1,1
printf "\n\n"
}
show_nodes_fs_details () {
# list ES nodes filesystem details
local env="$1"
usage_chk1 "$env" || return 1
${escmd[$env]} GET '_nodes/stats/fs?human&pretty' | jq -C . | less -r
}
show_nodes_circuit-breaker_summary () {
# list ES nodes circuit breaker tripped summaries
local env="$1"
usage_chk1 "$env" || return 1
printf "\nnode circuit breakers tripped counts"
printf "\n---------------------------------------------------\n"
${escmd[$env]} GET '_nodes/stats/breaker?pretty&human&filter_path=**.tripped,**.name' \
| jq '.[][][]' \
| paste - - - - - - - - - - - - - - - - - - \
| $sedCmd -e 's/{[ \t]\+/ /g' -e 's/"tripped"://g' -e 's/["},]//g' \
| column -t \
| sort
printf "\n--------- end of check ----------------------------\n\n"
cat <<-EOM
Circuit Breakers
----------------
Elasticsearch contains multiple circuit breakers used to prevent operations from causing an OutOfMemoryError.
Each breaker specifies a limit for how much memory it can use. Additionally, there is a parent-level breaker
that specifies the total amount of memory that can be used across all breakers.
-------------------------------------------------------------------------------------------------------------
* request
request circuit breaker allows Elasticsearch to prevent per-request data structures
(for example, memory used for calculating aggregations during a request) from exceeding
a certain amount of memory.
-------------------------------------------------------------------------------------------------------------
* fileddata
field data circuit breaker allows Elasticsearch to estimate the amount of memory a field
will require to be loaded into memory.
-------------------------------------------------------------------------------------------------------------
* in_flight_requests
in flight requests circuit breaker allows Elasticsearch to limit the memory usage of all
currently active incoming requests on transport or HTTP level from exceeding a certain
amount of memory on a node. The memory usage is based on the content length of the request
itself. This circuit breaker also considers that memory is not only needed for
representing the raw request but also as a structured object which is reflected by default
overhead.
-------------------------------------------------------------------------------------------------------------
* accounting
accounting circuit breaker allows Elasticsearch to limit the memory usage of things held
in memory that are not released when a request is completed. This includes things like
the Lucene segment memory.
-------------------------------------------------------------------------------------------------------------
* parent
there is a parent-level breaker that specifies the total amount of memory that can be used
across all breakers
-------------------------------------------------------------------------------------------------------------
Source: https://www.elastic.co/guide/en/elasticsearch/reference/current/circuit-breaker.html
EOM
}
show_nodes_circuit-breaker_details () {
# list ES nodes circuit breaker details
local env="$1"
usage_chk1 "$env" || return 1
${escmd[$env]} GET '_nodes/stats/breaker?pretty&human' | jq . -C | less -r
}
show_nodes_threadpools_active_rejected () {
# list ES nodes thread pool counts (_cat/thread_pool) ... any all zeros filtered out
local env="$1"
usage_chk1 "$env" || return 1
${escmd[$env]} GET '_cat/thread_pool?v&h=node_name,name,active,rejected,completed&s=node_name' \
| grep -v '0[ ]\+0[ ]\+0'
}
show_nodes_threadpools_details () {
# list ES nodes thread pool details (_cat/thread_pool)
local env="$1"
usage_chk1 "$env" || return 1
${escmd[$env]} GET '_cat/thread_pool?h=node_name,name,active,pool*,que*,rej*,larg*,co*,max*&s=name,node_name&v'
# https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-thread-pool.html
}
show_nodes_threadpools_summary () {
# list ES nodes thread pool (_cat/thread_pool)
local env="$1"
usage_chk1 "$env" || return 1
${escmd[$env]} GET '_cat/thread_pool?h=node_name,name,active,pool*,que*,rej*,larg*,comp*&s=name,node_name&v'
# https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-thread-pool.html
}
#5-----------------------------------------------
# shard mgmt funcs
##-----------------------------------------------
show_shards () {
# list all the index shards sorted by size (big->small)
local env="$1"
usage_chk1 "$env" || return 1
${escmd[$env]} GET '_cat/shards?v&human&pretty&s=store:desc,index,shard'
}
show_big_shards () {
# list top 20 shards for a given node's suffix (1a, 1b, etc.)
local env="$1"
local node="$2"
usage_chk2 "$env" "$node" || return 1
show_shards "$env" | grep -E "^index|${node}$" | head -40
}
show_small_shards () {
# list smallest 20 shards for a given node's suffix (1a, 1b, etc.)
local env="$1"
local node="$2"
usage_chk2 "$env" "$node" || return 1
show_shards "$env" | grep -E "^index|${node}$" | tail -40
}
show_hot_shards () {
# list today's "hot" shards for a given node's suffix (1a, 1b, etc.)
local env="$1"
local node="$2"
usage_chk2 "$env" "${node}$" || return 1
showShards=$(show_shards "$env")
showShardsHeader=$(echo "$showShards" | grep '^index')
shardDetailsFull=$(echo "$showShards" | grep -vE 'index')
uniqueIdxTypes=$(echo "$shardDetailsFull" | awk '{print $1}' | rev | cut -d"-" -f2- | cut -d"-" -f2- | rev | sort -u)
mostRecentIdxs=$(
echo "$uniqueIdxTypes" | while read line; do
echo "$shardDetailsFull" | awk '{print $1}' | grep "$line" | sort | tail -1
done
)
shardDetails=$(
(
echo "$showShardsHeader"
echo "$shardDetailsFull" \
| grep -f <(echo "$mostRecentIdxs") \
| grep -E "${node}$"
) | column -t
)
echo "$shardDetails"
}
show_shard_usage_by_node () {
# list all the index shards sorted by size (big->small)
local env="$1"
usage_chk1 "$env" || return 1
cat <<-EOM
A good rule-of-thumb is to ensure you keep the number of shards per node below 20 per GB heap it
has configured. A node with a 30GB heap should therefore have a maximum of 600 shards, but the
further below this limit you can keep it the better. This will generally help the cluster
stay in good health.
Source: https://www.elastic.co/blog/how-many-shards-should-i-have-in-my-elasticsearch-cluster
EOM
(
echo "node #shards"
echo "---- -------"
show_shards "$env" | awk '{print $(NF)}' | grep -v node | sort | uniq -c | awk '{print $2, $1}'
) | column -t
echo ""
}
relo_shard () {
# move an indices' shard from node suffix X to node suffix Y
local env="$1"
local shardName="$2"
local shardNum="$3"
local fromCode="$4"
local toCode="$5"
usage_chk5 "$env" "$shardName" "$shardNum" "$fromCode" "$toCode" || return 1
MOVE=$(cat <<-EOM
{
"commands" : [ {
"move" :
{
"index" : "${shardName}", "shard" : ${shardNum},
"from_node" : "${esnode[$env]}${fromCode}", "to_node" : "${esnode[$env]}${toCode}"
}
}
]
}
EOM
)
cmdOutput=$(${escmd[$env]} POST '_cluster/reroute' -d "$MOVE")
echo "${cmdOutput}" | grep -q '"acknowledged":true' && printf '{"acknowledged":true}\n' || echo "${cmdOutput}"
}
cancel_relo_shard () {
# cancel move of an index shard from node suffix X
local env="$1"
local shardName="$2"
local shardNum="$3"
local fromCode="$4"
usage_chk4 "$env" "$shardName" "$shardNum" "$fromCode" || return 1
CANCEL=$(cat <<-EOM
{
"commands" : [ {
"cancel" :
{
"index" : "${shardName}", "shard": ${shardNum},
"node": "${esnode[$env]}${fromCode}",
"allow_primary": true
}
}
]
}
EOM
)
cmdOutput=$(${escmd[$env]} POST '_cluster/reroute?explain&pretty' -d "$CANCEL")
echo "${cmdOutput}" | grep -q '"acknowledged":true' && printf '{"acknowledged":true}\n' || echo "${cmdOutput}"
}
cancel_relo_shards_all () {
# cancel all shard RELOCATIONS in recovery queue
local env="$1"
usage_chk1 "$env" || return 1
while read line; do
local shardName="$(echo $line | awk '{print $1}')"
local shardNum="$(echo $line | awk '{print $2}')"
local fromCode="$(echo $line | rev | cut -d'-' -f1 | rev | sed 's/0//')"
cancel_relo_shard "$env" "$shardName" "$shardNum" "$fromCode"
done < <(show_shards p | grep RELO | awk '{print $1,$2,$8}')
}
retry_unassigned_shards () {
# reallocate all unassignable shards (elapsed past 5 retries)
local env="$1"
usage_chk1 "$env" || return 1
cmdOutput=$(${escmd[$env]} POST '_cluster/reroute?retry_failed&explain&pretty')
echo "${cmdOutput}" | less
}
#6-----------------------------------------------
# shard size analysis funcs
##-----------------------------------------------
show_shard_distribution_by_node_last3days () {
# show distribution of day X's shards across nodes
local env="$1"
usage_chk1 "$env" || return 1
cat <<-EOM
429's (es_rejected_execution_exception)
---------------------------------------
Below shows the distribution of a given date's shards by node. If too many of a given days shards
end up on the same node, you may enounter 429s, for e.g.:
-------------------------------------------------------------------------------------------------------------
[2020-05-28T21:33:45,574][INFO ][logstash.outputs.elasticsearch][filebeat] retrying failed action with
response code: 429 ({"type"=>"es_rejected_execution_exception", "reason"=>"rejected execution of processing
of [2372784339][indices:data/write/bulk[s][p]]: request: BulkShardRequest [[filebeat-6.5.1-2020.05.28][13]]
containing [7] requests, target allocation id: basnD3RDQeurw3HJ-Ss0CQ, primary term: 1 on
EsThreadPoolExecutor[name = rdu-es-data-01j/write, queue capacity = 200,
org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor@7b8b41e3[Running, pool size = 40, active
threads = 40, queued tasks = 200, completed tasks = 1321763404]]"})
-------------------------------------------------------------------------------------------------------------
Sources:
- https://www.elastic.co/blog/performance-considerations-elasticsearch-indexing
- https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-threadpool.html
EOM
for day in {0..3}; do
local YYmmdd="$(calc_date "${day} days ago")"
printf "\n"
printf "[DATE: %s]\n\n" "$YYmmdd"
(
printf "node #shards\n"
printf -- "---- -------\n"
show_shards "$env" | grep "$YYmmdd" | awk '{print $NF}' | sort | uniq -c | awk '{print $2, $1}'
) | column -t
printf "\n==============================================\n"
done
printf "\n\n"
}
show_hot_idxs_shard_distribution_by_node () {
# show distribution of today's hot index shards across nodes
local env="$1"
usage_chk1 "$env" || return 1
shardDetailsFull=$(show_shards "$env" | grep 'STARTED' | grep -vE '^\.|ilm|index')
uniqueIdxTypes=$(echo "$shardDetailsFull" | awk '{print $1}' | rev | cut -d"-" -f2- | cut -d"-" -f2- | rev | sort -u)
mostRecentIdxs=$(
echo "$uniqueIdxTypes" | while read line; do
echo "$shardDetailsFull" | awk '{print $1}' | grep "$line" | sort | tail -1
done
)
shardDetails=$(
(
printf "node indexType #shards\n"
printf -- "---- --------- -------\n"
echo "$shardDetailsFull" \
| awk '{print $8, $1}' \
| sort -k1,2 \
| grep -f <(echo "$mostRecentIdxs") \
| uniq -c \
| awk '{print $2, $3, $1}'
) | column -t
)
nodes="$(echo "$shardDetails" | grep -vE '^$|node|--' | awk '{print $1}' | sort -u)"
colWidth="$(echo "$shardDetails" | grep '^node' | wc -c | awk '{print $1}')"
# adjust to remove NEWLINE
(( colWidth-- ))
dividingLine="$(printf -- '-%.0s' $(seq $colWidth))"
todayDate=$(calc_date '0 days')
printf "\n\n[DATE: %s]\n\n" "$todayDate"
colHeader="$(echo "$shardDetails" | grep -E '^node|^--')"
printf "\n\n%s\n" "$colHeader"
for node in $nodes; do
echo "$shardDetails" | grep -w "$node"
echo "$shardDetails" | awk -v node="${node}" '$1 == node {total += $3} END {printf("* * %0.0f\n"), total}'
done \
| column -t \
| $sedCmd "s/\(^\* .*\)/${dividingLine} \n\1\n/g" \
| sed 's/\*/ /g'
printf "\n\n"
}
###DEP #calc_hot_idxs_shard_sweet_spot \(\) {
###DEP # calculate optimal number of hot index shards per node
###DEP local env="$1"
###DEP usage_chk1 "$env" || return 1
###DEP
###DEP todayDate=$(calc_date '0 days')
###DEP todayDay=$(echo "$todayDate" | cut -d'.' -f2-3)
###DEP
###DEP numDailyHotShards=$(show_shards "$env" |grep "$todayDay" | grep -vE '^\.|f5|heart|syslog' | wc -l)
###DEP numNodes=$(list_nodes_storage "$env" | grep es-data | wc -l)
###DEP printf "\n\n"
###DEP printf "Optimal hot indexes' shards per node: %s\n\n\n" "$(ceiling_divide "$numDailyHotShards" "$numNodes")"
###DEP }
show_shards_biggerthan55gb () {
# show shards which are > 55GB (too big)
local env="$1"
usage_chk1 "$env" || return 1
shards=$(show_shards "$env")
cat <<-EOM
Shards > 55GB
$(printf "%s\n\n\n" "$(printf '=%.0s' {1..100})")
EOM
{
echo "$shards" | head -1
echo "$shards" | grep 'gb ' | sed 's/gb / /' \
| awk '$6 > 55 && sub("$", "gb", $6) || NR==1' \
| sort -k6,6gr
} | column -t
printf "%s\n\n\n" "$(printf '=%.0s' {1..100})"
}
show_idx_with_oversized_shards_summary () {
# show summary of indexes w/ shards > 55GB (too big)
local env="$1"
usage_chk1 "$env" || return 1
printf "\n\n"
printf "Daily Indicies w/ > 55GB shards"
printf "\n\n"
{
printf "days IdxType\n"
printf -- "---- -------\n"
show_shards_biggerthan55gb "$env" \
| grep -vE '===|^$|index|Shards' \
| awk '{print $1}' \
| sort -u \
| sed 's/-[0-9]\{4\}.*//' \
| uniq -c
} | column -t
printf "\n\n"
}
show_idx_with_oversized_shards_details () {
# show detailed view of indexes w/ shards > 55GB (too big)
local env="$1"
usage_chk1 "$env" || return 1
printf "\n\n"
printf "Daily Indicies w/ shards (primaries) > 55GB"
printf "\n\n"
{
printf "Idx Shard# ShardType ShardSize\n"
printf -- "--- ------- -------- ---------\n"
show_shards_biggerthan55gb "$env" \
| grep -vE 'Shards|===|index' \
| awk '$3 ~ /p/ {print $1, $2, $3, $6}' \
| sort -k1,1;
} | column -t
printf "\n\n"
}
#7-----------------------------------------------
# increase/decrease relo/recovery throttles
##-----------------------------------------------
show_rebalance_throttle () {
# show routing allocations for rebalancing & recoveries (current)
local env="$1"
usage_chk1 "$env" || return 1
showcfg_cluster "$env" | jq '.' | grep -E "allocation.(node|cluster|type)|recovery.max_bytes_per_sec"
}
show_node_concurrent_recoveries () {
# show cluster.routing.allocation.node_concurrent_recoveries
local env="$1"
usage_chk1 "$env" || return 1
printf "\n\n"
showcfg_cluster "$env" | grep 'concurrent.*recoveries'
printf "\n\n"