From dc2e895e4534c85d1b2272729208e8aede064ffb Mon Sep 17 00:00:00 2001 From: CN-JS-HuiBai Date: Sun, 5 Apr 2026 23:19:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Prometheus/install_blackbox_exporter.sh | 120 ++++++++++++++---------- 1 file changed, 70 insertions(+), 50 deletions(-) diff --git a/Prometheus/install_blackbox_exporter.sh b/Prometheus/install_blackbox_exporter.sh index a812707..2b4c2d0 100644 --- a/Prometheus/install_blackbox_exporter.sh +++ b/Prometheus/install_blackbox_exporter.sh @@ -70,29 +70,58 @@ else TARGETS_DIR="/blackbox_exporter/targets" fi if [ -t 0 ]; then + sudo mkdir -p "$TARGETS_DIR" + echo "=========================================================" + echo "Configuring monitoring objects (interactive menu)" echo "=========================================================" - echo "Configuring monitoring objects (optional)..." - read -rp "Enter targets for ICMP Ping (separated by space, e.g. 8.8.8.8 1.1.1.1): " ICMP_VAL - read -rp "Enter targets for HTTP check (separated by space, e.g. https://google.com): " HTTP_VAL - if [ -n "$ICMP_VAL" ] || [ -n "$HTTP_VAL" ]; then - sudo mkdir -p "$TARGETS_DIR" - if [ -n "$ICMP_VAL" ]; then - echo "- targets:" | sudo tee "$TARGETS_DIR/icmp.yml" > /dev/null - for t in $ICMP_VAL; do - echo " - $t" | sudo tee -a "$TARGETS_DIR/icmp.yml" > /dev/null - done - echo "Done: $TARGETS_DIR/icmp.yml" - fi - if [ -n "$HTTP_VAL" ]; then - echo "- targets:" | sudo tee "$TARGETS_DIR/http.yml" > /dev/null - for t in $HTTP_VAL; do - echo " - $t" | sudo tee -a "$TARGETS_DIR/http.yml" > /dev/null - done - echo "Done: $TARGETS_DIR/http.yml" - fi - fi - echo "=========================================================" + while true; do + echo "Choose a monitoring type to configure:" + echo "1) ICMP (Ping)" + echo "2) HTTP Status (2xx)" + echo "3) TCP Connect (Port Check)" + echo "4) Show Current Configuration" + echo "5) Finish Configuration" + read -rp "Your choice [1-5]: " CHOICE + + case $CHOICE in + 1) + read -rp "Enter ICMP targets (space separated, e.g. 8.8.8.8 1.1.1.1): " VAL + if [ -n "$VAL" ]; then + echo "- targets:" | sudo tee "$TARGETS_DIR/icmp.yml" > /dev/null + for t in $VAL; do echo " - $t" | sudo tee -a "$TARGETS_DIR/icmp.yml" > /dev/null; done + echo "Saved to: $TARGETS_DIR/icmp.yml" + fi + ;; + 2) + read -rp "Enter HTTP targets (space separated, e.g. https://google.com): " VAL + if [ -n "$VAL" ]; then + echo "- targets:" | sudo tee "$TARGETS_DIR/http.yml" > /dev/null + for t in $VAL; do echo " - $t" | sudo tee -a "$TARGETS_DIR/http.yml" > /dev/null; done + echo "Saved to: $TARGETS_DIR/http.yml" + fi + ;; + 3) + read -rp "Enter TCP targets (space separated, e.g. 1.1.1.1:443 8.8.8.8:53): " VAL + if [ -n "$VAL" ]; then + echo "- targets:" | sudo tee "$TARGETS_DIR/tcp.yml" > /dev/null + for t in $VAL; do echo " - $t" | sudo tee -a "$TARGETS_DIR/tcp.yml" > /dev/null; done + echo "Saved to: $TARGETS_DIR/tcp.yml" + fi + ;; + 4) + echo "Current Target Files in $TARGETS_DIR:" + ls -l "$TARGETS_DIR"/*.yml 2>/dev/null || echo "No targets configured yet." + ;; + 5) + break + ;; + *) + echo "Invalid choice. Please try again." + ;; + esac + echo "---------------------------------------------------------" + done fi @@ -127,36 +156,26 @@ if [ -f "$PROMETHEUS_YML" ]; then TEMP_CONF="/tmp/blackbox_jobs.yml" rm -f "$TEMP_CONF" - # Generate the job snippets - if [ -f "$TARGETS_DIR/icmp.yml" ] && ! grep -q "job_name: 'blackbox-icmp'" "$PROMETHEUS_YML"; then - cat <> "$TEMP_CONF" - - job_name: 'blackbox-icmp' - metrics_path: /probe - params: - module: [icmp] - file_sd_configs: - - files: - - $TARGETS_DIR/icmp.yml - relabel_configs: - - source_labels: [__address__] - target_label: __param_target - - source_labels: [__param_target] - target_label: instance - - target_label: __address__ - replacement: 127.0.0.1:${PORT} - -EOF - fi + # Mapping table for modules + # icmp -> icmp + # http -> http_2xx + # tcp -> tcp_connect - if [ -f "$TARGETS_DIR/http.yml" ] && ! grep -q "job_name: 'blackbox-http'" "$PROMETHEUS_YML"; then - cat <> "$TEMP_CONF" - - job_name: 'blackbox-http' + for type in icmp http tcp; do + MODULE_NAME="$type" + [ "$type" == "http" ] && MODULE_NAME="http_2xx" + [ "$type" == "tcp" ] && MODULE_NAME="tcp_connect" + + if [ -f "$TARGETS_DIR/${type}.yml" ] && ! grep -q "job_name: 'blackbox-${type}'" "$PROMETHEUS_YML"; then + echo "Adding job: blackbox-${type}" + cat <> "$TEMP_CONF" + - job_name: 'blackbox-${type}' metrics_path: /probe params: - module: [http_2xx] + module: [$MODULE_NAME] file_sd_configs: - files: - - $TARGETS_DIR/http.yml + - $TARGETS_DIR/${type}.yml relabel_configs: - source_labels: [__address__] target_label: __param_target @@ -166,11 +185,12 @@ EOF replacement: 127.0.0.1:${PORT} EOF - fi + fi + done if [ -f "$TEMP_CONF" ]; then if [ -t 0 ]; then - read -rp "Found $PROMETHEUS_YML. Would you like to append the blackbox jobs automatically? [y/N]: " DO_APPEND + read -rp "Found $PROMETHEUS_YML. Would you like to append the new blackbox jobs automatically? [y/N]: " DO_APPEND else DO_APPEND="y" fi @@ -182,11 +202,11 @@ EOF echo "Restarting Prometheus..." sudo /usr/bin/restart_prometheus else - echo "Please restart Prometheus to apply changes: sudo systemctl restart prometheus" + echo "Please restart Prometheus to apply changes." fi fi else - echo "Blackbox jobs already exist in $PROMETHEUS_YML or no targets configured." + echo "No new Blackbox jobs to add to $PROMETHEUS_YML." fi fi