No description
  • Python 57.6%
  • Shell 41.5%
  • Dockerfile 0.9%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-06-17 16:48:21 -05:00
config Normalize registry metadata 2026-06-09 23:36:09 -05:00
grafana/dashboards/power Add flexible UPS configuration 2026-06-09 18:45:00 -05:00
peanut Initial UPS monitoring stack 2026-06-08 21:14:20 -05:00
.dockerignore Add flexible UPS configuration 2026-06-09 18:45:00 -05:00
.env.example Harden multi-UPS USB recovery 2026-06-17 16:48:21 -05:00
.gitignore Add startup UPS registry discovery 2026-06-09 23:27:36 -05:00
CHANGELOG.md Harden multi-UPS USB recovery 2026-06-17 16:48:21 -05:00
docker-compose.yml Harden multi-UPS USB recovery 2026-06-17 16:48:21 -05:00
docker-entrypoint.sh Harden multi-UPS USB recovery 2026-06-17 16:48:21 -05:00
Dockerfile Harden multi-UPS USB recovery 2026-06-17 16:48:21 -05:00
nut-healthcheck.sh Harden multi-UPS USB recovery 2026-06-17 16:48:21 -05:00
nut-registry.py Add GOLDENMATE UPS registry recovery 2026-06-17 16:30:13 -05:00
nut-resilient-exporter.py Harden multi-UPS USB recovery 2026-06-17 16:48:21 -05:00
README.md Harden multi-UPS USB recovery 2026-06-17 16:48:21 -05:00

NUT UPSD

This project runs a reusable Docker Compose service for Network UPS Tools (NUT). It is intended for the machine that has one or more UPS devices physically attached by USB, while still exposing data to Prometheus/Grafana, Homepage, Home Assistant, and other NUT clients.

The default service is generic:

  • NUT/upsd on TCP 3493
  • Prometheus NUT exporter on TCP 9995
  • PeaNUT web UI on TCP 3494
  • USB HID UPS support through usbhid-ups
  • one or more UPS definitions from the startup registry, .env, NUT USB auto-discovery, or a custom config/ups.conf

Start

cp .env.example .env
chmod 0600 .env

Edit .env, replacing all change-me passwords. Optionally set NUT_HOST_LABEL to a stable short hostname. The default UPS_REGISTRY_ENABLED=true mode scans USB UPS devices on startup and maintains config/ups-registry.conf.

Bind-mounted app data is owned as ${APP_UID:-1000}:${APP_GID:-1000} by default. Change APP_UID and APP_GID if the host account that should edit ./config and ./peanut uses different IDs.

docker compose build
docker compose up -d
docker compose ps

On first startup, the registry assigns UPS names from the detected model. With NUT_HOST_LABEL=smoak, for example, discovered names may look like smoak-ups850, smoak-ups1350, and smoak-goldenmate-ups.

Verify

docker compose logs -f nut
docker compose exec nut upsc -l localhost
docker compose exec nut nut-healthcheck.sh
docker compose exec nut upsc "<ups-name>@localhost" ups.status
docker compose exec nut upscmd -l "<ups-name>@localhost"
curl -fsS "http://127.0.0.1:9995/metrics?target=nut:3493"

Useful NUT values include ups.status, battery.charge, battery.runtime, input.voltage, and ups.load.

Startup Registry Discovery

Registry mode is intended to avoid hand-maintaining ups.conf for normal USB UPS setups:

UPS_REGISTRY_ENABLED=true
UPS_REGISTRY_FILE=/config/ups-registry.conf
UPS_REGISTRY_STATE_FILE=/config/ups-registry.state
UPS_REGISTRY_AUTO_ADD=true

On every container start, the entrypoint runs nut-scanner -U -N, supplements the scan with USB HID UPS-like devices from /sys/bus/usb, merges the result with config/ups-registry.conf, and writes the generated runtime configuration to /etc/nut/ups.conf. The fallback recognizes common UPS vendors and HID smart-battery devices such as GOLDENMATE lithium UPS units that identify as -BMS- Smart-Battery.

