-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsplunknbox.sh
executable file
·6139 lines (5423 loc) · 282 KB
/
splunknbox.sh
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
#################################################################################
# __VERSION: 5.1-19 $
# __DATE: Thu May 31,2018 - 06:18:11PM -0600 $
# __AUTHOR: mhassan2 <[email protected]> $
#################################################################################
# Description:
# This script is intended to enable you to create number of Splunk infrastructure
# elements on the fly. A perfect tool to setup a quick Splunk lab for training
# or testing purposes. https://github.com/mhassan2/splunk-n-box
#
# List of capabilities:
# -Extensive Error and integrity checks
# -Load control (throttling) if exceeds total vCPU
# -Built-in dynamic host names and IP allocation
# -Create and configure large number of Splunk hosts very fast
# -Different logging levels (show docker commands executed)
# -Complete multi and single site cluster builds including CM,LM,MC and DEP servers
# -Manual and auto modes (standard configurations)
# -Modular design that can easily be converted to a higher-level language like python
# -Custom login-screen (helpful for lab & Search Parties scenarios)
# -Low resources requirements
# -Eliminate the need to learn docker (but you should)
# -OSX & Linux support
# -Works with windows10 WSL (Windows Subsystem for Linux) Ubuntu bash.
# -Automatic script upgrade (with version check).
# -AWS EC2 aware
#
# Licenses: Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
# Copyright [2017] [Mohamad Y. Hassan]
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
#
#
#Usage : splunknbox -v[2 3 4 5 6]
# v1-2 implied and cannot be changed
# -v3 [default] show sub-steps under each host build
# -v4 show remote CMD executed in docker container
# -v5 more verbosity (debug)
# -v6 even more verbosity (debug)
#################################################################################
#-------------Network stuff --------
ETH_OSX="lo0" #default interface to use with OSX laptop (el captain)
ETH_LINUX="ens3" #default interface to use with Linux server (Ubuntu 16.04
#-------------IP aliases --------
#LINUX is routed and hosts can be reached from anywhere in the network
#START_ALIAS_LINUX="192.168.1.100"; END_ALIAS_LINUX="192.168.1.254"
START_ALIAS_LINUX="192.168.1.100"; END_ALIAS_LINUX="192.168.1.250"
#OSX space will not be routed, and host reached from the laptop only
START_ALIAS_OSX="10.0.0.100"; END_ALIAS_OSX="10.0.0.250"
#START_ALIAS_OSX=10.2.237.1""; END_ALIAS_OSX="10.2.237.49"
DNSSERVER="192.168.1.19" #if running dnsmasq. Set to docker-host machine IP
#---------------------------------
#----------PATHS-------------------
#Full PATH is dynamic based on OS type, see detect_os()
#The following are set in detect_os()
#MOUNTPOINT=
#ETH=
#GREP=
#-----------------------------------
#----------Images--------------------
#My builds posted on docker hub -MyH
DEFAULT_SPLUNK_IMAGE="splunknbox/splunk_7.2.0"
#DEFAULT_SPLUNK_IMAGE="splunk/splunk" #official image
SPLUNK_DOCKER_HUB="registry.splunk.com" #internal to splunk.Requires login
#Available splunk demos registry.splunk.com
REPO_DEMO_IMAGES="workshop-boss-of-the-soc demo-azure demo-dbconnect demo-pci demo-itsi demo-es demo-vmware demo-citrix demo-cisco demo-stream demo-pan demo-aws demo-ms demo-unix demo-fraud demo-oi demo-healthcare workshop-splunking-endpoint workshop-ransomware-splunklive-2017 demo-connected-cars workshop-elastic-stack-lab"
REPO_DEMO_IMAGES=$(echo "$REPO_DEMO_IMAGES" | tr " " "\n"|sort -u|tr "\n" " ")
#3rd party images will be renamed to 3rd-* after each docker pull
REPO_3RDPARTY_IMAGES="mysql oraclelinux sebp/elk sequenceiq/hadoop-docker caioquirino/docker-cloudera-quickstart"
REPO_3RDPARTY_IMAGES=$(echo "$REPO_3RDPARTY_IMAGES" | tr " " "\n"|sort -u|tr "\n" " ")
MYSQL_PORT="3306"
DOWNLOAD_TIMEOUT="480" #how long before the progress_bar timeout (seconds)
#---------------------------------------
#----------Lunch & Learn stuff----------------
LL_APPS="splunk-datasets-add-on_10.tgz machine-learning-toolkit_210.tgz customer-search-party-app_10.tgz splunk-enterprise-65-overview_13.tgz splunk-6x-dashboard-examples_60.tgz splunk-common-information-model-cim_470.tgz eventgen.spl"
#python-for-scientific-computing-for-linux-64-bit_12.tgz" #too large for github 63M
LL_DATASETS="http_status.csv tutorialdata.zip"
#---------------------------------------
#-------- containers stuff --------------
MASTER_CONTAINER="MONITOR" #name of master container used to monitor ALL containers
BASEHOSTNAME="HOST" #default hostname to create
CM_BASE="CM"
MC_BASE="MC"
LM_BASE="LM"
DEP_BASE="DEP"
IDX_BASE="IDX"
SH_BASE="SH"
HF_BASE="HF"
SPLUNKNET="splunk-net" #default name for network (host-to-host comm)
#Splunk standard ports
SSHD_PORT="8022" #in case we need to enable sshd, not recommended
SPLUNKWEB_PORT="8000" #port splunkd is listing on
SPLUNKWEB_PORT_EXT="8000" #port mapped during docker run (customer facing)
MGMT_PORT="8089"
#KV_PORT="8191"
RECV_PORT="9997"
REPL_PORT="9887"
HEC_PORT="8088"
APP_SERVER_PORT="8065" #new to 6.5
APP_KEY_VALUE_PORT="8191" #new to 6.5
USERADMIN="admin"
USERPASS="hello1234"
#----------------------------------------------------
#----------Cluster stuff----------------
R_FACTOR="2" #default replication factor
S_FACTOR="2" #default search factor
DEF_SITE_REP_FACT="origin:2,total:3"
DEF_SITE_SEARCH_FACT="origin:1,total:2"
DEFAULT_SHC_LABEL="shcluster1"
DEFAULT_IDXC_LABEL="idxcluster1"
DEFAULT_SITE_LABEL="buttercup"
DEF_SINGLE_SITE="DC01" #used in single-site build
DEF_MULTI_SITES="LON HKG" #used in multi-site build
MYSECRET="mysecret" #defualt Pass4SymmKey
STD_IDXC_COUNT="3" #default IDXC count
STD_SHC_COUNT="3" #default SHC count
DEP_SHC_COUNT="1" #default DEP count
#------------------------------------------
#---------DIRECTORIES & Logs-----------------------------
DEFAULT_LOG_LEVEL=3
DEFAULT_TIMER=30
FILES_DIR="$PWD" #place anything needs to copy to container here
TMP_DIR="$PWD/tmp" #used as scrach space
LOGS_DIR="$PWD/logs" #store generated logs during run
CMDLOGBIN="$LOGS_DIR/splunknbox_bin.log" #capture all docker cmds (with color)
CMDLOGTXT="$LOGS_DIR/splunknbox.log" #capture all docker cmds (just ascii txt)
#LOGFILE="${0##*/}.log" #log file will be this_script_name.log
#SCREENLOGFILE="$LOGS_DIR/splunknbox_screens.log" #capture all screen shots during execution
HOSTSFILE="$PWD/docker-hosts.dnsmasq" #local host file. optional if dns caching is used
SPLUNK_LIC_DIR="$PWD/splunk_licenses" #place all your license file here
VOL_DIR="docker-volumes" #volumes mount point.Full path is dynamic based on OS type
SPLUNK_APPS_DIR="$PWD/splunk_apps"
SPLUNK_DATASETS_DIR="$PWD/tutorial_datasets"
CONT_LIST_FILE="$TMP_DIR/containers_list.tmp" #used in display_all_containers()
#-----------------------------------------
#--------Load control---------------------
MAXLOADTIME=10 #seconds increments for timer
MAXLOADAVG=4 #Not used
LOADFACTOR=3 #allow (3 x cores) of load on docker-host
LOADFACTOR_OSX=1 #allow (1 x cores) for the MAC (testing..)
#-----------------------------------------
#-------Misc------------------------------
GREP_OSX="/usr/local/bin/ggrep" #you MUST install Gnu grep on OSX
GREP_LINUX="/bin/grep" #default grep for Linux
PS4='$LINENO: ' #show line num when used bash -x ./script.sh
FLIPFLOP=0 #used to toggle color value in logline().Needs to be global
#Set the local splunkd path if you're running Splunk on this docker-host (ex laptop).
#Used in startup_checks() routine to detect local instance and kill it.
LOCAL_SPLUNKD="/opt/splunk/bin/splunk" #don't run local splunkd instance on docker-host
LOW_MEM_THRESHOLD=6.0 #threshold of recommended free system memory in GB
DOCKER_MIN_VER=1.13.1 #min recommended docker version
MACSPEAK_VOL="7" #default volume for MacOS speak feature
#--------------------------------------------------------------------
#--Progress status. tput (R,0) locations ---------
R_HEADER="0"
R_BANNER="1"
R_BUILD_SITE="3"
R_STEP1="4"
R_STEP2="5"
R_STEP3="6"
R_STEP4="7"
R_STEP5="8"
R_STEP6="9"
R_STEP7="10"
R_STEP8="11"
R_STEP9="12"
R_STEP10="13"
R_LINE="10"; R_ROLL="11" #defaults
#MAXLEN="55" #fill until for docker_status msgs (rolling scr size)
MAXLEN="56" #progress section length
C_PROGRESS="55" #progress section width
R_SCR_END_FACTOR="4" #max_rows - factor=rolling scr size
#---------------------------------------------------------
#--------COLORES ESCAPE CODES------------
#for i in `seq 1 100`; do printf "\033[48;5;${i}m${i} "; done
#https://misc.flogisoft.com/bash/tip_colors_and_formatting
NC='\033[0m' # No Color
Black="\033[0;30m"; White="\033[1;37m"
Red="\033[0;31m"; LightRed="\033[1;31m"
Green="\033[0;32m"; LightGreen="\033[1;32m"
BrownOrange="\033[0;33m"; Yellow="\033[1;33m"
Blue="\033[0;34m"; LightBlue="\033[1;34m"
Purple="\033[0;35m"; LightPurple="\033[1;35m"
Cyan="\033[0;36m"; LightCyan="\033[1;36m"
LightGray="\033[0;37m"; DarkGray="\033[1;30m"
WhiteOnLightBlue="\033[48;5;12m"
WhiteOnOrange="\033[48;5;166m"
WhiteOnGreen="\033[48;5;2m"
WhiteOnRed="\033[48;5;1m"
#used for docker status bar.
Gray1="241"
Gray2="100"
WhiteOnGray1="\033[97;48;5;${Gray1}m"; WhiteOnGray2="\033[97;48;5;${Gray2}m"
LightRedOnGray1="\033[91;48;5;${Gray1}m"; LightRedOnGray2="\033[91;48;5;${Gray2}m";
RedOnGray1="\033[31;48;5;${Gray1}m"; RedOnGray2="\033[31;48;5;${Gray2}m"
LightGreenOnGray1="\033[92;48;5;${Gray1}m";LightGreenOnGray2="\033[92;48;5;${Gray2}m"
LightYellowOnGray1="\033[93;48;5;${Gray1}m";LightYellowOnGray2="\033[93;48;5;${Gray2}m"
#used for phase/step titles displays
BoldWhiteOnRed="\033[1;1;5;41m"; BoldWhiteOnGreen="\033[1;1;5;42m"
BoldWhiteOnYellow="\033[1;1;5;43m"; BoldWhiteOnBlue="\033[1;1;5;44m"
BoldWhiteOnLightBlue="\033[1;1;5;104m"; BoldWhiteOnPink="\033[1;1;5;45m"
BoldWhiteOnTurquoise="\033[1;1;5;46m"; BoldYellowOnBlue="\033[1;33;44m"
BoldYellowOnPurple="\033[1;33;44m"
#-----------------------------------------
BoldWhiteOnBlue="\033[44;5;70;1m"
BoldGreenOnBlue="\033[44;5;92;1m"
BoldYellowOnBlue="\033[44;5;93;1m"
BoldRedOnBlue="\033[44;5;91;1m"
BoldWhiteOnDarkRed="\033[48;5;88;1m"
BoldWhiteOnDarkGreen="\033[48;5;28;1m"
BoldWhiteOnLimeGreen="\033[48;5;40;1m"
#R_PROGRESS_COLOR="${WhiteOnOrange}" #progress status
R_PROGRESS_COLOR="${BoldWhiteOnDarkGreen}" #progress status
R_BUILD_COLOR="${WhiteOnGray1}" #build site status
R_LINE_COLOR="$R_BUILD_COLOR" #R_BUILD line
#HEADER_COLOR="${WhiteOnGray1}"
HEADER_COLOR="${BoldWhiteOnBlue}"
FOOTER_COLOR1="${BoldWhiteOnBlue}" #sync with header colors
FOOTER_COLOR2="${BoldGreenOnBlue}" #docker counters colors
FOOTER_COLOR3="${BoldYellowOnBlue}" #when loadavg Yellow
FOOTER_COLOR4="${BoldRedOnBlue}" #when loadavg Red
#ACTION_COLOR="$BoldWhiteOnLightBlue" #containers CREATE, DEL,STOP...etc
ACTION_COLOR="$BoldWhiteOnGreen" #containers CREATE, DEL,STOP...etc
INACTIVE_TXT_COLOR="\033[1;30m" #unprocessed step
ACTIVE_TXT_COLOR="${White}" #processed step
#ACTIVE_TXT_COLOR="\033[1;35m"
#ACTIVE_TXT_COLOR="\033[1;91m"
DONE_STEP_COLOR="\033[1;32m"
DEFAULT_YES="\033[1;37mY\033[0m/n"
DEFAULT_NO="y/\033[1;37mN\033[0m"
#Emojis
ARROW_EMOJI="\xe2\x96\xb6"
ARROW_STOP_EMOJI="\xe2\x8f\xaf"
REPEAT_EMOJI="\xe2\x8f\xad"
CHECK_MARK_EMOJI="\xe2\x9c\x85"
OK_MARK_EMOJI="\xe2\x9c\x85"
OK_BUTTON_EMOJI="\xf0\x9f\x86\x97"
WARNING_EMOJI="\xe2\x9a\xa0\xef\xb8\x8f"
BULB_EMOJI="\xf0\x9f\x92\xa1"
DONT_ENTER_EMOJI="\xe2\x9b\x94"
DOLPHIN1_EMOJI="\xf0\x9f\x90\xb3"
DOLPHIN2_EMOJI="\xf0\x9f\x90\xac"
BATTERY_EMOJI="\xf0\x9f\x94\x8b"
COMPUTER_EMOJI="\xf0\x9f\x96\xa5"
OPTICALDISK_EMOJI="\xf0\x9f\x92\xbf"
YELLOWBOOK_EMOJI="\xf0\x9f\x93\x92"
YELLOW_LEFTHAND_EMOJI="\xf0\x9f\x91\x89"
TIMER_EMOJI="\xe2\x8f\xb0"
RED_BALL_EMOJI="\xf0\x9f\x94\xb4"
BELL_EMOJI="\xf0\x9f\x94\x94"
NO_BELL_EMOJI="\xf0\x9f\x94\x95"
#---------------------------------------
#**** Let the fun begin.I will see you 5000+ lines later! ***
#Log level is controlled with I/O redirection. Must be first thing executed in a bash script
# Redirect stdout ( > ) into a named pipe ( >() ) running "tee"
#exec >> >(tee -i $SCREENLOGFILE)
exec 2>&1
#---------------------------------------------------------------------------------------------------------------
_debug_function_inputs() {
func_name="$1"; arg_num="$2"; param_list="$3"; calls="$4"
#printf "#--------------------[$func_name] ( $param_list )------------------\n" >> $CMDLOGTXT
calls_count=`echo $calls|wc -w|sed 's/ //g'`
calls=`echo $calls| sed 's/ / <- /g'`
printf "\n${LightRed}CALLS:($calls_count) =>${Yellow}[$calls]${NC}\n" >&5
printf "\n${LightRed}STARTING: $func_name() :=> ${Purple}args:[$arg_num] ${Yellow}(${LightGreen}$param_list${Yellow})${NC}\n" >&5
return 0
} #end _debug_function_inputs()
#---------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------
function catimg() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
#this is cut & paste from imgcat package
# tmux requires unrecognized OSC sequences to be wrapped with DCS tmux;
# <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It
# only accepts ESC backslash for ST.
function print_osc() {
if [[ $TERM == screen* ]] ; then
printf "\033Ptmux;\033\033]"
else
printf "\033]"
fi
}
# More of the tmux workaround described above.
function print_st() {
if [[ $TERM == screen* ]] ; then
printf "\a\033\\"
else
printf "\a"
fi
}
# print_image filename inline base64contents print_filename
# filename: Filename to convey to client
# inline: 0 or 1
# base64contents: Base64-encoded contents
# print_filename: If non-empty, print the filename
# before outputting the image
function print_image() {
print_osc
printf '1337;File='
if [[ -n "$1" ]]; then
printf 'name='`printf "%s" "$1" | base64`";"
fi
VERSION=$(base64 --version 2>&1)
if [[ "$VERSION" =~ fourmilab ]]; then
BASE64ARG=-d
elif [[ "$VERSION" =~ GNU ]]; then
BASE64ARG=-di
else
BASE64ARG=-D
fi
printf "%s" "$3" | base64 $BASE64ARG | wc -c | awk '{printf "size=%d",$1}'
printf ";inline=$2"
printf ":"
printf "%s" "$3"
print_st
printf '\n'
if [[ -n "$4" ]]; then
echo $1
fi
}
function error() {
echo "ERROR: $*" 1>&2
}
function show_help() {
echo "Usage: imgcat [-p] filename ..." 1>& 2
echo " or: cat filename | imgcat" 1>& 2
}
## Main
if [ -t 0 ]; then
has_stdin=f
else
has_stdin=t
fi
# Show help if no arguments and no stdin.
if [ $has_stdin = f -a $# -eq 0 ]; then
show_help
exit
fi
# Look for command line flags.
while [ $# -gt 0 ]; do
case "$1" in
-h|--h|--help)
show_help
exit
;;
-p|--p|--print)
print_filename=1
;;
-*)
error "Unknown option flag: $1"
show_help
exit 1
;;
*)
if [ -r "$1" ] ; then
has_stdin=f
print_image "$1" 1 "$(base64 < "$1")" "$print_filename"
else
error "imgcat: $1: No such file or directory"
exit 2
fi
;;
esac
shift
done
# Read and print stdin
if [ $has_stdin = t ]; then
print_image "" 1 "$(cat | base64)" ""
fi
return
} #end catimg()
#---------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------
logline() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
#Log docker CMD string to logfile. Group event color by host
cmd="$1"; curr_host="$2"
#DATE=` date +'%b %e %R'`
DATE=`date +%Y-%m-%d:%H:%M:%S`
#echo "curr[$curr_host] prev[$prev_host]" >> $CMDLOGBIN
#change log entry color when the remote docker cmd executed in new host
#if [ "$FLIPFLOP" == 0 ] && [ "$curr_host" != "$prev_host" ]; then
# FLIPFLOP=1; COLOR="${LightBlue}"; echo > $CMDLOGBIN
#elif [ "$FLIPFLOP" == 1 ] && [ "$curr_host" != "$prev_host" ]; then
# FLIPFLOP=2; COLOR="${Yellow}"; echo > $CMDLOGBIN
#elif [ "$FLIPFLOP" == 2 ] && [ "$curr_host" != "$prev_host" ]; then
# FLIPFLOP=0; COLOR="${LightCyan}"; echo > $CMDLOGBIN
#fi
#printf "${White}[$DATE]" >>$CMDLOGBIN
printf "[$DATE:$curr_host] $CMD\n" >> $CMDLOGTXT
#echo "[$DATE] $CMDLOGBIN
#sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" -i $CMDLOGBIN
#prev_host=$curr_host
return 0
} #end logline()
#---------------------------------------------------------------------------------------------------------------
##### OS ######
#---------------------------------------------------------------------------------------------------------
show_docker_system_prune() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
clear
printf "${DONT_ENTER_EMOJI}${LightRed} WARNING!\n"
printf "This is a destructive command. May need to restart the script...${NC}\n"
docker system prune
echo
return
} #end show_docker_system_prune()
#---------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------
show_docker_system_df() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
echo
docker system df
echo
return
} #end show_docker_system_df()
#---------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------
restart_docker_mac() { ### NOT USED YET ####
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
osascript -e 'quit app "Docker.app"' #quit
open -a /Applications/Docker.app #start
return 0
} #end restart_docker_mac()
#---------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------
start_docker_mac() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
read -p " >> Should I attempt to start [may not work with all MacOS versions]? [Y/n]? " answer
if [ -z "$answer" ] || [ "$answer" == "Y" ] || [ "$answer" == "y" ]; then
open -a /Applications/Docker.app ; pausing "45"
is_running=`docker info|$GREP Images`
if [ -z "$is_running" ]; then
printf "${Red}Did not work! Please start docker from the UI...exiting...${NC}\n\n"
printf " ${Red}>>${NC} installation https://docs.docker.com/v1.10/mac/step_one/ ${NC}\n"
exit
fi
else
printf " ${Red}>>${NC} installation https://docs.docker.com/v1.10/mac/step_one/ ${NC}\n"
printf "Exiting...\n" ; exit
fi
return 0
} #end start_docker_mac()
#---------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------
start_docker_liunx() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
read -p " >> Should I attempt to start [may not work with all MacOS versions]? [Y/n]? " answer
if [ -z "$answer" ] || [ "$answer" == "Y" ] || [ "$answer" == "y" ]; then
start docker ; pausing 30
is_running=`docker info|$GREP Images`
if [ -z "$is_running" ]; then
printf "${Red}Did not work! Please start docker from the UI... Exiting!${NC}\n\n"
printf " ${Red}>>${NC} installation: https://docs.docker.com/engine/installation/ ${NC}\n"
exit
fi
else
printf " ${Red}>>${NC} installation: https://docs.docker.com/engine/installation/ ${NC}\n"
printf "Exiting...\n" ; exit
fi
return 0
} #end start_docker_linux()
#---------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------
check_root() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
# Check that we're in a BASH shell
if [[ $EUID -eq 0 ]]; then
echo "This script must NOT be run as root" 1>&2
exit 1
fi
} #end check_root()
#---------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------
check_shell() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
# Check that we're in a BASH shell
if test -z "$BASH" ; then
echo "This script ${0##*/} must be run in the BASH shell... Aborting."; echo;
exit 192
fi
return 0
} #end check_shell()
#---------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------
remove_ip_aliases() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
#Delete ip aliases on the interface (OS dependent)
clear
screen_header "${HEADER_COLOR}" "Splunk N' A Box v$GIT_VER: ${Yellow}MAIN MENU -> REMOVE iP ALIASES"
printf "\n"
#display_all_containers
echo
printf "${DONT_ENTER_EMOJI} ${LightRed}WARNING! \n"
printf "You are about to remove IP aliases. This will kill any container already binded to IP ${NC}\n"
echo
read -p "Are you sure you want to proceed? [y/N]? " answer
if [ "$answer" == "y" ] || [ "$answer" == "Y" ]; then
base_ip=`echo $START_ALIAS | cut -d"." -f1-3 `; # base_ip=$base_ip"."
start_octet4=`echo $START_ALIAS | cut -d"." -f4 `
docker_mc_start_octet4=`expr $start_octet4 - 1`
end_octet4=`echo $END_ALIAS | cut -d"." -f4 `
#---------
if [ "$os" == "Darwin" ]; then
read -p "Enter interface where IP aliases are binded to (default $ETH): " eth; if [ -z "$eth" ]; then eth="$ETH_OSX"; fi
sudo ifconfig $eth $base_ip.$docker_mc_start_octet4 255.255.255.0 -alias #special alias
for i in `seq $start_octet4 $end_octet4`; do
sudo ifconfig $eth $base_ip.$i 255.255.255.0 -alias
echo -ne "${NC}Removing: >> $eth:${Purple}$base_ip.${Yellow}$i\r"
done
echo
printf "\n${LightRed}You must restart the script to regain functionality!${NC}\n"
elif [ "$os" == "Linux" ]; then
read -p "Enter interface where IP aliases are binded to (default $ETH): " eth; if [ -z "$eth" ]; then eth="$ETH_LINUX"
sudo ifconfig $eth:$docker_mc_start_octet4 $base_ip.$docker_mc_start_octet4 down; #special aliases
for ((i=$start_octet4; i<=$end_octet4 ; i++)) do
echo -ne "${NC}Removing: >> $eth:${Purple}$base_ip.${Yellow}$i\r"
sudo ifconfig $eth:$i "$base_ip.$i" down;
done
echo
printf "\n${LightRed}You must restart the script to regain functionality!${NC}\n"
fi #elif
fi
#---------
else
printf "${NC}\n"
return 0
fi # answer
echo
return 0
} #end remove_ip_aliases()
#---------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------
setup_ip_aliases() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
#Check if ip aliases created, if not them bring up (tested on Ubuntu 16.04). Quick and dirty method. May need to change
base_ip=`echo $START_ALIAS | cut -d"." -f1-3 `; # base_ip=$base_ip"."
start_octet4=`echo $START_ALIAS | cut -d"." -f4 `
end_octet4=`echo $END_ALIAS | cut -d"." -f4 `
docker_mc_start_octet4=`expr $start_octet4 - 1`
printf "${Blue} ${ARROW_EMOJI}${ARROW_EMOJI}${NC} Checking if last IP alias is configured on any NIC [${Yellow}$END_ALIAS${NC}]..."
last_alias=`ifconfig | $GREP $END_ALIAS `
if [ -n "$last_alias" ]; then
printf "${Green}${OK_MARK_EMOJI} OK\n"
else
printf "${Red}NOT FOUND!${NC}\n"
fi
if [ "$os" == "Darwin" ] && [ -z "$last_alias" ]; then
#interfaces_list=`networksetup -listnetworkserviceorder|grep Hardware|sed 's/(Hardware Port: //g'|sed 's/)//g'`
#interfaces_list=`ifconfig | pcregrep -M -o '^[^\t:]+:([^\n]|\n\t)*status: active'`
#printf " ${Red}>>${NC}List of active interfaces (loopback is recommend for MacOS):\n"
#printf "Loopback, Device: lo\n$interfaces_list\n"
#for nic in "$interfaces_list"; do printf "xxx %-s4\n" "$nic"; done
read -p "Enter interface to bind aliases to (default $ETH): " eth; if [ -z "$eth" ]; then eth="$ETH_OSX"; fi
printf "Building IP aliases for OSX...[$base_ip.$start_octet4-$end_octet4]\n"
printf "Building special IP aliases for $MASTER_CONTAINER (used for docker monitoring only)...[$base_ip.$docker_mc_start_octet4]\n"
sudo ifconfig $eth $base_ip.$docker_mc_start_octet4 255.255.255.0 alias #special alias
#to remove aliases repeat with -alias switch
for i in `seq $start_octet4 $end_octet4`; do
sudo ifconfig $eth $base_ip.$i 255.255.255.0 alias
echo -ne "${NC}Adding: >> $eth:${Purple}$base_ip.${Yellow}$i\r"
done
elif [ "$os" == "Linux" ] && [ -z "$last_alias" ]; then
read -p "Enter interface to bind aliases to (default $ETH): " eth; if [ -z "$eth" ]; then eth="$ETH_LINUX"; fi
printf "Building IP aliases for LINUX...[$base_ip.$start_octet4-$end_octet4]\n"
printf "Building special IP aliases for $MASTER_CONTAINER (used for docker monitoring only)...[$base_ip.$docker_mc_start_octet4]\n"
sudo ifconfig $eth:$docker_mc_start_octet4 "$base_ip.$docker_mc_start_octet4" up #special alias
for ((i=$start_octet4; i<=$end_octet4 ; i++)) do
echo -ne "${NC}Adding: >> $eth:${Purple}$base_ip.${Yellow}$i\r"
sudo ifconfig $eth:$i "$base_ip.$i" up;
done
fi
#printf "${NC}\n"
read -p $'\033[1;32mHit <ENTER> to continue...\e[0m'
return 0
} #end setup_ip_aliases()
#---------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------
check_load() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
#We need to throttle back host creation if running on low powered server. Set to 4 x numb of cores
#stress testing tool:
#https://www.tecmint.com/linux-cpu-load-stress-test-with-stress-ng-tool/
#Example: stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 10s
if [ "$os" == "Darwin" ]; then
cores=`sysctl -n hw.ncpu`
elif [ "$os" == "Linux" ]; then
cores=`$GREP -c ^processor /proc/cpuinfo`
fi
#int=${float%.*}
#if [ $(echo " $test > $k" | bc) -eq 1 ] float comparison
t=$MAXLOADTIME;
while true
do
if [ "$os" == "Darwin" ]; then
#sudo memory_pressure -l warn| head -n 28
loadavg=`sysctl -n vm.loadavg | awk '{print $2}'`
LOADFACTOR=$LOADFACTOR_OSX
os_free_mem=`top -l 1 | head -n 10 | $GREP PhysMem | awk '{print $2}' | sed 's/G//g' `
else
loadavg=`cat /proc/loadavg |awk '{print $1}'|sed 's/,//g'`
os_free_mem=`free -g|$GREP -i mem|awk '{print $2}' `
fi
load=${loadavg%.*}
#load=10 #debug
MAXLOADAVG=`echo $cores \* $LOADFACTOR | bc -l `
c=`echo " $load > $MAXLOADAVG" | bc `;
printf "${LightRed}DEBUG:=> ${Yellow}In $FUNCNAME(): ${Purple} OS:[$os] MAX ALLOWED LOAD:[$MAXLOADAVG] current load:[$loadavg] cores[$cores]${NC}\n" >&5
if [ "$c" == "1" ]; then
echo
for c in $(seq 1 $t); do
tput cup $(($LINES-2)) $(( ( $COLUMNS - 80 ) / 2 ))
echo -ne "${LightRed}Pausing execution due to high load avg [$loadavg]. Max allowed[$MAXLOADAVG] Wait: ${Yellow}$t${NC} seconds... ${Yellow}$c${NC}\033[0K\r"
sleep 1
done
t=`expr $t + $t`
else
break
fi
done
return 0
} #end check_load()
#---------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------
check_for_ubuntu_pkgs() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
#----------
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking [bc] package:${NC} "
condition=$(which bc 2>/dev/null | $GREP -v "not found" | wc -l)
if [ $condition -eq 0 ]; then
printf "${BrownOrange}Installing [bc]${NC}:"
progress_bar_pkg_download "sudo apt-get install bc -y"
else
printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
fi
#----------
#----------
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking [wget] package:${NC} "
condition=$(which wget 2>/dev/null | $GREP -v "not found" | wc -l)
if [ $condition -eq 0 ]; then
printf "${BrownOrange}Installing [wget]${NC}:"
progress_bar_pkg_download "sudo apt-get install wget -y"
brew link --overwrite wget
#if brew link failed due to premission issue; run sudo chown -R `whoami` /usr/local
else
printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
fi
#----------
#----------
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking optional [imgcat] package:${NC} "
condition=$(which imgcat 2>/dev/null | $GREP -v "not found" | wc -l)
if [ $condition -eq 0 ]; then
printf "${BrownOrange}Installing [imgcat]${NC}:"
#progress_bar_pkg_download "sudo apt-get install imgcat -y"
progress_bar_pkg_download "sudo curl -o /usr/local/bin/imgcat -O https://raw.githubusercontent.com/gnachman/iTerm2/master/tests/imgcat"
sudo chmod +x /usr/local/bin/imgcat
else
printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
fi
#----------
#----------
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking optional [timeout] package:${NC} "
condition=$(which timeout 2>/dev/null | $GREP -v "not found" | wc -l)
if [ $condition -eq 0 ]; then
printf "${BrownOrange}Installing [timeout]${NC}:"
progress_bar_pkg_download "sudo apt-get install timeout -y"
else
printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
alias timeout="gtimeout"
fi
#----------
#----------
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking optional [graphviz] package:${NC} "
condition=$(which dot 2>/dev/null | $GREP -v "not found" | wc -l)
if [ $condition -eq 0 ]; then
printf "${BrownOrange}Installing [graphviz]${NC}:"
progress_bar_pkg_download "sudo apt-get install graphviz -y"
else
printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
fi
#----------
#----------
#printf "${Yellow} ${ARROW_EMOJI}${NC} Checking optional [tmux] package:${NC} "
#condition=$(which tmux 2>/dev/null | $GREP -v "not found" | wc -l)
#if [ $condition -eq 0 ]; then
# printf "${BrownOrange}Installing [tmux]${NC}:"
# progress_bar_pkg_download "sudo apt-get install tmux -y"
#else
# printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
#fi
#----------
return 0
} #end check_for_ubuntu_pkgs()
#---------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------
check_for_MACOS_pkgs() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
##make sure you use built-in grep in this function ####
#----------
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking Xcode commandline tools:${NC} "
cmd=$(xcode-select -p)
if [ -n $cmd ]; then
printf "${Green}${CHECK_MARK_EMOJI} Installed\n${NC}"
else
printf "${Yellow}Running [xcode-select --install]${NC}\n"
cmd=$(xcode-select --install)
fi
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking brew package management:${NC} "
condition=$(which brew 2>/dev/null | grep -v "not found" | wc -l)
if [ $condition -eq 0 ]; then
printf "${BrownOrange}Installing [brew]${NC}:"
#get brew ruby install script
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install > install_brew.rb
sed -ie 's/c = getc/c = 13/g' install_brew.rb #remove prompt in install.rb script
progress_bar_pkg_download "/usr/bin/ruby install_brew.rb"
else
printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
fi
#----------
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking bc package:${NC} "
condition=$(which bc 2>/dev/null | grep -v "not found" | wc -l)
if [ $condition -eq 0 ]; then
printf "${BrownOrange}Installing [bc]${NC}:"
progress_bar_pkg_download "brew install bc"
else
printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
fi
#----------
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking pcre package:${NC} "
cmd=$(brew ls pcre --versions)
if [ -n "$cmd" ]; then
printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
else
printf "${BrownOrange}Installing [pcre]${NC}:"
progress_bar_pkg_download "brew install pcre"
# brew install pcre
fi
#--------------
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking wget package:${NC} "
cmd=$(brew ls wget --versions)
if [ -n "$cmd" ]; then
printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
else
printf "${BrownOrange}Installing [wget]${NC}:"
progress_bar_pkg_download "brew install wget"
fi
#----------
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking GNU grep package:${NC} "
cmd=$(brew ls grep --versions|cut -d" " -f2) #use native OS grep on this one!
if [ -n "$cmd" ]; then
printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
else
printf "${BrownOrange}Installing [ggrep]${NC}:"
brew tap homebrew/dupes > /dev/null 2>&1
#progress_bar_pkg_download "brew install homebrew/dupes/grep"
progress_bar_pkg_download "brew install grep"
# printf "${BrownOrange}Running [sudo ln -s /usr/local/Cellar/grep/$cmd/bin/ggrep /usr/local/bin/ggrep]${NC}\n"
# sudo ln -s /usr/local/Cellar/grep/$cmd/bin/ggrep /usr/local/bin/ggrep
fi
#printf "${Yellow}Running [brew list]${NC}\n"
# brew list --versions
#----------
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking optional [imagcat] package:${NC} "
cmd=$(brew ls imgcat --versions)
if [ -n "$cmd" ]; then
printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
else
printf "${BrownOrange}Installing [imgcat]${NC}:"
progress_bar_pkg_download "brew tap eddieantonio/eddieantonio"
progress_bar_pkg_download "brew install imgcat"
fi
#----------
#----------
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking optional [gtimeout] package:${NC} "
cmd=$(brew ls coreutils --versions)
if [ -n "$cmd" ]; then
printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
else
printf "${BrownOrange}Installing [coreutils]${NC}:"
progress_bar_pkg_download "brew install coreutils"
alias timeout="gtimeout"
fi
#----------
#----------
printf "${Yellow} ${ARROW_EMOJI}${NC} Checking optional [graphviz] package:${NC} "
cmd=$(brew ls graphviz --versions)
if [ -n "$cmd" ]; then
printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
else
printf "${BrownOrange}Installing [graphviz]${NC}:"
progress_bar_pkg_download "brew install graphviz"
fi
#----------
#----------
#printf "${Yellow} ${ARROW_EMOJI}${NC} Checking optional [tmux] package:${NC} "
#cmd=$(brew ls tmux --versions)
#if [ -n "$cmd" ]; then
# printf "${Green}${CHECK_MARK_EMOJI} Installed${NC}\n"
#else
# printf "${BrownOrange}Installing [tmux]${NC}:"
# progress_bar_pkg_download "brew install tmux"
#fi
#----------
echo
return 0
} #end check_for_MACOS_pkgs()
#---------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------
startup_checks() {
_debug_function_inputs "${FUNCNAME}" "$#" "[$1][$2][$3][$4][$5]" "${FUNCNAME[*]}"
printf "${WhiteOnGray1}Splunk N' A Box (v${Yellow}$GIT_VER${WhiteOnGray1}): Starting validation checks...${NC}\n"
printf "${LightBlue}==> ${NC}$os_banner${NC}\n"
#-------------------sanity checks -------------------
check_root #should not as root
check_shell #must have bash
#Make sure working directories exist
mkdir -p $LOGS_DIR
mkdir -p $TMP_DIR
mkdir -p $SPLUNK_LIC_DIR
mkdir -p $SPLUNK_APPS_DIR
mkdir -p $SPLUNK_DATASETS_DIR
#----------------------------------------------------
#----------Gnu grep installed? MacOS only-------------
if [ "$os" == "Darwin" ]; then
printf "${Blue} ${ARROW_EMOJI}${ARROW_EMOJI}${NC} Checking for required MacOS packages...\n"
check_for_MACOS_pkgs
fi
if [ "$os" == "Linux" ]; then
printf "${Blue} ${ARROW_EMOJI}${ARROW_EMOJI}${NC} Checking for required Ubuntu Linux packages...\n"
check_for_ubuntu_pkgs
fi
#----------Gnu grep installed? MacOS only-------------
#-----------check for another copy of script running?---------
printf "${Blue} ${ARROW_EMOJI}${ARROW_EMOJI}${NC} Checking if we have instances of this script running...${NC}"
this_script_name="${0##*/}"
pid_list=`ps -efa | $GREP "$this_script_name" | $GREP "/bin/bash" |$GREP -v $$ |awk '{printf $2" " }'`
#echo "running: ps -efa | grep splunknbox.sh | grep \"/bin/bash\" |grep -v \$\$ |awk '{printf \$2\" \" }"
if [ -n "$pid_list" ]; then
printf "\n"
printf " ${Red}>>${NC} Detected running instance(s) of $this_script_name [$pid_list]${NC}\n"
read -p " >> This script doesn't support multiple instances. Kill them? [Y/n]? " answer
if [ -z "$answer" ] || [ "$answer" == "Y" ] || [ "$answer" == "y" ]; then
sudo kill -9 $pid_list
#kill any stray docker pull requests
kill $(ps -ax|$GREP "docker pull"|$GREP -v "grep"|awk '{print $1}') >/dev/null 2>&1
fi
printf "\n"
else
printf "${Green}${OK_MARK_EMOJI} OK${NC}\n"
fi
#-----------other scripts running?---------
#-----------docker daemon running check----
printf "${Blue} ${ARROW_EMOJI}${ARROW_EMOJI}${NC} Checking if docker daemon is running "
is_running=`docker info|$GREP Images 2>/dev/null `
if [ -z "$is_running" ] && [ "$os" == "Darwin" ]; then
printf "${Red}NOT RUNNING!${NC}\n"
start_docker_mac
elif [ -z "$is_running" ] && [ "$os" == "Linux" ]; then
printf "${Red}NOT RUNNING!${NC}\n"
start_docker_linux
fi
#expected to arrive at this point only if docker is running, therefore we can collect dockerinfo
if [ -n "$is_running" ]; then
dockerinfo_ver=`docker info| $GREP 'Server Version'| awk '{printf $3}'| tr -d '\n' `
dockerinfo_cpu=`docker info| $GREP 'CPU' | awk '{printf $2}'| tr -d '\n' `
dockerinfo_mem1=`docker info| $GREP 'Total Memory'| awk '{printf $3}'|sed 's/GiB//g'| tr -d '\n' `
dockerinfo_mem=`echo "$dockerinfo_mem1 / 1" | bc `
#echo "DOCKER: ver:[$dockerinfo_ver] cpu:[$dockerinfo_cpu] totmem:[$dockerinfo_mem] ";exit
printf "${Yellow}[ver:$dockerinfo_ver]${NC}..."
#must use actuall escape code for emoji in awk
awk -v n1=$dockerinfo_ver -v n2=$DOCKER_MIN_VER \
'BEGIN {if (n1<n2) printf ("\033[0;33mWarning! Recommending %s+\n", n2); else printf ("\033[0;32m\xe2\x9c\x85 OK!\n");}'
#printf "${Green}${OK_MARK_EMOJI} OK${NC}\n"
fi
#-----------docker daemon running check----
#-----------Gathering OS memory/cpu info---
if [ "$os" == "Linux" ]; then
cores=`$GREP -c ^processor /proc/cpuinfo`
os_used_mem=`free -g|$GREP -i mem|awk '{print $3}' `
os_free_mem=`free -g|$GREP -i mem|awk '{print $4}' `
os_total_mem=`free -g|$GREP -i mem|awk '{print $2}' `
os_free_mem_perct=`echo "($os_free_mem * 100) / $os_total_mem"| bc`
elif [ "$os" == "Darwin" ]; then
cores=`sysctl -n hw.ncpu`
os_used_mem=`top -l 1 -s 0|$GREP PhysMem|tr -d '[[:punct:]]'|awk '{print $2}' `
if ( compare "$os_used_mem" "M" ); then
os_used_mem=`echo $os_used_mem | tr -d '[[:alpha:]]'` #strip M
os_used_mem=`printf "%0.1f\n" $(bc -q <<< scale=6\;$os_used_mem/1024)` #convert float from MB to GB
else
os_used_mem=`echo $os_used_mem | tr -d '[[:alpha:]]'` #strip G
fi
os_wired_mem=`top -l 1 -s 0|$GREP PhysMem|tr -d '[[:punct:]]'|awk '{print $4}' `
if ( compare "$os_wired_mem" "M" ); then
os_wired_mem=`echo $os_wired_mem | tr -d '[[:alpha:]]'` #strip M
os_wired_mem=`printf "%0.1f\n" $(bc -q <<< scale=6\;$os_wired_mem/1024)` #convert float from MB to GB
else
os_wired_mem=`echo $os_wired_mem | tr -d '[[:alpha:]]'` #strip G
fi
os_unused_mem=`top -l 1 -s 0|$GREP PhysMem|tr -d '[[:punct:]]'|awk '{print $6}' `
if ( compare "$os_unused_mem" "M" ); then
os_unused_mem=`echo $os_unused_mem | tr -d '[[:alpha:]]'` #strip M
os_unused_mem=`printf "%0.1f\n" $(bc -q <<< scale=6\;$os_unused_mem/1024)` #convert float from MB to GB
else
os_unused_mem=`echo $os_unused_mem | tr -d '[[:alpha:]]'` #strip G
fi
#echo "MEM: used:[$os_used_mem] wired:[$os_wired_mem] unused:[$os_unused_mem]"
os_free_mem=$os_unused_mem
#os_total_mem=`echo $os_used_mem + $os_wired_mem + $os_unused_mem | bc`
os_total_mem=`hostinfo|$GREP "memory available"| awk '{print $4}'`