added other services and descs

This commit is contained in:
Lewis Wynne 2026-04-29 16:44:24 +01:00
parent ccc6a9e7a2
commit 8b09bcace0
3 changed files with 189 additions and 69 deletions

View file

@ -18,15 +18,36 @@
services.uptime = { services.uptime = {
enable = true; enable = true;
interval = "1min";
displayDays = 90; displayDays = 90;
services = { intro = ''
website = "https://ily.rs"; This status page is written in pure bash. It tracks 90 days of
forgejo = "https://git.ily.rs"; historical data per service. Each category may probe at its own
foundry = "https://foundry.ily.rs"; interval; that's noted next to the category description.
wiki = "https://wiki.ily.rs/health-ping"; '';
penfield = "https://penfield.ily.rs"; categories = [
}; {
description = "These first sites are all hosted personally.";
intervalSeconds = 60;
services = [
{ name = "website"; url = "https://ily.rs"; }
{ name = "guestbook"; url = "https://ily.rs/guestbook"; }
{ name = "git"; url = "https://git.ily.rs"; }
{ name = "records"; url = "https://c.ily.rs"; }
{ name = "penfield"; url = "https://penfield.ily.rs"; }
{ name = "wiki"; url = "https://wiki.ily.rs/health-ping"; }
{ name = "foundry"; url = "https://foundry.ily.rs"; }
];
}
{
description = "Other services I like to keep track of.";
intervalSeconds = 300;
hideUrls = true;
services = [
{ name = "co-surf"; url = "https://co-surf.com"; }
{ name = "frontline"; url = "https://essexfrontline.org.uk"; }
];
}
];
}; };
networking.hostName = "lab"; networking.hostName = "lab";

View file

