instantlinux / docker-tools

Docker tools for developer productivity & entertainment
Apache License 2.0
272 stars 89 forks source link

nut-upsd - telegram support #128

Open S474N opened 1 year ago

S474N commented 1 year ago

I have one idea for improvement - Telegram support, for example for sending state of UPS (without power, etc.).

instantlinux commented 1 year ago

The NUT Tools package from which this container is built doesn't have built-in notifications: it provides an API for monitoring, via TCP port 3493.

A typical alert system is Nagios, which has a few rivals such as OpGenie, Zabbix and/or cloud services like DataDog. If you run nagios, you can use the standard check_ups plugin and set up notifications to any medium you choose, such as the telegram integration for nagios.

S474N commented 1 year ago

Why so complicated?

In persistent storage we have file upsmon.conf. Here we can define NOTIFYCMD (and another commands).

Simple SH script:

#!/bin/sh

hostname=`hostname`

curl -s \
  --data parse_mode=HTML \
  --data chat_id=XXXXXX \
  --data text="<b>nut-upsd </b>%0A      <i>sending from <b>#RPi4B</b></i>%0A%0A${1}" \
  "https://api.telegram.org/botXXXXX:YYY/sendMessage" \
  > /dev/null 2>&1

exit 0

But have problem with permision of this file :(

28. 08. 2023 5:44:55 sh: /etc/nut/local/send_telegram_message.sh: Permission denied
28. 08. 2023 15:44:25 253948.886598 UPS apc750@localhost - jede z baterii!
28. 08. 2023 15:44:25 sh: /etc/nut/local/send_telegram_message.sh: Permission denied
28. 08. 2023 15:44:50 253973.891283 UPS apc750@localhost - obnoven privod elektriny.
28. 08. 2023 15:44:50 sh: /etc/nut/local/send_telegram_message.sh: Permission denied
31. 08. 2023 5:35:42 476626.527492  UPS eaton650@localhost - jede z baterii!
instantlinux commented 1 year ago

Please open a pull request rather than an Issue if you want this suggestion implemented.

It should implement more than one notification integration, though: why would this container image solely support Telegram: why not Twitter/X, slack, email, other APIs?

S474N commented 1 year ago

Email is default in UPS settings. I don't know, if Twitter/X has API for this purposes.

As I wrote above, I have still problem with permissions denied, although script has 777 privileges and owned by root user (tested also pi).

instantlinux commented 1 year ago

In your testing, check the ownership and permissions of the shell script as well as the directory holding it (you created /etc/nut/local). In the published container, I see the config files under /etc/nut set to 640 permissions, owner root, group nut. Since APIs require passwords or secure tokens, this interface script needs to reference environment variables that are passed to the container as secrets.

S474N commented 1 year ago

OK, copied script to /etc/nut directly in container and set owner: image

And I will test it :)