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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ flowchart LR
B["Proxmox API · FRITZ!Box<br/>game panels · AdGuard"] -- "exporters" --> P
C["firewall log · SSH ·<br/>containers · website"] -- "Alloy + GeoIP" --> L[("Loki")]
L --> CS["CrowdSec<br/>watch only"] --> P
P --> G["Grafana<br/>4 dashboards · 19 alert rules"]
P --> G["Grafana<br/>4 dashboards · 20 alert rules"]
L --> G
G -- "alerts" --> T["Telegram"]
```
Expand All @@ -362,7 +362,7 @@ flowchart LR
| **Logs** | Loki + Alloy: SSH, every container, the OPNsense firewall log and the website (with the real visitor IP behind the tunnel), each with country and city |
| **Attacks** | CrowdSec reads those logs and recognises brute force, scans and web exploits, **watch only**: nothing is blocked |
| **Dashboards** | four, generated by a Python script: Overview, Hosts (a switch per machine), Games, Security (dark world map) |
| **Alerts** | 19 rules to my own Telegram bot: 🔴 any time, 🟠 not at night, 🎮 when a game server stops. Switched-off nodes are not an incident |
| **Alerts** | 20 rules to my own Telegram bot: 🔴 any time, 🟠 not at night, 🎮 when a game server stops. Switched-off nodes are not an incident, just a soft 🟠 "is off" |

Prometheus **pulls**: it reaches out to each target on an interval, so adding a
host means editing `prometheus.yml` and installing an exporter, and never
Expand Down
54 changes: 53 additions & 1 deletion compose/monitoring/grafana/provisioning/alerting/rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,58 @@
"folder": "Homelab",
"interval": "1m",
"rules": [
{
"uid": "host-off",
"title": "Power-saving node off",
"condition": "C",
"for": "2m",
"noDataState": "OK",
"execErrState": "Error",
"labels": {
"severity": "warning"
},
"annotations": {
"summary": "{{ .Labels.node }} is off",
"description": "Fine if you switched it off yourself. You get an OK message when it's back."
},
"isPaused": false,
"data": [
{
"refId": "A",
"relativeTimeRange": {
"from": 900,
"to": 0
},
"datasourceUid": "prometheus-homelab",
"model": {
"refId": "A",
"expr": "max by (node) (up{job=\"proxmox-host\", node=~\"pve2|pve3\"}) < bool 1",
"instant": true,
"intervalMs": 60000,
"maxDataPoints": 43200
}
},
{
"refId": "C",
"datasourceUid": "__expr__",
"model": {
"refId": "C",
"type": "threshold",
"expression": "A",
"conditions": [
{
"evaluator": {
"type": "gt",
"params": [
0
]
}
}
]
}
}
]
},
{
"uid": "disk-80",
"title": "Disk over 80%",
Expand Down Expand Up @@ -495,7 +547,7 @@
"severity": "warning"
},
"annotations": {
"summary": "{{ .Labels.node }} {{ .Labels.mountpoint }} will be full in < 7 days",
"summary": "{{ .Labels.node }} {{ .Labels.mountpoint }} will be full within 7 days",
"description": "Based on the last 6 hours of growth."
},
"isPaused": false,
Expand Down
12 changes: 11 additions & 1 deletion docs/24-alerting.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ link. Everything else is one tap away in Grafana.
| 🔴 | Internet down | the FRITZ!Box reports the WAN link down | 3 min |
| 🟠 | Disk / storage over 80 % | as above, earlier | 30 min |
| 🟠 | Disk full within 7 days | `predict_linear` over 6 h says < 0 in 7 days | 2 h |
| 🟠 | Power-saving node off | pve2 or pve3 cannot be scraped ("pve3 is off", then an OK message when it's back) | 2 min |
| 🟠 | RAM over 90 % | any monitored machine | 15 min |
| 🟠 | CrowdSec detected an attack | a CrowdSec scenario matched in the last 10 min | 0 |
| 🟠 | Many failed SSH logins | > 10 in 10 min on LXC 102 (Loki) | 0 |
Expand Down Expand Up @@ -202,7 +203,7 @@ into a night of messages:

| Naive rule | Problem | Fix |
|---|---|---|
| Host down | fires for pve2/pve3 every evening | not alerted for pve2/pve3 (`node!~"pve2\|pve3"`) |
| Host down | fires for pve2/pve3 every evening | no 🔴 for pve2/pve3 (`node!~"pve2\|pve3"`); instead a soft 🟠 "Power-saving node off": a warning, so never at night, and "fine if you switched it off yourself" |
| Guest with autostart stopped | every guest on pve2 counts as "stopped" | `and on (node) (up{job="proxmox-host"} == 1)`: only while its node is up |
| OPNsense stopped logging | OPNsense lives on pve2 | a two-query rule: Loki count **and** "pve2 is up" |

Expand All @@ -215,6 +216,15 @@ B (Prometheus): max(up{job="proxmox-host", node="pve2"}) or vector(0)
C (math): $A < 1 && $B == 1
```

