-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.sh
More file actions
1441 lines (1183 loc) · 42.1 KB
/
agent.sh
File metadata and controls
1441 lines (1183 loc) · 42.1 KB
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
#==============================================================================
# ProbeShell 监控组件管理脚本
# 支持组件: blackbox_exporter, node_exporter, vmagent, promtail
#==============================================================================
set -euo pipefail # 严格模式:遇到错误立即退出
#==============================================================================
# 全局变量和配置
#==============================================================================
# 版本配置
readonly BLACKBOX_VERSION="0.27.0"
readonly NODE_VERSION="1.9.1"
readonly VMAGENT_VERSION="1.128.0"
readonly PROMTAIL_VERSION="3.5.7"
# 组件配置
readonly AVAILABLE_COMPONENTS=("blackbox" "node_exporter" "vmagent" "promtail")
declare -a SELECTED_COMPONENTS=()
# 安装路径
readonly BLACKBOX_PATH="/usr/local/bin/blackbox"
readonly NODE_PATH="/usr/local/bin/node"
readonly VMAGENT_PATH="/usr/local/bin/vmagent"
readonly PROMTAIL_PATH="/usr/local/bin/promtail"
# 服务名称映射
declare -A SERVICE_MAP=(
["blackbox"]="blackbox"
["node_exporter"]="node_exporter"
["vmagent"]="vmagent"
["promtail"]="promtail"
)
# 安装路径映射
declare -A INSTALL_PATH_MAP=(
["blackbox"]="$BLACKBOX_PATH"
["node_exporter"]="$NODE_PATH"
["vmagent"]="$VMAGENT_PATH"
["promtail"]="$PROMTAIL_PATH"
)
# 操作模式
INSTALL=false
UNINSTALL=false
LIST=false
DRY_RUN=false
# 配置参数
MAIN_DOMAIN=""
LOKI_DOMAIN=""
INSTANCE_NAME=""
VM_USERNAME=""
VM_PASSWORD=""
DELETE_LOGS="n"
BLACKBOX_TARGETS=""
CACHE_SIZE="512MiB"
CACHE_SIZE_SET=false # 标记是否显式设置了 cache-size
# 系统信息
ARCH=""
ARCH_SUFFIX=""
# 临时文件列表(用于清理)
declare -a TEMP_FILES=()
#==============================================================================
# 颜色和样式
#==============================================================================
# 基础颜色
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly PURPLE='\033[0;35m'
readonly CYAN='\033[0;36m'
readonly WHITE='\033[1;37m'
readonly NC='\033[0m' # No Color
# 样式
readonly BOLD='\033[1m'
readonly DIM='\033[2m'
readonly UNDERLINE='\033[4m'
# 组合颜色
readonly BRIGHT_GREEN='\033[1;32m'
readonly BRIGHT_BLUE='\033[1;34m'
readonly BRIGHT_CYAN='\033[1;36m'
readonly BRIGHT_YELLOW='\033[1;33m'
#==============================================================================
# 工具函数
#==============================================================================
# 打印消息
log_info() {
echo -e "${BRIGHT_BLUE}ℹ ${NC}${BLUE}$*${NC}"
}
log_success() {
echo -e "${BRIGHT_GREEN}✓${NC} ${GREEN}$*${NC}"
}
log_warn() {
echo -e "${BRIGHT_YELLOW}⚠${NC} ${YELLOW}$*${NC}"
}
log_error() {
echo -e "${RED}✗${NC} ${RED}${BOLD}$*${NC}" >&2
}
# 打印分隔线
print_separator() {
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
}
# 打印标题
print_header() {
echo ""
echo -e "${BRIGHT_CYAN}╔════════════════════════════════════════════════════════════════╗${NC}"
printf "${BRIGHT_CYAN}║${NC} ${BOLD}${WHITE}%-60s${NC} ${BRIGHT_CYAN}║${NC}\n" "$1"
echo -e "${BRIGHT_CYAN}╚════════════════════════════════════════════════════════════════╝${NC}"
echo ""
}
# 打印子标题
print_subheader() {
echo ""
echo -e "${CYAN}┌─ ${BOLD}$1${NC}"
echo -e "${CYAN}└─────────────────────────────────────────────────────────────────${NC}"
}
# 打印信息框
print_box() {
local message="$1"
local color="${2:-$CYAN}"
echo ""
echo -e "${color}┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓${NC}"
printf "${color}┃${NC} %-60s ${color}┃${NC}\n" "$message"
echo -e "${color}┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛${NC}"
echo ""
}
# 美化的输入提示
prompt_input() {
local prompt_text="$1"
local default_value="$2"
local result
if [ -n "$default_value" ]; then
echo -e "${CYAN}▸${NC} ${BOLD}${prompt_text}${NC} ${DIM}(默认: ${default_value})${NC}"
else
echo -e "${CYAN}▸${NC} ${BOLD}${prompt_text}${NC}"
fi
echo -ne "${BRIGHT_CYAN} ➜ ${NC}"
read -r result
echo "$result"
}
# 打印欢迎横幅
print_banner() {
clear
echo ""
echo -e "${BRIGHT_CYAN}"
echo " ╔═══════════════════════════════════════════════════════════╗"
echo " ║ ║"
echo " ║ ██████╗ ██████╗ ██████╗ ██████╗ ███████╗ ║"
echo " ║ ██╔══██╗██╔══██╗██╔═══██╗██╔══██╗██╔════╝ ║"
echo " ║ ██████╔╝██████╔╝██║ ██║██████╔╝█████╗ ║"
echo " ║ ██╔═══╝ ██╔══██╗██║ ██║██╔══██╗██╔══╝ ║"
echo " ║ ██║ ██║ ██║╚██████╔╝██████╔╝███████╗ ║"
echo " ║ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝ ║"
echo " ║ ║"
echo " ╚═══════════════════════════════════════════════════════════╝"
echo -e "${NC}"
echo ""
echo -e "${DIM} 版本信息:${NC}"
echo -e " ${CYAN}•${NC} Blackbox Exporter: ${GREEN}v${BLACKBOX_VERSION}${NC}"
echo -e " ${CYAN}•${NC} Node Exporter: ${GREEN}v${NODE_VERSION}${NC}"
echo -e " ${CYAN}•${NC} VictoriaMetrics: ${GREEN}v${VMAGENT_VERSION}${NC}"
echo -e " ${CYAN}•${NC} Promtail: ${GREEN}v${PROMTAIL_VERSION}${NC}"
echo ""
print_separator
echo ""
}
# 错误处理和清理
cleanup() {
local exit_code=$?
if [ $exit_code -ne 0 ]; then
log_error "脚本执行失败,退出码: $exit_code"
fi
# 清理临时文件
for temp_file in "${TEMP_FILES[@]}"; do
if [ -f "$temp_file" ]; then
rm -f "$temp_file" 2>/dev/null || true
fi
done
}
trap cleanup EXIT
# 检查命令是否存在
command_exists() {
command -v "$1" &> /dev/null
}
# 检查是否以 root 权限运行
check_root() {
if [ "$EUID" -ne 0 ]; then
log_error "请使用 root 权限运行此脚本"
exit 1
fi
}
# 确认操作
confirm() {
local prompt="$1"
local response
read -p "$prompt [y/N]: " response
case "$response" in
[yY][eE][sS]|[yY]) return 0 ;;
*) return 1 ;;
esac
}
#==============================================================================
# 系统检测和验证
#==============================================================================
# 获取系统架构
detect_architecture() {
ARCH=$(uname -m)
case "$ARCH" in
x86_64)
ARCH_SUFFIX="linux-amd64"
;;
aarch64)
ARCH_SUFFIX="linux-arm64"
;;
*)
log_error "不支持的架构: $ARCH"
log_error "仅支持 x86_64 和 aarch64"
exit 1
;;
esac
log_info "检测到系统架构: $ARCH ($ARCH_SUFFIX)"
}
# 检查系统依赖
check_system_dependencies() {
log_info "检查系统依赖..."
local missing_deps=()
local required_commands=("wget" "curl" "tar" "systemctl" "unzip")
for cmd in "${required_commands[@]}"; do
if ! command_exists "$cmd"; then
missing_deps+=("$cmd")
fi
done
if [ ${#missing_deps[@]} -gt 0 ]; then
log_warn "缺少以下依赖: ${missing_deps[*]}"
return 1
fi
log_success "系统依赖检查通过"
return 0
}
# 安装系统依赖
install_system_dependencies() {
print_header "安装系统依赖"
if check_system_dependencies; then
log_info "系统依赖已满足,跳过安装"
return 0
fi
log_info "更新软件包列表..."
apt update || {
log_error "apt update 失败"
return 1
}
log_info "安装依赖包..."
apt install -y unzip ntpsec-ntpdate wget curl || {
log_error "依赖包安装失败"
return 1
}
log_info "配置时区和时间同步..."
timedatectl set-timezone Asia/Shanghai || log_warn "设置时区失败"
ntpdate ntp.aliyun.com || log_warn "时间同步失败"
# 添加定时任务
local cron_job="0 3 * * * /usr/sbin/ntpdate ntp.aliyun.com > /dev/null 2>&1"
if ! crontab -l 2>/dev/null | grep -Fxq "$cron_job"; then
(crontab -l 2>/dev/null; echo "$cron_job") | crontab - || log_warn "添加 cron 任务失败"
fi
log_success "系统依赖安装完成"
}
#==============================================================================
# 组件状态检测
#==============================================================================
# 检查组件是否已安装
is_component_installed() {
local component="$1"
local install_path="${INSTALL_PATH_MAP[$component]}"
if [ -d "$install_path" ] && [ -f "/etc/systemd/system/${SERVICE_MAP[$component]}.service" ]; then
return 0
fi
return 1
}
# 检查服务是否运行
is_service_running() {
local service="$1"
systemctl is-active --quiet "$service" 2>/dev/null
}
# 检查服务是否启用
is_service_enabled() {
local service="$1"
systemctl is-enabled --quiet "$service" 2>/dev/null
}
# 获取已安装的组件列表
get_installed_components() {
local -a installed=()
for component in "${AVAILABLE_COMPONENTS[@]}"; do
if is_component_installed "$component"; then
installed+=("$component")
fi
done
echo "${installed[@]}"
}
# 显示组件状态
show_component_status() {
local has_installed=false
# 组件名称映射(用于更好的显示)
declare -A COMPONENT_NAMES=(
["blackbox"]="Blackbox Exporter"
["node_exporter"]="Node Exporter"
["vmagent"]="VictoriaMetrics Agent"
["promtail"]="Promtail"
)
for component in "${AVAILABLE_COMPONENTS[@]}"; do
local service="${SERVICE_MAP[$component]}"
local status="未安装"
local status_icon="○"
local color="$DIM"
local display_name="${COMPONENT_NAMES[$component]}"
if is_component_installed "$component"; then
has_installed=true
if is_service_running "$service"; then
status="运行中"
status_icon="●"
color="$BRIGHT_GREEN"
elif is_service_enabled "$service"; then
status="已停止"
status_icon="◐"
color="$YELLOW"
else
status="未启用"
status_icon="○"
color="$YELLOW"
fi
fi
printf " ${color}${status_icon}${NC} ${CYAN}%-25s${NC} ${color}%-12s${NC}\n" "$display_name" "$status"
done
if ! $has_installed; then
echo ""
log_info "没有已安装的组件"
fi
echo ""
}
#==============================================================================
# 组件选择和验证
#==============================================================================
# 验证组件名称
validate_component() {
local component="$1"
for valid_component in "${AVAILABLE_COMPONENTS[@]}"; do
if [ "$component" == "$valid_component" ]; then
return 0
fi
done
return 1
}
# 验证所有选中的组件
validate_selected_components() {
for component in "${SELECTED_COMPONENTS[@]}"; do
if ! validate_component "$component"; then
log_error "无效的组件名称: $component"
log_error "可用组件: ${AVAILABLE_COMPONENTS[*]}"
exit 1
fi
done
}
# 检查组件是否被选中
is_component_selected() {
local component="$1"
# 如果没有选中任何组件,则全部选中
if [ ${#SELECTED_COMPONENTS[@]} -eq 0 ]; then
return 0
fi
for selected in "${SELECTED_COMPONENTS[@]}"; do
if [ "$selected" == "$component" ]; then
return 0
fi
done
return 1
}
# 交互式选择组件
select_components_interactive() {
local mode="$1" # "install" 或 "uninstall"
local mode_text="安装"
local mode_action="安装"
local mode_icon="📦"
if [ "$mode" == "uninstall" ]; then
mode_text="卸载"
mode_action="卸载"
mode_icon="🗑️"
fi
print_header "${mode_icon} 选择要${mode_text}的组件"
# 如果是卸载模式,显示已安装的组件
if [ "$mode" == "uninstall" ]; then
local installed_components=($(get_installed_components))
if [ ${#installed_components[@]} -eq 0 ]; then
log_warn "没有已安装的组件"
exit 0
fi
log_info "已安装的组件: ${installed_components[*]}"
echo ""
fi
echo -e "${BOLD}${WHITE}可用组件列表:${NC}"
echo ""
echo -e " ${BRIGHT_CYAN}1)${NC} ${CYAN}blackbox_exporter${NC} ${DIM}- HTTP/TCP 探测${NC}"
echo -e " ${BRIGHT_CYAN}2)${NC} ${CYAN}node_exporter${NC} ${DIM}- 系统监控${NC}"
echo -e " ${BRIGHT_CYAN}3)${NC} ${CYAN}vmagent${NC} ${DIM}- 指标收集代理${NC}"
echo -e " ${BRIGHT_CYAN}4)${NC} ${CYAN}promtail${NC} ${DIM}- 日志收集${NC}"
echo -e " ${BRIGHT_GREEN}5)${NC} ${GREEN}全部${mode_action}${NC}"
echo ""
print_separator
echo ""
echo -e "${CYAN}▸${NC} 输入组件编号,用空格分隔 ${DIM}(例如: 1 2 3)${NC}"
echo -ne "${BRIGHT_CYAN} ➜ ${NC}"
read -r choices
SELECTED_COMPONENTS=()
for choice in $choices; do
case $choice in
1) SELECTED_COMPONENTS+=("blackbox") ;;
2) SELECTED_COMPONENTS+=("node_exporter") ;;
3) SELECTED_COMPONENTS+=("vmagent") ;;
4) SELECTED_COMPONENTS+=("promtail") ;;
5) SELECTED_COMPONENTS=("${AVAILABLE_COMPONENTS[@]}"); break ;;
*)
log_error "无效选项: $choice"
exit 1
;;
esac
done
if [ ${#SELECTED_COMPONENTS[@]} -eq 0 ]; then
log_error "至少选择一个组件"
exit 1
fi
echo ""
log_info "已选择组件: ${SELECTED_COMPONENTS[*]}"
echo ""
}
#==============================================================================
# 配置验证
#==============================================================================
# 验证 vmagent 配置
validate_vmagent_config() {
local errors=()
if [ -z "$MAIN_DOMAIN" ]; then
errors+=("VictoriaMetrics 地址不能为空")
fi
if [ -z "$INSTANCE_NAME" ]; then
errors+=("实例名称不能为空")
fi
if [ -z "$VM_USERNAME" ]; then
errors+=("用户名不能为空")
fi
if [ -z "$VM_PASSWORD" ]; then
errors+=("密码不能为空")
fi
# 验证缓存大小格式
if ! [[ "$CACHE_SIZE" =~ ^[0-9]+(KB|MB|GB|TB|KiB|MiB|GiB|TiB)$ ]]; then
errors+=("缓存大小格式无效,应为如: 512MiB, 1GB, 2GB")
fi
if [ ${#errors[@]} -gt 0 ]; then
log_error "vmagent 配置验证失败:"
for error in "${errors[@]}"; do
log_error " - $error"
done
return 1
fi
return 0
}
# 验证 promtail 配置
validate_promtail_config() {
local errors=()
if [ -z "$LOKI_DOMAIN" ]; then
errors+=("Loki 地址不能为空")
fi
if [ -z "$INSTANCE_NAME" ]; then
errors+=("实例名称不能为空")
fi
if [ ${#errors[@]} -gt 0 ]; then
log_error "promtail 配置验证失败:"
for error in "${errors[@]}"; do
log_error " - $error"
done
return 1
fi
return 0
}
#==============================================================================
# 下载和文件处理
#==============================================================================
# 安全下载文件
safe_download() {
local url="$1"
local output="$2"
local description="${3:-文件}"
log_info "下载 $description..."
log_info "URL: $url"
if $DRY_RUN; then
log_info "[DRY RUN] 跳过下载: $output"
return 0
fi
# 如果文件已存在,先备份
if [ -f "$output" ]; then
local backup="${output}.bak.$$"
mv "$output" "$backup"
TEMP_FILES+=("$backup")
fi
# 使用 wget 下载,显示进度,最多重试 3 次
if ! wget --progress=bar:force -t 3 -T 30 -O "$output" "$url" 2>&1; then
log_error "下载失败: $description"
return 1
fi
# 验证文件是否下载成功
if [ ! -f "$output" ] || [ ! -s "$output" ]; then
log_error "下载的文件无效: $output"
return 1
fi
log_success "下载完成: $description"
return 0
}
# 解压 tar.gz 文件
extract_tarball() {
local archive="$1"
local description="${2:-压缩包}"
log_info "解压 $description..."
if $DRY_RUN; then
log_info "[DRY RUN] 跳过解压: $archive"
return 0
fi
if ! tar -zxf "$archive"; then
log_error "解压失败: $archive"
return 1
fi
log_success "解压完成: $description"
TEMP_FILES+=("$archive") # 标记为待删除
return 0
}
#==============================================================================
# Blackbox Exporter 安装
#==============================================================================
install_blackbox() {
print_header "安装 blackbox_exporter"
if is_component_installed "blackbox" && ! $DRY_RUN; then
log_warn "blackbox_exporter 已安装"
if ! confirm "是否重新安装?"; then
return 0
fi
uninstall_component "blackbox"
fi
local archive="blackbox_exporter-${BLACKBOX_VERSION}.${ARCH_SUFFIX}.tar.gz"
local url="https://github.com/prometheus/blackbox_exporter/releases/download/v${BLACKBOX_VERSION}/${archive}"
safe_download "$url" "$archive" "blackbox_exporter" || return 1
extract_tarball "$archive" "blackbox_exporter" || return 1
local extracted_dir="blackbox_exporter-${BLACKBOX_VERSION}.${ARCH_SUFFIX}"
if $DRY_RUN; then
log_info "[DRY RUN] 跳过移动文件到 $BLACKBOX_PATH"
else
if [ ! -d "$extracted_dir" ]; then
log_error "解压目录不存在: $extracted_dir"
return 1
fi
rm -rf "$BLACKBOX_PATH"
mv "$extracted_dir" "$BLACKBOX_PATH" || return 1
TEMP_FILES+=("$extracted_dir")
fi
# 下载配置文件
safe_download \
"https://raw.githubusercontent.com/Sm1rkBoy/ProbeShell/main/blackbox/blackbox.yml" \
"${BLACKBOX_PATH}/blackbox.yml" \
"blackbox 配置文件" || return 1
# 下载服务文件
safe_download \
"https://raw.githubusercontent.com/Sm1rkBoy/ProbeShell/main/service/blackbox.service" \
"/etc/systemd/system/blackbox.service" \
"blackbox 服务文件" || return 1
if ! $DRY_RUN; then
chmod 644 /etc/systemd/system/blackbox.service
fi
log_success "blackbox_exporter 安装完成"
return 0
}
#==============================================================================
# Node Exporter 安装
#==============================================================================
install_node_exporter() {
print_header "安装 node_exporter"
if is_component_installed "node_exporter" && ! $DRY_RUN; then
log_warn "node_exporter 已安装"
if ! confirm "是否重新安装?"; then
return 0
fi
uninstall_component "node_exporter"
fi
local archive="node_exporter-${NODE_VERSION}.${ARCH_SUFFIX}.tar.gz"
local url="https://github.com/prometheus/node_exporter/releases/download/v${NODE_VERSION}/${archive}"
safe_download "$url" "$archive" "node_exporter" || return 1
extract_tarball "$archive" "node_exporter" || return 1
local extracted_dir="node_exporter-${NODE_VERSION}.${ARCH_SUFFIX}"
if $DRY_RUN; then
log_info "[DRY RUN] 跳过移动文件到 $NODE_PATH"
else
if [ ! -d "$extracted_dir" ]; then
log_error "解压目录不存在: $extracted_dir"
return 1
fi
rm -rf "$NODE_PATH"
mv "$extracted_dir" "$NODE_PATH" || return 1
TEMP_FILES+=("$extracted_dir")
fi
# 下载服务文件
safe_download \
"https://raw.githubusercontent.com/Sm1rkBoy/ProbeShell/main/service/node_exporter.service" \
"/etc/systemd/system/node_exporter.service" \
"node_exporter 服务文件" || return 1
if ! $DRY_RUN; then
chmod 644 /etc/systemd/system/node_exporter.service
fi
log_success "node_exporter 安装完成"
return 0
}
#==============================================================================
# VAgent 安装
#==============================================================================
# 生成 blackbox 探测目标配置
generate_blackbox_targets() {
local endpoint_file="${VMAGENT_PATH}/endpoint.yml"
mkdir -p "$VMAGENT_PATH"
if [ -n "$BLACKBOX_TARGETS" ]; then
log_info "配置自定义 blackbox 探测目标..."
if $DRY_RUN; then
log_info "[DRY RUN] 跳过生成探测目标配置"
return 0
fi
# 生成配置文件头部
cat > "$endpoint_file" << 'EOF'
# Blackbox Exporter Targets - Auto Generated
# Format: Prometheus file_sd_configs
EOF
# 将逗号分隔的目标转换为数组
IFS=',' read -ra TARGET_ARRAY <<< "$BLACKBOX_TARGETS"
for target in "${TARGET_ARRAY[@]}"; do
# 移除前后空格
target=$(echo "$target" | xargs)
# 提取域名作为标签
local label=$(echo "$target" | sed -E 's|https?://||' | sed 's|/.*||' | sed 's|:.*||')
# 生成配置条目
cat >> "$endpoint_file" << EOF
- targets:
- "$target"
labels:
endpoint: "$label"
EOF
done
log_success "已生成探测目标配置: $endpoint_file"
log_info "配置了 ${#TARGET_ARRAY[@]} 个探测目标"
else
# 下载默认配置
if [ ! -f "$endpoint_file" ] || confirm "探测目标配置已存在,是否覆盖?"; then
safe_download \
"https://raw.githubusercontent.com/Sm1rkBoy/ProbeShell/main/vmagent/endpoint.yml" \
"$endpoint_file" \
"默认探测目标配置" || return 1
fi
fi
return 0
}
# 获取 vmagent 配置
get_vmagent_config() {
if [ -z "$MAIN_DOMAIN" ]; then
echo ""
log_info "VictoriaMetrics写入地址一般是<ip>:8428,如果有反代输入域名即可"
read -p "请输入 VictoriaMetrics 写入地址: " MAIN_DOMAIN
fi
if [ -z "$INSTANCE_NAME" ]; then
read -p "请输入VPS名称(如: GreenCloud.JP.6666): " INSTANCE_NAME
fi
if [ -z "$VM_USERNAME" ]; then
read -p "请输入 VictoriaMetrics 用户名: " VM_USERNAME
fi
if [ -z "$VM_PASSWORD" ]; then
read -sp "请输入 VictoriaMetrics 密码: " VM_PASSWORD
echo ""
fi
if [ "$CACHE_SIZE_SET" = false ]; then
echo ""
log_info "vmagent 离线缓存设置(连接不上服务器时本地缓存数据)"
read -p "请输入缓存大小 (默认: 512MiB, 示例: 1GB, 2GB): " user_cache_size
if [ -n "$user_cache_size" ]; then
CACHE_SIZE="$user_cache_size"
fi
fi
# 验证配置
validate_vmagent_config || exit 1
}
install_vmagent() {
print_header "安装 vmagent"
if is_component_installed "vmagent" && ! $DRY_RUN; then
log_warn "vmagent 已安装"
if ! confirm "是否重新安装?"; then
return 0
fi
uninstall_component "vmagent"
fi
# 获取配置参数
get_vmagent_config
local archive="vmutils-${ARCH_SUFFIX}-v${VMAGENT_VERSION}.tar.gz"
local url="https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v${VMAGENT_VERSION}/${archive}"
safe_download "$url" "$archive" "vmutils" || return 1
extract_tarball "$archive" "vmutils" || return 1
if $DRY_RUN; then
log_info "[DRY RUN] 跳过安装 vmagent 二进制"
else
# 清理不需要的文件
rm -rf vmalert-prod vmalert-tool-prod vmauth-prod vmbackup-prod vmctl-prod vmrestore-prod 2>/dev/null || true
# 创建目录并移动文件
mkdir -p "$VMAGENT_PATH"
mv vmagent-prod "${VMAGENT_PATH}/vmagent" || return 1
chmod +x "${VMAGENT_PATH}/vmagent"
chown root:root "${VMAGENT_PATH}/vmagent"
fi
# 拼接 remote write URL
local remote_write_url="${MAIN_DOMAIN}/api/v1/write"
# 下载服务文件和配置
safe_download \
"https://raw.githubusercontent.com/Sm1rkBoy/ProbeShell/main/service/vmagent.service" \
"/etc/systemd/system/vmagent.service" \
"vmagent 服务文件" || return 1
safe_download \
"https://raw.githubusercontent.com/Sm1rkBoy/ProbeShell/main/vmagent/prometheus.yml" \
"${VMAGENT_PATH}/prometheus.yml" \
"vmagent 配置文件" || return 1
# 生成或下载探测目标配置
generate_blackbox_targets || return 1
if ! $DRY_RUN; then
# 替换服务文件中的配置
sed -i "s|-remoteWrite.url=.*|-remoteWrite.url=${remote_write_url}|g" /etc/systemd/system/vmagent.service
sed -i "s/-remoteWrite.basicAuth.username=VM_USERNAME/-remoteWrite.basicAuth.username=${VM_USERNAME}/g" /etc/systemd/system/vmagent.service
sed -i "s/-remoteWrite.basicAuth.password=VM_PASSWORD/-remoteWrite.basicAuth.password=${VM_PASSWORD}/g" /etc/systemd/system/vmagent.service
sed -i "s/-remoteWrite.maxDiskUsagePerURL=CACHE_SIZE/-remoteWrite.maxDiskUsagePerURL=${CACHE_SIZE}/g" /etc/systemd/system/vmagent.service
# 替换配置文件中的实例名
sed -i "s/\${instance_name}/${INSTANCE_NAME}/g" "${VMAGENT_PATH}/prometheus.yml"
chmod 644 /etc/systemd/system/vmagent.service
fi
log_success "vmagent 安装完成"
return 0
}
#==============================================================================
# Promtail 安装
#==============================================================================
# 获取 promtail 配置
get_promtail_config() {
if [ -z "$LOKI_DOMAIN" ]; then
read -p "请输入 Loki 写入地址: " LOKI_DOMAIN
fi
if [ -z "$INSTANCE_NAME" ]; then
read -p "请输入VPS名称(如: GreenCloud.JP.6666): " INSTANCE_NAME
fi
if [ -z "$VM_USERNAME" ]; then
read -p "请输入认证用户名 (可选,直接回车跳过): " VM_USERNAME
fi
if [ -z "$VM_PASSWORD" ] && [ -n "$VM_USERNAME" ]; then
read -sp "请输入认证密码: " VM_PASSWORD
echo ""
fi
# 验证配置
validate_promtail_config || exit 1
}
install_promtail() {
print_header "安装 promtail"
if is_component_installed "promtail" && ! $DRY_RUN; then
log_warn "promtail 已安装"
if ! confirm "是否重新安装?"; then
return 0
fi
uninstall_component "promtail"
fi
# 获取配置参数
get_promtail_config
local archive="promtail-${ARCH_SUFFIX}.zip"
local url="https://github.com/grafana/loki/releases/download/v${PROMTAIL_VERSION}/${archive}"
safe_download "$url" "$archive" "promtail" || return 1
if $DRY_RUN; then
log_info "[DRY RUN] 跳过解压和安装 promtail"
else
mkdir -p "$PROMTAIL_PATH"
unzip -o "$archive" || return 1
mv "promtail-${ARCH_SUFFIX}" "${PROMTAIL_PATH}/promtail" || return 1
chmod +x "${PROMTAIL_PATH}/promtail"
chown root:root "${PROMTAIL_PATH}/promtail"
TEMP_FILES+=("$archive")
fi
local loki_push_url="${LOKI_DOMAIN}/loki/api/v1/push"
# 下载服务文件和配置
safe_download \
"https://raw.githubusercontent.com/Sm1rkBoy/ProbeShell/main/service/promtail.service" \
"/etc/systemd/system/promtail.service" \
"promtail 服务文件" || return 1
safe_download \
"https://raw.githubusercontent.com/Sm1rkBoy/ProbeShell/main/promtail/promtail.yml" \
"${PROMTAIL_PATH}/promtail.yml" \
"promtail 配置文件" || return 1
if ! $DRY_RUN; then
# 替换配置
sed -i "s|instance: ''|instance: '${INSTANCE_NAME}'|g" "${PROMTAIL_PATH}/promtail.yml"
sed -i "s|url:|url: ${loki_push_url}|" "${PROMTAIL_PATH}/promtail.yml"
# 如果提供了认证信息,添加到配置
if [ -n "$VM_USERNAME" ] && [ -n "$VM_PASSWORD" ]; then
sed -i "s|username:|username: ${VM_USERNAME}|" "${PROMTAIL_PATH}/promtail.yml"
sed -i "s|password:|password: ${VM_PASSWORD}|" "${PROMTAIL_PATH}/promtail.yml"
fi
chmod 644 /etc/systemd/system/promtail.service
fi
log_success "promtail 安装完成"
return 0
}
#==============================================================================
# 组件启动和停止
#==============================================================================
# 启动组件服务
start_component() {
local component="$1"
local service="${SERVICE_MAP[$component]}"
if $DRY_RUN; then
log_info "[DRY RUN] 跳过启动服务: $service"
return 0
fi