Post

Automatic Power Outage Management for Proxmox Server Using Raspberry Pi and NodeMCU

How to safely shutdown and auto-start your Proxmox server during power failures with a low-cost UPS monitor system.

Automatic Power Outage Management for Proxmox Server Using Raspberry Pi and NodeMCU

Automatic Proxmox Shutdown and Power-On Using Raspberry Pi, NodeMCU (ESP8266), and UPS Power Monitoring

Managing unexpected power outages is crucial to avoid data loss and hardware damage on your Proxmox server. In this guide, we’ll create a simple, cost-effective system to:

  • Detect mains power loss using a NodeMCU ESP8266 microcontroller.
  • Notify a Raspberry Pi to monitor power state.
  • Automatically shutdown the Proxmox server when mains power fails.
  • Wake the server using Wake-on-LAN (WoL) when power is restored.

Components Used

Component Purpose
NodeMCU ESP8266 Detects main power availability
Raspberry Pi Monitors NodeMCU and sends WoL packets
Proxmox Server The main server to shutdown and wake
UPS (Battery Backup) Supplies power during outage

How It Works

  1. NodeMCU is powered from the main power line. It becomes unreachable (offline) when main power is lost (because no UPS backup).
  2. Raspberry Pi continuously pings NodeMCU to check if main power is available.
  3. When NodeMCU goes offline (power outage detected), the Pi initiates a clean shutdown of the Proxmox server.
  4. When NodeMCU comes back online (power restored), the Pi sends a Wake-on-LAN packet to power up the Proxmox server.

Step 1: Setting up the NodeMCU ESP8266

  • Program the NodeMCU with firmware to respond to pings (default firmware or simple pingable setup).
  • Power it directly from the main power line (e.g., 5V USB adapter connected to mains).
  • When main power is lost, NodeMCU powers off immediately.

Step 2: Raspberry Pi Monitoring Script

Create a script on the Pi (/opt/wol/esp-monitor-wol.sh) that:

  • Pings the NodeMCU IP address repeatedly.
  • Logs all actions and power state changes to /home/pi/wol-monitor.log.
  • Uses a lockfile (/tmp/proxmox_wol_sent.lock) to avoid sending multiple Wake-on-LAN packets unnecessarily.
  • Includes verification delays to confirm stable power state changes and avoid false positives.
  • When NodeMCU is unreachable for several checks, initiates Proxmox shutdown via SSH.
  • When NodeMCU returns online, waits for Proxmox to fully shut down and then sends a WoL packet to boot the server.

Full Script: /opt/wol/esp-monitor-wol.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh

ESP_IP="192.168.1.100"           # NodeMCU IP
PROXMOX_IP="192.168.1.50"        # Proxmox server IP
PROXMOX_USER="root"              # SSH user on Proxmox
SERVER_MAC="00:11:22:33:44:55"   # Proxmox server MAC for WoL

LOGFILE="/home/pi/wol-monitor.log"
LOCKFILE="/tmp/proxmox_wol_sent.lock"

PING_COUNT=3
PING_WAIT=1
DELAY_AFTER_ONLINE=30           # Seconds to wait after NodeMCU comes online before WoL
DELAY_AFTER_OFFLINE=10          # Seconds to confirm power outage before shutdown
SLEEP_BETWEEN_CHECKS=60         # Interval between checks

log() {
    echo "$(date): $1" >> "$LOGFILE"
}

is_online() {
    ping -c $PING_COUNT -W $PING_WAIT $1 > /dev/null 2>&1
}

send_wol() {
    /usr/bin/ether-wake -i eth0 $SERVER_MAC
    log "Sent Wake-on-LAN packet to $SERVER_MAC"
}

proxmox_shutdown() {
    ssh -o ConnectTimeout=10 -o BatchMode=yes $PROXMOX_USER@$PROXMOX_IP 'shutdown -h now'
    log "Sent shutdown command to Proxmox server"
}

proxmox_is_online() {
    ping -c 1 -W 1 $PROXMOX_IP > /dev/null 2>&1
}

main() {
    log "Starting WOL monitor..."

    while true; do
        if is_online $ESP_IP; then
            log "NodeMCU online and stable. Waiting $DELAY_AFTER_ONLINE seconds..."
            sleep $DELAY_AFTER_ONLINE

            if proxmox_is_online; then
                log "Proxmox already online, skipping WoL."
                # Remove lockfile if present since server is online
                if [ -f "$LOCKFILE" ]; then
                    rm -f "$LOCKFILE"
                    log "Removed WoL lockfile"
                fi
            else
                if [ ! -f "$LOCKFILE" ]; then
                    send_wol
                    touch "$LOCKFILE"
                else
                    log "WoL already sent, waiting for Proxmox to boot."
                fi
            fi

        else
            log "NodeMCU offline. Verifying power outage..."
            sleep $DELAY_AFTER_OFFLINE
            if ! is_online $ESP_IP; then
                log "Confirmed power outage detected."
                if proxmox_is_online; then
                    log "Proxmox is online, initiating shutdown."
                    proxmox_shutdown
                else
                    log "Proxmox already offline, no action needed."
                fi
                # Remove lockfile after shutdown (server is off)
                if [ -f "$LOCKFILE" ]; then
                    rm -f "$LOCKFILE"
                    log "Removed WoL lockfile due to shutdown"
                fi
            else
                log "False power outage detected, NodeMCU back online."
            fi
        fi

        sleep $SLEEP_BETWEEN_CHECKS
    done
}

main

Step 3: Enable Passwordless SSH From Pi to Proxmox

To allow the Pi to send shutdown commands without password prompts:

1
2
ssh-keygen -t rsa
ssh-copy-id root@192.168.1.50

Test by running:

1
ssh root@192.168.1.50 uptime

Step 4: Enable Wake-on-LAN on Proxmox Server

  • Enable WoL in BIOS/UEFI.
  • Confirm WoL support:
1
ethtool eth0
  • Install etherwake on the Pi:
1
2
3
sudo apk add etherwake        # Alpine Linux
# or
sudo apt install etherwake    # Debian/Raspbian

Step 5: Run Script Automatically on Pi Boot

Add to Pi user crontab:

1
crontab -e

Add the line:

1
@reboot /opt/wol/esp-monitor-wol.sh &

Make sure script is executable:

1
chmod +x /opt/wol/esp-monitor-wol.sh

Monitoring and Logs

  • The script logs to /home/pi/wol-monitor.log.
  • Check logs anytime with:
1
tail -f /home/pi/wol-monitor.log

Summary

This setup ensures your Proxmox server:

  • Shuts down cleanly on power loss detected by the NodeMCU going offline.
  • Automatically wakes up when power returns, triggered by the Raspberry Pi sending WoL.
  • Avoids repeated Wake-on-LAN packets using a lockfile.
  • Confirms power outages with delays and multiple pings to avoid false shutdowns.
  • Provides detailed logging for easy troubleshooting.

This post is licensed under CC BY 4.0 by the author.