### 4b · A "<" in a message text drops the whole message

"Disk full within 7 days" fired, but nothing ever reached Telegram. Its text
said "will be full in < 7 days". The contact point sends with `parse_mode`
HTML, so Telegram read `< 7` as a broken HTML tag and rejected the whole
message (400 "Unsupported start tag"). Grafana only logs that error, every
5 minutes. The text now says "within 7 days", and `gen_alerts.py` refuses any
`<` or `&` in alert texts, so this cannot come back.

### 5 · `$` means three different things

- In **docker compose** files, `$` starts a variable. A regex like `($|/)` in a
Expand Down
8 changes: 5 additions & 3 deletions runbooks/23-alerts-to-telegram.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,17 @@ In `/opt/monitoring/grafana/provisioning/alerting/`
| `contact-points.yml` | two Telegram receivers on the same bot: `telegram` (with "OK again" messages) and `telegram-games` (without) | rarely |
| `policies.yml` | critical and 🎮 → any time; warning → muted 23:00-08:00 Europe/Berlin; group per alert and node; repeat every 4 h | quiet hours, timezone |
| `templates.yml` | the message: emoji, one bold line, one explanation, a link | to taste |
| `rules.yml` | the 19 rules. **Generated**, don't edit by hand | via `gen_alerts.py` |
| `rules.yml` | the 20 rules. **Generated**, don't edit by hand | via `gen_alerts.py` |

### Adapt the rules to your lab

Rules are written in [`scripts/grafana/gen_alerts.py`](../scripts/grafana/gen_alerts.py).
Read it once; each rule is one call with its query, message and `for`. Things
that are specific to this lab and that you will want to change:

- `node!~"pve2|pve3"` in "Host down": those two nodes are switched off on purpose.
Use your own names, or remove it.
- `node!~"pve2|pve3"` in "Host down" and `node=~"pve2|pve3"` in "Power-saving node off":
those two nodes are switched off on purpose, so they get a soft 🟠 "is off" instead of 🔴.
Use your own names, or remove both.
- `id!="lxc/101"` in "Guest with autostart stopped": one guest excluded on
purpose. Remove it or put your own (with a comment saying why).
- `node="pve2"` in "OPNsense stopped sending logs": the node the firewall runs on.
Expand Down Expand Up @@ -148,6 +149,7 @@ then "✅ OK again" for both once it is back.
| rule shows "Error", message about `$$A` | `$$` written in a rule file: alert-rule files take `$A` literally, only contact points expand variables |
| a rule never fires in a test | the query lacks `bool` (`< 1` instead of `< bool 1`), see docs/24 |
| "internet down" when only the exporter died | `or vector(0)` in that rule, see docs/24 |
| a rule fires (Alerting shows it) but no Telegram message; Grafana log says 400 "Unsupported start tag" | a `<` or `&` in the alert text (Telegram HTML). The generator refuses them now |
| messages every evening for a switched-off node | exclude it from "Host down" and gate its guests on its `up`, see docs/24 |