The registry file is the persistent source of truth:

  • A newly scanned UPS is added to config/ups-registry.conf automatically.
  • A known UPS that is scanned again is started with its assigned registry name.
  • A known UPS that is not scanned is exposed through dummy-ups with ups.status=OFF and an alarm noting that it was not detected.
  • Missing placeholder devices emit zero/default values for charge, runtime, load, nominal power, and input/output voltage so Prometheus/Grafana keeps the same metric series shape while the UPS is offline.
  • Missing placeholder devices are not monitored by upsmon, so a disconnected known device does not trigger shutdown handling.
  • To rename a UPS, rename its section in config/ups-registry.conf and restart.
  • To remove a UPS, delete its section from config/ups-registry.conf and restart.

The registry uses normal NUT option names. You can add device-specific driver options to a section, and they will be preserved when the registry is rewritten. config/ups-registry.state records identities that have already been seen so a deleted registry section is not immediately re-added while the device is still connected.

For devices whose raw USB descriptor fields are misleading, the registry keeps human-readable vendor, product, serial, and desc values while writing raw matching filters as match.vendor, match.product, and match.serial. The generated runtime /etc/nut/ups.conf maps those match.* fields back to NUT's vendor, product, and serial driver options.

If you intentionally removed a registry section and later want that same device auto-added again, remove its matching line from config/ups-registry.state or delete the state file and restart.

The entrypoint starts UPS drivers one at a time, so one failed driver start does not prevent upsd from serving the UPS devices that did start. If a UPS disappears while the container is already running, the NUT healthcheck will fail for that device. By default, the entrypoint first restarts only the failed UPS driver and leaves upsd running, which keeps healthy UPS devices available while a stale USB driver handle is reclaimed. If that per-device recovery fails, then after three failed checks at 30 second intervals the container exits so Docker can restart it and the registry can rescan attached devices. Tune this with NUT_RESTART_FAILED_DRIVERS, NUT_DRIVER_RESTART_SETTLE, NUT_EXIT_ON_HEALTHCHECK_FAILURE, NUT_HEALTHCHECK_INTERVAL, and NUT_HEALTHCHECK_FAILURE_LIMIT. To list the currently failed UPS names without changing anything, run nut-healthcheck.sh --list-failed in the container. If the service started with one or more known UPS devices missing, it checks every NUT_RESCAN_MISSING_INTERVAL seconds for those devices to return. When one is seen again, the container exits so Docker can restart it and replace the placeholder with the real UPS driver.

One-Shot USB Discovery

NUT can generate ups.conf sections from attached USB UPS devices. To use that mode, set a stable host label, leave UPS_NAMES blank, and enable scanner discovery with registry mode disabled:

UPS_REGISTRY_ENABLED=false
NUT_HOST_LABEL=kate
UPS_NAMES=
UPS_AUTO_DISCOVER=true

The entrypoint runs nut-scanner -U -N, rewrites scanner section names into stable names, applies the default low-battery thresholds, and monitors every discovered section. The naming pattern is:

<host-label>-<model>

CyberPower CP1500PFCLCD becomes ups1500, for example kate-ups1500. If multiple discovered devices produce the same base name, a meaningful serial number is used as the suffix when available; otherwise the entrypoint falls back to numeric suffixes such as kate-ups850-1 and kate-ups850-2. If you have duplicate UPS models with placeholder serials and need physical-port stability, use explicit UPS_NAMES or config/ups.conf.

USB Discovery

Use these commands on the Docker host if you need to pin a UPS by USB vendor, product, or model string:

lsusb

