/docker-apcupsd

docker image for apcupsd

Primary LanguageShell

docker-apcupsd

Docker image of apcupsd supporting (partial) configuration through environment files and notifications using webhooks (instead of the default email notifications).

Usage

docker run --device=/dev/usb/hiddev0 \
           fopina/apcupsd

This would monitor your UPS and events are available in docker logs.

But the main idea of the container is to be able to receive notifications when these events occur, much like apcupsd default behavior of sending emails.

Notifications are configurable through environment variables to be easily defined when creating the container.

Default values are the follow (clear in the Dockerfile):

ENV WEBHOOK_URL=""
ENV WEBHOOK_TEMPLATE='{{ env "EVENT" }} triggered on {{ env "UPSID" }} - {{ env "MSG" }}'
ENV WEBHOOK_EVENTS="changeme commfailure commok offbattery onbattery"
ENV EVENT_CHANGEME="battery needs changing NOW"
ENV EVENT_COMMFAILURE="communications with UPS lost"
ENV EVENT_COMMOK="communications with UPS restored"
ENV EVENT_OFFBATTERY="power has returned"
ENV EVENT_ONBATTERY="POWER FAILURE !!!"
  • WEBHOOK_URL is the endpoint of your notification system, such as https://tgbots.skmobi.com/pushit/105e48ff92b92263f3397ed55f275a81 if you're using PushitBot or https://api.telegram.org/botYOURTOKEN/sendMessage if you're pushing directly to Telegram API. Being empty means no notification is sent, only logged

  • WEBHOOK_TEMPLATE is the payload to be sent (POST) to the WEBHOOK_URL. It uses Go templating language (through confgen) so you can format the payload however it is required, using the existing event defined environment variables:

    • EVENT - the event name (as in WEBHOOK_EVENTS)
    • UPSID - the UPS device ID as provided by apcupsd
    • MSG - the optionally defined custom message per event using the EVENT_xxx variables, explained below
    • any other environment variable defined in the system (such as HOSTNAME) or that you defined when creating the container
  • WEBHOOK_EVENTS the events that should trigger a call to the webhook URL (delimited by whitespace). Those in the dockerfile are the default list but you can choose any events from the full list generated by apcupsd (refer to its documentation to understand what they mean):

    killpower,commfailure,commok,powerout,onbattery,offbattery,mainsback,failing,
    timeout,loadlimit,runlimit,doreboot,doshutdown,annoyme,emergency,changeme,
    remotedown,startselftest,endselftest,battdetach,battattach
    
  • EVENT_xxx custom message to be mapped to a given event named xxx (use uppercase). This message becomes available for WEBHOOK_TEMPLATE as the variable MSG. For events that do not have a message mapped, MSG will be the event name (like EVENT variable)

There is an helper you can use to your webhook/template setup - test-webhook:

docker run --rm \
           -e WEBHOOK_URL=https://tgbots.skmobi.com/pushit/105e48ff92b92263f3397ed55f275a81 \
           -e WEBHOOK_TEMPLATE='msg={{ env "EVENT" }} triggered on {{ env "UPSID" }} - {{ env "MSG" }}' \
           fopina/apcupsd \
           test-webhook changeme

would be the example to test the template with a valid payload for PushitBot