nut-server with E-Mail alerts

This builds on the nut server writeup I did. It adds alerting for when the UPS goes on battery then comes off battery. All these steps are done on the nut server.

🚀 Docker Modification Steps

  1. Modify your docker compose. The red bolded lines are what's added.
  2. services:
      nut-server:
        image: nut-server-smtp
        container_name: nut-server
        restart: unless-stopped
        privileged: true
        build: .
        environment:
          - TZ=America/New_York
          - UPS_NAME=apc600
          - UPS_DRIVER=usbhid-ups
          - UPS_PORT=auto
          - UPS_DESC=APC BE600M1
          - USERNAME=YOUR-USERNAME
          - PASSWORD=YOUR-PASSWORD
        entrypoint:
          - /bin/sh
          - -c
          - |
              mkdir -p /var/run/nut
              chown nut:nut /var/run/nut
              /usr/sbin/upssched &
              exec /usr/local/bin/entrypoint.sh
        devices:
          - /dev/bus/usb:/dev/bus/usb
        ports:
          - "3493:3493"
        volumes:
          - ./config:/etc/nut
          - ./logs:/var/log/nut
          - ./upssched-cmd.sh:/usr/local/bin/upssched-cmd.sh
          - ./send-email.sh:/usr/local/bin/send-email.sh
          - ./msmtprc:/etc/msmtprc:ro
          - ./msmtp.log:/var/log/msmtp.log
  3. Create upssched-cmd.sh

    In ~/docker/nut, create upssched-cmd.sh:

    #!/bin/sh
    
    case "$1" in
      notify-on-battery)
        /usr/local/bin/send-email.sh "UPS on battery" "Your UPS has switched to battery power."
        ;;
      notify-on-line)
        /usr/local/bin/send-email.sh "UPS back on line" "Your UPS has returned to AC power."
        ;;
    esac

    Make it executable: chmod 666 upssched-cmd.sh

  4. Create send-email.sh
  5. In ~/docker/nut, create send-email.sh:

    #!/bin/sh
    SUBJECT="$1"
    BODY="$2"
    
    echo -e "Subject: $SUBJECT\n\n$BODY" | msmtp t;Your-Email-Address>

    Make it executable: chmod 666 send-email.sh

  6. Create Dockerfile
  7. In ~/docker/nut, create Dockerfile:

    FROM instantlinux/nut-upsd:latest
    RUN apk add --no-cache msmtp

Build nut

From within ~/docker/nut run this: docker compose build

Start it

From within ~/docker/nut run this: docker compose up -d

Test it

Unplug the UPS from the wall. Wait a few seconds and plug it back in. You should get two emails, one telling you that you're on battery and one saying that you're back online.

📜 View Logs

docker logs nut-server

You can also see the email log by looking in ~/docker/nut/msmtp.log

If you find my content useful, please consider supporting this page:

☕ Buy Me a Coffee