|
Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
|
Health monitor with dependency management, auto-recovery, and statistics. More...
#include <health_monitor.h>

Public Member Functions | |
| health_monitor ()=default | |
| Default constructor with default configuration. | |
| health_monitor (const health_monitor_config &config) | |
| Construct with custom configuration. | |
| virtual | ~health_monitor () |
| Destructor. Stops the monitoring loop if running. | |
| common::Result< bool > | register_check (const std::string &name, std::shared_ptr< health_check > check) |
| Register a named health check. | |
| common::Result< bool > | unregister_check (const std::string &name) |
| Remove a previously registered health check. | |
| common::Result< health_check_result > | check (const std::string &name) |
| Execute a single named health check (with dependency verification). | |
| std::unordered_map< std::string, health_check_result > | check_all () |
| Execute all registered health checks. | |
| common::Result< bool > | add_dependency (const std::string &dependent, const std::string &dependency) |
| Add a dependency between two registered health checks. | |
| common::VoidResult | start () |
| Start the periodic health monitoring background thread. | |
| common::VoidResult | stop () |
| Stop the periodic health monitoring background thread. | |
| bool | is_running () const |
| Check whether the monitoring background thread is running. | |
| void | refresh () |
| Manually refresh all health checks and trigger recovery if needed. | |
| void | register_recovery_handler (const std::string &check_name, std::function< bool()> handler) |
| Register a recovery handler for a named health check. | |
| health_status | get_overall_status () |
| Get the aggregate health status across all cached results. | |
| health_monitor_stats | get_stats () const |
| Get accumulated health monitoring statistics. | |
| std::string | get_health_report () |
| Generate a human-readable health report. | |
| health_check_result | check_health () const |
| Quick self-check of the health monitor itself. | |
Private Member Functions | |
| void | run_monitoring_loop () |
| void | update_stats (const health_check_result &result) |
Private Attributes | |
| health_monitor_config | config_ |
| health_monitor_stats | stats_ |
| health_dependency_graph | dependency_graph_ |
| std::shared_mutex | mutex_ |
| std::mutex | lifecycle_mutex_ |
| std::mutex | cv_mutex_ |
| std::condition_variable | cv_ |
| std::unordered_map< std::string, std::shared_ptr< health_check > > | checks_ |
| std::unordered_map< std::string, std::function< bool()> > | recovery_handlers_ |
| std::unordered_map< std::string, health_check_result > | cached_results_ |
| std::atomic< bool > | running_ {false} |
| std::thread | monitor_thread_ |
Health monitor with dependency management, auto-recovery, and statistics.
Manages a collection of named health checks, runs them periodically on a background thread, maintains cached results, and optionally invokes recovery handlers when checks fail.
All public methods are thread-safe. Internal state is protected by std::shared_mutex (data) and std::mutex (lifecycle).
Definition at line 696 of file health_monitor.h.
|
default |
Default constructor with default configuration.
|
inlineexplicit |
Construct with custom configuration.
| config | Health monitor configuration settings |
Definition at line 705 of file health_monitor.h.
|
inlinevirtual |
Destructor. Stops the monitoring loop if running.
Definition at line 708 of file health_monitor.h.
References stop().

|
inline |
Add a dependency between two registered health checks.
| dependent | Name of the check that depends on another |
| dependency | Name of the check being depended upon |
Definition at line 795 of file health_monitor.h.
References kcenon::monitoring::health_dependency_graph::add_dependency(), dependency_graph_, and mutex_.

|
inline |
Execute a single named health check (with dependency verification).
| name | Name of the check to execute |
Definition at line 756 of file health_monitor.h.
References cached_results_, kcenon::monitoring::health_dependency_graph::check_with_dependencies(), checks_, dependency_graph_, mutex_, kcenon::monitoring::not_found, kcenon::monitoring::error_info::to_common_error(), and update_stats().
Referenced by check_all(), refresh(), and register_check().


|
inline |
Execute all registered health checks.
Definition at line 776 of file health_monitor.h.
References cached_results_, check(), checks_, mutex_, and update_stats().
Referenced by demonstrate_health_monitoring().


|
inline |
Quick self-check of the health monitor itself.
Definition at line 961 of file health_monitor.h.
References kcenon::monitoring::healthy, kcenon::monitoring::health_check_result::message, kcenon::monitoring::health_check_result::status, and kcenon::monitoring::health_check_result::timestamp.
Referenced by main().

|
inline |
Generate a human-readable health report.
Definition at line 925 of file health_monitor.h.
References cached_results_, kcenon::monitoring::degraded, kcenon::monitoring::healthy, mutex_, and kcenon::monitoring::unhealthy.
Referenced by demonstrate_health_monitoring().

|
inline |
Get the aggregate health status across all cached results.
Definition at line 889 of file health_monitor.h.
References cached_results_, checks_, kcenon::monitoring::degraded, kcenon::monitoring::healthy, mutex_, kcenon::monitoring::unhealthy, and kcenon::monitoring::unknown.
Referenced by demonstrate_health_monitoring().

