修改脚本
This commit is contained in:
@@ -62,74 +62,24 @@ else
|
|||||||
echo "No TTY detected, using default port: $PORT"
|
echo "No TTY detected, using default port: $PORT"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Configure monitoring targets
|
# Create system user if not exists
|
||||||
if [ -d "/etc/prometheus" ]; then
|
if ! id -u blackbox_exporter >/dev/null 2>&1; then
|
||||||
TARGETS_DIR="/etc/prometheus/blackbox_targets"
|
echo "Creating blackbox_exporter system user..."
|
||||||
echo "Prometheus detected, targets will be saved to $TARGETS_DIR"
|
sudo useradd --no-create-home --shell /bin/false blackbox_exporter
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Set permissions and capabilities
|
||||||
|
echo "Setting permissions and capabilities..."
|
||||||
|
sudo chown -R blackbox_exporter:blackbox_exporter /blackbox_exporter
|
||||||
|
# Grant raw socket capability for ICMP probing
|
||||||
|
if command -v setcap >/dev/null 2>&1; then
|
||||||
|
sudo setcap 'cap_net_raw+ep' /blackbox_exporter/blackbox_exporter
|
||||||
else
|
else
|
||||||
TARGETS_DIR="/blackbox_exporter/targets"
|
echo "Warning: setcap not found. ICMP probing might require root."
|
||||||
fi
|
|
||||||
if [ -t 0 ]; then
|
|
||||||
sudo mkdir -p "$TARGETS_DIR"
|
|
||||||
echo "========================================================="
|
|
||||||
echo "Configuring monitoring objects (interactive menu)"
|
|
||||||
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|2|3)
|
|
||||||
case $CHOICE in
|
|
||||||
1) TYPE="icmp"; DESC="ICMP Ping"; EX="8.8.8.8";;
|
|
||||||
2) TYPE="http"; DESC="HTTP Status"; EX="https://google.com";;
|
|
||||||
3) TYPE="tcp"; DESC="TCP Connect"; EX="1.1.1.1:443";;
|
|
||||||
esac
|
|
||||||
|
|
||||||
read -rp "Clear existing $DESC targets? [y/N]: " CLEAR_EXIST
|
|
||||||
[[ "$CLEAR_EXIST" =~ ^[Yy]$ ]] && sudo rm -f "$TARGETS_DIR/$TYPE.yml"
|
|
||||||
|
|
||||||
while true; do
|
|
||||||
echo "--- Adding $DESC Object ---"
|
|
||||||
read -rp "Enter target (e.g. $EX, leave empty to stop): " T_VAL
|
|
||||||
[ -z "$T_VAL" ] && break
|
|
||||||
read -rp "Enter a descriptive name for this target: " T_NAME
|
|
||||||
|
|
||||||
[ ! -f "$TARGETS_DIR/$TYPE.yml" ] && sudo touch "$TARGETS_DIR/$TYPE.yml"
|
|
||||||
sudo tee -a "$TARGETS_DIR/$TYPE.yml" > /dev/null <<EOF
|
|
||||||
- targets: ['$T_VAL']
|
|
||||||
labels:
|
|
||||||
instance_name: '$T_NAME'
|
|
||||||
EOF
|
|
||||||
echo "Added $T_VAL ($T_NAME)"
|
|
||||||
done
|
|
||||||
;;
|
|
||||||
4)
|
|
||||||
echo "Current Target Configuration in $TARGETS_DIR:"
|
|
||||||
for f in "$TARGETS_DIR"/*.yml; do
|
|
||||||
[ -e "$f" ] || continue
|
|
||||||
echo "--- $(basename "$f") ---"
|
|
||||||
sudo cat "$f"
|
|
||||||
done
|
|
||||||
;;
|
|
||||||
5)
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "Invalid choice. Please try again."
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
echo "---------------------------------------------------------"
|
|
||||||
done
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Create systemd service file
|
# Create systemd service file
|
||||||
SERVICE_FILE="/etc/systemd/system/blackbox_exporter.service"
|
SERVICE_FILE="/etc/systemd/system/blackbox_exporter.service"
|
||||||
echo "Creating systemd service file..."
|
echo "Creating systemd service file..."
|
||||||
@@ -140,10 +90,15 @@ Wants=network-online.target
|
|||||||
After=network-online.target
|
After=network-online.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
User=root
|
User=blackbox_exporter
|
||||||
|
Group=blackbox_exporter
|
||||||
WorkingDirectory=/blackbox_exporter
|
WorkingDirectory=/blackbox_exporter
|
||||||
ExecStart=/blackbox_exporter/blackbox_exporter --config.file=/blackbox_exporter/blackbox.yml --web.listen-address=":${PORT}"
|
ExecStart=/blackbox_exporter/blackbox_exporter --config.file=/blackbox_exporter/blackbox.yml --web.listen-address=":${PORT}"
|
||||||
Restart=always
|
Restart=always
|
||||||
|
NoNewPrivileges=true
|
||||||
|
PrivateTmp=true
|
||||||
|
ProtectSystem=full
|
||||||
|
ProtectHome=true
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
@@ -154,69 +109,9 @@ echo "Enabling and starting blackbox_exporter service..."
|
|||||||
sudo systemctl daemon-reload
|
sudo systemctl daemon-reload
|
||||||
sudo systemctl enable --now blackbox_exporter.service
|
sudo systemctl enable --now blackbox_exporter.service
|
||||||
|
|
||||||
# Automatically integrate with Prometheus if present
|
|
||||||
PROMETHEUS_YML="/etc/prometheus/prometheus.yml"
|
|
||||||
if [ -f "$PROMETHEUS_YML" ]; then
|
|
||||||
echo "Integrating with Prometheus..."
|
|
||||||
TEMP_CONF="/tmp/blackbox_jobs.yml"
|
|
||||||
rm -f "$TEMP_CONF"
|
|
||||||
|
|
||||||
# Mapping table for modules
|
|
||||||
# icmp -> icmp
|
|
||||||
# http -> http_2xx
|
|
||||||
# tcp -> tcp_connect
|
|
||||||
|
|
||||||
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 <<EOF >> "$TEMP_CONF"
|
|
||||||
- job_name: 'blackbox-${type}'
|
|
||||||
metrics_path: /probe
|
|
||||||
params:
|
|
||||||
module: [$MODULE_NAME]
|
|
||||||
file_sd_configs:
|
|
||||||
- files:
|
|
||||||
- $TARGETS_DIR/${type}.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
|
|
||||||
done
|
|
||||||
|
|
||||||
if [ -f "$TEMP_CONF" ]; then
|
|
||||||
if [ -t 0 ]; then
|
|
||||||
read -rp "Found $PROMETHEUS_YML. Would you like to append the new blackbox jobs automatically? [y/N]: " DO_APPEND
|
|
||||||
else
|
|
||||||
DO_APPEND="y"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$DO_APPEND" =~ ^[Yy]$ ]]; then
|
|
||||||
cat "$TEMP_CONF" | sudo tee -a "$PROMETHEUS_YML" > /dev/null
|
|
||||||
echo "Jobs appended to $PROMETHEUS_YML."
|
|
||||||
if [ -f "/usr/bin/restart_prometheus" ]; then
|
|
||||||
echo "Restarting Prometheus..."
|
|
||||||
sudo /usr/bin/restart_prometheus
|
|
||||||
else
|
|
||||||
echo "Please restart Prometheus to apply changes."
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo "No new Blackbox jobs to add to $PROMETHEUS_YML."
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "========================================================="
|
echo "========================================================="
|
||||||
echo "Blackbox Exporter installation and integration completed!"
|
echo "Blackbox Exporter installation completed!"
|
||||||
echo "Listening on port: ${PORT}"
|
echo "Listening on port: ${PORT}"
|
||||||
|
echo "Config file: /blackbox_exporter/blackbox.yml"
|
||||||
echo "========================================================="
|
echo "========================================================="
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user