@ -2,8 +2,62 @@
let let
cfg = config.services.uptime; cfg = config.services.uptime;
servicesEnv = lib.concatStringsSep "\n" serviceSubmodule = lib.types.submodule {
(lib.mapAttrsToList (name: url: "${name} ${url}") cfg.services); options = {
name = lib.mkOption {
type = lib.types.str;
description = "Service name. Used as the log filename and the row label.";
};
url = lib.mkOption {
type = lib.types.str;
description = "URL to probe.";
};
};
};
categorySubmodule = lib.types.submodule {
options = {
description = lib.mkOption {
type = lib.types.str;
default = "";
description = "Free-form text shown above this category's table.";
};
intervalSeconds = lib.mkOption {
type = lib.types.int;
default = 60;
description = "Minimum interval between probes for services in this category.";
};
hideUrls = lib.mkOption {
type = lib.types.bool;
default = false;
description = "If true, omit the URL column when rendering services in this category.";
};
services = lib.mkOption {
type = lib.types.listOf serviceSubmodule;
default = [];
example = [{ name = "git"; url = "https://git.ily.rs"; }];
description = "Ordered list of services to probe. Render order matches list order.";
};
};
};
configFile = pkgs.writeText "uptime-config" (lib.concatStringsSep "\n" (
(lib.imap0 (i: cat:
"CAT\t${toString i}\t${toString cat.intervalSeconds}\t${if cat.hideUrls then "1" else "0"}\t${cat.description}"
) cfg.categories)
++
(lib.concatLists (lib.imap0 (i: cat:
map (svc: "SVC\t${toString i}\t${svc.name}\t${svc.url}") cat.services
) cfg.categories))
));
introFile = pkgs.writeText "uptime-intro" cfg.intro;
catIntervals = map (c: c.intervalSeconds) cfg.categories;
timerSeconds =
if catIntervals == []
then 60
else lib.foldl' (a: b: if a < b then a else b) 86400 catIntervals;
runScript = pkgs.writeShellApplication { runScript = pkgs.writeShellApplication {
name = "uptime-run"; name = "uptime-run";
@ -15,12 +69,6 @@ in
options.services.uptime = { options.services.uptime = {
enable = lib.mkEnableOption "minimal text-only uptime status page"; enable = lib.mkEnableOption "minimal text-only uptime status page";
interval = lib.mkOption {
type = lib.types.str;
default = "5min";
description = "Probe interval, passed to the timer's OnUnitActiveSec.";
};
outputPath = lib.mkOption { outputPath = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = "/var/lib/uptime/status.txt"; default = "/var/lib/uptime/status.txt";
@ -31,11 +79,16 @@ in
''; '';
}; };
services = lib.mkOption { intro = lib.mkOption {
type = lib.types.attrsOf lib.types.str; type = lib.types.str;
default = {}; default = "";
example = { forgejo = "https://git.ily.rs"; }; description = "Free-form text shown at the top of the status page (before any categories).";
description = "Map of service name to URL to probe."; };
categories = lib.mkOption {
type = lib.types.listOf categorySubmodule;
default = [];
description = "Ordered list of service categories. Each is rendered as its own table.";
}; };
retentionDays = lib.mkOption { retentionDays = lib.mkOption {
@ -47,7 +100,7 @@ in
displayDays = lib.mkOption { displayDays = lib.mkOption {
type = lib.types.int; type = lib.types.int;
default = 30; default = 30;
description = "How many days of history to render in the long-term bar (1 cell = 1 day)."; description = "How many days of history to render in the long-term bar.";
}; };
displayHours = lib.mkOption { displayHours = lib.mkOption {
@ -75,7 +128,8 @@ in
after = [ "network-online.target" ]; after = [ "network-online.target" ];
wants = [ "network-online.target" ]; wants = [ "network-online.target" ];
environment = { environment = {
SERVICES = servicesEnv; CONFIG_PATH = "${configFile}";
INTRO_PATH = "${introFile}";
OUTPUT_PATH = cfg.outputPath; OUTPUT_PATH = cfg.outputPath;
RETENTION_DAYS = toString cfg.retentionDays; RETENTION_DAYS = toString cfg.retentionDays;
DISPLAY_DAYS = toString cfg.displayDays; DISPLAY_DAYS = toString cfg.displayDays;
@ -97,7 +151,7 @@ in
wantedBy = [ "timers.target" ]; wantedBy = [ "timers.target" ];
timerConfig = { timerConfig = {
OnBootSec = "1min"; OnBootSec = "1min";
OnUnitActiveSec = cfg.interval; OnUnitActiveSec = "${toString timerSeconds}s";
Unit = "uptime.service"; Unit = "uptime.service";
}; };
}; };

View file

@ -1,4 +1,5 @@
: "${SERVICES:?must be set}" : "${CONFIG_PATH:?must be set}"
: "${INTRO_PATH:?must be set}"
: "${OUTPUT_PATH:?must be set}" : "${OUTPUT_PATH:?must be set}"
: "${RETENTION_DAYS:?must be set}" : "${RETENTION_DAYS:?must be set}"
: "${DISPLAY_DAYS:?must be set}" : "${DISPLAY_DAYS:?must be set}"
@ -10,21 +11,38 @@ mkdir -p "$state_dir"
now=$(date -u +%s) now=$(date -u +%s)
retention_cutoff=$(( now - RETENTION_DAYS * 86400 )) retention_cutoff=$(( now - RETENTION_DAYS * 86400 ))
# Parse categories into parallel arrays indexed by category number.
cat_intervals=()
cat_hideurls=()
cat_descriptions=()
while IFS=$'\t' read -r tag idx interval hideurls desc; do
if [ "$tag" = "CAT" ]; then
cat_intervals[$idx]=$interval
cat_hideurls[$idx]=$hideurls
cat_descriptions[$idx]=$desc
fi
done < "$CONFIG_PATH"
# Compute name column width across all services.
max_name_len=0 max_name_len=0
while IFS= read -r line; do while IFS=$'\t' read -r tag _ name _; do
[ -z "$line" ] && continue [ "$tag" != "SVC" ] && continue
n=${line%% *} (( ${#name} > max_name_len )) && max_name_len=${#name}
(( ${#n} > max_name_len )) && max_name_len=${#n} done < "$CONFIG_PATH"
done <<< "$SERVICES"
name_col=$(( max_name_len + 2 )) name_col=$(( max_name_len + 2 ))
# Probe each service and rotate its log. # Probe phase: gate per-service by the owning category's intervalSeconds.
while IFS= read -r line; do while IFS=$'\t' read -r tag cat_idx name url; do
[ -z "$line" ] && continue [ "$tag" != "SVC" ] && continue
name=${line%% *} interval=${cat_intervals[$cat_idx]}
url=${line#* }
log="$state_dir/$name.log" log="$state_dir/$name.log"
if [ -s "$log" ]; then
last_ts=$(tail -n 1 "$log" | awk '{print $1}')
age=$(( now - last_ts ))
(( age < interval )) && continue
fi
code=$(curl -fsS --max-time 10 -o /dev/null -w '%{http_code}' "$url" 2>/dev/null || true) code=$(curl -fsS --max-time 10 -o /dev/null -w '%{http_code}' "$url" 2>/dev/null || true)
if [ -z "$code" ] || [ "$code" = "000" ]; then if [ -z "$code" ] || [ "$code" = "000" ]; then
code="000" code="000"
@ -38,15 +56,11 @@ while IFS= read -r line; do
awk -v cutoff="$retention_cutoff" '$1 >= cutoff' "$log" > "$log.tmp" awk -v cutoff="$retention_cutoff" '$1 >= cutoff' "$log" > "$log.tmp"
mv "$log.tmp" "$log" mv "$log.tmp" "$log"
done <<< "$SERVICES" done < "$CONFIG_PATH"
# Render. bucket_size and cell_count are parameters so we can add # Render helpers.
# hour/minute granularity rows later without restructuring the awk.
render_row() { render_row() {
local log_file="$1" local log_file="$1" now_arg="$2" bucket_size="$3" cells="$4"
local now_arg="$2"
local bucket_size="$3"
local cells="$4"
if [ ! -s "$log_file" ]; then if [ ! -s "$log_file" ]; then
local pad local pad
@ -89,18 +103,6 @@ render_row() {
}' "$log_file" }' "$log_file"
} }
day_bar_cells=30
day_bucket=$(( DISPLAY_DAYS * 86400 / day_bar_cells ))
days_per_cell=$(( DISPLAY_DAYS / day_bar_cells ))
if (( days_per_cell == 1 )); then
day_unit="1 day"
else
day_unit="$days_per_cell days"
fi
hour_bar_cells="$DISPLAY_HOURS"
hour_bucket=3600
scale_bar() { scale_bar() {
local cells="$1" left="$2" right="$3" local cells="$1" left="$2" right="$3"
local fill=$(( cells - ${#left} - ${#right} )) local fill=$(( cells - ${#left} - ${#right} ))
@ -123,28 +125,71 @@ center() {
fi fi
} }
human_interval() {
local s="$1"
if (( s < 60 )); then printf '%d seconds' "$s"
elif (( s == 60 )); then printf '1 minute'
elif (( s < 3600 )); then printf '%d minutes' "$(( s / 60 ))"
elif (( s == 3600 )); then printf '1 hour'
else printf '%d hours' "$(( s / 3600 ))"
fi
}
day_bar_cells=30
day_bucket=$(( DISPLAY_DAYS * 86400 / day_bar_cells ))
days_per_cell=$(( DISPLAY_DAYS / day_bar_cells ))
if (( days_per_cell == 1 )); then
day_unit="1 day"
else
day_unit="$days_per_cell days"
fi
hour_bar_cells="$DISPLAY_HOURS"
hour_bucket=3600
day_scale=$(scale_bar "$day_bar_cells" "<-${DISPLAY_DAYS}d" "now->") day_scale=$(scale_bar "$day_bar_cells" "<-${DISPLAY_DAYS}d" "now->")
hour_scale=$(scale_bar "$hour_bar_cells" "<-${DISPLAY_HOURS}h" "now->") hour_scale=$(scale_bar "$hour_bar_cells" "<-${DISPLAY_HOURS}h" "now->")
day_label=$(center "$day_bar_cells" "1 cell = $day_unit")
hour_label=$(center "$hour_bar_cells" "1 cell = 1 hour")
tmp="$OUTPUT_PATH.tmp" tmp="$OUTPUT_PATH.tmp"
{ {
printf '# updated %s\n\n' "$(date -u -d "@$now" '+%Y-%m-%d %H:%M:%S UTC')" printf '# updated %s\n' "$(date -u -d "@$now" '+%Y-%m-%d %H:%M:%S UTC')"
if [ -s "$INTRO_PATH" ]; then
intro_content=$(cat "$INTRO_PATH")
printf '\n%s\n' "$intro_content"
fi
n_cats=${#cat_intervals[@]}
for (( cat_idx = 0; cat_idx < n_cats; cat_idx++ )); do
desc=${cat_descriptions[$cat_idx]}
interval=${cat_intervals[$cat_idx]}
printf '\n'
if [ -n "$desc" ]; then
printf '%s (probed every %s)\n\n' "$desc" "$(human_interval "$interval")"
fi
printf "%-${name_col}s%s %s\n" '' "$day_scale" "$hour_scale" printf "%-${name_col}s%s %s\n" '' "$day_scale" "$hour_scale"
while IFS= read -r line; do
[ -z "$line" ] && continue while IFS=$'\t' read -r tag c_idx name url; do
name=${line%% *} [ "$tag" != "SVC" ] && continue
url=${line#* } [ "$c_idx" != "$cat_idx" ] && continue
log="$state_dir/$name.log" log="$state_dir/$name.log"
read -r day_bar day_pct _ < <(render_row "$log" "$now" "$day_bucket" "$day_bar_cells") read -r day_bar day_pct _ < <(render_row "$log" "$now" "$day_bucket" "$day_bar_cells")
read -r hour_bar _ state < <(render_row "$log" "$now" "$hour_bucket" "$hour_bar_cells") read -r hour_bar _ state < <(render_row "$log" "$now" "$hour_bucket" "$hour_bar_cells")
if [ "${cat_hideurls[$cat_idx]}" = "1" ]; then
printf "%-${name_col}s%s %s %s %s%%\n" \
"$name" "$day_bar" "$hour_bar" "$state" "$day_pct"
else
printf "%-${name_col}s%s %s %s %s%% %s\n" \ printf "%-${name_col}s%s %s %s %s%% %s\n" \
"$name" "$day_bar" "$hour_bar" "$state" "$day_pct" "$url" "$name" "$day_bar" "$hour_bar" "$state" "$day_pct" "$url"
done <<< "$SERVICES" fi
done < "$CONFIG_PATH"
printf "%-${name_col}s%s %s\n" '' \ printf "%-${name_col}s%s %s\n" '' "$day_label" "$hour_label"
"$(center "$day_bar_cells" "1 cell = $day_unit")" \ done
"$(center "$hour_bar_cells" "1 cell = 1 hour")"
printf '\nlegend: = up - degraded _ down . no data\n' printf '\nlegend: = up - degraded _ down . no data\n'
} > "$tmp" } > "$tmp"