diff --git a/deployments/alerts/README.md b/deployments/alerts/README.md index daeeaa93..98118c6d 100644 --- a/deployments/alerts/README.md +++ b/deployments/alerts/README.md @@ -1,6 +1,6 @@ # Node alerts -Two small alerts send Telegram messages from each node. Both read the same settings file and run every 5 minutes from a systemd timer. +Two small alerts send Telegram messages from each node. Both read the same settings file and run every 5 minutes from a systemd timer. `tn-node-alert` can also watch the whole network, from a node or from a host that runs no node. ## tn-pg-alert: Postgres runs a program @@ -16,9 +16,46 @@ Each run reads the container's log lines written since the last run that sent it - Docker restarted the kwild container on its own, which means it crashed. A manual restart or a recreate does not count. - the kwild container is not running. +These messages go to the Telegram chat and the system journal. With `NODE_ALERTS=journal` they go to the journal only (`journalctl -t tn-node-alert`), and the chat hears nothing about this node. With `NODE_CONTAINER=none` the script skips these checks, for a host that only watches the network. + +## tn-node-alert: the network is down + +Set `GATEWAY_RPC` to the JSON-RPC address of the network's gateway, and each run also checks the network. It reads the block height from the gateway and from each node in `NODE_RPCS`, and keeps the time each one last showed a new block. A target that does not answer keeps its last height, so its clock keeps running. The network is down when: + +- the gateway has shown no new block for `STALL_MINUTES`, or +- every node in `NODE_RPCS` has shown no new block for `STALL_MINUTES`, and the gateway has no block newer than theirs. + +The gateway decides: while it serves blocks newer than the listed nodes have, the network counts as up, even if none of those nodes answers. + +While the network is down, a message goes out and repeats every `REPEAT_MINUTES`. One more message says when blocks move again and how long they stopped. For example: + +```text +TN DOWN: no new block through the gateway for 37 min, last height 3229140 seen at 09:18 UTC. The gateway is not answering. node-1: 3229140 (not answering), node-2: 3229140. +TN DOWN: no node has a new block for 36 min (node-1: 3229140 at 09:18 UTC, node-2: 3229139 at 09:18 UTC). The gateway still answers at height 3229140. +TN OK: blocks are moving again through the gateway, height 3229190, after 41 min. +``` + +These messages always go to the chat and the journal, whatever `NODE_ALERTS` says. A message that fails to send goes out on the next run. If the chain starts again from a lower height, delete `/var/lib/tn-alert/network-state`, or the network looks down until it passes the old height. + +## A chat shared with other teams + +When other teams read the chat too, let it hear only about network outages: + +1. On one host outside the nodes, install the alerts (see below) and add to its `/etc/tn-alert/telegram.env`: + + ```bash + GATEWAY_RPC=https://gateway.example.com/rpc/v1 + NODE_RPCS='node-1=http://10.0.0.1:8484/rpc/v1 node-2=http://10.0.0.2:8484/rpc/v1' + NODE_CONTAINER=none + STALL_MINUTES=35 + ``` + + The host must reach every address in `NODE_RPCS`. It runs no Postgres, so turn its `tn-pg-alert` off: `sudo systemctl disable --now tn-pg-alert.timer` +2. On each node, add `NODE_ALERTS=journal`. `tn-pg-alert` on the node still sends to the chat. + ## Install on a node -From a checkout of this repository on the node: +From a checkout of this repository on the node, or on the host that watches the network: ```bash cd deployments/alerts @@ -40,7 +77,7 @@ The script installs `/usr/local/bin/tn-pg-alert` and `/usr/local/bin/tn-node-ale TELEGRAM_TOPIC_ID=42 ``` -4. Check that messages arrive: `sudo tn-pg-alert --test` and `sudo tn-node-alert --test` +4. Check that messages arrive: `sudo tn-pg-alert --test` and `sudo tn-node-alert --test`. The test message goes to the chat even with `NODE_ALERTS=journal`. No restart is needed. The next timer run reads the file. @@ -52,8 +89,16 @@ No restart is needed. The next timer run reads the file. | `TELEGRAM_CHAT_ID` | empty | Chat that receives the alerts | | `TELEGRAM_TOPIC_ID` | empty | Topic in a chat with topics, where the alerts go | | `PG_CONTAINER` | `tn-postgres` | Name of the node's Postgres container | -| `NODE_CONTAINER` | `tn-node` | Name of the node's kwild container | +| `NODE_CONTAINER` | `tn-node` | Name of the node's kwild container, or `none` to skip the checks of this node | | `NODE_RPC` | `http://127.0.0.1:8484/rpc/v1` | The node's JSON-RPC address | -| `STALL_MINUTES` | `10` | Minutes without a new block before the first alert | -| `REPEAT_MINUTES` | `60` | Minutes between repeats while a stall lasts | +| `NODE_ALERTS` | `chat` | Where this node's messages go: `chat` (the chat and the journal) or `journal` (the journal only) | | `NODE_LABEL` | the hostname | Node name shown in the message | +| `GATEWAY_RPC` | empty | The gateway's JSON-RPC address. Set it to watch the network | +| `NODE_RPCS` | empty | Nodes to watch with the gateway, as `label=url` pairs separated by spaces | +| `NETWORK_LABEL` | `TN` | Network name shown in the network messages | +| `STALL_MINUTES` | `10` | Minutes without a new block before the first alert, for the node and for the network | +| `REPEAT_MINUTES` | `60` | Minutes between repeats while a stall or an outage lasts | + +## Test + +`./node-health-alert.test.sh` runs the script against a local fake RPC server and a fake Telegram. It needs bash, python3 and curl. diff --git a/deployments/alerts/node-health-alert.sh b/deployments/alerts/node-health-alert.sh index 6b48cd35..f83d95ac 100755 --- a/deployments/alerts/node-health-alert.sh +++ b/deployments/alerts/node-health-alert.sh @@ -1,20 +1,33 @@ #!/usr/bin/env bash -# Sends a Telegram message when this node stops moving: its block height has -# not changed for STALL_MINUTES, or Docker restarted its kwild container on its -# own (a crash), or the container is not running. A stall alert repeats every +# Watches this node and, when GATEWAY_RPC is set, the whole network. +# +# This node: a message when its block height has not changed for STALL_MINUTES, +# when Docker restarted its kwild container on its own (a crash), or when the +# container is not running. A stall message repeats every REPEAT_MINUTES while +# it lasts, and one message says when blocks move again. These messages go to +# Telegram, or to the system journal only with NODE_ALERTS=journal. +# +# The network: a message when it is down, meaning the gateway has shown no new +# block for STALL_MINUTES, or every node in NODE_RPCS has shown none for +# STALL_MINUTES and the gateway has no block newer than theirs. It repeats every # REPEAT_MINUTES while it lasts, and one message says when blocks move again. +# These messages always go to Telegram. # # Settings come from /etc/tn-alert/telegram.env, shared with tn-pg-alert: # TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID where the message goes # TELEGRAM_TOPIC_ID topic (thread) in a forum chat, optional -# NODE_CONTAINER kwild container, default tn-node +# NODE_CONTAINER kwild container, default tn-node; none skips this node # NODE_RPC JSON-RPC URL, default http://127.0.0.1:8484/rpc/v1 +# NODE_ALERTS where this node's messages go: chat (default) or journal +# NODE_LABEL node name in the message, default the hostname +# GATEWAY_RPC gateway JSON-RPC URL; set it to watch the network +# NODE_RPCS nodes to watch with it, label=url pairs split by spaces +# NETWORK_LABEL network name in the message, default TN # STALL_MINUTES default 10 # REPEAT_MINUTES default 60 -# NODE_LABEL node name in the message, default the hostname -# Without a token and a chat id, the alert goes to the system journal only. +# Without a token and a chat id, every message goes to the system journal only. # -# Usage: tn-node-alert check the node +# Usage: tn-node-alert check the node and the network # tn-node-alert --test send a test message set -euo pipefail @@ -26,9 +39,19 @@ if [ -r "$ENV_FILE" ]; then fi NODE_CONTAINER=${NODE_CONTAINER:-tn-node} NODE_RPC=${NODE_RPC:-http://127.0.0.1:8484/rpc/v1} +NODE_ALERTS=${NODE_ALERTS:-chat} STALL_MINUTES=${STALL_MINUTES:-10} REPEAT_MINUTES=${REPEAT_MINUTES:-60} NODE_LABEL=${NODE_LABEL:-$(hostname)} +GATEWAY_RPC=${GATEWAY_RPC:-} +NODE_RPCS=${NODE_RPCS:-} +NETWORK_LABEL=${NETWORK_LABEL:-TN} +# For tests: another Telegram server, and a fixed time (TN_ALERT_NOW, Unix seconds). +TELEGRAM_API=${TELEGRAM_API:-https://api.telegram.org} +if [ "$NODE_ALERTS" != chat ] && [ "$NODE_ALERTS" != journal ]; then + echo "tn-node-alert: NODE_ALERTS must be chat or journal, not ${NODE_ALERTS}" >&2 + exit 2 +fi send() { local text=$1 code topic=() logged=0 @@ -41,7 +64,7 @@ send() { topic=(--data-urlencode "message_thread_id=${TELEGRAM_TOPIC_ID}") fi # The URL holds the token, so it goes through stdin, not the command line. - code=$(printf 'url = "https://api.telegram.org/bot%s/sendMessage"\n' "$TELEGRAM_BOT_TOKEN" | + code=$(printf 'url = "%s/bot%s/sendMessage"\n' "$TELEGRAM_API" "$TELEGRAM_BOT_TOKEN" | curl -sS -m 20 -o /dev/null -w '%{http_code}' -K - \ --data-urlencode "chat_id=${TELEGRAM_CHAT_ID}" ${topic[@]+"${topic[@]}"} \ --data-urlencode "text=${text}") || code=000 @@ -51,14 +74,176 @@ send() { fi } +# Messages about this node alone: to the chat, or with NODE_ALERTS=journal to +# the journal only. +node_send() { + if [ "$NODE_ALERTS" = journal ]; then + logger -t tn-node-alert -- "$1" + else + send "$1" + fi +} + +# Prints the block height that user.health reports at the JSON-RPC URL $1, +# or nothing when it does not answer. +rpc_height() { + curl -s -m 10 -X POST "$1" -H 'content-type: application/json' \ + -d '{"jsonrpc":"2.0","id":1,"method":"user.health","params":{}}' 2>/dev/null | + grep -o '"block_height"[[:space:]]*:[[:space:]]*[0-9]*' | head -1 | grep -o '[0-9]*$' || true +} + +hhmm() { date -u -d "@$1" +%H:%M; } + if [ "${1:-}" = "--test" ]; then send "${NODE_LABEL}: test message from tn-node-alert" exit fi mkdir -p "$STATE_DIR" +now=${TN_ALERT_NOW:-$(date -u +%s)} +failed=0 + +# probe URL: reads a network target and sets h (the highest height it has +# shown), at (when that height first showed) and up (no when it did not +# answer). A target that stops answering keeps its clock running. +probe() { + local got + got=$(rpc_height "$1") + h=${seen_h[$1]:-0} at=${seen_at[$1]:-$now} up=yes + if [ -z "$got" ]; then + up=no + elif [ "$got" -gt "$h" ]; then + h=$got at=$now + fi + seen_h[$1]=$h seen_at[$1]=$at +} + +# Saved state: when the last network alert went out (0 when none is open), when +# the last new block came before that alert, the minutes of an outage whose +# recovery message has not gone out yet (0 when none), then one line per +# target: its URL, highest height and when that height first showed. +check_network() { + local file=$STATE_DIR/network-state alerted_at=0 last_block=0 recovery=0 + local url h at up v label spec stalled since down="" msg why best=0 + local gw_h gw_at gw_up node_h=0 node_at=0 count=0 failing=0 gw_list="" node_list="" + local -a nodes=() urls=() + local -A seen_h=() seen_at=() + if [ -r "$file" ]; then + { + read -r alerted_at last_block recovery || true + while read -r url h at; do + if [[ $h =~ ^[0-9]+$ && $at =~ ^[0-9]+$ ]]; then + seen_h[$url]=$h seen_at[$url]=$at + fi + done + } <"$file" + fi + for v in alerted_at last_block recovery; do + [[ ${!v} =~ ^[0-9]+$ ]] || printf -v "$v" '%s' 0 + done + # The highest block any target showed before this run. An open outage + # closes only on a block above it, so a new or replaced target that shows + # an old height does not count as the network moving. + for h in ${seen_h[@]+"${seen_h[@]}"}; do + [ "$h" -le "$best" ] || best=$h + done + + probe "$GATEWAY_RPC" + gw_h=$h gw_at=$at gw_up=$up + urls=("$GATEWAY_RPC") + read -r -d '' -a nodes <<<"$NODE_RPCS" || true + for spec in ${nodes[@]+"${nodes[@]}"}; do + label=${spec%%=*} url=${spec#*=} + probe "$url" + urls+=("$url") + count=$((count + 1)) + [ $(((now - at) / 60)) -lt "$STALL_MINUTES" ] || failing=$((failing + 1)) + [ "$h" -le "$node_h" ] || node_h=$h + [ "$at" -le "$node_at" ] || node_at=$at + why="" + [ "$up" = yes ] || why=" (not answering)" + if [ "$h" -gt 0 ]; then + gw_list+="${gw_list:+, }${label}: ${h}${why}" + node_list+="${node_list:+, }${label}: ${h} at $(hhmm "$at") UTC${why}" + else + gw_list+="${gw_list:+, }${label}: no answer yet" + node_list+="${node_list:+, }${label}: no answer yet" + fi + done + + # The gateway decides: a block newer than every listed node's means the + # network moves, even when none of those nodes answers. + if [ $(((now - gw_at) / 60)) -ge "$STALL_MINUTES" ]; then + down=gateway since=$gw_at + msg="${NETWORK_LABEL} DOWN: no new block through the gateway for $(((now - since) / 60)) min, " + if [ "$gw_h" -gt 0 ]; then + msg+="last height ${gw_h} seen at $(hhmm "$gw_at") UTC." + else + msg+="no height seen yet." + fi + [ "$gw_up" = yes ] || msg+=" The gateway is not answering." + [ -z "$gw_list" ] || msg+=" ${gw_list}." + elif [ "$count" -gt 0 ] && [ "$failing" -eq "$count" ] && [ "$gw_h" -le "$node_h" ]; then + down=nodes since=$node_at + msg="${NETWORK_LABEL} DOWN: no node has a new block for $(((now - since) / 60)) min (${node_list})." + if [ "$gw_up" = yes ]; then + msg+=" The gateway still answers at height ${gw_h}." + else + msg+=" The gateway is not answering." + fi + fi + + if [ -n "$down" ]; then + if [ "$alerted_at" -eq 0 ] || [ $(((now - alerted_at) / 60)) -ge "$REPEAT_MINUTES" ]; then + if send "$msg"; then + [ "$alerted_at" -gt 0 ] || last_block=$since recovery=0 + alerted_at=$now + else + failed=1 + fi + fi + else + # Ages alone can look fresh without a new block (a replaced gateway + # address, a clock that stepped back), so an open outage closes only + # when some target shows a block above the best one seen before. + if [ "$alerted_at" -gt 0 ] && { [ "$gw_h" -gt "$best" ] || [ "$node_h" -gt "$best" ]; }; then + stalled=$(((now - last_block) / 60)) + recovery=$((stalled > 0 ? stalled : 1)) + alerted_at=0 + fi + # A recovery message that fails is kept and sent again on the next run. + if [ "$recovery" -gt 0 ]; then + if [ "$gw_up" = yes ]; then + msg="${NETWORK_LABEL} OK: blocks are moving again through the gateway, height ${gw_h}, after ${recovery} min." + else + msg="${NETWORK_LABEL} OK: blocks are moving again, height $((gw_h > node_h ? gw_h : node_h)), after ${recovery} min. The gateway is not answering." + fi + if send "$msg"; then + recovery=0 + else + failed=1 + fi + fi + fi + + { + printf '%s %s %s\n' "$alerted_at" "$last_block" "$recovery" + for url in "${urls[@]}"; do + printf '%s %s %s\n' "$url" "${seen_h[$url]}" "${seen_at[$url]}" + done + } >"$file.tmp" + sync "$file.tmp" 2>/dev/null || true + mv -f "$file.tmp" "$file" +} + +if [ -n "$GATEWAY_RPC" ]; then + check_network +fi +if [ "$NODE_CONTAINER" = none ]; then + exit "$failed" +fi + state="$STATE_DIR/node-state" -now=$(date -u +%s) # Saved state: height, when it last changed, restart count, when the last stall # alert went out (0 when none is open), and the minutes of a stall whose @@ -72,27 +257,23 @@ for v in last_height changed_at last_restarts alerted_at recovery; do done [ "$changed_at" -gt 0 ] || changed_at=$now -failed=0 - # Crashes: Docker counts only the restarts its restart policy made, so a # manual restart or a recreate does not alert. A recreate resets the count. if info=$(docker inspect -f '{{.RestartCount}} {{.State.Status}} {{.State.ExitCode}}' "$NODE_CONTAINER" 2>/dev/null); then read -r restarts status exit_code <<<"$info" if [ "$status" != running ]; then - send "${NODE_LABEL}: ${NODE_CONTAINER} is not running (status ${status}, last exit code ${exit_code})." || failed=1 + node_send "${NODE_LABEL}: ${NODE_CONTAINER} is not running (status ${status}, last exit code ${exit_code})." || failed=1 elif [ "$last_restarts" -ge 0 ] && [ "$restarts" -gt "$last_restarts" ]; then - send "${NODE_LABEL}: ${NODE_CONTAINER} restarted by itself $((restarts - last_restarts)) time(s) since the last check (${restarts} in total). Check: docker logs --tail 50 ${NODE_CONTAINER}" || failed=1 + node_send "${NODE_LABEL}: ${NODE_CONTAINER} restarted by itself $((restarts - last_restarts)) time(s) since the last check (${restarts} in total). Check: docker logs --tail 50 ${NODE_CONTAINER}" || failed=1 fi else restarts=$last_restarts - send "${NODE_LABEL}: cannot inspect the container ${NODE_CONTAINER}." || failed=1 + node_send "${NODE_LABEL}: cannot inspect the container ${NODE_CONTAINER}." || failed=1 fi # Height: a node that does not answer keeps its last height, so it counts as # stalled too. -height=$(curl -s -m 10 -X POST "$NODE_RPC" -H 'content-type: application/json' \ - -d '{"jsonrpc":"2.0","id":1,"method":"user.health","params":{}}' 2>/dev/null | - grep -o '"block_height"[[:space:]]*:[[:space:]]*[0-9]*' | head -1 | grep -o '[0-9]*$') || true +height=$(rpc_height "$NODE_RPC") answering=yes if [ -z "$height" ]; then answering=no @@ -112,7 +293,7 @@ else { [ "$alerted_at" -eq 0 ] || [ $(((now - alerted_at) / 60)) -ge "$REPEAT_MINUTES" ]; }; then why="" [ "$answering" = no ] && why=" The node RPC is not answering." - if send "${NODE_LABEL}: no new block for ${stalled} min, still at height ${height}.${why}"; then + if node_send "${NODE_LABEL}: no new block for ${stalled} min, still at height ${height}.${why}"; then alerted_at=$now else failed=1 @@ -122,7 +303,7 @@ fi # A recovery message that fails is kept and sent again on the next run. if [ "$recovery" -gt 0 ]; then - if send "${NODE_LABEL}: blocks are moving again, height ${height}, after ${recovery} min without a new block."; then + if node_send "${NODE_LABEL}: blocks are moving again, height ${height}, after ${recovery} min without a new block."; then recovery=0 else failed=1 diff --git a/deployments/alerts/node-health-alert.test.sh b/deployments/alerts/node-health-alert.test.sh new file mode 100755 index 00000000..156749a9 --- /dev/null +++ b/deployments/alerts/node-health-alert.test.sh @@ -0,0 +1,231 @@ +#!/usr/bin/env bash +# Tests node-health-alert.sh against a local fake: one HTTP server answers +# user.health at / with the height in $tmp/height/ (no file: no +# answer) and records each Telegram sendMessage text. docker and logger are +# fakes too. Needs bash, python3 and curl. +# +# Usage: ./node-health-alert.test.sh +set -euo pipefail + +script=$(cd "$(dirname "$0")" && pwd)/node-health-alert.sh +tmp=$(mktemp -d) +trap 'kill "$server" 2>/dev/null || true; rm -rf "$tmp"' EXIT +mkdir -p "$tmp/bin" "$tmp/height" + +exec 3< <(exec python3 -I - "$tmp" <<'EOF' +import http.server, os, sys, urllib.parse +d = sys.argv[1] +class Fake(http.server.BaseHTTPRequestHandler): + def log_message(self, *args): + pass + def reply(self, code, body): + self.send_response(code) + self.send_header('content-length', str(len(body))) + self.end_headers() + self.wfile.write(body) + def do_POST(self): + body = self.rfile.read(int(self.headers.get('content-length', 0))).decode() + if self.path.endswith('/sendMessage'): + if os.path.exists(os.path.join(d, 'telegram-down')): + return self.reply(502, b'') + with open(os.path.join(d, 'sent'), 'a') as f: + f.write(urllib.parse.parse_qs(body)['text'][0] + '\n') + return self.reply(200, b'{"ok":true}') + try: + with open(os.path.join(d, 'height', self.path.strip('/'))) as f: + height = int(f.read()) + except (OSError, ValueError): + return self.reply(503, b'') + self.reply(200, b'{"jsonrpc":"2.0","id":1,"result":{"chain_id":"tn","block_height":%d,"healthy":true}}' % height) +server = http.server.ThreadingHTTPServer(('127.0.0.1', 0), Fake) +print(server.server_address[1], flush=True) +server.serve_forever() +EOF +) +server=$! +read -r port <&3 +rpc=http://127.0.0.1:$port + +cat >"$tmp/bin/docker" <<'EOF' +#!/bin/sh +# docker inspect prints the line in $FAKE_DIR/docker, and fails without it. +cat "$FAKE_DIR/docker" +EOF +cat >"$tmp/bin/logger" <<'EOF' +#!/bin/sh +# logger appends the message, its last argument, to $FAKE_DIR/journal. +for a; do :; done +printf '%s\n' "$a" >>"$FAKE_DIR/journal" +EOF +chmod +x "$tmp/bin/docker" "$tmp/bin/logger" + +t0=$(date -u -d '2026-11-24 09:00' +%s) +fails=0 status=0 case_dir="" run_args=() + +# new_case SETTING...: a fresh state directory and settings file, all targets silent. +new_case() { + case_dir=$(mktemp -d "$tmp/case.XXXXXX") + printf '%s\n' TELEGRAM_BOT_TOKEN=123:abc TELEGRAM_CHAT_ID=-100 NODE_LABEL=node-a "$@" >"$case_dir/env" + rm -f "$tmp/height/"* "$tmp/telegram-down" + echo "0 running 0" >"$tmp/docker" +} + +# at MINUTE [TARGET=HEIGHT|TARGET=-]...: sets what each target answers (- for +# nothing), then runs the script at that minute after 09:00 UTC. +at() { + local minute=$1 kv + shift + for kv; do + if [ "${kv#*=}" = - ]; then + rm -f "$tmp/height/${kv%%=*}" + else + echo "${kv#*=}" >"$tmp/height/${kv%%=*}" + fi + done + : >"$tmp/sent" + : >"$tmp/journal" + status=0 + PATH="$tmp/bin:$PATH" FAKE_DIR=$tmp TN_ALERT_ENV="$case_dir/env" TN_ALERT_STATE="$case_dir/state" \ + TN_ALERT_NOW=$((t0 + minute * 60)) TELEGRAM_API=$rpc \ + bash "$script" ${run_args[@]+"${run_args[@]}"} >/dev/null 2>&1 || status=$? +} + +check() { # check NAME FILE [LINE...]: FILE holds exactly these lines + local name=$1 file=$2 want="" + shift 2 + [ $# -eq 0 ] || want=$(printf '%s\n' "$@") + if [ "$(cat "$file")" = "$want" ]; then + echo "ok $name" + else + fails=$((fails + 1)) + printf 'FAIL %s\n want: %s\n got: %s\n' "$name" "$want" "$(cat "$file")" + fi +} +sent() { check "$1" "$tmp/sent" "${@:2}"; } +journal() { check "$1" "$tmp/journal" "${@:2}"; } +exited() { # exited NAME STATUS: the run exited with this status + if [ "$status" = "$2" ]; then + echo "ok $1" + else + fails=$((fails + 1)) + echo "FAIL $1: exit status $status, want $2" + fi +} + +echo "# default settings: this node's messages go to the chat, as before" +new_case "NODE_RPC=$rpc/local" +at 0 local=100; sent "first run"; journal "first run, journal"; exited "first run exits 0" 0 +at 9; sent "9 min" +at 10; sent "stall at 10 min" "node-a: no new block for 10 min, still at height 100." +journal "stall goes to the journal too" "node-a: no new block for 10 min, still at height 100." +at 15 local=-; sent "no repeat before 60 min" +at 70; sent "repeat" "node-a: no new block for 70 min, still at height 100. The node RPC is not answering." +touch "$tmp/telegram-down" +at 75 local=105; sent "recovery send fails"; exited "failed send exits 1" 1 +rm "$tmp/telegram-down" +at 80 local=110; sent "recovery retried" "node-a: blocks are moving again, height 110, after 75 min without a new block." +echo "1 running 0" >"$tmp/docker" +at 85 local=115; sent "crash" "node-a: tn-node restarted by itself 1 time(s) since the last check (1 in total). Check: docker logs --tail 50 tn-node" +echo "1 exited 137" >"$tmp/docker" +at 90 local=120; sent "not running" "node-a: tn-node is not running (status exited, last exit code 137)." +rm "$tmp/docker" +at 95 local=125; sent "cannot inspect" "node-a: cannot inspect the container tn-node." +run_args=(--test) +at 96; sent "--test" "node-a: test message from tn-node-alert" +run_args=() + +echo "# NODE_ALERTS=journal: this node's messages go to the journal only" +new_case "NODE_RPC=$rpc/local" NODE_ALERTS=journal +at 0 local=100 +at 10; sent "stall: nothing to the chat"; journal "stall in the journal" "node-a: no new block for 10 min, still at height 100." +echo "1 exited 1" >"$tmp/docker" +at 15 local=101; sent "crash and recovery: nothing to the chat"; exited "exits 0" 0 +journal "crash and recovery in the journal" "node-a: tn-node is not running (status exited, last exit code 1)." \ + "node-a: blocks are moving again, height 101, after 15 min without a new block." +new_case "NODE_RPC=$rpc/local" NODE_ALERTS=jornal +at 0 local=100; exited "unknown NODE_ALERTS exits 2" 2 + +net=("GATEWAY_RPC=$rpc/gw" "NODE_RPCS='node-1=$rpc/n1 node-2=$rpc/n2'" NODE_CONTAINER=none STALL_MINUTES=35) + +echo "# network: one node stalled 40 min while the gateway moves" +new_case "${net[@]}" +for m in 0 10 20 30 35 40; do + at "$m" gw=$((1000 + m)) n1=990 n2=$((1000 + m)) + sent "$m min: nothing to the chat"; journal "$m min: nothing in the journal"; exited "$m min: exits 0" 0 +done + +echo "# network: no node answers this host for 40 min, the gateway moves" +new_case "${net[@]}" +for m in 0 10 20 30 35 40; do + at "$m" gw=$((1000 + m)) n1=- n2=-; sent "$m min: nothing to the chat" +done + +echo "# network: every node stalled 36 min, the gateway moves ahead of them" +new_case "${net[@]}" +at 0 gw=1000 n1=1000 n2=1000 +for m in 10 20 30 36 40; do + at "$m" gw=$((1000 + m)); sent "$m min: nothing to the chat" +done + +echo "# network: the gateway stops answering" +new_case "${net[@]}" +at 0 gw=3229140 n1=3229140 n2=3229140 +at 5 gw=- n1=- +at 30; sent "30 min: nothing" +at 34; sent "34 min: nothing" +at 35; sent "35 min: DOWN" "TN DOWN: no new block through the gateway for 35 min, last height 3229140 seen at 09:00 UTC. The gateway is not answering. node-1: 3229140 (not answering), node-2: 3229140." +at 40; sent "next run: nothing" +at 94; sent "94 min: nothing" +at 95; sent "repeat after 60 min" "TN DOWN: no new block through the gateway for 95 min, last height 3229140 seen at 09:00 UTC. The gateway is not answering. node-1: 3229140 (not answering), node-2: 3229140." +at 100 gw=3229190; sent "OK" "TN OK: blocks are moving again through the gateway, height 3229190, after 100 min." +at 105 gw=3229195; sent "OK only once" + +echo "# network: every node stalled 36 min, the gateway answers at their height" +new_case "${net[@]}" +at 0 gw=3229139 n1=3229140 n2=3229139 +at 10 gw=3229140 +at 34; sent "34 min: nothing" +at 36; sent "36 min: DOWN" "TN DOWN: no node has a new block for 36 min (node-1: 3229140 at 09:00 UTC, node-2: 3229139 at 09:00 UTC). The gateway still answers at height 3229140." +at 41 gw=3229141; sent "the gateway gets ahead: OK" "TN OK: blocks are moving again through the gateway, height 3229141, after 41 min." + +echo "# network: failed sends are retried" +new_case "${net[@]}" +at 0 gw=500 n1=500 n2=500 +touch "$tmp/telegram-down" +at 35; sent "DOWN send fails"; exited "exits 1" 1 +rm "$tmp/telegram-down" +at 40; sent "DOWN retried" "TN DOWN: no new block through the gateway for 40 min, last height 500 seen at 09:00 UTC. node-1: 500, node-2: 500." +touch "$tmp/telegram-down" +at 45 gw=501; sent "OK send fails"; exited "exits 1" 1 +rm "$tmp/telegram-down" +at 50 gw=502; sent "OK retried" "TN OK: blocks are moving again through the gateway, height 502, after 45 min." +at 55 gw=503; sent "then nothing" + +echo "# network: the clock steps back during an outage" +new_case "${net[@]}" +at 0 gw=700 n1=700 n2=700 +at 1 gw=- +at 35; sent "DOWN" "TN DOWN: no new block through the gateway for 35 min, last height 700 seen at 09:00 UTC. The gateway is not answering. node-1: 700, node-2: 700." +at 10; sent "clock 25 min back: no OK" +at 36; sent "clock back on time: nothing" +at 50 gw=701; sent "real recovery: OK" "TN OK: blocks are moving again through the gateway, height 701, after 50 min." + +echo "# network, gateway only: its address changes during an outage" +new_case "GATEWAY_RPC=$rpc/gw" NODE_CONTAINER=none STALL_MINUTES=35 +at 0 gw=800 +at 1 gw=- +at 35; sent "DOWN" "TN DOWN: no new block through the gateway for 35 min, last height 800 seen at 09:00 UTC. The gateway is not answering." +echo "GATEWAY_RPC=$rpc/gw2" >>"$case_dir/env" +at 40 gw2=800; sent "new address at the old height: no OK" +at 45; sent "still no new block: nothing" +at 50 gw2=801; sent "a new block through the new address: OK" "TN OK: blocks are moving again through the gateway, height 801, after 50 min." + +echo "# network watch with a node check on the same host" +new_case "${net[@]}" NODE_CONTAINER=tn-node "NODE_RPC=$rpc/local" NODE_ALERTS=journal +at 0 gw=10 n1=10 n2=10 local=10 +at 35; sent "network DOWN goes to the chat" "TN DOWN: no new block through the gateway for 35 min, last height 10 seen at 09:00 UTC. node-1: 10, node-2: 10." +journal "both in the journal" "TN DOWN: no new block through the gateway for 35 min, last height 10 seen at 09:00 UTC. node-1: 10, node-2: 10." \ + "node-a: no new block for 35 min, still at height 10." + +[ "$fails" -eq 0 ] && echo "all passed" || echo "$fails failed" +[ "$fails" -eq 0 ]