From 7af0a4e7fd9593fbfb5f88b7a33b4495cd89016c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Av=C3=A9?= Date: Mon, 10 Jun 2024 16:31:23 +0200 Subject: [PATCH] Mon Jun 10 04:31:23 PM CEST 2024 --- home/Mallorea.nix | 18 +---------- home/Vault.nix | 63 ++++----------------------------------- home/utils/disk_check.nix | 18 +++++++++++ 3 files changed, 25 insertions(+), 74 deletions(-) create mode 100644 home/utils/disk_check.nix diff --git a/home/Mallorea.nix b/home/Mallorea.nix index bd2905e..392a5ac 100644 --- a/home/Mallorea.nix +++ b/home/Mallorea.nix @@ -1,23 +1,7 @@ { inputs, config, pkgs, ... }: let scripts = { - disk_check = { - when = "*-*-* *:00:00"; - script = toString (pkgs.writeShellScript "script" '' - REPORT_EMAIL=email@thomasave.be - ZPOOL_STATUS=$(zpool status -x) - if [ "$ZPOOL_STATUS" = "all pools are healthy" ] || [ "$ZPOOL_STATUS" = "no pools available" ] - then - printf 0 > /var/db/zpool.status - else - if [ "$(cat /var/db/zpool.status)" -eq 0 ] - then - zpool status | mail -s "ZPOOL NOT HEALTHY" $REPORT_EMAIL - printf 1 > /var/db/zpool.status - fi - fi - ''); - }; + disk_check = import ./utils/disk_check.nix {inherit pkgs;}; vdirsyncer = { when = "*:0/15"; script = toString (pkgs.writeShellScript "script" '' diff --git a/home/Vault.nix b/home/Vault.nix index c531240..68406a0 100644 --- a/home/Vault.nix +++ b/home/Vault.nix @@ -1,51 +1,5 @@ { 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"] - ).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, "Vault 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" ]; - Timer = { - Persistent = true; - OnCalendar = cfg.when; - Unit = "${name}.service"; - }; - }; - mkService = name: cfg: { - Unit.Description = name; - Unit.OnFailure = "status_notify@%n.service"; - Install = { WantedBy = [ "default.target" ]; }; - Service = { ExecStart = cfg.script; }; - }; scripts = { disk_check = { when = "*-*-* *:00:00"; @@ -102,7 +56,12 @@ in { }; xdg.enable = true; - imports = [ (import ./common.nix { inherit inputs config pkgs; }) ./ssh ]; + imports = [ + (import ./common.nix { inherit inputs config pkgs; }) + (import ./utils/services.nix { inherit pkgs; scripts=scripts; }) + ./ssh + ]; + programs.ssh.matchBlocks."*".identityFile = "/home/server/.secrets/SSH/Vault/id_ed25519"; home.sessionVariables = { @@ -111,14 +70,4 @@ in { XDG_RUNTIME_DIR = "/run/user/$(id -u)"; }; - 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 disk_check.timer } diff --git a/home/utils/disk_check.nix b/home/utils/disk_check.nix new file mode 100644 index 0000000..71e2b03 --- /dev/null +++ b/home/utils/disk_check.nix @@ -0,0 +1,18 @@ +{pkgs}: +{ + when = "*-*-* *:00:00"; + script = toString (pkgs.writeShellScript "script" '' + REPORT_EMAIL=email@thomasave.be + ZPOOL_STATUS=$(zpool status -x) + if [ "$ZPOOL_STATUS" = "all pools are healthy" ] || [ "$ZPOOL_STATUS" = "no pools available" ] + then + printf 0 > /var/db/zpool.status + else + if [ "$(cat /var/db/zpool.status)" -eq 0 ] + then + zpool status | mail -s "ZPOOL NOT HEALTHY" $REPORT_EMAIL + printf 1 > /var/db/zpool.status + fi + fi + ''); +}