Skip to content

Commit

Permalink
fixup! Monitor System Upgrades with Healthchecks.io
Browse files Browse the repository at this point in the history
  • Loading branch information
kusold committed Jan 19, 2024
1 parent f3ae953 commit 5868789
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
10 changes: 5 additions & 5 deletions modules/autoupgrade.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
system.autoUpgrade.dates = "daily";
system.autoUpgrade.persistent = true;

systemctl.service."nixos-upgrade" = {
onFailure = [ "healthchecks-monitor@nixos-upgrade-${config.networking.hostName}:failure" ]
onSuccess = [ "healthchecks-monitor@nixos-upgrade-${config.networking.hostName}:success" ]
wants = [ "multi-user.target" "healthchecks-monitor@nixos-upgrade-${config.networking.hostName}:start" ];
}
systemd.services."nixos-upgrade" = {
onFailure = [ "healthchecks-monitor@nixos-upgrade-${config.networking.hostName}:failure.service" ];
onSuccess = [ "healthchecks-monitor@nixos-upgrade-${config.networking.hostName}:success.service" ];
wants = [ "multi-user.target" "healthchecks-monitor@nixos-upgrade-${config.networking.hostName}:start.service" ];
};
}
38 changes: 22 additions & 16 deletions modules/healthchecks-io-monitor.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{ config, pkgs, ... }:
{
age.secrets.monitoring-healthchecks-url.file = ../secrets/monitoring-healthchecks-url.age;
config.age.secrets.monitoring-healthchecks-url.file = ../secrets/monitoring-healthchecks-url.age;


# [Unit]
Expand All @@ -10,25 +10,31 @@
# OnSuccess=healthcheck-monitor@deda567a-21e0-4744-ba9e-603c51e258b0:success.service
# Wants=healthcheck-monitor@deda567a-21e0-4744-ba9e-603c51e258b0:start.service

systemd.services.healthchecks-monitor = {
config.systemd.services."healthchecks-monitor@" = {
description = "Pings healthchecks (%i)";
wantedBy = [ "multi-user.target" ];
path = [ pkgs.curl ];
scriptArgs = "%i";
script = ''
HC_URL=$(cat ${age.secrets.monitoring-healthchecks-url.file})
IFS=: read -r SLUG ACTION <<< "%i"
if [ "$ACTION" = "start" ]; then
LOGS=""
EXIT_CODE="start"
else
LOGS=$(journalctl --no-pager -n 50 -u $MONITOR_UNIT)
EXIT_CODE=$MONITOR_EXIT_STATUS
fi
curl -fSs -m 10 --retry 3 --data-raw "$LOGS" "$HC_URL/$SLUG/$EXIT_CODE?create=1"
'';
path = [ pkgs.bash pkgs.curl ];
# Scripts did not work because the special systemd variables were not getting passed in. I had to collapse it to a single line for ExecStart
# scriptArgs = "%i";
# script = ''
# HC_URL=$(cat ${config.age.secrets.monitoring-healthchecks-url.path})
# IFS=: read -r SLUG ACTION <<< "%i"
# if [ "$ACTION" = "start" ]; then
# LOGS=""
# EXIT_CODE="start"
# else
# #LOGS=$(journalctl --no-pager -n 50 -u $MONITOR_UNIT)
# LOGS=""
# EXIT_CODE=$MONITOR_EXIT_STATUS
# fi
# echo "curl -fSs -m 10 --retry 3 --data-raw \"$LOGS\" \"$HC_URL/$SLUG/$EXIT_CODE?create=1\""
# curl -fSs -m 10 --retry 3 --data-raw "$LOGS" "$HC_URL/$SLUG/$EXIT_CODE?create=1"
# '';
serviceConfig = {
Type = "oneshot";
ExecStart = ''
${pkgs.bash}/bin/bash -c 'HC_URL=$(cat ${config.age.secrets.monitoring-healthchecks-url.path}); IFS=: read -r SLUG ACTION <<< "%i"; if [ "$ACTION" = "start" ]; then LOGS="" && EXIT_CODE="start"; else LOGS=$(journalctl --no-pager -n 50 -u $MONITOR_UNIT) && EXIT_CODE=$MONITOR_EXIT_STATUS; fi && echo "curl -fSs -m 10 --retry 3 --data-raw \"$LOGS\" \"$HC_URL/$SLUG/$EXIT_CODE?create=1\"" && curl -fSs -m 10 --retry 3 --data-raw "$LOGS" "$HC_URL/$SLUG/$EXIT_CODE?create=1"'
'';
};
};

Expand Down

0 comments on commit 5868789

Please sign in to comment.