https://t.me/RX1948
Server : Apache
System : Linux iad1-shared-b8-43 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
User : dh_edsupp ( 6597262)
PHP Version : 8.2.26
Disable Function : NONE
Directory :  /opt/prometheus-monitoring-scripts/lib/python3.10/site-packages/custom_exporters/mailq/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //opt/prometheus-monitoring-scripts/lib/python3.10/site-packages/custom_exporters/mailq/mailman.py
"""
Mailman Queue Exporter

This module provides a Prometheus exporter for Mailman queue sizes,
monitoring both 'in' and 'out' queues across multiple mailman instances.
"""
import sys
from pathlib import Path
from typing import Dict, List
from ..base import BaseExporter

# Default queue types to monitor
DEFAULT_QUEUE_TYPES = ["in", "out"]
# Default base path for Mailman instances
DEFAULT_BASE_PATH = "/dh/mailman"

class CustomExporter(BaseExporter):
    """
    Prometheus exporter for Mailman queue sizes.
    
    This exporter scans Mailman instances to count files in queue directories,
    providing metrics for queue sizes per service. Exits with non-zero code on errors.
    """
    
    def metric_config(self) -> Dict:
        """
        Define metrics for the Mailman queue exporter.
        
        Returns:
            Dict: Configuration for Prometheus metrics
        """
        return {
            "mailman_queue_size": {"type": "Gauge", "labels": ["service", "queue"]},
        }
    
    def generate(self) -> None:
        """
        Generate Prometheus metrics for Mailman queue sizes.
        
        Scans all Mailman service directories in the configured base path,
        counts files in 'in' and 'out' queues, and exports metrics with
        service and queue type labels. Exits with non-zero code on errors.
        """
        # Get metrics from configuration
        queue_size_metric = self.metrics.get('mailman_queue_size')
        
        # Get base path from args or use default
        base_path = self.args[0] if len(self.args) >= 1 else DEFAULT_BASE_PATH
        queue_types = DEFAULT_QUEUE_TYPES
        
        try:
            # Find all valid Mailman services
            valid_services = self._find_valid_services(base_path, queue_types)
            
            # Exit with error if no valid services found
            if not valid_services:
                sys.exit(1)
                
            # Process each service
            for service_name, queue_paths in valid_services.items():
                for queue_type, queue_path in queue_paths.items():
                    try:
                        # Count files in this queue
                        count = len([f for f in queue_path.glob("*") if f.is_file()])
                        queue_size_metric.labels(service=service_name, queue=queue_type).set(count)
                    except Exception:
                        # Exit with error on file counting issues
                        sys.exit(1)
                        
        except Exception:
            # Exit with error on any other exception
            sys.exit(1)
            
    def _find_valid_services(self, base_path: str, queue_types: List[str]) -> Dict[str, Dict[str, Path]]:
        """
        Find valid Mailman services that have the required queue directories.
        
        Args:
            base_path: Base path containing Mailman service directories
            queue_types: List of queue types to look for
            
        Returns:
            Dict mapping service names to dicts of queue paths
            
        Raises:
            Exception: If base path doesn't exist or can't be accessed
        """
        valid_services = {}
        base_dir = Path(base_path)
        
        if not base_dir.exists() or not base_dir.is_dir():
            raise Exception(f"Base path {base_path} does not exist or is not a directory")
            
        # Check all directories in the base path
        for service_dir in base_dir.iterdir():
            if not service_dir.is_dir():
                continue
                
            service_name = service_dir.name
            queue_base = service_dir / "qfiles"
            
            # Skip if not a valid mailman service (no qfiles directory)
            if not queue_base.exists() or not queue_base.is_dir():
                continue
                
            # Check for required queue directories
            service_queues = {}
            for queue_type in queue_types:
                queue_path = queue_base / queue_type
                if queue_path.exists() and queue_path.is_dir():
                    service_queues[queue_type] = queue_path
                    
            # Only include service if it has at least one of the required queues
            if service_queues:
                valid_services[service_name] = service_queues
                
        return valid_services

https://t.me/RX1948 - 2025