monitoring: alert on unknown servers in the Hetzner project
Some checks failed
deploy / ansible (push) Failing after 41s

The VPC ping sweep misses a server that drops ICMP or is not attached
to the private network. Prometheus now lists the project's servers from
the Hetzner API with a read-only token and marks each one known or not
against monitoring_hcloud_servers.

Nothing is scraped from the servers: every discovered target is
Prometheus itself with all samples dropped, which leaves one `up`
series per server. A second alert fires when the list stops updating,
so a revoked token does not silently disable the check.

Assisted-by: Claude:fable-5-2
This commit is contained in:
Sergei Poljanski 2026-11-16 00:13:00 +04:00
commit ebd4f110f4
Signed by: asxpi
GPG key ID: 4F8851660FA4121B
3 changed files with 381 additions and 190 deletions

View file

@ -102,8 +102,8 @@
},
{
"type": "stat",
"title": "VPC devices",
"description": "Addresses answering ping on the private network, not in security_vpc_hosts",
"title": "VPC devices, servers",
"description": "Addresses answering ping on the private network that are not in security_vpc_hosts, plus servers in the Hetzner project that are not in monitoring_hcloud_servers",
"datasource": {
"type": "prometheus",
"uid": "prometheus"
@ -114,7 +114,7 @@
"type": "prometheus",
"uid": "prometheus"
},
"expr": "count(group by (ip) (security_vpc_host_info{known=\"false\"})) or vector(0)",
"expr": "(count(group by (ip) (security_vpc_host_info{known=\"false\"})) or vector(0)) + (count(up{job=\"hcloud\",known=\"false\"}) or vector(0))",
"refId": "A",
"instant": true,
"range": false
@ -885,6 +885,117 @@
},
"id": 13
},
{
"type": "table",
"title": "Hetzner project servers",
"description": "Servers the Hetzner API lists for the project, refreshed every minute. Catches a server that drops ping or is not attached to the VPC.",
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"expr": "group by (server, type, status, public_ip, private_ip, known) (up{job=\"hcloud\"})",
"refId": "A",
"instant": true,
"range": false,
"format": "table"
}
],
"gridPos": {
"h": 4,
"w": 24,
"x": 0,
"y": 14
},
"fieldConfig": {
"defaults": {
"custom": {
"filterable": true
}
},
"overrides": [
{
"matcher": {
"id": "byName",
"options": "known"
},
"properties": [
{
"id": "mappings",
"value": [
{
"type": "value",
"options": {
"true": {
"text": "yes",
"color": "green"
},
"false": {
"text": "NO",
"color": "red"
}
}
}
]
},
{
"id": "custom.cellOptions",
"value": {
"type": "color-background"
}
},
{
"id": "custom.width",
"value": 80
}
]
},
{
"matcher": {
"id": "byName",
"options": "Value"
},
"properties": [
{
"id": "custom.hidden",
"value": true
}
]
}
]
},
"transformations": [
{
"id": "organize",
"options": {
"excludeByName": {
"Time": true,
"__name__": true,
"instance": true,
"job": true
},
"indexByName": {
"server": 0,
"type": 1,
"status": 2,
"public_ip": 3,
"private_ip": 4,
"known": 5
}
}
}
],
"options": {
"showHeader": true,
"cellHeight": "sm"
},
"id": 31
},
{
"type": "table",
"title": "Listening ports",
@ -910,7 +1021,7 @@
"h": 9,
"w": 24,
"x": 0,
"y": 14
"y": 18
},
"fieldConfig": {
"defaults": {
@ -1005,7 +1116,7 @@
"h": 1,
"w": 24,
"x": 0,
"y": 23
"y": 27
},
"panels": [],
"id": 15
@ -1035,7 +1146,7 @@
"h": 8,
"w": 8,
"x": 0,
"y": 24
"y": 28
},
"fieldConfig": {
"defaults": {
@ -1145,7 +1256,7 @@
"h": 8,
"w": 6,
"x": 8,
"y": 24
"y": 28
},
"fieldConfig": {
"defaults": {
@ -1254,7 +1365,7 @@
"h": 8,
"w": 10,
"x": 14,
"y": 24
"y": 28
},
"fieldConfig": {
"defaults": {
@ -1363,7 +1474,7 @@
"h": 7,
"w": 8,
"x": 0,
"y": 32
"y": 36
},
"fieldConfig": {
"defaults": {
@ -1488,7 +1599,7 @@
"h": 7,
"w": 6,
"x": 8,
"y": 32
"y": 36
},
"fieldConfig": {
"defaults": {
@ -1597,7 +1708,7 @@
"h": 7,
"w": 10,
"x": 14,
"y": 32
"y": 36
},
"fieldConfig": {
"defaults": {
@ -1689,7 +1800,7 @@
"h": 1,
"w": 24,
"x": 0,
"y": 39
"y": 43
},
"panels": [],
"id": 22
@ -1718,7 +1829,7 @@
"h": 7,
"w": 12,
"x": 0,
"y": 40
"y": 44
},
"fieldConfig": {
"defaults": {
@ -1772,7 +1883,7 @@
"h": 7,
"w": 12,
"x": 12,
"y": 40
"y": 44
},
"fieldConfig": {
"defaults": {
@ -1825,7 +1936,7 @@
"h": 9,
"w": 12,
"x": 0,
"y": 47
"y": 51
},
"options": {
"showTime": true,
@ -1859,7 +1970,7 @@
"h": 9,
"w": 12,
"x": 12,
"y": 47
"y": 51
},
"options": {
"showTime": true,
@ -1878,7 +1989,7 @@
"h": 1,
"w": 24,
"x": 0,
"y": 56
"y": 60
},
"panels": [],
"id": 27
@ -1908,7 +2019,7 @@
"h": 7,
"w": 8,
"x": 0,
"y": 57
"y": 61
},
"fieldConfig": {
"defaults": {
@ -1979,7 +2090,7 @@
"h": 7,
"w": 16,
"x": 8,
"y": 57
"y": 61
},
"fieldConfig": {
"defaults": {
@ -2042,7 +2153,7 @@
"h": 8,
"w": 24,
"x": 0,
"y": 64
"y": 68
},
"options": {
"showTime": true,
@ -2054,4 +2165,4 @@
"id": 30
}
]
}
}

View file

@ -255,6 +255,38 @@ groups:
for: 2m
labels: { severity: critical }
annotations: { summary: "{{ $labels.ip }} answers on the VPC (seen from {{ $labels.host }}) and is not in security_vpc_hosts" }
- uid: sec-hcloud-server-unknown
title: "Unknown server in the Hetzner project"
condition: C
data:
- refId: A
relativeTimeRange: { from: 900, to: 0 }
datasourceUid: prometheus
model: { refId: A, expr: "up{job=\"hcloud\",known=\"false\"}", instant: true, intervalMs: 30000, maxDataPoints: 43200 }
- refId: C
datasourceUid: __expr__
model: { refId: C, type: threshold, expression: A, conditions: [ { evaluator: { type: gt, params: [0] } } ] }
noDataState: OK
execErrState: Error
for: 2m
labels: { severity: critical }
annotations: { summary: "Server {{ $labels.server }} ({{ $labels.type }}, {{ $labels.public_ip }}, private {{ $labels.private_ip }}) exists in the Hetzner project and is not in monitoring_hcloud_servers" }
- uid: sec-hcloud-inventory-stale
title: "Hetzner server list not updating"
condition: C
data:
- refId: A
relativeTimeRange: { from: 900, to: 0 }
datasourceUid: prometheus
model: { refId: A, expr: "absent(up{job=\"hcloud\"}) or increase(prometheus_sd_refresh_failures_total{mechanism=\"hetzner\"}[30m]) > 3", instant: true, intervalMs: 30000, maxDataPoints: 43200 }
- refId: C
datasourceUid: __expr__
model: { refId: C, type: threshold, expression: A, conditions: [ { evaluator: { type: gt, params: [0] } } ] }
noDataState: OK
execErrState: Error
for: 10m
labels: { severity: critical }
annotations: { summary: "Prometheus cannot list the servers of the Hetzner project (token revoked or API unreachable): new servers go unnoticed" }
- uid: sec-user-unknown
title: "Unknown login account"
condition: C

View file

@ -54,6 +54,40 @@ scrape_configs:
- targets: ["localhost:9090", "loki:3100", "grafana:3000", "blackbox:9115"]
labels: { host: asxpio-ops }
# Servers in the Hetzner project, from the API (read-only token). Nothing
# is scraped from them: every target is Prometheus itself with all
# samples dropped, which leaves one `up` series per server.
- job_name: hcloud
scrape_interval: 60s
hetzner_sd_configs:
- role: hcloud
authorization:
credentials: "{{ monitoring_hcloud_token }}"
relabel_configs:
- source_labels: [__meta_hetzner_server_name]
target_label: server
- source_labels: [__meta_hetzner_server_name]
target_label: instance
- source_labels: [__meta_hetzner_public_ipv4]
target_label: public_ip
- regex: __meta_hetzner_hcloud_private_ipv4_.+
action: labelmap
replacement: private_ip
- source_labels: [__meta_hetzner_hcloud_server_type]
target_label: type
- source_labels: [__meta_hetzner_server_status]
target_label: status
- target_label: known
replacement: "false"
- source_labels: [__meta_hetzner_server_name]
regex: '{{ monitoring_hcloud_servers | map("regex_escape") | join("|") }}'
target_label: known
replacement: "true"
- target_label: __address__
replacement: localhost:9090
metric_relabel_configs:
- action: drop
{% for module in monitoring_probes | map(attribute='module') | unique %}
- job_name: probe_{{ module }}
metrics_path: /probe