refactor pihole-exporter

This commit is contained in:
CrazyWolf13 2025-12-09 10:52:19 +01:00
parent 6478d0d647
commit 76097c1e02

View File

@ -8,6 +8,7 @@
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main/misc/core.func) source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main/misc/core.func)
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main/misc/tools.func) source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main/misc/tools.func)
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main/misc/build.func) source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main/misc/build.func)
source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main/misc/error_handler.func)
load_functions load_functions
# Enable error handling # Enable error handling
@ -43,75 +44,78 @@ fi
# ============================================================================== # ==============================================================================
# UNINSTALL # UNINSTALL
# ============================================================================== # ==============================================================================
if [[ -d "$INSTALL_PATH" ]]; then function uninstall() {
echo -e "${YW}⚠️ pihole-exporter is already installed.${CL}" msg_info "Uninstalling Pihole-Exporter"
echo -n "Uninstall ${APP}? (y/N): " if [[ "$OS" == "Alpine" ]]; then
read -r uninstall_prompt rc-service pihole-exporter stop &>/dev/null
if [[ "${uninstall_prompt,,}" =~ ^(y|yes)$ ]]; then rc-update del pihole-exporter &>/dev/null
msg_info "Uninstalling pihole-exporter" rm -f "$SERVICE_PATH"
if [[ "$OS" == "Debian" ]]; then else
systemctl disable --now pihole-exporter.service &>/dev/null systemctl disable -q --now pihole-exporter
rm -f "$SERVICE_PATH" rm -f "$SERVICE_PATH"
else
rc-service pihole-exporter stop &>/dev/null
rc-update del pihole-exporter &>/dev/null
rm -f "$SERVICE_PATH"
fi
rm -rf "$INSTALL_PATH" "$CONFIG_PATH" ~/.pihole-exporter
msg_ok "${APP} has been uninstalled."
exit 0
fi fi
rm -rf "$INSTALL_PATH" "$CONFIG_PATH"
rm -f "/usr/local/bin/update_pihole-exporter"
rm -f "$HOME/.pihole-exporter"
msg_ok "Pihole-Exporter has been uninstalled"
}
# ============================================================================== # ==============================================================================
# UPDATE # UPDATE
# ============================================================================== # ==============================================================================
function update() {
echo -n "Update pihole-exporter? (y/N): " if check_for_gh_release "pihole-exporter" "eko/pihole-exporter"; then
read -r update_prompt msg_info "Stopping service"
if [[ "${update_prompt,,}" =~ ^(y|yes)$ ]]; then if [[ "$OS" == "Alpine" ]]; then
if check_for_gh_release "pihole-exporter" "eko/pihole-exporter"; then rc-service pihole-exporter stop &>/dev/null
fetch_and_deploy_gh_release "pihole-exporter" "eko/pihole-exporter" else
setup_go systemctl stop pihole-exporter
msg_info "Updating pihole-exporter"
cd /opt/pihole-exporter/
$STD /usr/local/bin/go build -o ./pihole-exporter
msg_ok "Updated Successfully!"
fi fi
exit 0 msg_ok "Stopped service"
else
echo -e "${YW}⚠️ Update skipped. Exiting.${CL}" fetch_and_deploy_gh_release "pihole-exporter" "eko/pihole-exporter" "tarball" "latest"
exit 0 setup_go
msg_info "Building Pihole-Exporter"
cd /opt/pihole-exporter/
$STD /usr/local/bin/go build -o ./pihole-exporter
msg_ok "Built Pihole-Exporter"
msg_info "Starting service"
if [[ "$OS" == "Alpine" ]]; then
rc-service pihole-exporter start
else
systemctl start pihole-exporter
fi
msg_ok "Started service"
msg_ok "Updated successfully"
exit
fi fi
fi }
# ============================================================================== # ==============================================================================
# INSTALL # INSTALL
# ============================================================================== # ==============================================================================
echo -e "${YW}⚠️ pihole-exporter is not installed.${CL}" function install() {
read -erp "Enter the protocol to use (http/https), default https: " pihole_PROTOCOL read -erp "Enter the protocol to use (http/https), default https: " pihole_PROTOCOL
read -erp "Enter the hostname of Pihole, example: (127.0.0.1): " pihole_HOSTNAME read -erp "Enter the hostname of Pihole, example: (127.0.0.1): " pihole_HOSTNAME
read -erp "Enter the port of Pihole, default 443: " pihole_PORT read -erp "Enter the port of Pihole, default 443: " pihole_PORT
read -rsp "Enter Pihole password: " pihole_PASSWORD read -rsp "Enter Pihole password: " pihole_PASSWORD
printf "\n" printf "\n"
read -erp "Do you want to skip TLS-Verification (if using a self-signed Certificate on Pi-Hole) [y/N]: " SKIP_TLS read -erp "Do you want to skip TLS-Verification (if using a self-signed Certificate on Pi-Hole) [y/N]: " SKIP_TLS
read -erp "Install qbittorrent-exporter? (y/n): " install_prompt if [[ "${SKIP_TLS,,}" =~ ^(y|yes)$ ]]; then
if [[ "${SKIP_TLS,,}" =~ ^(y|yes)$ ]]; then pihole_SKIP_TLS="true"
pihole_SKIP_TLS="true" fi
fi
if ! [[ "${install_prompt,,}" =~ ^(y|yes)$ ]]; then
echo -e "${YW}⚠️ Installation skipped. Exiting.${CL}"
exit 0
fi
fetch_and_deploy_gh_release "pihole-exporter" "eko/pihole-exporter" "tarball" "latest" fetch_and_deploy_gh_release "pihole-exporter" "eko/pihole-exporter" "tarball" "latest"
setup_go setup_go
msg_info "Installing pihole-exporter on ${OS}" msg_info "Building Pihole-Exporter on ${OS}"
cd /opt/pihole-exporter/ cd /opt/pihole-exporter/
$STD /usr/local/bin/go build -o ./pihole-exporter $STD /usr/local/bin/go build -o ./pihole-exporter
msg_ok "Installed pihole-exporter" msg_ok "Built Pihole-Exporter"
msg_info "Creating configuration" msg_info "Creating configuration"
cat <<EOF >"$CONFIG_PATH" cat <<EOF >"$CONFIG_PATH"
# https://github.com/eko/pihole-exporter/?tab=readme-ov-file#available-cli-options # https://github.com/eko/pihole-exporter/?tab=readme-ov-file#available-cli-options
PIHOLE_PASSWORD="${pihole_PASSWORD}" PIHOLE_PASSWORD="${pihole_PASSWORD}"
PIHOLE_HOSTNAME="${pihole_HOSTNAME}" PIHOLE_HOSTNAME="${pihole_HOSTNAME}"
@ -119,11 +123,11 @@ PIHOLE_PORT="${pihole_PORT:-443}"
SKIP_TLS_VERIFICATION="${pihole_SKIP_TLS:-false}" SKIP_TLS_VERIFICATION="${pihole_SKIP_TLS:-false}"
PIHOLE_PROTOCOL="${pihole_PROTOCOL:-https}" PIHOLE_PROTOCOL="${pihole_PROTOCOL:-https}"
EOF EOF
msg_ok "Created configuration" msg_ok "Created configuration"
msg_info "Creating service" msg_info "Creating service"
if [[ "$OS" == "Debian" ]]; then if [[ "$OS" == "Debian" ]]; then
cat <<EOF >"$SERVICE_PATH" cat <<EOF >"$SERVICE_PATH"
[Unit] [Unit]
Description=pihole-exporter Description=pihole-exporter
After=network.target After=network.target
@ -138,19 +142,24 @@ Restart=always
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target
EOF EOF
systemctl enable -q --now pihole-exporter systemctl daemon-reload
else systemctl enable -q --now pihole-exporter
cat <<EOF >"$SERVICE_PATH" else
cat <<EOF >"$SERVICE_PATH"
#!/sbin/openrc-run #!/sbin/openrc-run
command="$INSTALL_PATH" name="pihole-exporter"
command_args="" description="Pi-hole Exporter for Prometheus"
command="${INSTALL_PATH}/pihole-exporter"
command_background=true command_background=true
directory="/opt/pihole-exporter" directory="/opt/pihole-exporter"
pidfile="/opt/pihole-exporter/pidfile" pidfile="/run/\${RC_SVCNAME}.pid"
output_log="/var/log/pihole-exporter.log"
error_log="/var/log/pihole-exporter.log"
depend() { depend() {
need net need net
after firewall
} }
start_pre() { start_pre() {
@ -159,10 +168,82 @@ start_pre() {
fi fi
} }
EOF EOF
chmod +x "$SERVICE_PATH" chmod +x "$SERVICE_PATH"
rc-update add pihole-exporter default &>/dev/null rc-update add pihole-exporter default
rc-service pihole-exporter start &>/dev/null rc-service pihole-exporter start
fi fi
msg_ok "Service created successfully" msg_ok "Created and started service"
echo -e "${CM} ${GN}${APP} is reachable at: ${BL}http://$CURRENT_IP:9617/metrics${CL}" # Create update script
msg_info "Creating update script"
cat <<'UPDATEEOF' >/usr/local/bin/update_pihole-exporter
#!/usr/bin/env bash
# pihole-exporter Update Script
type=update bash -c "$(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main/tools/addon/pihole-exporter.sh)"
UPDATEEOF
chmod +x /usr/local/bin/update_pihole-exporter
msg_ok "Created update script (/usr/local/bin/update_pihole-exporter)"
echo ""
msg_ok "Pihole-Exporter installed successfully"
msg_ok "Metrics: ${BL}http://${CURRENT_IP}:${DEFAULT_PORT}/metrics${CL}"
msg_ok "Config: ${BL}${CONFIG_PATH}${CL}"
}
# ==============================================================================
# MAIN
# ==============================================================================
header_info
ensure_usr_local_bin_persist
get_current_ip &>/dev/null
# Handle type=update (called from update script)
if [[ "${type:-}" == "update" ]]; then
if [[ -d "$INSTALL_PATH" && -f "$INSTALL_PATH/pihole-exporter" ]]; then
update
else
msg_error "Pihole-Exporter is not installed. Nothing to update."
exit 1
fi
exit 0
fi
# Check if already installed
if [[ -d "$INSTALL_PATH" && -f "$INSTALL_PATH/pihole-exporter" ]]; then
msg_warn "Pihole-Exporter is already installed."
echo ""
echo -n "${TAB}Uninstall Pihole-Exporter? (y/N): "
read -r uninstall_prompt
if [[ "${uninstall_prompt,,}" =~ ^(y|yes)$ ]]; then
uninstall
exit 0
fi
echo -n "${TAB}Update Pihole-Exporter? (y/N): "
read -r update_prompt
if [[ "${update_prompt,,}" =~ ^(y|yes)$ ]]; then
update
exit 0
fi
msg_warn "No action selected. Exiting."
exit 0
fi
# Fresh installation
msg_warn "Pihole-Exporter is not installed."
echo ""
echo -e "${TAB}${INFO} This will install:"
echo -e "${TAB} - Pi-hole Exporter (Go binary)"
echo -e "${TAB} - Systemd/OpenRC service"
echo ""
echo -n "${TAB}Install Pihole-Exporter? (y/N): "
read -r install_prompt
if [[ "${install_prompt,,}" =~ ^(y|yes)$ ]]; then
install
else
msg_warn "Installation cancelled. Exiting."
exit 0
fi