---
Expand Down
12 changes: 10 additions & 2 deletions scripts/grafana/gen_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def rule(uid, title, expr, summary, desc, sev, for_="5m", ds=PROM, nodata="OK",
"conditions": [{"evaluator": {"type": "gt", "params": [0]}}]})}]}

CRIT = [
# pve2 + pve3 are switched off on purpose to save power -> no "down" alert for them.
# pve2 + pve3 are switched off on purpose to save power -> only a soft 🟠 "is off" message (see WARN).
rule("host-down", "Host down", f'max by (node) (up{{{HOSTS}, node!~"pve2|pve3"}}) < bool 1',
"{{ .Labels.node }} is DOWN", "Prometheus can't reach {{ .Labels.node }} for 2 minutes.", "critical", "2m"),
rule("guest-stopped", "Guest with autostart stopped",
Expand All @@ -47,12 +47,15 @@ def rule(uid, title, expr, summary, desc, sev, for_="5m", ds=PROM, nodata="OK",
"Internet is DOWN (FRITZ!Box)", "The DSL/WAN link is down. You get this message once it's back.", "critical", "3m"),
]
WARN = [
# pve2 + pve3: switched off on purpose to save power, but the user still wants to know (no 🔴, not at night).
rule("host-off", "Power-saving node off", 'max by (node) (up{job="proxmox-host", node=~"pve2|pve3"}) < bool 1',
"{{ .Labels.node }} is off", "Fine if you switched it off yourself. You get an OK message when it's back.", "warning", "2m"),
rule("disk-80", "Disk over 80%", f'max by (node, mountpoint) (1 - node_filesystem_avail_bytes{{{HOSTS},{FS}}} / node_filesystem_size_bytes{{{HOSTS},{FS}}}) > bool 0.8',
"{{ .Labels.node }} {{ .Labels.mountpoint }} is over 80% full", "Time to clean up.", "warning", "30m"),
rule("storage-80", "Proxmox storage over 80%", 'max by (id) (pve_disk_usage_bytes{id=~"storage/.*"} / pve_disk_size_bytes{id=~"storage/.*"}) > bool 0.8',
"Storage {{ .Labels.id }} is over 80% full", "Time to clean up.", "warning", "30m"),
rule("disk-full-7d", "Disk full within 7 days", f'max by (node, mountpoint) (predict_linear(node_filesystem_avail_bytes{{{HOSTS},{FS},node!=""}}[6h], 7*86400) < bool 0)',
"{{ .Labels.node }} {{ .Labels.mountpoint }} will be full in < 7 days", "Based on the last 6 hours of growth.", "warning", "2h"),
"{{ .Labels.node }} {{ .Labels.mountpoint }} will be full within 7 days", "Based on the last 6 hours of growth.", "warning", "2h"),
rule("ram-90", "RAM over 90%", f'max by (node) (1 - node_memory_MemAvailable_bytes{{{HOSTS}}} / node_memory_MemTotal_bytes{{{HOSTS}}}) > bool 0.9',
"{{ .Labels.node }} RAM is over 90%", "For 15 minutes. Something uses too much memory.", "warning", "15m"),
rule("crowdsec", "CrowdSec detected an attack", '(sum by (name) (increase(cs_bucket_overflowed_total[10m])) or vector(0)) > bool 0',
Expand Down Expand Up @@ -86,6 +89,11 @@ def rule(uid, title, expr, summary, desc, sev, for_="5m", ds=PROM, nodata="OK",
"It was running and is not anymore. If you stopped it yourself, ignore this.", "game", "1m"),
]

# Telegram uses parse_mode HTML: a "<" or "&" in a text makes Telegram reject the whole message (400 Bad Request).
for r in CRIT + WARN + GAMES:
for t in r["annotations"].values():
assert "<" not in t and "&" not in t, f'{r["uid"]}: no "<" or "&" in alert texts (Telegram HTML)'

doc = {"apiVersion": 1, "groups": [
{"orgId": 1, "name": "critical", "folder": "Homelab", "interval": "1m", "rules": CRIT},
{"orgId": 1, "name": "warning", "folder": "Homelab", "interval": "1m", "rules": WARN},
Expand Down
Loading