Skip to main content

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",
  "timestamp": "2025-01-01T00:00:00+00:00",
  "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
}

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). Use stop() 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

  • Cumulative counters since process start:
    • db_error_count: number of failed DB probes
    • mqtt_error_count: number of failed MQTT publishes
    • api_error_count: number of failed API health checks (REST mode only)
  • Failure rates (percentages):
    • db_failure_rate: failed DB probes / total DB probe attempts
    • mqtt_failure_rate: failed MQTT publishes / total MQTT publish attempts
    • api_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)
  • Debug mode (debug_mode=True): alternates between "degraded" and "unavailable" every 5 seconds for testing

Changelog

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

healthcheck_reporter-0.2.4.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

healthcheck_reporter-0.2.4-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file healthcheck_reporter-0.2.4.tar.gz.

File metadata

  • Download URL: healthcheck_reporter-0.2.4.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for healthcheck_reporter-0.2.4.tar.gz
Algorithm Hash digest
SHA256 727ecbeb1ca3fb982d320bc92bf8477188802351ec6233866942cf9fbc881e81
MD5 1d920cfe081713dc37acbaa5fe5d97fc
BLAKE2b-256 f1c5afba5336fd7e6f470745049d612e3eaf474924e12752b6352d48dab18caf

See more details on using hashes here.

File details

Details for the file healthcheck_reporter-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for healthcheck_reporter-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 16b55c421e63c8b73d1f9440e2de509fdc838385ebfa6176c4971efa65e672a0
MD5 7e4b918baa74a67f7993f64a1d3b715b
BLAKE2b-256 67340f972c3f3787b62309b70c5da97926b7d6af58e4e1e490e2babcb0065c86

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page