A Docker container that monitors a Network UPS Tools (NUT) server and automatically shuts down the host machine when the UPS battery becomes critically low.
- 🔋 Monitors UPS status via NUT protocol
- 🚨 Automatic host shutdown on critical battery conditions
- 🐳 Containerized for easy deployment
- 🔄 Graceful Docker container shutdown before host shutdown
- 📊 Continuous status logging
- 🏥 Built-in health checks
- 🔧 Configurable check intervals and UPS parameters
- Docker and Docker Compose installed
- A running NUT server (like
instantlinux/nut-upsd) - Host machine that needs automatic shutdown protection
version: '3.8'
services:
ups-monitor:
image: awushensky/nut-client:latest
container_name: ups-monitor
restart: unless-stopped
environment:
UPS_SERVER: 192.168.1.100 # Your NUT server IP
UPS_PORT: 3493
UPS_NAME: ups
CHECK_INTERVAL: 30
volumes:
- /proc:/host/proc:ro
privileged: true
pid: host| Variable | Default | Description |
|---|---|---|
UPS_SERVER |
localhost |
IP address of your NUT server |
UPS_PORT |
3493 |
Port of your NUT server |
UPS_NAME |
ups |
Name of the UPS as configured in NUT |
CHECK_INTERVAL |
30 |
How often to check UPS status (seconds) |
This container uses the upsc command for read-only UPS status queries. In standard NUT configurations, upsd always provides read-only access to anonymous clients. The user authentication feature is there only for granting access to control actions (e.g. upsrw changing UPS parameters, or upscmd triggering calibration, or upsmon setting 'forced shutdown').
If your NUT server requires authentication even for read access, you may need to:
- Configure network-level security (VPN, firewall rules)
- Use TLS certificates with
CERTREQUEST REQUIREin upsd.conf - Contact the container maintainer for authentication support
privileged: true- Required for host shutdown capability via nsenter/signalingpid: host- Required for accessing host's init process (PID 1)
Why
pid: host? The container uses various methods to execute shutdown commands on the host. Withoutpid: host, PID 1 refers to the container's init process, not the host's, making host shutdown impossible.
Shutdown Methods: The container automatically detects the best available shutdown method:
nsenter- Direct host namespace access (most reliable)systemd_signal- Systemd-specific signals when systemd is detectedsystemctl- If systemctl command is available in containersysrq- Kernel SysRq trigger (if writable)signal- Direct init process signaling (fallback)
The instantlinux/nut-upsd container is configured entirely through environment variables - no config files needed!
services:
nut-server:
image: instantlinux/nut-upsd:latest
container_name: nut-server
restart: unless-stopped
environment:
NAME: "servers" # UPS name (what clients connect to)
SERIAL: YOUR_UPS_SERIAL_HERE # Your UPS serial number
API_USER: ${NUT_UPS_USER} # Username for API access
API_PASSWORD: ${NUT_UPS_PASSWORD} # Password for API access
DRIVER: usbhid-ups # UPS driver (optional)
ports:
- "3493:3493"
privileged: true
devices:
- /dev/bus/usb:/dev/bus/usb- Monitoring: Container continuously polls the NUT server for UPS status
- Detection: Watches for
OB(On Battery) +LB(Low Battery) conditions - Shutdown Sequence:
- Logs critical condition
- Stops all Docker containers gracefully
- Waits 10 seconds for cleanup
- Shuts down the host using
nsenterto break out of container namespace
The monitor looks for these UPS status combinations:
OL- Online (normal operation)OB- On Battery (power outage)LB- Low Battery (critical - triggers shutdown)OB LB- On Battery + Low Battery (immediate shutdown)
Monitor the container logs to see UPS status:
docker logs -f ups-monitorExample output:
Starting UPS monitor for ups@192.168.1.100:3493
✓ Host namespace access confirmed
2024-01-15 10:30:00: UPS Status: OL, Battery: 100%
2024-01-15 10:30:30: UPS Status: OB, Battery: 95%
2024-01-15 10:35:00: CRITICAL - UPS is on battery and low battery detected!
2024-01-15 10:35:00: Initiating emergency shutdown sequence...
git clone https://github.com/your-username/ups-monitor.git
cd ups-monitor
docker build -t ups-monitor .- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Create an issue for bugs or feature requests
- Check the NUT documentation for UPS compatibility
- Ensure your UPS is properly configured with NUT before using this monitor