Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 51 additions & 6 deletions deployments/alerts/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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.

Expand All @@ -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.
219 changes: 200 additions & 19 deletions deployments/alerts/node-health-alert.sh
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading
Loading