""" Core Module - Basic dp-ha-ctl checks. Used for metrics relating to core dp-ha-ctl functionality like Redis connection and service status. """ from ..base import BaseExporter from ..utils import run_command_silent, parse_json_silent, debug_log class CustomExporter(BaseExporter): """ Exporter for core dp-ha-ctl checks. Runs checks related to Redis connection and service status. """ def metric_config(self): """Define metrics for core functionality checks""" return { "dp_hactl_redis_connected": {"type": "Gauge", "labels": []}, "dp_hactl_service_up": {"type": "Gauge", "labels": []}, } def generate(self): """Run all core checks and collect metrics""" debug_log("Running core dp-ha-ctl checks") # The utility functions will handle errors silently self._check_redis_connection() self._check_service_status() def _check_redis_connection(self): """Check if Redis backend is connected""" redis_connected_metric = self.metrics.get("dp_hactl_redis_connected") debug_log("Checking Redis connection with dp-ha-ctl ha-status") # Use the silent command utility that handles errors - discarding the result # since we only care if it runs without error run_command_silent( ["dp-ha-ctl", "ha-status"], timeout=15 ) # If we get here, the command was successful (exit code 0) debug_log("Redis connection check successful") redis_connected_metric.set(1) def _check_service_status(self): """Check if dp-ha-ctl service is up""" service_up_metric = self.metrics.get("dp_hactl_service_up") debug_log("Checking service status with dp-ha-ctl dump-all-state") # Use the silent command utility that handles errors result = run_command_silent( ["dp-ha-ctl", "dump-all-state"], timeout=15 ) # If we get here, check if output is valid JSON debug_log("Parsing service status output as JSON") # The parse_json_silent utility will exit with code 1 if parsing fails parse_json_silent(result.stdout) # If we get here, JSON was valid debug_log("Service status check successful") service_up_metric.set(1)