Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jpmurray/57ae0bc3128c9937159c250512846ae7 to your computer and use it in GitHub Desktop.

Select an option

Save jpmurray/57ae0bc3128c9937159c250512846ae7 to your computer and use it in GitHub Desktop.
install-NUT-on-proxmox.sh
#!/usr/bin/env bash
set -euo pipefail
# =========================================
# Proxmox NUT client setup for Synology UPS
# Script based on forked gist's instructions.
# =========================================
# --- Configurable variables ---
UPS_NAME="${UPS_NAME:-ups}"
NAS_IP="${NAS_IP:-192.168.1.28}"
POWERVALUE="${POWERVALUE:-1}"
NUT_USER="${NUT_USER:-monuser}"
NUT_PASS="${NUT_PASS:-secret}"
ROLE="${ROLE:-slave}"
NUT_CONF="/etc/nut/nut.conf"
UPSMON_CONF="/etc/nut/upsmon.conf"
TMPFILES_CONF="/usr/lib/tmpfiles.d/nut-common-tmpfiles.conf"
MONITOR_LINE="MONITOR ${UPS_NAME}@${NAS_IP} ${POWERVALUE} ${NUT_USER} ${NUT_PASS} ${ROLE}"
require_root() {
if [[ "${EUID}" -ne 0 ]]; then
echo "This script must be run as root."
echo "Try: sudo bash $0"
exit 1
fi
}
backup_file() {
local file="$1"
if [[ -f "$file" ]]; then
cp -a "$file" "${file}.bak.$(date +%Y%m%d-%H%M%S)"
fi
}
install_nut_client() {
echo "Installing nut-client..."
apt-get update
apt-get install -y nut-client
}
create_tmpfiles_override() {
echo "Creating empty tmpfiles override at ${TMPFILES_CONF} ..."
mkdir -p "$(dirname "$TMPFILES_CONF")"
: > "$TMPFILES_CONF"
chmod 0644 "$TMPFILES_CONF"
}
set_nut_mode() {
echo "Configuring ${NUT_CONF} ..."
backup_file "$NUT_CONF"
if [[ -f "$NUT_CONF" ]] && grep -qE '^\s*MODE=' "$NUT_CONF"; then
sed -i 's|^\s*MODE=.*|MODE=netclient|' "$NUT_CONF"
else
printf '\nMODE=netclient\n' >> "$NUT_CONF"
fi
}
set_upsmon_monitor() {
echo "Configuring ${UPSMON_CONF} ..."
backup_file "$UPSMON_CONF"
touch "$UPSMON_CONF"
sed -i '/^\s*MONITOR\s\+/d' "$UPSMON_CONF"
printf '\n%s\n' "$MONITOR_LINE" >> "$UPSMON_CONF"
}
enable_and_start_services() {
echo "Reloading systemd tmpfiles..."
systemd-tmpfiles --create || true
echo "Enabling and starting nut-monitor..."
systemctl enable nut-monitor
systemctl restart nut-monitor
}
show_status() {
echo
echo "===== Service status ====="
systemctl --no-pager --full status nut-client || true
echo
systemctl --no-pager --full status nut-monitor || true
}
test_upsc() {
echo
echo "===== Testing UPS connectivity ====="
if command -v upsc >/dev/null 2>&1; then
upsc "${UPS_NAME}@${NAS_IP}" || {
echo
echo "upsc test failed."
echo "Check:"
echo " - Synology NUT server is enabled"
echo " - NAS IP is correct"
echo " - Username/password are correct"
echo " - Firewall allows TCP 3493"
return 1
}
else
echo "upsc command not found."
return 1
fi
}
print_summary() {
echo
echo "===== Configuration applied ====="
echo "NUT mode : netclient"
echo "UPS name : ${UPS_NAME}"
echo "NAS IP : ${NAS_IP}"
echo "Power value : ${POWERVALUE}"
echo "NUT username : ${NUT_USER}"
echo "Role : ${ROLE}"
echo "Tmpfiles fix : ${TMPFILES_CONF}"
echo
echo "Monitor line:"
echo " ${MONITOR_LINE}"
echo
echo "Manual verification:"
echo " upsc ${UPS_NAME}@${NAS_IP}"
echo
echo "On the Synology NAS:"
echo " upsc -c ${UPS_NAME}"
}
main() {
require_root
install_nut_client
create_tmpfiles_override
set_nut_mode
set_upsmon_monitor
enable_and_start_services
show_status
test_upsc || true
print_summary
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment