2022-11-04 19:35:57 +01:00
|
|
|
{ custom }: { config, pkgs, ... }:
|
2022-08-27 16:30:54 +02:00
|
|
|
let
|
2022-08-28 11:43:48 +02:00
|
|
|
send-to-telegram = pkgs.writeShellScript "send-to-telegram" ''
|
2022-11-04 13:06:15 +01:00
|
|
|
export $(${pkgs.gnugrep}/bin/grep -v '^#' ${config.age.secrets.telegramNotifyEnv.path} | ${pkgs.findutils}/bin/xargs)
|
2022-08-27 16:30:54 +02:00
|
|
|
URL="https://api.telegram.org/bot$TELEGRAM_KEY/sendMessage"
|
2022-08-28 11:43:48 +02:00
|
|
|
${pkgs.curl}/bin/curl -s -d "chat_id=$CHAT_ID&disable_web_page_preview=1&text=$1" $URL > /dev/null'';
|
|
|
|
|
|
|
|
unit-status-telegram = pkgs.writeShellScript "unit-status-telegram" ''
|
2022-08-27 17:16:23 +02:00
|
|
|
UNIT="$1"
|
|
|
|
|
|
|
|
UNITSTATUS="$(systemctl status $UNIT)"
|
|
|
|
ALERT="$(echo -e "\u26A0")"
|
|
|
|
|
2022-08-28 11:43:48 +02:00
|
|
|
${send-to-telegram} "$ALERT Unit failed $UNIT $ALERT
|
2022-08-27 17:16:23 +02:00
|
|
|
Status:
|
|
|
|
$UNITSTATUS"'';
|
2022-08-27 16:30:54 +02:00
|
|
|
in
|
|
|
|
{
|
2022-11-04 19:35:57 +01:00
|
|
|
age.secrets.telegramNotifyEnv.file = "${custom.inputs.self}/scrts/telegram_notify_env.age";
|
2022-08-27 17:16:23 +02:00
|
|
|
systemd.services."unit-status-telegram@" = {
|
|
|
|
description = "Unit Status Telegram Service";
|
|
|
|
unitConfig = {
|
2022-11-12 18:02:00 +01:00
|
|
|
After = "network-online.target";
|
2022-08-27 17:16:23 +02:00
|
|
|
};
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
2022-08-28 11:43:48 +02:00
|
|
|
ExecStart = "${unit-status-telegram} %I";
|
2022-08-27 17:16:23 +02:00
|
|
|
};
|
|
|
|
};
|
2022-08-27 16:30:54 +02:00
|
|
|
}
|