feat(package): sentinel: some changes
This commit is contained in:
@@ -104,14 +104,14 @@
|
|||||||
inherit system;
|
inherit system;
|
||||||
modules = [
|
modules = [
|
||||||
self.nixosModules.hectic
|
self.nixosModules.hectic
|
||||||
{ services.hardware.lenovo-ideapad-15arh7 = opts; }
|
{ hectic.hardware.lenovo-ideapad-15arh7 = opts; }
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
cases = {
|
cases = {
|
||||||
enable = { enable = true; };
|
#enable = { enable = true; };
|
||||||
disabled = { enable = false; };
|
#disabled = { enable = false; };
|
||||||
customFoo = { enable = true; foo = "bar"; };
|
#customFoo = { enable = true; foo = "bar"; };
|
||||||
};
|
};
|
||||||
in nixpkgs.lib.mapAttrs
|
in nixpkgs.lib.mapAttrs
|
||||||
(name: opts: (mkSys system opts).config.system.build.toplevel) cases;
|
(name: opts: (mkSys system opts).config.system.build.toplevel) cases;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#!/bin/dash
|
#!/bin/dash
|
||||||
|
|
||||||
# requirements: ssh-to-age nixos-anywhere
|
# requirements: ssh-to-age nixos-anywhere
|
||||||
# jjjn
|
|
||||||
|
|
||||||
# ssh proxydoe 'cat /etc/os-release 2>/dev/null || echo "no /etc/os-release"' | grep '^NAME=NixOS$'
|
# ssh proxydoe 'cat /etc/os-release 2>/dev/null || echo "no /etc/os-release"' | grep '^NAME=NixOS$'
|
||||||
# NAME=NixOS
|
# NAME=NixOS
|
||||||
|
|||||||
@@ -12,6 +12,26 @@
|
|||||||
# TIMEOUT=5
|
# TIMEOUT=5
|
||||||
# AUTH_FILE="/path/htpasswd-like" # lines: user:pass
|
# AUTH_FILE="/path/htpasswd-like" # lines: user:pass
|
||||||
|
|
||||||
|
# output example
|
||||||
|
# {
|
||||||
|
# "checks": [],
|
||||||
|
# "summary": {
|
||||||
|
# "total": 0,
|
||||||
|
# "ok": 0
|
||||||
|
# }
|
||||||
|
# "disk": {
|
||||||
|
# "volumes": [
|
||||||
|
# {
|
||||||
|
# "mount": "/dev",
|
||||||
|
# "size_blocks": 195760,
|
||||||
|
# "used": 0,
|
||||||
|
# "avail": 195760,
|
||||||
|
# "use_percent": "0%"
|
||||||
|
# }
|
||||||
|
# ]
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
|
||||||
: "${TIMEOUT:=5}"
|
: "${TIMEOUT:=5}"
|
||||||
: "${VOLUMES:=$(df -P | awk 'NR>1{print $6}')}"
|
: "${VOLUMES:=$(df -P | awk 'NR>1{print $6}')}"
|
||||||
|
|
||||||
|
|||||||
13
package/sentinèlla/scheme.sql
Normal file
13
package/sentinèlla/scheme.sql
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
CREATE TABLE status (
|
||||||
|
id INTEGER PRIMARY KEY, -- integer in sqlite almoust infinity
|
||||||
|
time DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
status TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE disk (
|
||||||
|
id INTEGER PRIMARY KEY, -- integer in sqlite almoust infinity
|
||||||
|
time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
server TEXT NOT NULL,
|
||||||
|
disk TEXT NOT NULL,
|
||||||
|
space INTEGER NOT NULL
|
||||||
|
);
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
#!/bin/dash
|
#!/bin/dash
|
||||||
# sentinel.sh — polls probe backends (/status) and notifies on status change via Telegram
|
# sentinel.sh — polls probe backends (/status) and notifies on status change via Telegram
|
||||||
|
# requirements:
|
||||||
|
# curl, awk, sqlite, jq
|
||||||
# Env:
|
# Env:
|
||||||
# SERVERS="http://host1:8080,http://host2:8080"
|
# SERVERS="http://host1:8080,http://host2:8080"
|
||||||
# TOKENS="-,b64token2" # CSV aligned with SERVERS; "-" means no auth
|
# TOKENS="-,b64token2" # CSV aligned with SERVERS; "-" means no auth
|
||||||
@@ -12,6 +14,24 @@
|
|||||||
|
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
|
#SQLITE_SCHEME="
|
||||||
|
#CREATE TABLE IF NOT EXISTS status (
|
||||||
|
# id INTEGER PRIMARY KEY, -- integer in sqlite almoust infinity
|
||||||
|
# time DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
# status TEXT NOT NULL
|
||||||
|
#);
|
||||||
|
#
|
||||||
|
#CREATE TABLE IF NOT EXISTS disk (
|
||||||
|
# id INTEGER PRIMARY KEY, -- integer in sqlite almoust infinity
|
||||||
|
# time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
# server TEXT NOT NULL,
|
||||||
|
# disk TEXT NOT NULL,
|
||||||
|
# space INTEGER NOT NULL
|
||||||
|
#);"
|
||||||
|
|
||||||
|
PREFIX_OK="OK "
|
||||||
|
PREFIX_FAIL="FAIL"
|
||||||
|
|
||||||
TIMEOUT=${TIMEOUT:-5}
|
TIMEOUT=${TIMEOUT:-5}
|
||||||
POLLING_INTERVAL_SEC=${POLLING_INTERVAL_SEC:-3}
|
POLLING_INTERVAL_SEC=${POLLING_INTERVAL_SEC:-3}
|
||||||
SERVERS=${SERVERS:-}
|
SERVERS=${SERVERS:-}
|
||||||
@@ -48,14 +68,14 @@ get_csv() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
notify() {
|
notify() {
|
||||||
msg=$1
|
msg=${1:?}
|
||||||
if [ -n "$TOKEN" ] && [ -n "$CHAT_ID" ]; then
|
if [ -n "$TOKEN" ] && [ -n "$CHAT_ID" ]; then
|
||||||
curl -sS -m "$TIMEOUT" -X POST "https://api.telegram.org/bot${TOKEN}/sendMessage" \
|
curl -sS -m "$TIMEOUT" -X POST "https://api.telegram.org/bot${TOKEN}/sendMessage" \
|
||||||
-d "chat_id=${CHAT_ID}" \
|
-d "chat_id=${CHAT_ID}" \
|
||||||
--data-urlencode "text=${msg}" >/dev/null || printf >&2 'notify failed: %s\n' "$msg"
|
--data-urlencode "text=${msg}" >/dev/null || log error "notify failed: $msg"
|
||||||
else
|
|
||||||
printf >&2 '%s\n' "$msg"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
log notice "notify message: ${WHITE}${msg}${NC}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# sid(text)
|
# sid(text)
|
||||||
@@ -78,7 +98,7 @@ list_failures() {
|
|||||||
|
|
||||||
# server_status_message(msg_prefix, server, ok_amount, total_amount, fail_list)
|
# server_status_message(msg_prefix, server, ok_amount, total_amount, fail_list)
|
||||||
server_status_message() {
|
server_status_message() {
|
||||||
msg=$(printf '%s: %s [%s/%s]%s' "${1:?}" "${2:?}" "${3:?}" "${4:?}" "$5")
|
printf '%s: %s [%s/%s]%s' "${1:?}" "${2:?}" "${3:?}" "${4:?}" "$5"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- main loop ---
|
# --- main loop ---
|
||||||
@@ -102,26 +122,26 @@ while :; do
|
|||||||
code=$(sh -c "curl -sS -m \"$TIMEOUT\" -w '%{http_code}' -o \"$tmpb\" $auth_h \"$url\"") || code="000"
|
code=$(sh -c "curl -sS -m \"$TIMEOUT\" -w '%{http_code}' -o \"$tmpb\" $auth_h \"$url\"") || code="000"
|
||||||
body=$(cat "$tmpb"); rm -f "$tmpb"
|
body=$(cat "$tmpb"); rm -f "$tmpb"
|
||||||
|
|
||||||
log info "server ${WHITE}${srv}${NC}\ncode ${WHITE}${code}${NC}\nbody ${WHITE}${body}${NC}"
|
#log info "server ${WHITE}${srv}${NC}\ncode ${WHITE}${code}${NC}\nbody ${WHITE}${body}${NC}"
|
||||||
|
|
||||||
ok="down"; tot=0; good=0
|
ok="down"; total=0; good=0
|
||||||
if [ "$code" = "200" ]; then
|
if [ "$code" = "200" ]; then
|
||||||
s=$(printf '%s' "$body" | parse_summary || true)
|
summary=$(printf '%s' "$body" | parse_summary || true)
|
||||||
[ -n "$s" ] && { tot=${s%% *}; good=${s#* }; }
|
[ -n "$summary" ] && { total=${summary%% *}; good=${summary#* }; }
|
||||||
[ "$tot" -eq "$good" ] && ok="up"
|
[ "$total" -eq "$good" ] && ok="up"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
msg_prefix=$( [ "$ok" = "up" ] && printf 'OK' || printf 'FAIL' )
|
msg_prefix=$( [ "$ok" = "up" ] && printf '%s' "$PREFIX_OK" || printf '%s' "$PREFIX_FAIL" )
|
||||||
fail_list=""
|
fail_list=""
|
||||||
if [ "$ok" = "down" ] && [ -n "$body" ]; then
|
if [ "$ok" = "down" ] && [ -n "$body" ]; then
|
||||||
fails=$(printf '%s' "$body" | list_failures | sed 's/[ ]$//')
|
fails=$(printf '%s' "$body" | list_failures | sed 's/[ ]$//')
|
||||||
[ -n "$fails" ] && fail_list=" — ${fails}"
|
[ -n "$fails" ] && fail_list=" — ${fails}"
|
||||||
fi
|
fi
|
||||||
msg=$(server_status_message "$msg_prefix" "$srv" "$good" "$tot" "$fail_list")
|
msg=$(server_status_message "$msg_prefix" "$srv" "$good" "$total" "$fail_list")
|
||||||
|
|
||||||
sfile="${STATE_DIR}/$(sid "$srv").state"
|
sfile="${STATE_DIR}/$(sid "$srv").state"
|
||||||
last=""; [ -f "$sfile" ] && last=$(cat "$sfile")
|
last=""; [ -f "$sfile" ] && last=$(cat "$sfile")
|
||||||
cur="${ok}:${good}/${tot}:${code}"
|
cur="${ok}:${good}/${total}:${code}"
|
||||||
if [ "$cur" != "$last" ]; then
|
if [ "$cur" != "$last" ]; then
|
||||||
notify "$msg"
|
notify "$msg"
|
||||||
printf '%s' "$cur" >"$sfile"
|
printf '%s' "$cur" >"$sfile"
|
||||||
|
|||||||
Reference in New Issue
Block a user