|
inline |
Get accumulated health monitoring statistics.
Definition at line 916 of file health_monitor.h.
|
inline |
Check whether the monitoring background thread is running.
Definition at line 841 of file health_monitor.h.
References running_.
|
inline |
Manually refresh all health checks and trigger recovery if needed.
Runs every registered check, updates cached results and statistics, and invokes recovery handlers for unhealthy checks when auto-recovery is enabled.
Definition at line 851 of file health_monitor.h.
References cached_results_, check(), checks_, config_, kcenon::monitoring::health_monitor_config::enable_auto_recovery, kcenon::monitoring::health_monitor_stats::last_check_time, mutex_, kcenon::monitoring::health_monitor_stats::recovery_attempts, recovery_handlers_, stats_, kcenon::monitoring::health_monitor_stats::successful_recoveries, kcenon::monitoring::unhealthy, and update_stats().
Referenced by demonstrate_health_monitoring(), and run_monitoring_loop().


|
inline |
Register a named health check.
| name | Unique name for this check |
| check | The health check implementation |
Definition at line 716 of file health_monitor.h.
References kcenon::monitoring::health_dependency_graph::add_node(), kcenon::monitoring::already_exists, check(), checks_, dependency_graph_, and mutex_.
Referenced by demonstrate_health_monitoring(), and main().


|
inline |
Register a recovery handler for a named health check.
| check_name | Name of the health check this handler is for |
| handler | Callable that attempts recovery; returns true on success |
Definition at line 878 of file health_monitor.h.
References mutex_, and recovery_handlers_.
Referenced by demonstrate_health_monitoring().

|
inlineprivate |
Definition at line 970 of file health_monitor.h.
References kcenon::monitoring::health_monitor_config::check_interval, config_, cv_, cv_mutex_, refresh(), and running_.
Referenced by start().


|
inline |
Start the periodic health monitoring background thread.
Definition at line 804 of file health_monitor.h.
References lifecycle_mutex_, monitor_thread_, run_monitoring_loop(), and running_.
Referenced by demonstrate_health_monitoring(), and main().


|
inline |
Stop the periodic health monitoring background thread.
Definition at line 820 of file health_monitor.h.
References cv_, lifecycle_mutex_, monitor_thread_, and running_.
Referenced by demonstrate_health_monitoring(), main(), HealthMonitoringTest::SetUp(), HealthMonitoringTest::TearDown(), and ~health_monitor().

|
inline |
Remove a previously registered health check.
| name | Name of the check to remove |
Definition at line 737 of file health_monitor.h.
References checks_, mutex_, kcenon::monitoring::not_found, recovery_handlers_, and kcenon::monitoring::error_info::to_common_error().

|
inlineprivate |
Definition at line 981 of file health_monitor.h.
References kcenon::monitoring::degraded, kcenon::monitoring::health_monitor_stats::degraded_checks, kcenon::monitoring::healthy, kcenon::monitoring::health_monitor_stats::healthy_checks, stats_, kcenon::monitoring::health_check_result::status, kcenon::monitoring::health_monitor_stats::total_checks, kcenon::monitoring::unhealthy, and kcenon::monitoring::health_monitor_stats::unhealthy_checks.
Referenced by check(), check_all(), and refresh().

|
private |
Definition at line 1009 of file health_monitor.h.
Referenced by check(), check_all(), get_health_report(), get_overall_status(), and refresh().
|
private |
Definition at line 1007 of file health_monitor.h.
Referenced by check(), check_all(), get_overall_status(), refresh(), register_check(), and unregister_check().
|
private |
Definition at line 998 of file health_monitor.h.
Referenced by refresh(), and run_monitoring_loop().
|
private |
Definition at line 1005 of file health_monitor.h.
Referenced by run_monitoring_loop(), and stop().
|
private |
Definition at line 1004 of file health_monitor.h.
Referenced by run_monitoring_loop().
|
private |
Definition at line 1000 of file health_monitor.h.
Referenced by add_dependency(), check(), and register_check().
|
private |
Definition at line 1003 of file health_monitor.h.
|
private |
Definition at line 1012 of file health_monitor.h.
|
mutableprivate |
Definition at line 1002 of file health_monitor.h.
Referenced by add_dependency(), check(), check_all(), get_health_report(), get_overall_status(), get_stats(), refresh(), register_check(), register_recovery_handler(), and unregister_check().
|
private |
Definition at line 1008 of file health_monitor.h.
Referenced by refresh(), register_recovery_handler(), and unregister_check().
|
private |
Definition at line 1011 of file health_monitor.h.
Referenced by is_running(), run_monitoring_loop(), start(), and stop().
|
private |
Definition at line 999 of file health_monitor.h.
Referenced by get_stats(), refresh(), and update_stats().