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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ The project doesn't use version numbers. Each entry is one merge into `master`,

Nothing yet.

## 2026-11-22 · [#9](https://github.com/tronyx/HealthChecks-Linux/pull/9)

### Added

- A probe can have a `host` setting, for example `host = "snipe-it.{domain}"`. The probe sends it in the `Host` header instead of the host of its URL. A redirect to the same scheme, host and port keeps it, and a redirect to another host does not. Use it for an application that answers only to its public name.

### Changed

- The `snipe-it` check of the example configuration tries `/health` instead of `/login`, and its internal probe sends `host = "snipe-it.{domain}"`. Since 8.8.4, Snipe-IT answers `400` to the IP address of the server.

## 2026-11-18 · [#8](https://github.com/tronyx/HealthChecks-Linux/pull/8)

### Added
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Each probe is one GET request. A probe has these settings:
| `verify_tls` | If `false`, the probe accepts any HTTPS certificate. Use it for a self-signed certificate on your own network | `true` |
| `send_token` | If `true`, the probe sends your Organizr API key in a `token` header | `false` |
| `auth` | The name of an `[auth.<name>]` table. See [Secrets](#-secrets) | |
| `host` | The host name that the probe sends in the `Host` header, for example `snipe-it.{domain}`. It can use the names from `[hosts]`. Use it when an application answers only to its public name. A redirect to another host does not get it | The host of `url` |

This check tests Sonarr through the reverse proxy and on the server:

Expand Down Expand Up @@ -315,6 +316,7 @@ All users on the computer can write to `/tmp`. Thus, other users can create or d
| A probe returns `301` or `302` | The URL redirects to a different URL. Find the final URL with the command below, and use that URL in the probe. Or set `follow_redirects = true` |
| An Arr application with a login page fails | If you turned on the login page (authentication) in Sonarr, Radarr, Prowlarr, or another Arr application, add `login` to the URLs, for example `https://{domain}/sonarr/login` |
| An external probe fails, but the internal probe passes | Make sure that the subdomain or the subfolder in the probe agrees with your reverse proxy. If you use Organizr, make sure that `organizr_token` is correct and that the probe has `send_token = true` |
| An internal probe returns `400`, but the external probe passes | The application accepts only its public host name, as Snipe-IT does since 8.8.4. Set `host` on the internal probe to that name, for example `host = "snipe-it.{domain}"` |
| A probe shows `error: timed out` or `Connection refused` | The script did not get an answer. Make sure that the address and the port are correct, and that the host that runs the script can connect to them |
| A probe shows `CERTIFICATE_VERIFY_FAILED` | The HTTPS certificate is not valid, for example because it is self-signed. If the address is on your own network, set `verify_tls = false` on the probe |
| The script shows `the previous run is still going, so this run is skipped` | A run takes longer than the time between two runs. Lower `timeout`, increase `workers`, or run the script less often |
Expand Down
6 changes: 4 additions & 2 deletions healthchecks.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,15 @@ probes = [
{ url = "http://{primary}:8580/sabnzbd/" },
]

# Since 8.8.4, Snipe-IT answers 400 to any host other than the host of its
# APP_URL, so the internal probe sends that host.
[[check]]
name = "snipe-it"
uuid = ""
enabled = false
probes = [
{ url = "https://snipe-it.{domain}/login", send_token = true },
{ url = "http://{primary}:8005/login" },
{ url = "https://snipe-it.{domain}/health", send_token = true },
{ url = "http://{primary}:8005/health", host = "snipe-it.{domain}" },
]

# If you turned on the login page in Sonarr (Sonarr v4 asks for one), add
Expand Down
32 changes: 22 additions & 10 deletions healthchecks_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
print(f"healthchecks-linux: Python 3.10 or newer is needed, but this is Python {_version}", file=sys.stderr)
sys.exit(2)

__version__ = "2026-11-18"
__version__ = "2026-11-22"

EXIT_OK = 0
EXIT_ERROR = 1
Expand Down Expand Up @@ -82,7 +82,8 @@
GENERAL_KEYS = ("ping_url", "lock_dir", "timeout", "workers", "send_details", "organizr_token")
AUTH_KEYS = ("user", "password", "password_env")
CHECK_KEYS = ("name", "uuid", "enabled", "kind", "probes", "url", "component")
PROBE_KEYS = ("url", "expect", "follow_redirects", "verify_tls", "send_token", "auth")
PROBE_KEYS = ("url", "expect", "follow_redirects", "verify_tls", "send_token", "auth", "host")
HOST_RE = re.compile(r"[A-Za-z0-9.-]+(:[0-9]{1,5})?")
KINDS = ("http", "statuspage")


Expand Down Expand Up @@ -120,6 +121,7 @@ class Probe:
verify_tls: bool = True
send_token: bool = False
auth: Credentials | None = None
host: str = ""


@dataclass(frozen=True)
Expand Down Expand Up @@ -225,15 +227,15 @@ def _check_url(url: str, where: str, what: str) -> str:
return url


def _fill(template: str, hosts: Mapping[str, str], where: str) -> str:
def _fill(template: str, hosts: Mapping[str, str], where: str, what: str = "url") -> str:
"""Replace each {name} in template with its value from [hosts]."""

def replace(match: re.Match[str]) -> str:
name = match.group(1)
if name not in hosts:
raise ConfigError(f"{where}: unknown host '{{{name}}}'{_suggest(name, list(hosts))}; add it to [hosts]")
if not hosts[name]:
raise ConfigError(f"{where}: url uses {{{name}}}, but [hosts] {name} is empty")
raise ConfigError(f"{where}: {what} uses {{{name}}}, but [hosts] {name} is empty")
return hosts[name]

return PLACEHOLDER_RE.sub(replace, template)
Expand Down Expand Up @@ -331,9 +333,16 @@ def _probe(
raise ConfigError(f"{where}: auth must be a string")
if auth_name is not None and auth_name not in auths:
raise ConfigError(f"{where}: no [auth.{auth_name}] table{_suggest(auth_name, list(auths))}")
host = _get_str(table, "host", where, "")
if "host" in table and not host:
raise ConfigError(f"{where}: host must be a host name, such as app.example.com, and it is empty")
credentials = None
if live:
url = _check_url(_fill(url, hosts, where), where, "url")
if host:
host = _fill(host, hosts, where, "host")
if not HOST_RE.fullmatch(host):
raise ConfigError(f"{where}: host must be a host name, such as app.example.com, not {host}")
if auth_name is not None:
credentials = _credentials(auth_name, auths[auth_name], env, where)
return Probe(
Expand All @@ -343,6 +352,7 @@ def _probe(
verify_tls=_get_bool(table, "verify_tls", where, True),
send_token=_get_bool(table, "send_token", where, False),
auth=credentials,
host=host,
)


Expand Down Expand Up @@ -521,10 +531,10 @@ def _origin(url: str) -> tuple[str, str]:


class _Redirects(urllib.request.HTTPRedirectHandler):
"""Follow redirects only when asked, and send the secret headers again only to the same scheme, host and port.
"""Follow redirects only when asked, and send the kept headers again only to the same scheme, host and port.

urllib copies normal headers to every redirect target, including other hosts, so the secrets are added as
unredirected headers and copied here by hand.
urllib copies normal headers to every redirect target, including other hosts, so the secrets and the Host
header of the probe are added as unredirected headers and copied here by hand.
"""

def __init__(self, follow: bool, secret_headers: Mapping[str, str]) -> None:
Expand Down Expand Up @@ -577,13 +587,15 @@ def fetch(probe: Probe, token: str, timeout: float, read_limit: int = 0) -> tupl

Any HTTP answer is returned, whatever its status; no answer at all raises ProbeFailed.
"""
secrets = _secret_headers(probe, token)
kept = _secret_headers(probe, token)
if probe.host:
kept["Host"] = probe.host
opener = urllib.request.build_opener(
_Redirects(probe.follow_redirects, secrets),
_Redirects(probe.follow_redirects, kept),
urllib.request.HTTPSHandler(context=_tls_context(probe.verify_tls)),
)
request = urllib.request.Request(probe.url, method="GET", headers={"User-Agent": USER_AGENT})
for name, value in secrets.items():
for name, value in kept.items():
request.add_unredirected_header(name, value)
try:
with opener.open(request, timeout=timeout) as response:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ def test_general_settings_and_placeholders(tmp_path: Path) -> None:
assert internal.follow_redirects and not internal.verify_tls


def test_probe_host_uses_the_hosts_table(tmp_path: Path) -> None:
text = '[hosts]\ndomain = "example.test"\nprimary = "10.0.0.5"\n\n' + check(
probes='{ url = "http://{primary}:8005/health", host = "snipe-it.{domain}" }, { url = "http://{primary}/" }'
)
named, plain = load_config(_write(tmp_path, text), {}).checks[0].probes
assert named.host == "snipe-it.example.test"
assert plain.host == ""


@pytest.mark.parametrize("host", ["https://app.example.test", "app.example.test/x", "app example.test", ""])
def test_probe_host_must_be_a_host_name(tmp_path: Path, host: str) -> None:
with pytest.raises(ConfigError, match="host must be a host name"):
load_config(_write(tmp_path, check(probes=f'{{ url = "http://example.test/", host = "{host}" }}')), {})


def test_lock_dir_expands_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("HOME", str(tmp_path))
config = load_config(_write(tmp_path, '[general]\nlock_dir = "~/locks"\n' + check()), {})
Expand Down
27 changes: 27 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,33 @@ def test_secrets_do_not_follow_a_redirect_to_another_host(server: FakeServer, ot
assert "authorization" not in landing.headers


def test_probe_sends_its_own_host_by_default(server: FakeServer) -> None:
server.answer("/", Answer(200))
run_probe(Probe(f"{server.url}/"), "", 5)
assert server.requests[0].headers["host"] == server.host


def test_probe_sends_the_host_that_it_names(server: FakeServer) -> None:
# Snipe-IT 8.8.4 answers 400 to a Host header other than the host of its APP_URL.
server.answer("/health", Answer(200))
assert run_probe(Probe(f"{server.url}/health", host="snipe-it.example.test"), "", 5).passed
assert server.requests[0].headers["host"] == "snipe-it.example.test"


def test_host_follows_a_redirect_to_the_same_host(server: FakeServer) -> None:
server.answer("/old", Answer(301, headers={"Location": "/new"}))
server.answer("/new", Answer(200))
run_probe(Probe(f"{server.url}/old", follow_redirects=True, host="app.example.test"), "", 5)
assert server.requests_to("/new")[0].headers["host"] == "app.example.test"


def test_host_does_not_follow_a_redirect_to_another_host(server: FakeServer, other_server: FakeServer) -> None:
server.answer("/old", Answer(302, headers={"Location": f"{other_server.url}/landing"}))
other_server.answer("/landing", Answer(200))
assert run_probe(Probe(f"{server.url}/old", follow_redirects=True, host="app.example.test"), "", 5).passed
assert other_server.requests[0].headers["host"] == other_server.host


def test_basic_auth_header(server: FakeServer) -> None:
server.answer("/", Answer(200))
run_probe(Probe(f"{server.url}/", auth=Credentials("admin", "pw:with:colons")), "", 5)
Expand Down
Loading