forked from spiritLHLS/ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecs.sh
4566 lines (4399 loc) · 183 KB
/
ecs.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
#!/usr/bin/env bash
# by spiritlhl
# from https://github.com/spiritLHLS/ecs
cd /root >/dev/null 2>&1
myvar=$(pwd)
ver="2023.10.09"
changeLog="VPS融合怪测试(集百家之长)"
# =============== 默认输入设置 ===============
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
PLAIN="\033[0m"
_red() { echo -e "\033[31m\033[01m$@\033[0m"; }
_green() { echo -e "\033[32m\033[01m$@\033[0m"; }
_yellow() { echo -e "\033[33m\033[01m$@\033[0m"; }
_blue() { echo -e "\033[36m\033[01m$@\033[0m"; }
reading() { read -rp "$(_green "$1")" "$2"; }
utf8_locale=$(locale -a 2>/dev/null | grep -i -m 1 -E "UTF-8|utf8")
if [[ -z "$utf8_locale" ]]; then
_yellow "No UTF-8 locale found"
else
export LC_ALL="$utf8_locale"
export LANG="$utf8_locale"
export LANGUAGE="$utf8_locale"
_green "Locale set to $utf8_locale"
fi
menu_mode=true
swhc_mode=true
if [ $# -eq 3 ]; then
main_menu_option="$1"
sub_menu_option="$2"
sub_of_sub_menu_option="$3"
# 使用正则表达式检查参数格式
if [[ $main_menu_option =~ ^[0-9]+(\.[0-9]{1,4})?$ ||
$sub_menu_option =~ ^[0-9]+(\.[0-9]{1,4})?$ ||
$sub_of_sub_menu_option =~ ^[0-9]+(\.[0-9]{1,4})?$ ]]; then
swhc_mode=false
else
echo "参数格式不符合要求,必须是纯数字或数字和小数点的组合,小数点只能有4个或没有。"
exit 1
fi
if [[ $main_menu_option == *.* ]]; then
target_ipv4="$main_menu_option"
fi
if [[ $sub_menu_option == *.* ]]; then
target_ipv4="$sub_menu_option"
fi
if [[ $sub_of_sub_menu_option == *.* ]]; then
target_ipv4="$sub_of_sub_menu_option"
fi
if [ -n "$target_ipv4" ]; then
test_area_local=("你本地的IPV4地址")
test_ip_local=("$target_ipv4")
fi
menu_mode=false
fi
break_status=true
# =============== 自定义基础参数 ==============
shorturl=""
TEMP_DIR='/tmp/ecs'
temp_file_apt_fix="${TEMP_DIR}/apt_fix.txt"
WorkDir="/tmp/.LemonBench"
test_area_g=("广州电信" "广州联通" "广州移动")
test_ip_g=("58.60.188.222" "210.21.196.6" "120.196.165.24")
test_area_s=("上海电信" "上海联通" "上海移动")
test_ip_s=("202.96.209.133" "210.22.97.1" "211.136.112.200")
test_area_b=("北京电信" "北京联通" "北京移动")
test_ip_b=("219.141.136.12" "202.106.50.1" "221.179.155.161")
test_area_c=("成都电信" "成都联通" "成都移动")
test_ip_c=("61.139.2.69" "119.6.6.6" "211.137.96.205")
test_area_6=("广东电信" "广东联通" "广东移动")
test_ip_6=("2401:1d40:3100::1" "2408:8001:3000::1" "2409:8054:306c::1")
BrowserUA="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36"
# =============== 基础信息设置 ===============
REGEX=("debian|astra" "ubuntu" "centos|red hat|kernel|oracle linux|alma|rocky" "'amazon linux'" "fedora" "arch" "freebsd")
RELEASE=("Debian" "Ubuntu" "CentOS" "CentOS" "Fedora" "Arch" "FreeBSD")
PACKAGE_UPDATE=("! apt-get update && apt-get --fix-broken install -y && apt-get update" "apt-get update" "yum -y update" "yum -y update" "yum -y update" "pacman -Sy" "pkg update")
PACKAGE_INSTALL=("apt-get -y install" "apt-get -y install" "yum -y install" "yum -y install" "yum -y install" "pacman -Sy --noconfirm --needed" "pkg install -y")
PACKAGE_REMOVE=("apt-get -y remove" "apt-get -y remove" "yum -y remove" "yum -y remove" "yum -y remove" "pacman -Rsc --noconfirm" "pkg delete")
PACKAGE_UNINSTALL=("apt-get -y autoremove" "apt-get -y autoremove" "yum -y autoremove" "yum -y autoremove" "yum -y autoremove" "" "pkg autoremove")
CMD=("$(grep -i pretty_name /etc/os-release 2>/dev/null | cut -d \" -f2)" "$(hostnamectl 2>/dev/null | grep -i system | cut -d : -f2)" "$(lsb_release -sd 2>/dev/null)" "$(grep -i description /etc/lsb-release 2>/dev/null | cut -d \" -f2)" "$(grep . /etc/redhat-release 2>/dev/null)" "$(grep . /etc/issue 2>/dev/null | cut -d \\ -f1 | sed '/^[ ]*$/d')" "$(grep -i pretty_name /etc/os-release 2>/dev/null | cut -d \" -f2)" "$(uname -s)")
SYS="${CMD[0]}"
[[ -n $SYS ]] || exit 1
for ((int = 0; int < ${#REGEX[@]}; int++)); do
if [[ $(echo "$SYS" | tr '[:upper:]' '[:lower:]') =~ ${REGEX[int]} ]]; then
SYSTEM="${RELEASE[int]}"
[[ -n $SYSTEM ]] && break
fi
done
# =================== 其他脚本相关设置 ===================
export DEBIAN_FRONTEND=noninteractive
rm -rf test_result.txt >/dev/null 2>&1
if [ ! -d "/tmp" ]; then
mkdir /tmp
fi
# =============== 脚本退出执行相关函数 部分 ===============
trap _exit INT QUIT TERM
_exit() {
# 终止信号捕获 - ctrl+c
echo -e "\n${Msg_Error}Exiting ...\n"
_red "检测到退出操作,脚本终止!\n"
global_exit_action
rm_script
exit 1
}
global_startup_init_action() {
# 清理残留, 为新一次的运行做好准备
echo -e "${Msg_Info}Initializing Running Enviorment, Please wait ..."
rm -rf "$WorkDir"
rm -rf /.tmp_LBench/
mkdir "$WorkDir"/
echo -e "${Msg_Info}Checking Dependency ..."
Check_SysBench
BenchFunc_Systeminfo_GetSysteminfo
echo -e "${Msg_Info}Starting Test ..."
}
global_exit_action() {
reset_default_sysctl >/dev/null 2>&1
build_text
if [ -n "$shorturl" ]; then
_green " 短链:"
_blue " $shorturl"
fi
rm -rf ${TEMP_DIR}
rm -rf ${WorkDir}/
rm -rf /.tmp_LBench/
rm -rf *00_00
}
_exists() {
# 查询对应变量或组件是否存在
local cmd="$1"
if eval type type >/dev/null 2>&1; then
eval type "$cmd" >/dev/null 2>&1
elif command >/dev/null 2>&1; then
command -v "$cmd" >/dev/null 2>&1
else
which "$cmd" >/dev/null 2>&1
fi
local rt=$?
return ${rt}
}
reset_default_sysctl() {
# 还原系统原有的设置
if [ -f /etc/security/limits.conf ]; then
cp /etc/security/limits.conf.backup /etc/security/limits.conf
rm /etc/security/limits.conf.backup
fi
if which systemctl >/dev/null 2>&1; then
if [ -f "$sysctl_conf" ]; then
cp "$sysctl_conf_backup" "$sysctl_conf"
check_and_cat_file "$sysctl_default" >>"$sysctl_conf"
$sysctl_path -p 2>/dev/null
cp "$sysctl_conf_backup" "$sysctl_conf"
rm "$sysctl_conf_backup"
rm "$sysctl_default"
fi
$sysctl_path -p 2>/dev/null
fi
}
next() {
echo -en "\r"
[ "${Var_OSRelease}" = "freebsd" ] && printf "%-72s\n" "-" | tr ' ' '-' && return
printf "%-72s\n" "-" | sed 's/\s/-/g'
}
# =============== 组件预安装及文件预下载 部分 ===============
checkver() {
check_cdn_file
running_version=$(sed -n '8s/ver="\(.*\)"/\1/p' "$0")
curl -L "${cdn_success_url}https://raw.githubusercontent.com/spiritLHLS/ecs/main/ecs.sh" -o ecs1.sh && chmod 777 ecs1.sh
downloaded_version=$(sed -n '8s/ver="\(.*\)"/\1/p' ecs1.sh)
if [ "$running_version" != "$downloaded_version" ]; then
_yellow "更新脚本从 $ver 到 $downloaded_version"
mv ecs1.sh "$0"
./ecs.sh
else
_green "本脚本已是最新脚本无需更新"
rm -rf ecs1.sh*
fi
}
check_root() {
[[ $EUID -ne 0 ]] && echo -e "${RED}请使用 root 用户运行本脚本!${PLAIN}" && exit 1
}
check_update() {
_yellow "Updating package management sources"
if command -v apt-get >/dev/null 2>&1; then
apt_update_output=$(apt-get update 2>&1)
echo "$apt_update_output" >"$temp_file_apt_fix"
if grep -q 'NO_PUBKEY' "$temp_file_apt_fix"; then
public_keys=$(grep -oE 'NO_PUBKEY [0-9A-F]+' "$temp_file_apt_fix" | awk '{ print $2 }')
joined_keys=$(echo "$public_keys" | paste -sd " ")
_yellow "No Public Keys: ${joined_keys}"
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ${joined_keys}
apt-get update
if [ $? -eq 0 ]; then
_green "Fixed"
fi
fi
rm "$temp_file_apt_fix"
else
${PACKAGE_UPDATE[int]}
fi
}
check_sudo() {
_yellow "checking sudo"
if ! command -v sudo >/dev/null 2>&1; then
_yellow "Installing sudo"
${PACKAGE_INSTALL[int]} sudo >/dev/null 2>&1
fi
}
check_curl() {
if ! which curl >/dev/null; then
_yellow "Installing curl"
${PACKAGE_INSTALL[int]} curl
fi
if [ $? -ne 0 ]; then
apt-get -f install >/dev/null 2>&1
${PACKAGE_INSTALL[int]} curl
fi
}
check_wget() {
if ! which wget >/dev/null; then
_yellow "Installing wget"
${PACKAGE_INSTALL[int]} wget
fi
}
check_free() {
[ "${Var_OSRelease}" = "freebsd" ] && return
if ! command -v free >/dev/null 2>&1; then
_yellow "Installing procps"
${PACKAGE_INSTALL[int]} procps
fi
}
check_lsb_release() {
[ "${Var_OSRelease}" = "freebsd" ] && return
if ! command -v lsb_release >/dev/null 2>&1; then
_yellow "Installing lsb-release"
${PACKAGE_INSTALL[int]} lsb-release
fi
}
check_lscpu() {
if ! command -v lscpu >/dev/null 2>&1; then
_yellow "Installing lscpu"
${PACKAGE_INSTALL[int]} lscpu
fi
}
check_unzip() {
if ! command -v unzip >/dev/null 2>&1; then
_yellow "Installing unzip"
${PACKAGE_INSTALL[int]} unzip
fi
}
check_ping() {
_yellow "checking ping"
if ! which ping >/dev/null; then
_yellow "Installing ping"
${PACKAGE_INSTALL[int]} iputils-ping >/dev/null 2>&1
${PACKAGE_INSTALL[int]} ping >/dev/null 2>&1
fi
}
check_nc() {
_yellow "checking nc"
if ! command -v nc >/dev/null; then
_yellow "Installing nc"
if command -v apt >/dev/null; then
${PACKAGE_INSTALL[int]} netcat >/dev/null 2>&1
else
${PACKAGE_INSTALL[int]} nc >/dev/null 2>&1
fi
fi
}
check_stun() {
_yellow "checking stun"
if ! command -v stun >/dev/null 2>&1; then
_yellow "Installing stun"
${PACKAGE_INSTALL[int]} stun-client >/dev/null 2>&1
fi
}
check_tar() {
_yellow "checking tar"
if ! command -v tar &>/dev/null; then
_yellow "Installing tar"
${PACKAGE_INSTALL[int]} tar
fi
if [ $? -ne 0 ]; then
apt-get -f install >/dev/null 2>&1
${PACKAGE_INSTALL[int]} tar >/dev/null 2>&1
fi
}
check_lsof() {
_yellow "checking lsof"
if ! command -v lsof &>/dev/null; then
_yellow "Installing lsof"
${PACKAGE_INSTALL[int]} lsof
fi
if [ $? -ne 0 ]; then
apt-get -f install >/dev/null 2>&1
${PACKAGE_INSTALL[int]} lsof >/dev/null 2>&1
fi
}
check_haveged() {
[ "${Var_OSRelease}" = "freebsd" ] && return
_yellow "checking haveged"
if ! command -v haveged >/dev/null 2>&1; then
${PACKAGE_INSTALL[int]} haveged >/dev/null 2>&1
fi
if which systemctl >/dev/null 2>&1; then
systemctl disable --now haveged
systemctl enable --now haveged
else
service haveged stop
service haveged start
fi
}
check_dnsutils() {
_yellow "Installing dnsutils"
if [ "${Var_OSRelease}" == "centos" ]; then
yum -y install dnsutils >/dev/null 2>&1
yum -y install bind-utils >/dev/null 2>&1
elif [ "${Var_OSRelease}" == "arch" ]; then
pacman -S --noconfirm --needed bind >/dev/null 2>&1
else
${PACKAGE_INSTALL[int]} dnsutils >/dev/null 2>&1
fi
}
checkpip() {
[ "${Var_OSRelease}" = "freebsd" ] && curl -L https://bootstrap.pypa.io/get-pip.py -o get-pip.py && chmod +x get-pip.py && python3 get-pip.py && rm -rf get-pip.py && return
local pvr="$1"
local pip_version=$(pip --version 2>&1)
if [[ $? -eq 0 && $pip_version != *"command not found"* ]]; then
_blue "$pip_version"
else
_yellow "installing python${pvr}-pip"
${PACKAGE_INSTALL[int]} python${pvr}-pip
pip_version=$(pip --version 2>&1)
if [[ $? -eq 0 ]]; then
_blue "$pip_version"
else
_red "python${pvr}-pip installation failed, please install it manually"
return
fi
fi
}
checkpystun() {
_yellow "checking pystun"
local python_command
local pip_command
if command -v python3 >/dev/null 2>&1; then
python_command="python3"
pip_command="pip3"
_blue "$($python_command --version 2>&1)"
elif command -v python >/dev/null 2>&1; then
python_command="python"
pip_command="pip"
_blue "$($python_command --version 2>&1)"
else
_yellow "installing python3"
${PACKAGE_INSTALL[int]} python3
if command -v python3 >/dev/null 2>&1; then
python_command="python3"
pip_command="pip3"
_blue "$($python_command --version 2>&1)"
elif command -v python >/dev/null 2>&1; then
python_command="python"
pip_command="pip"
_blue "$($python_command --version 2>&1)"
else
_yellow "installing python"
${PACKAGE_INSTALL[int]} python
if command -v python3 >/dev/null 2>&1; then
python_command="python3"
pip_command="pip3"
_blue "$($python_command --version 2>&1)"
elif command -v python >/dev/null 2>&1; then
python_command="python"
pip_command="pip"
_blue "$($python_command --version 2>&1)"
else
return
fi
fi
fi
if [[ $python_command == "python3" ]]; then
checkpip 3
if ! command -v pystun3 >/dev/null 2>&1; then
_yellow "Installing pystun3"
if ! "$pip_command" install -q pystun3 >/dev/null 2>&1; then
"$pip_command" install -q pystun3
fi
fi
fi
if [[ $python_command == "python" ]]; then
checkpip
if [[ $($python_command --version 2>&1) == Python\ 2* ]]; then
_yellow "Installing pystun"
if ! "$pip_command" install -q pystun >/dev/null 2>&1; then
"$pip_command" install -q pystun
fi
fi
fi
}
check_and_cat_file() {
local file="$1"
# 检测文件是否存在
if [[ -f "$file" ]]; then
# 判断文件内容是否为空或只包含空行
if [[ -s "$file" ]] && [[ "$(grep -vE '^\s*$' "$file")" ]]; then
:
else
truncate -s 0 "$file"
fi
else
truncate -s 0 "$file"
fi
cat "$file"
}
# 后台静默预下载文件并解压
pre_download() {
if [ -n "$LBench_Result_SystemBit_Full" ]; then
if [ "$LBench_Result_SystemBit_Full" = "arm" ]; then
tp_sys="arm64"
else
tp_sys="$LBench_Result_SystemBit_Full"
fi
fi
for file in "$@"; do
case $file in
sysbench)
wget -O $TEMP_DIR/sysbench.zip "${cdn_success_url}https://github.com/akopytov/sysbench/archive/1.0.20.zip"
unzip $TEMP_DIR/sysbench.zip -d ${TEMP_DIR}
;;
dp)
curl -sL -k "${cdn_success_url}https://github.com/sjlleo/VerifyDisneyPlus/releases/download/1.01/dp_1.01_linux_${tp_sys}" -o $TEMP_DIR/dp && chmod +x $TEMP_DIR/dp
;;
nf)
curl -sL -k "${cdn_success_url}https://github.com/sjlleo/netflix-verify/releases/download/v3.1.0/nf_linux_${tp_sys}" -o $TEMP_DIR/nf && chmod +x $TEMP_DIR/nf
;;
tubecheck)
curl -sL -k "${cdn_success_url}https://github.com/sjlleo/TubeCheck/releases/download/1.0Beta/tubecheck_1.0beta_linux_${tp_sys}" -o $TEMP_DIR/tubecheck && chmod +x $TEMP_DIR/tubecheck
;;
media_lmc_check)
curl -sL -k "${cdn_success_url}https://raw.githubusercontent.com/lmc999/RegionRestrictionCheck/main/check.sh" -o $TEMP_DIR/media_lmc_check.sh && chmod 777 $TEMP_DIR/media_lmc_check.sh
;;
besttrace)
curl -sL -k "${cdn_success_url}https://raw.githubusercontent.com/fscarmen/tools/main/besttrace/${BESTTRACE_FILE}" -o $TEMP_DIR/$BESTTRACE_FILE && chmod +x $TEMP_DIR/$BESTTRACE_FILE
;;
nexttrace)
NEXTTRACE_VERSION=$(curl -sSL "https://api.github.com/repos/nxtrace/Ntrace-core/releases/latest" | awk -F \" '/tag_name/{print $4}') && curl -sL -k "${cdn_success_url}https://github.com/nxtrace/Ntrace-core/releases/download/${NEXTTRACE_VERSION}/${NEXTTRACE_FILE}" -o $TEMP_DIR/$NEXTTRACE_FILE && chmod +x $TEMP_DIR/$NEXTTRACE_FILE
;;
backtrace)
wget -q -O $TEMP_DIR/backtrace.tar.gz https://github.com/zhanghanyun/backtrace/releases/latest/download/$BACKTRACE_FILE
tar -xf $TEMP_DIR/backtrace.tar.gz -C $TEMP_DIR
;;
yabsiotest)
curl -sL -k "${cdn_success_url}https://raw.githubusercontent.com/spiritLHLS/ecs/main/archive/yabsiotest.sh" -o yabsiotest.sh && chmod +x yabsiotest.sh
;;
ecsspeed_ping)
curl -sL -k "${cdn_success_url}https://raw.githubusercontent.com/spiritLHLS/ecsspeed/main/script/ecsspeed-ping.sh" -o $TEMP_DIR/ecsspeed-ping.sh && chmod +x $TEMP_DIR/ecsspeed-ping.sh
;;
*)
echo "Invalid file: $file"
;;
esac
done
}
# =============== 其他相关信息查询 部分 ===============
declare -A sysctl_vars=(
["fs.file-max"]=1024000
["net.core.rmem_max"]=134217728
["net.core.wmem_max"]=134217728
["net.core.netdev_max_backlog"]=250000
["net.core.somaxconn"]=1024000
["net.ipv4.conf.all.rp_filter"]=0
["net.ipv4.conf.default.rp_filter"]=0
["net.ipv4.conf.lo.arp_announce"]=2
["net.ipv4.conf.all.arp_announce"]=2
["net.ipv4.conf.default.arp_announce"]=2
["net.ipv4.ip_forward"]=1
["net.ipv4.ip_local_port_range"]="1024 65535"
["net.ipv4.neigh.default.gc_stale_time"]=120
["net.ipv4.tcp_syncookies"]=1
["net.ipv4.tcp_tw_reuse"]=1
["net.ipv4.tcp_low_latency"]=1
["net.ipv4.tcp_fin_timeout"]=10
["net.ipv4.tcp_window_scaling"]=1
["net.ipv4.tcp_keepalive_time"]=10
["net.ipv4.tcp_timestamps"]=0
["net.ipv4.tcp_sack"]=1
["net.ipv4.tcp_fack"]=1
["net.ipv4.tcp_syn_retries"]=3
["net.ipv4.tcp_synack_retries"]=3
["net.ipv4.tcp_max_syn_backlog"]=16384
["net.ipv4.tcp_max_tw_buckets"]=8192
["net.ipv4.tcp_fastopen"]=3
["net.ipv4.tcp_mtu_probing"]=1
["net.ipv4.tcp_rmem"]="4096 87380 67108864"
["net.ipv4.tcp_wmem"]="4096 65536 67108864"
["net.ipv6.conf.all.forwarding"]=1
["net.ipv6.conf.default.forwarding"]=1
["net.nf_conntrack_max"]=25000000
["net.netfilter.nf_conntrack_max"]=25000000
["net.netfilter.nf_conntrack_tcp_timeout_time_wait"]=30
["net.netfilter.nf_conntrack_tcp_timeout_established"]=180
["net.netfilter.nf_conntrack_tcp_timeout_close_wait"]=30
["net.netfilter.nf_conntrack_tcp_timeout_fin_wait"]=30
)
sysctl_conf="/etc/sysctl.conf"
sysctl_conf_backup="/etc/sysctl.conf.backup"
sysctl_default="${TEMP_DIR}/sysctl_backup.txt"
sysctl_path=$(which sysctl)
variable_exists() {
local variable="$1"
grep -q "^$variable=" "$sysctl_conf"
}
optimized_kernel() {
_yellow "optimizing resource limits"
if [ -f /etc/security/limits.conf ]; then
cp /etc/security/limits.conf /etc/security/limits.conf.backup
cat >/etc/security/limits.conf <<EOF
* soft nofile 512000
* hard nofile 512000
* soft nproc 512000
* hard nproc 512000
root soft nofile 512000
root hard nofile 512000
root soft nproc 512000
root hard nproc 512000
EOF
fi
if which systemctl >/dev/null 2>&1; then
_yellow "optimizing sysctl configuration"
declare -A default_values
if [ -f "$sysctl_conf" ]; then
if [ ! -f "$sysctl_conf_backup" ]; then
cp "$sysctl_conf" "$sysctl_conf_backup"
fi
while IFS= read -r line; do
variable="${line%%=*}"
variable="${variable%%[[:space:]]*}"
default_value="${line#*=}"
default_values["$variable"]="$default_value"
done < <($sysctl_path -a)
echo "" >"$sysctl_default"
for variable in "${!sysctl_vars[@]}"; do
value="${sysctl_vars[$variable]}"
if variable_exists "$variable"; then
sed -i "s/^$variable=.*/$variable=$value/" "$sysctl_conf"
else
echo "$variable=$value" >>"$sysctl_conf"
default_value="${default_values[$variable]}"
echo "$variable=$default_value" >>"$sysctl_default"
fi
done
$sysctl_path -p 2>/dev/null
fi
fi
}
check_cdn() {
local o_url=$1
for cdn_url in "${cdn_urls[@]}"; do
if curl -sL -k "$cdn_url$o_url" --max-time 6 | grep -q "success" >/dev/null 2>&1; then
export cdn_success_url="$cdn_url"
return
fi
sleep 0.5
done
export cdn_success_url=""
}
check_cdn_file() {
check_cdn "https://raw.githubusercontent.com/spiritLHLS/ecs/main/back/test"
if [ -n "$cdn_success_url" ]; then
_yellow "CDN available, using CDN"
else
_yellow "No CDN available, no use CDN"
fi
}
check_time_zone() {
_yellow "adjusting the time"
if command -v ntpd >/dev/null 2>&1; then
if which systemctl >/dev/null 2>&1; then
systemctl stop chronyd
systemctl stop ntpd
else
service chronyd stop
service ntpd stop
fi
if lsof -i:123 | grep -q "ntpd"; then
echo "Port 123 is already in use. Skipping ntpd command."
else
ntpd -gq
if which systemctl >/dev/null 2>&1; then
systemctl start ntpd
else
service ntpd start
fi
fi
sleep 0.5
return
fi
if ! command -v chronyd >/dev/null 2>&1; then
${PACKAGE_INSTALL[int]} chrony >/dev/null 2>&1
fi
if which systemctl >/dev/null 2>&1; then
systemctl stop chronyd
chronyd -q
systemctl start chronyd
else
service chronyd stop
chronyd -q
service chronyd start
fi
sleep 0.5
}
check_china() {
_yellow "IP area being detected ......"
if [[ -z "${CN}" ]]; then
if [[ $(curl -m 6 -s https://ipapi.co/json | grep 'China') != "" ]]; then
_yellow "根据ipapi.co提供的信息,当前IP可能在中国"
read -e -r -p "是否选用中国镜像完成相关组件安装? ([y]/n) " input
case $input in
[yY][eE][sS] | [yY])
echo "使用中国镜像"
CN=true
;;
[nN][oO] | [nN])
echo "不使用中国镜像"
;;
*)
echo "使用中国镜像"
CN=true
;;
esac
else
if [[ $? -ne 0 ]]; then
if [[ $(curl -m 6 -s cip.cc) =~ "中国" ]]; then
_yellow "根据cip.cc提供的信息,当前IP可能在中国"
read -e -r -p "是否选用中国镜像完成相关组件安装? [Y/n] " input
case $input in
[yY][eE][sS] | [yY])
echo "使用中国镜像"
CN=true
;;
[nN][oO] | [nN])
echo "不使用中国镜像"
;;
*)
echo "不使用中国镜像"
;;
esac
fi
fi
fi
fi
}
statistics_of_run-times() {
COUNT=$(
curl -4 -ksm1 "https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FspiritLHLS%2Fecs&count_bg=%2379C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=&edge_flat=true" 2>&1 ||
curl -6 -ksm1 "https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FspiritLHLS%2Fecs&count_bg=%2379C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=&edge_flat=true" 2>&1
) &&
TODAY=$(expr "$COUNT" : '.*\s\([0-9]\{1,\}\)\s/.*') && TOTAL=$(expr "$COUNT" : '.*/\s\([0-9]\{1,\}\)\s.*')
}
# =============== 基础系统信息 部分 ===============
systemInfo_get_os_release() {
if [ -f "/etc/centos-release" ]; then # CentOS
Var_OSRelease="centos"
if [ "$(rpm -qa | grep -o el6 | sort -u)" = "el6" ]; then
Var_CentOSELRepoVersion="6"
Var_OSReleaseVersion="$(cat /etc/centos-release | awk '{print $3}')"
elif [ "$(rpm -qa | grep -o el7 | sort -u)" = "el7" ]; then
Var_CentOSELRepoVersion="7"
Var_OSReleaseVersion="$(cat /etc/centos-release | awk '{print $4}')"
elif [ "$(rpm -qa | grep -o el8 | sort -u)" = "el8" ]; then
Var_CentOSELRepoVersion="8"
Var_OSReleaseVersion="$(cat /etc/centos-release | awk '{print $4}')"
else
local Var_CentOSELRepoVersion="unknown"
Var_OSReleaseVersion="<Unknown Release>"
fi
elif [ -f "/etc/fedora-release" ]; then # Fedora
Var_OSRelease="fedora"
Var_OSReleaseVersion="$(cat /etc/fedora-release | awk '{print $3,$4,$5,$6,$7}')"
elif [ -f "/etc/redhat-release" ]; then # RedHat
Var_OSRelease="rhel"
if [ "$(rpm -qa | grep -o el6 | sort -u)" = "el6" ]; then
Var_RedHatELRepoVersion="6"
Var_OSReleaseVersion="$(cat /etc/redhat-release | awk '{print $3}')"
elif [ "$(rpm -qa | grep -o el7 | sort -u)" = "el7" ]; then
Var_RedHatELRepoVersion="7"
Var_OSReleaseVersion="$(cat /etc/redhat-release | awk '{print $4}')"
elif [ "$(rpm -qa | grep -o el8 | sort -u)" = "el8" ]; then
Var_RedHatELRepoVersion="8"
Var_OSReleaseVersion="$(cat /etc/redhat-release | awk '{print $4}')"
else
local Var_RedHatELRepoVersion="unknown"
Var_OSReleaseVersion="<Unknown Release>"
fi
elif [ -f "/etc/astra_version" ]; then # Astra
Var_OSRelease="astra"
local Var_OSReleaseVersionShort="$(cat /etc/debian_version | awk '{printf "%d\n",$1}')"
if [ "${Var_OSReleaseVersionShort}" = "7" ]; then
Var_OSReleaseVersion_Codename="wheezy"
elif [ "${Var_OSReleaseVersionShort}" = "8" ]; then
Var_OSReleaseVersion_Codename="jessie"
elif [ "${Var_OSReleaseVersionShort}" = "9" ]; then
Var_OSReleaseVersion_Codename="stretch"
elif [ "${Var_OSReleaseVersionShort}" = "10" ]; then
Var_OSReleaseVersion_Codename="buster"
elif [ "${Var_OSReleaseVersionShort}" = "11" ]; then
Var_OSReleaseVersion_Codename="bullseye"
elif [ "${Var_OSReleaseVersionShort}" = "12" ]; then
Var_OSReleaseVersion_Codename="bookworm"
else
Var_OSReleaseVersion_Codename="sid"
fi
elif [ -f "/etc/lsb-release" ]; then # Ubuntu
Var_OSRelease="ubuntu"
Var_OSReleaseVersion="$(cat /etc/os-release | awk -F '[= "]' '/VERSION/{print $3,$4,$5,$6,$7}' | head -n1)"
cleaned_string=$(echo "$Var_OSReleaseVersion" | sed 's/[^0-9A-Za-z.]//g')
if [[ "$cleaned_string" =~ \. ]]; then
Var_OSReleaseVersion=${cleaned_string%%.*}
else
Var_OSReleaseVersion=${cleaned_string}
fi
elif [ -f "/etc/debian_version" ]; then # Debian
Var_OSRelease="debian"
local Var_OSReleaseVersion="$(cat /etc/debian_version | awk '{print $1}')"
local Var_OSReleaseVersionShort="$(cat /etc/debian_version | awk '{printf "%d\n",$1}')"
if [ "${Var_OSReleaseVersionShort}" = "7" ]; then
Var_OSReleaseVersion_Codename="wheezy"
elif [ "${Var_OSReleaseVersionShort}" = "8" ]; then
Var_OSReleaseVersion_Codename="jessie"
elif [ "${Var_OSReleaseVersionShort}" = "9" ]; then
Var_OSReleaseVersion_Codename="stretch"
elif [ "${Var_OSReleaseVersionShort}" = "10" ]; then
Var_OSReleaseVersion_Codename="buster"
elif [ "${Var_OSReleaseVersionShort}" = "11" ]; then
Var_OSReleaseVersion_Codename="bullseye"
elif [ "${Var_OSReleaseVersionShort}" = "12" ]; then
Var_OSReleaseVersion_Codename="bookworm"
else
Var_OSReleaseVersion_Codename="sid"
fi
elif [ -f "/etc/alpine-release" ]; then # Alpine Linux
Var_OSRelease="alpinelinux"
Var_OSReleaseVersion="$(cat /etc/alpine-release | awk '{print $1}')"
elif [ -f "/etc/almalinux-release" ]; then # almalinux
Var_OSRelease="almalinux"
Var_OSReleaseVersion="$(cat /etc/almalinux-release | awk '{print $3,$4,$5,$6,$7}')"
elif [ -f "/etc/arch-release" ]; then # archlinux
Var_OSRelease="arch"
elif [ -f "/etc/freebsd-update.conf" ] && [ -d "/usr/src" ]; then # freebsd
Var_OSRelease="freebsd"
else
Var_OSRelease="unknown" # 未知系统分支
fi
if [ -f /etc/os-release ]; then
DISTRO=$(grep 'PRETTY_NAME' /etc/os-release | cut -d '"' -f 2)
fi
}
get_system_bit() {
local sysarch="$(uname -m)"
if [ "${sysarch}" = "unknown" ] || [ "${sysarch}" = "" ]; then
local sysarch="$(arch)"
fi
# 根据架构信息设置系统位数并下载文件,其余 * 包括了 x86_64
case "${sysarch}" in
"i386" | "i686")
LBench_Result_SystemBit_Short="32"
LBench_Result_SystemBit_Full="i386"
BESTTRACE_FILE=besttracemac
NEXTTRACE_FILE=nexttrace_darwin_amd64
;;
"armv7l" | "armv8" | "armv8l" | "aarch64")
LBench_Result_SystemBit_Short="arm"
LBench_Result_SystemBit_Full="arm"
BESTTRACE_FILE=besttracearm
BACKTRACE_FILE=backtrace-linux-arm64.tar.gz
NEXTTRACE_FILE=nexttrace_linux_arm64
;;
*)
LBench_Result_SystemBit_Short="64"
LBench_Result_SystemBit_Full="amd64"
BESTTRACE_FILE=besttrace
BACKTRACE_FILE=backtrace-linux-amd64.tar.gz
NEXTTRACE_FILE=nexttrace_linux_amd64
;;
esac
}
# https://github.com/LemonBench/LemonBench/blob/main/LemonBench.sh
# ===========================================================================
# -> 系统信息模块 (Entrypoint) -> 执行
function BenchFunc_Systeminfo_GetSysteminfo() {
BenchAPI_Systeminfo_GetCPUinfo
BenchAPI_Systeminfo_GetVMMinfo
BenchAPI_Systeminfo_GetMemoryinfo
BenchAPI_Systeminfo_GetDiskinfo
BenchAPI_Systeminfo_GetOSReleaseinfo
# BenchAPI_Systeminfo_GetLinuxKernelinfo
}
#
# -> 系统信息模块 (Collector) -> 获取CPU信息
function BenchAPI_Systeminfo_GetCPUinfo() {
# CPU 基础信息检测
local r_modelname && r_modelname="$(lscpu -B 2>/dev/null | grep -oP -m1 "(?<=Model name:).*(?=)" | sed -e 's/^[ ]*//g')"
local r_cachesize_l1d_b && r_cachesize_l1d_b="$(lscpu -B 2>/dev/null | grep -oP "(?<=L1d cache:).*(?=)" | sed -e 's/^[ ]*//g')"
local r_cachesize_l1i_b && r_cachesize_l1i_b="$(lscpu -B 2>/dev/null | grep -oP "(?<=L1i cache:).*(?=)" | sed -e 's/^[ ]*//g')"
local r_cachesize_l1_b && r_cachesize_l1_b="$(echo "$r_cachesize_l1d_b" "$r_cachesize_l1i_b" | awk '{printf "%d\n",$1+$2}')"
local r_cachesize_l1_k && r_cachesize_l1_k="$(echo "$r_cachesize_l1_b" | awk '{printf "%.2f\n",$1/1024}')"
local t_cachesize_l1_k && t_cachesize_l1_k="$(echo "$r_cachesize_l1_b" | awk '{printf "%d\n",$1/1024}')"
if [ "$t_cachesize_l1_k" -ge "1024" ]; then
local r_cachesize_l1_m && r_cachesize_l1_m="$(echo "$r_cachesize_l1_k" | awk '{printf "%.2f\n",$1/1024}')"
local r_cachesize_l1="$r_cachesize_l1_m MB"
else
local r_cachesize_l1="$r_cachesize_l1_k KB"
fi
local r_cachesize_l2_b && r_cachesize_l2_b="$(lscpu -B 2>/dev/null | grep -oP "(?<=L2 cache:).*(?=)" | sed -e 's/^[ ]*//g')"
local r_cachesize_l2_k && r_cachesize_l2_k="$(echo "$r_cachesize_l2_b" | awk '{printf "%.2f\n",$1/1024}')"
local t_cachesize_l2_k && t_cachesize_l2_k="$(echo "$r_cachesize_l2_b" | awk '{printf "%d\n",$1/1024}')"
if [ "$t_cachesize_l2_k" -ge "1024" ]; then
local r_cachesize_l2_m && r_cachesize_l2_m="$(echo "$r_cachesize_l2_k" | awk '{printf "%.2f\n",$1/1024}')"
local r_cachesize_l2="$r_cachesize_l2_m MB"
else
local r_cachesize_l2="$r_cachesize_l2_k KB"
fi
local r_cachesize_l3_b && r_cachesize_l3_b="$(lscpu -B 2>/dev/null | grep -oP "(?<=L3 cache:).*(?=)" | sed -e 's/^[ ]*//g')"
local r_cachesize_l3_k && r_cachesize_l3_k="$(echo "$r_cachesize_l3_b" | awk '{printf "%.2f\n",$1/1024}')"
local t_cachesize_l3_k && t_cachesize_l3_k="$(echo "$r_cachesize_l3_b" | awk '{printf "%d\n",$1/1024}')"
if [ "$t_cachesize_l3_k" -ge "1024" ]; then
local r_cachesize_l3_m && r_cachesize_l3_m="$(echo "$r_cachesize_l3_k" | awk '{printf "%.2f\n",$1/1024}')"
local r_cachesize_l3="$r_cachesize_l3_m MB"
else
local r_cachesize_l3="$r_cachesize_l3_k KB"
fi
local r_sockets && r_sockets="$(lscpu -B 2>/dev/null | grep -oP "(?<=Socket\(s\):).*(?=)" | sed -e 's/^[ ]*//g')"
if [ "$r_sockets" -ge "2" ]; then
local r_cores && r_cores="$(lscpu -B 2>/dev/null | grep -oP "(?<=Core\(s\) per socket:).*(?=)" | sed -e 's/^[ ]*//g')"
r_cores="$(echo "$r_sockets" "$r_cores" | awk '{printf "%d\n",$1*$2}')"
local r_threadpercore && r_threadpercore="$(lscpu -B 2>/dev/null | grep -oP "(?<=Thread\(s\) per core:).*(?=)" | sed -e 's/^[ ]*//g')"
local r_threads && r_threads="$(echo "$r_cores" "$r_threadpercore" | awk '{printf "%d\n",$1*$2}')"
r_threads="$(echo "$r_threadpercore" "$r_cores" | awk '{printf "%d\n",$1*$2}')"
else
local r_cores && r_cores="$(lscpu -B 2>/dev/null | grep -oP "(?<=Core\(s\) per socket:).*(?=)" | sed -e 's/^[ ]*//g')"
local r_threadpercore && r_threadpercore="$(lscpu -B 2>/dev/null | grep -oP "(?<=Thread\(s\) per core:).*(?=)" | sed -e 's/^[ ]*//g')"
local r_threads && r_threads="$(echo "$r_cores" "$r_threadpercore" | awk '{printf "%d\n",$1*$2}')"
fi
# CPU AES能力检测
# local t_aes && t_aes="$(awk -F ': ' '/flags/{print $2}' /proc/cpuinfo 2>/dev/null | grep -oE "\baes\b" | sort -u)"
# [[ "${t_aes}" = "aes" ]] && Result_Systeminfo_CPUAES="1" || Result_Systeminfo_CPUAES="0"
# CPU AVX能力检测
# local t_avx && t_avx="$(awk -F ': ' '/flags/{print $2}' /proc/cpuinfo 2>/dev/null | grep -oE "\bavx\b" | sort -u)"
# [[ "${t_avx}" = "avx" ]] && Result_Systeminfo_CPUAVX="1" || Result_Systeminfo_CPUAVX="0"
# CPU AVX512能力检测
# local t_avx512 && t_avx512="$(awk -F ': ' '/flags/{print $2}' /proc/cpuinfo 2>/dev/null | grep -oE "\bavx512\b" | sort -u)"
# [[ "${t_avx512}" = "avx" ]] && Result_Systeminfo_CPUAVX512="1" || Result_Systeminfo_CPUAVX512="0"
# CPU 虚拟化能力检测
local t_vmx_vtx && t_vmx_vtx="$(awk -F ': ' '/flags/{print $2}' /proc/cpuinfo 2>/dev/null | grep -oE "\bvmx\b" | sort -u)"
local t_vmx_svm && t_vmx_svm="$(awk -F ': ' '/flags/{print $2}' /proc/cpuinfo 2>/dev/null | grep -oE "\bsvm\b" | sort -u)"
if [ "$t_vmx_vtx" = "vmx" ]; then
Result_Systeminfo_VirtReady="1"
Result_Systeminfo_CPUVMX="Intel VT-x"
elif [ "$t_vmx_svm" = "svm" ]; then
Result_Systeminfo_VirtReady="1"
Result_Systeminfo_CPUVMX="AMD-V"
else
if [ -c "/dev/kvm" ]; then
Result_Systeminfo_VirtReady="1"
Result_Systeminfo_CPUVMX="unknown"
else
Result_Systeminfo_VirtReady="0"
Result_Systeminfo_CPUVMX="unknown"
fi
fi
# 输出结果
Result_Systeminfo_CPUModelName="$r_modelname"
Result_Systeminfo_CPUSockets="$r_sockets"
Result_Systeminfo_CPUCores="$r_cores"
Result_Systeminfo_CPUThreads="$r_threads"
Result_Systeminfo_CPUCacheSizeL1="$r_cachesize_l1"
Result_Systeminfo_CPUCacheSizeL2="$r_cachesize_l2"
Result_Systeminfo_CPUCacheSizeL3="$r_cachesize_l3"
}
#
# -> 系统信息模块 (Collector) -> 获取内存及Swap信息
function BenchAPI_Systeminfo_GetMemoryinfo() {
# 内存信息
local r_memtotal_kib && r_memtotal_kib="$(awk '/MemTotal/{print $2}' /proc/meminfo | head -n1)"
local r_memtotal_mib && r_memtotal_mib="$(echo "$r_memtotal_kib" | awk '{printf "%.2f\n",$1/1024}')"
local r_memtotal_gib && r_memtotal_gib="$(echo "$r_memtotal_kib" | awk '{printf "%.2f\n",$1/1048576}')"
local r_meminfo_memfree_kib && r_meminfo_memfree_kib="$(awk '/MemFree/{print $2}' /proc/meminfo | head -n1)"
local r_meminfo_buffers_kib && r_meminfo_buffers_kib="$(awk '/Buffers/{print $2}' /proc/meminfo | head -n1)"
local r_meminfo_cached_kib && r_meminfo_cached_kib="$(awk '/Cached/{print $2}' /proc/meminfo | head -n1)"
local r_memfree_kib && r_memfree_kib="$(echo "$r_meminfo_memfree_kib" "$r_meminfo_buffers_kib" "$r_meminfo_cached_kib" | awk '{printf $1+$2+$3}')"
local r_memfree_mib && r_memfree_mib="$(echo "$r_memfree_kib" | awk '{printf "%.2f\n",$1/1024}')"
local r_memfree_gib && r_memfree_gib="$(echo "$r_memfree_kib" | awk '{printf "%.2f\n",$1/1048576}')"
local r_memused_kib && r_memused_kib="$(echo "$r_memtotal_kib" "$r_memfree_kib" | awk '{printf $1-$2}')"
local r_memused_mib && r_memused_mib="$(echo "$r_memused_kib" | awk '{printf "%.2f\n",$1/1024}')"
local r_memused_gib && r_memused_gib="$(echo "$r_memused_kib" | awk '{printf "%.2f\n",$1/1048576}')"
# 交换信息
local r_swaptotal_kib && r_swaptotal_kib="$(awk '/SwapTotal/{print $2}' /proc/meminfo | head -n1)"
local r_swaptotal_mib && r_swaptotal_mib="$(echo "$r_swaptotal_kib" | awk '{printf "%.2f\n",$1/1024}')"
local r_swaptotal_gib && r_swaptotal_gib="$(echo "$r_swaptotal_kib" | awk '{printf "%.2f\n",$1/1048576}')"
local r_swapfree_kib && r_swapfree_kib="$(awk '/SwapFree/{print $2}' /proc/meminfo | head -n1)"
local r_swapfree_mib && r_swapfree_mib="$(echo "$r_swapfree_kib" | awk '{printf "%.2f\n",$1/1024}')"
local r_swapfree_gib && r_swapfree_gib="$(echo "$r_swapfree_kib" | awk '{printf "%.2f\n",$1/1048576}')"
local r_swapused_kib && r_swapused_kib="$(echo "$r_swaptotal_kib" "${r_swapfree_kib}" | awk '{printf $1-$2}')"
local r_swapused_mib && r_swapused_mib="$(echo "$r_swapused_kib" | awk '{printf "%.2f\n",$1/1024}')"
local r_swapused_gib && r_swapused_gib="$(echo "$r_swapused_kib" | awk '{printf "%.2f\n",$1/1048576}')"
# 数据加工
if [ "$r_memused_kib" -lt "1024" ] && [ "$r_memtotal_kib" -lt "1048576" ]; then
Result_Systeminfo_Memoryinfo="$r_memused_kib KiB / $r_memtotal_mib MiB"
elif [ "$r_memused_kib" -lt "1048576" ] && [ "$r_memtotal_kib" -lt "1048576" ]; then
Result_Systeminfo_Memoryinfo="$r_memused_mib MiB / $r_memtotal_mib MiB"
elif [ "$r_memused_kib" -lt "1048576" ] && [ "$r_memtotal_kib" -lt "1073741824" ]; then
Result_Systeminfo_Memoryinfo="$r_memused_mib MiB / $r_memtotal_gib GiB"
else
Result_Systeminfo_Memoryinfo="$r_memused_gib GiB / $r_memtotal_gib GiB"
fi
if [ "$r_swaptotal_kib" -eq "0" ]; then
Result_Systeminfo_Swapinfo="[ no swap partition or swap file detected ]"
elif [ "$r_swapused_kib" -lt "1024" ] && [ "$r_swaptotal_kib" -lt "1048576" ]; then
Result_Systeminfo_Swapinfo="$r_swapused_kib KiB / $r_swaptotal_mib MiB"
elif [ "$r_swapused_kib" -lt "1024" ] && [ "$r_swaptotal_kib" -lt "1073741824" ]; then
Result_Systeminfo_Swapinfo="$r_swapused_kib KiB / $r_swaptotal_gib GiB"
elif [ "$r_swapused_kib" -lt "1048576" ] && [ "$r_swaptotal_kib" -lt "1048576" ]; then
Result_Systeminfo_Swapinfo="$r_swapused_mib MiB / $r_swaptotal_mib MiB"
elif [ "$r_swapused_kib" -lt "1048576" ] && [ "$r_swaptotal_kib" -lt "1073741824" ]; then
Result_Systeminfo_Swapinfo="$r_swapused_mib MiB / $r_swaptotal_gib GiB"
else
Result_Systeminfo_Swapinfo="$r_swapused_gib GiB / $r_swaptotal_gib GiB"
fi
}
#
# -> 系统信息模块 (Collector) -> 获取磁盘信息
function BenchAPI_Systeminfo_GetDiskinfo() {
# 磁盘信息
local r_diskpath_root && r_diskpath_root="$(df -x tmpfs / | awk "NR>1" | sed ":a;N;s/\\n//g;ta" | awk '{print $1}')"
local r_disktotal_kib && r_disktotal_kib="$(df -x tmpfs / | grep -oE "[0-9]{4,}" | awk 'NR==1 {print $1}')"
local r_disktotal_mib && r_disktotal_mib="$(echo "$r_disktotal_kib" | awk '{printf "%.2f\n",$1/1024}')"
local r_disktotal_gib && r_disktotal_gib="$(echo "$r_disktotal_kib" | awk '{printf "%.2f\n",$1/1048576}')"
local r_disktotal_tib && r_disktotal_tib="$(echo "$r_disktotal_kib" | awk '{printf "%.2f\n",$1/1073741824}')"
local r_diskused_kib && r_diskused_kib="$(df -x tmpfs / | grep -oE "[0-9]{4,}" | awk 'NR==2 {print $1}')"
local r_diskused_mib && r_diskused_mib="$(echo "$r_diskused_kib" | awk '{printf "%.2f\n",$1/1024}')"
local r_diskused_gib && r_diskused_gib="$(echo "$r_diskused_kib" | awk '{printf "%.2f\n",$1/1048576}')"
local r_diskused_tib && r_diskused_tib="$(echo "$r_diskused_kib" | awk '{printf "%.2f\n",$1/1073741824}')"
local r_diskfree_kib && r_diskfree_kib="$(df -x tmpfs / | grep -oE "[0-9]{4,}" | awk 'NR==3 {print $1}')"
local r_diskfree_mib && r_diskfree_mib="$(echo "$r_diskfree_kib" | awk '{printf "%.2f\n",$1/1024}')"
local r_diskfree_gib && r_diskfree_gib="$(echo "$r_diskfree_kib" | awk '{printf "%.2f\n",$1/1048576}')"
local r_diskfree_tib && r_diskfree_tib="$(echo "$r_diskfree_kib" | awk '{printf "%.2f\n",$1/1073741824}')"
# 数据加工
Result_Systeminfo_DiskRootPath="$r_diskpath_root"
if [ "$r_diskused_kib" -lt "1048576" ]; then
Result_Systeminfo_Diskinfo="$r_diskused_mib MiB / $r_disktotal_mib MiB"
elif [ "$r_diskused_kib" -lt "1048576" ] && [ "$r_disktotal_kib" -lt "1073741824" ]; then
Result_Systeminfo_Diskinfo="$r_diskused_mib MiB / $r_disktotal_gib GiB"
elif [ "$r_diskused_kib" -lt "1073741824" ] && [ "$r_disktotal_kib" -lt "1073741824" ]; then
Result_Systeminfo_Diskinfo="$r_diskused_gib GiB / $r_disktotal_gib GiB"
elif [ "$r_diskused_kib" -lt "1073741824" ] && [ "$r_disktotal_kib" -ge "1073741824" ]; then