每日shell脚本之自动配置Prometheus.yml并支持选择监控的节点数量、输入自定义IP和端口设置
bash
#!/bin/bash
# 获取用户输入的节点数量
read -p "请输入要监控的节点数量: " node_count
# 生成Prometheus.yml配置文件
cat << EOF > Prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
EOF
for ((i=1; i<=$node_count; i++))
do
read -p "请输入第 $i 个节点的IP地址: " node_ip
read -p "请输入第 $i 个节点的端口号: " node_port
cat << EOF >> Prometheus.yml
- job_name: 'node$i'
static_configs:
- targets: ['$node_ip:$node_port']
EOF
done
cat << EOF >> Prometheus.yml
storage:
local:
path: /var/lib/prometheus
rule_files:
- /etc/prometheus/alert.rules
remote_write:
- url: http://remote-write-url:8080/api/v1/write
write_relabel_configs:
- source_labels: [__name__]
regex: '(.*)'
target_label: __name__
replacement: 'new_metric_name'
EOF
# 启动Prometheus进程
./prometheus --config.file=Prometheus.yml &