feat(package): sentinèlla: sentinel: loging

This commit is contained in:
2025-10-19 15:16:38 +00:00
parent abdc808693
commit dbe4dfc2bc
11 changed files with 376 additions and 180 deletions

View File

@@ -0,0 +1,54 @@
NC='\033[0m'
# Regular text colors
BLACK='\033[30m'
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
MAGENTA='\033[35m'
CYAN='\033[36m'
WHITE='\033[37m'
# Bright text colors
BBLACK='\033[90m'
BRED='\033[91m'
BGREEN='\033[92m'
BYELLOW='\033[93m'
BBLUE='\033[94m'
BMAGENTA='\033[95m'
BCYAN='\033[96m'
BWHITE='\033[97m'
# Background colors
BG_BLACK='\033[40m'
BG_RED='\033[41m'
BG_GREEN='\033[42m'
BG_YELLOW='\033[43m'
BG_BLUE='\033[44m'
BG_MAGENTA='\033[45m'
BG_CYAN='\033[46m'
BG_WHITE='\033[47m'
# Bright background colors
BG_BBLACK='\033[100m'
BG_BRED='\033[101m'
BG_BGREEN='\033[102m'
BG_BYELLOW='\033[103m'
BG_BBLUE='\033[104m'
BG_BMAGENTA='\033[105m'
BG_BCYAN='\033[106m'
BG_BWHITE='\033[107m'
# Text effects
RESET='\033[0m'
BOLD='\033[1m'
DIM='\033[2m'
ITALIC='\033[3m'
UNDERLINE='\033[4m'
BLINK='\033[5m'
INVERSE='\033[7m'
HIDDEN='\033[8m'
STRIKE='\033[9m'
: "$NC" "$BLACK" "$RED" "$GREEN" "$YELLOW" "$BLUE" "$MAGENTA" "$CYAN" "$WHITE" "$BBLACK" "$BRED" "$BGREEN" "$BYELLOW" "$BBLUE" "$BMAGENTA" "$BCYAN" "$BWHITE" "$BG_BLACK" "$BG_RED" "$BG_GREEN" "$BG_YELLOW" "$BG_BLUE" "$BG_MAGENTA" "$BG_CYAN" "$BG_WHITE" "$BG_BBLACK" "$BG_BRED" "$BG_BGREEN" "$BG_BYELLOW" "$BG_BBLUE" "$BG_BMAGENTA" "$BG_BCYAN" "$BG_BWHITE" "$RESET" "$BOLD" "$DIM" "$ITALIC" "$UNDERLINE" "$BLINK" "$INVERSE" "$HIDDEN" "$STRIKE"

View File

@@ -1,4 +1,4 @@
{ symlinkJoin, writeShellApplication, socat, dash, hectic, curl, gawk }:
{ symlinkJoin, writeTextFile, socat, dash, hectic, curl, gawk }:
let
shell = "${dash}/bin/dash";
bashOptions = [
@@ -31,7 +31,12 @@ let
inherit shell bashOptions;
name = "sentinel";
runtimeInputs = [ hectic.shellplot curl ];
text = builtins.readFile ./sentinel.sh;
text = ''
${builtins.readFile ./log.sh}
${builtins.readFile ./colors.sh}
${builtins.readFile ./sentinel.sh}
'';
};
in
symlinkJoin {

View File

@@ -0,0 +1,21 @@
#!/bin/dash
log() {
level="$1"; shift
case "$level" in
trace) color="$MAGENTA" ;;
debug) color="$BLUE" ;;
info) color="$GREEN" ;;
notice) color="$CYAN" ;;
warn) color="$YELLOW" ;;
error) color="$RED" ;;
*) color="$WHITE" ;;
esac
# shellcheck disable=SC1003
fmt="$(printf "%s" "$1" | sed 's/\\033\[0m/''\'"$color"'/g')"
shift
printf "%b\n" "$color$fmt$NC" "$@"
}

View File

@@ -3,23 +3,35 @@
# Env:
# SERVERS="http://host1:8080,http://host2:8080"
# TOKENS="-,b64token2" # CSV aligned with SERVERS; "-" means no auth
# TOKEN="..." # Telegram bot token
# CHAT_ID="..." # Telegram chat id
# TG_TOKEN="..." # Telegram bot token
# TG_CHAT_ID="..." # Telegram chat id
# TIMEOUT=5 # curl timeout seconds (default 5)
# POLLING_INTERVAL_SEC=3 # default 3
# STATE_DIR=/tmp/sentinel # default /tmp/sentinel
# STATE_DIR=/var/lib/sentinel # default /var/lib/sentinel
# SPAM=0 # if 1 will notify every poling, default 0
set -eu
TIMEOUT=${TIMEOUT:-5}
POLLING_INTERVAL_SEC=${POLLING_INTERVAL_SEC:-3}
STATE_DIR=${STATE_DIR:-$(mktemp -d)}
SERVERS=${SERVERS:-}
TOKENS=${TOKENS:-}
TOKEN=${TOKEN:-}
CHAT_ID=${CHAT_ID:-}
SPAM=${SPAM:-0}
STATE_DIR=${STATE_DIR:-/var/lib/sentinel}
mkdir -p "$STATE_DIR" 2>/dev/null || {
# TODO: some sort of message?
STATE_DIR="$HOME/.local/$(basename "$STATE_DIR")"
mkdir -p "$STATE_DIR"
}
mkdir -p "$STATE_DIR" 2>/dev/null || mkdir -p "$HOME/.local/$(basename "$STATE_DIR")"
[ -n "$SERVERS" ] || { printf >&2 'SERVERS not set\n'; exit 1; }
[ -n "$TOKEN" ] || { printf >&2 'TOKEN not set\n'; exit 1; }
[ -n "$CHAT_ID" ] || { printf >&2 'CHAT_ID not set\n'; exit 1; }
# If TOKENS unset, synthesize "-" for each server
if [ -z "$TOKENS" ]; then
@@ -27,13 +39,11 @@ if [ -z "$TOKENS" ]; then
TOKENS=$(awk -v n="$n" 'BEGIN{for(i=1;i<=n;i++){printf("-"); if(i<n)printf(",")}}')
fi
mkdir -p "$STATE_DIR"
# --- helpers ---
# get_csv VAR idx -> echo idx-th field (1-based) from CSV string VAR
# get_csv(csv_variable, index)
# echo idx-th field (1-based) from CSV string VAR
get_csv() {
# shellcheck disable=SC2001
printf '%s' "$1" | sed 's/,/\n/g' | awk -v n="$2" 'NR==n{print; exit}'
}
@@ -48,6 +58,7 @@ notify() {
fi
}
# sid(text)
sid() { printf '%s' "$1" | cksum | awk '{print $1}'; }
parse_summary() {
@@ -65,6 +76,7 @@ list_failures() {
# --- main loop ---
while :; do
log info "pooling ${WHITE}${POLLING_INTERVAL_SEC}${NC} sec"
i=1
while :; do
srv=$(get_csv "$SERVERS" "$i") || true
@@ -79,6 +91,8 @@ while :; do
code=$(sh -c "curl -sS -m \"$TIMEOUT\" -w '%{http_code}' -o \"$tmpb\" $auth_h \"$url\"") || code="000"
body=$(cat "$tmpb"); rm -f "$tmpb"
log info "server ${WHITE}${srv}${NC}\ncode ${WHITE}${code}${NC}\nbody ${WHITE}${body}${NC}"
ok="down"; tot=0; good=0
if [ "$code" = "200" ]; then
s=$(printf '%s' "$body" | parse_summary || true)