Tue Jun 4 01:48:34 AM CEST 2024

This commit is contained in:
Thomas Avé 2024-06-04 01:48:34 +02:00
parent e0e75470e8
commit abb64b1d01
1 changed files with 42 additions and 1 deletions

View File

@ -1,5 +1,35 @@
{ inputs, config, pkgs, ... }:
let
notify_script = (pkgs.writers.writePython3Bin "telegram-notify.py" { libraries = [ pkgs.python3Packages.python-telegram-bot ]; } ''
import telegram
import asyncio
import sys
import subprocess
async def run():
text = subprocess.check_output(
["journalctl", "--user", "-u", sys.argv[1], "-b", "0"]
).decode("utf-8")
with open("/home/server/mail.log", "a") as f:
f.write("===========================================")
f.write(text)
bot = telegram.Bot(token="381718873:AAElFmI2BDjumCehhWicuksE0vutrPSkoGA")
chat_id = 125754925
await bot.send_message(
chat_id, "Mallorea encountered an error in the service: " + sys.argv[1]
)
await bot.send_message(chat_id, text)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait([loop.create_task(run())]))
loop.close()
'');
mkTimer = name: cfg:
{
Install.WantedBy = [ "timers.target" ];
@ -12,6 +42,7 @@
mkService = name: cfg:
{
Unit.Description = name;
Unit.OnFailure = "status_notify@%n.service";
Install = {
WantedBy = [ "default.target" ];
};
@ -105,6 +136,16 @@ in
XDG_RUNTIME_DIR="/run/user/$(id -u)";
};
systemd.user.services = pkgs.lib.mapAttrs mkService scripts;
systemd.user.services = pkgs.lib.mapAttrs mkService scripts //
(pkgs.lib.mapAttrs mkService {
"status_notify@" = {
script = "${notify_script}/bin/telegram-notify.py %i";
};});
systemd.user.timers = pkgs.lib.mapAttrs mkTimer scripts;
# Don't forget to enable these timers! Or reboot, after which it should also be activated automatically
# systemctl --user enable --now backup.timer
# systemctl --user enable --now sync_vault.timer
# systemctl --user enable --now mbsync.timer
# systemctl --user enable --now vdirsyncer.timer
# systemctl --user enable --now disk_check.timer
}