A health checker package to report using MQTT
Project description
healthcheck-reporter
Lightweight health reporter that probes database connectivity and reports via MQTT or REST API.
Installation
pip install healthcheck-reporter
Usage
MQTT Mode
from healthcheck_reporter import Reporter, ReporterConfig
config = ReporterConfig(
database_host="db.example.local",
database_port=5432,
database_name="appdb",
database_password="secret",
database_user="app",
mqtt_host="mqtt.example.local",
mqtt_port=1883,
mqtt_client_id="service-A-health",
mqtt_topic="services/health",
mqtt_user=None, # or "user"
mqtt_password=None, # or "password"
)
reporter = Reporter(
config,
mode="mqtt",
interval_seconds=30.0, # configurable, non-blocking
debug_mode=False, # set to True for testing status transitions
)
reporter.start()
# ... your service runs ...
# On shutdown
reporter.stop()
REST API Mode
from healthcheck_reporter import Reporter, ReporterConfig
config = ReporterConfig(
database_host="db.example.local",
database_port=5432,
database_name="appdb",
database_password="secret",
database_user="app",
api_host="0.0.0.0", # or "127.0.0.1" for localhost only
api_port=8080,
api_path="api/whatever/health", # customizable path
)
reporter = Reporter(
config,
mode="rest",
debug_mode=False, # set to True for testing status transitions
)
# This will start a FastAPI server on port 8080 in the background
reporter.start() # Non-blocking, starts server in background thread
# ... your service continues running ...
# On shutdown
reporter.stop()
Published payload
{
"database_status": "ok",
"mqtt_client_id": "service-A-health",
"unix_timestamp": 1735689600,
"db_error_count": 0,
"mqtt_error_count": 0,
"db_failure_rate": 0.0,
"mqtt_failure_rate": 0.0,
"overall_status": "operational",
"api_error_count": 0,
"api_failure_rate": 0.0,
"uptime": 12.34
}
Notes
- A real PostgreSQL connection is attempted first (psycopg2 is required); otherwise it falls back to a TCP probe if the driver is unavailable in the environment.
- Both modes:
start()runs in a background thread (non-blocking). Usestop()on shutdown. - REST mode: The health endpoint returns the same JSON structure as MQTT and is available at
http://{api_host}:{api_port}/{api_path}. - Grafana integration: If Grafana hits the endpoint when it's down, it will get a connection error (expected behavior). The health reporter tracks API availability internally and reports it in the metrics.
Metrics
When running in REST mode, a Prometheus endpoint is available at /metrics.
Point Prometheus to scrape http://{api_host}:{api_port}/metrics.
- Cumulative counters since process start:
db_error_count: number of failed DB probesmqtt_error_count: number of failed MQTT publishesapi_error_count: number of failed API health checks (REST mode only)
- Failure rates (percentages):
db_failure_rate: failed DB probes / total DB probe attemptsmqtt_failure_rate: failed MQTT publishes / total MQTT publish attemptsapi_failure_rate: failed API health checks / total API health check attempts (REST mode only)
- Derived overall status:
operational: DB is currently reachable and all failure rates < 20%degraded: failure rate of DB, MQTT, or API >= 20%unavailable: current DB probe failed or API is down (REST mode)- Numeric code metric (Prometheus):
healthcheck_overall_status_code— 0 operational, 1 degraded, 2 unavailable
- Debug mode (
debug_mode=True): alternates between "degraded" and "unavailable" every 5 seconds for testing
Changelog
- See the full changelog at: https://github.com/fernandodazadev/healthcheck-reporter/blob/main/CHANGELOG.md
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file healthcheck_reporter-0.3.1.tar.gz.
File metadata
- Download URL: healthcheck_reporter-0.3.1.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0354a230140e76ed71d6a05e907b69fd5618d6a51020503590aee22e93c89f55
|
|
| MD5 |
4c1400f04d581206ab4b0ae8a33c32b7
|
|
| BLAKE2b-256 |
86380ba3541cc012efc0ae8374086e609897c2568bb6d12421e86eddce9bf69d
|
File details
Details for the file healthcheck_reporter-0.3.1-py3-none-any.whl.
File metadata
- Download URL: healthcheck_reporter-0.3.1-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a8e30620b321ea39e0b7faa19f00bf1dcf249ba4065af177a6297be0c2d5449
|
|
| MD5 |
238a7647050c5a27968765ff5181bf99
|
|
| BLAKE2b-256 |
939e967638fd68ff9acc8ea81b3e604a1741a864ce43a07eb7283ce88e5ac73d
|