for d in /sys/bus/usb/devices/*; do
    [ -f "$d/idVendor" ] || continue
    printf "%s " "$d"
    cat "$d/idVendor" "$d/idProduct" "$d/product" 2>/dev/null
done

Then set values such as:

UPS_VENDORID=0764
UPS_PRODUCTID=0501
UPS_PRODUCT=CP1500PFCLCD
UPS_DESC=Rack UPS

Leave those fields blank when one USB UPS is attached and port=auto is unambiguous.

Multiple UPS Devices

Use UPS_NAMES for one or more generated UPS definitions. Add any number of names separated by spaces, then define per-device variables using the UPS name uppercased with non-alphanumeric characters replaced by _.

UPS_NAMES=rack_ups desk-ups

UPS_RACK_UPS_DRIVER=usbhid-ups
UPS_RACK_UPS_PORT=auto
UPS_RACK_UPS_DESC=Rack UPS
UPS_RACK_UPS_VENDORID=0764
UPS_RACK_UPS_PRODUCTID=0501
UPS_RACK_UPS_LOW_RUNTIME=600

UPS_DESK_UPS_DRIVER=usbhid-ups
UPS_DESK_UPS_PORT=auto
UPS_DESK_UPS_DESC=Desk UPS

The default UPS_NAME, UPS_DRIVER, UPS_PORT, and related variables are kept for simple one-UPS setups and compatibility with older .env files. They apply when UPS_NAMES includes the same value as UPS_NAME.

The older UPS2_* variables are still supported for existing deployments, but new configurations should prefer UPS_NAMES because it scales without adding more numbered variables.

The NUT container reads ${NUT_ENV_FILE:-.env} so arbitrary per-device UPS_<NAME>_* variables are available inside the container. If you want to keep those variables in a NUT-only file, set NUT_ENV_FILE=nut.env in .env and put the generated UPS variables in nut.env.

If you only want upsmon to monitor a subset of configured devices, set:

UPS_MONITOR_NAMES=rack_ups desk-ups

UPS_HEALTHCHECK_NAMES is independent and is available when the healthcheck should check a different subset. Leave both blank to monitor and check every configured UPS.

upsmon runs in the container. NUT_SHUTDOWN_CMD defaults to /bin/true, so low-battery monitoring is active without shutting down the Docker host unless you deliberately replace that command with host-integrated shutdown handling. If drivers are slow to appear, increase NUT_UPSD_READY_TIMEOUT.

Custom ups.conf

If the env-based configuration is not expressive enough for a driver, copy config/ups.conf.example to config/ups.conf and edit it directly. Set UPS_REGISTRY_ENABLED=false to use this fully manual mode. When config/ups.conf exists and is non-empty, the entrypoint uses it as /etc/nut/ups.conf. The entrypoint and healthcheck parse the section names from that file automatically.

Low Battery Rules

UPS_IGNORELB=true is the default because some HID devices report low-battery state unreliably. With ignorelb, NUT derives low-battery state from:

  • battery.charge <= battery.charge.low
  • or battery.runtime <= battery.runtime.low

Some HID smart-battery UPSes report battery.runtime=0 while online and only publish a real estimate while discharging. GOLDENMATE -BMS- Smart-Battery devices are treated as charge-only for low-battery detection to avoid a false LB state on AC power.

Tune these with:

UPS_LOW_CHARGE=10
UPS_LOW_RUNTIME=300

Per-UPS overrides are also supported, such as UPS_RACK_UPS_LOW_RUNTIME=600.

Prometheus

The included exporter exposes metrics at:

http://HOST:9995/metrics?target=HOST_OR_CONTAINER:3493

It queries each UPS independently. If one UPS returns a NUT error such as DATA-STALE or DRIVER-NOT-CONNECTED, the scrape still succeeds for healthy UPS devices and emits nut_ups_up{ups="<name>"} 0 plus nut_ups_error{ups="<name>",error="<code>"} 1 for the failed device.

Example Prometheus scrape job:

- job_name: nut
  metrics_path: /metrics
  static_configs:
    - targets:
        - 10.0.0.64:3493
      labels:
        host: kate
        service: ups
    - targets:
        - 10.0.0.200:3493
      labels:
        host: smoak
        service: ups
  relabel_configs:
    - source_labels:
        - __address__
      target_label: __param_target
    - source_labels:
        - __param_target
      target_label: instance
    - target_label: __address__
      replacement: 10.0.0.64:9995

Useful metrics:

  • nut_ups_up
  • nut_ups_error
  • nut_ups_info
  • nut_battery_charge
  • nut_battery_runtime_seconds
  • nut_load
  • nut_ups_status{status="OL"}
  • nut_ups_status{status="OB"}
  • nut_ups_status{status="LB"}

Grafana

This repo includes a provisionable dashboard at grafana/dashboards/power/nut-ups.json. The dashboard includes separate panels for:

  • NUT low-battery flag
  • low charge
  • low runtime
  • battery charge
  • runtime
  • load
  • voltage

The dashboard selectors are dynamic:

query_result(count by (job) (nut_ups_info))
query_result(count by (instance) (nut_ups_info{job=~"${job:regex}"}))
query_result(count by (ups) (nut_ups_info{job=~"${job:regex}", instance=~"${instance:regex}"}))

Any UPS exposed by a scraped NUT server should appear automatically. Dashboard queries group by job, instance, and ups, so the default UPS name ups can exist on multiple hosts without their metrics being merged.

PeaNUT and Homepage

PeaNUT reads peanut/settings.yml. Add every NUT server you want PeaNUT to display:

NUT_SERVERS:
  - HOST: nut
    PORT: 3493
  - HOST: 10.0.0.200
    PORT: 3493

For Homepage, use the peanut widget and the UPS key from NUT:

- UPS:
    widget:
      type: peanut
      url: http://HOST:3494
      key: ups
      username: "{{HOMEPAGE_VAR_PEANUT_USERNAME}}"
      password: "{{HOMEPAGE_VAR_PEANUT_PASSWORD}}"

If Homepage is in a separate Docker Compose project, using the host-published PeaNUT port is usually more portable than sharing an external Docker network. Set PEANUT_HOST_BIND accordingly. Keep it at 127.0.0.1 unless another container or host needs to reach it.

Home Assistant

Add the built-in Network UPS Tools (NUT) integration:

  • Host: Docker host IP or DNS name
  • Port: 3493
  • Username: value of NUT_HOME_ASSISTANT_USER
  • Password: value of NUT_HOME_ASSISTANT_PASSWORD

The Home Assistant NUT user has instcmds = ALL, so Home Assistant can expose supported commands such as beeper control and battery tests.

Current Deployment Notes

This repository is currently configured locally through ignored .env and external monitoring files for:

  • kate: local UPS devices ups1500 and ups850
  • smoak: remote UPS devices smoak-ups1350 and smoak-ups850 at 10.0.0.200:3493

Prometheus is configured outside this repo at:

/home/gaarai/docker/monitoring/prometheus/prometheus.yml

Homepage is configured outside this repo at:

/home/gaarai/docker/homepage/config/services.yaml

Security

NUT_PORT controls the port where upsd listens inside the container. NUT_HOST_PORT controls the port published on the Docker host. Keep both at 3493 unless you have a specific port conflict.

NUT_HOST_BIND=0.0.0.0 and NUT_EXPORTER_HOST_BIND=0.0.0.0 expose NUT and the Prometheus exporter on the LAN. Bind them to a specific trusted interface or firewall them to trusted monitoring clients.

The NUT service uses an env file so unlimited UPS_<NAME>_* variables can be passed through without editing docker-compose.yml. The default is .env; use NUT_ENV_FILE=nut.env if you want a narrower env file for the NUT container.

PeaNUT is credential-protected, but it should still be treated as an internal service. Prefer PEANUT_HOST_BIND=127.0.0.1 unless another trusted container or host needs access.