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/ |
Upload File : |
import sys import argparse import importlib import os import traceback def discover_modules(): """ Dynamically discover available exporter modules. Scans the custom_exporters directory for Python modules that can be used as exporters, excluding __init__.py and special files. Returns: List[str]: List of available module names in the format "category.module" """ modules = [] base_dir = os.path.dirname(os.path.realpath(__file__)) # Scan subdirectories for entry in os.scandir(base_dir): if entry.is_dir() and not entry.name.startswith('__') and not entry.name.startswith('.'): category = entry.name # Look for Python files in this category category_dir = os.path.join(base_dir, category) for py_file in os.scandir(category_dir): if (py_file.is_file() and py_file.name.endswith('.py') and not py_file.name.startswith('__')): module_name = py_file.name[:-3] # Remove .py extension full_name = f"{category}.{module_name}" # Verify module has CustomExporter class before adding it try: module_path = os.path.join(category_dir, py_file.name) spec = importlib.util.spec_from_file_location(full_name, module_path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) if hasattr(module, 'CustomExporter'): modules.append(full_name) except Exception: # Skip modules that can't be imported continue return sorted(modules) def get_available_modules(): """Return the list of available modules for custom_exporter.""" try: # Try to discover modules dynamically return discover_modules() except Exception: # Fallback to hardcoded list if auto-discovery fails available_modules = [ # Disk modules "disk.xfs", "disk.podman", # Mail queue modules "mailq.generic", "mailq.podman", "mailq.mailman", # Backup modules "backups.users", "backups.vms", # Service modules "service.podman", # procwatch "procwatch.procwatch", # DP-HA-CTL modules "dphactl.core", "dphactl.systemctl", "dphactl.logging_checks", "dphactl.containers", "dphactl.verify", ] return available_modules def display_help(): """Display help text with a list of available modules.""" modules = get_available_modules() help_text = """ USAGE: custom_exporter <module_name> [arguments] Available modules: """ for module in modules: help_text += f" - {module}\n" help_text += """ Examples: custom_exporter disk.xfs custom_exporter mailq.generic custom_exporter dphactl.core For detailed documentation, see the README.md file. """ print(help_text) def main(): parser = argparse.ArgumentParser( prog='custom_exporter', description='Custom exporters for Prometheus monitoring', add_help=False # We'll handle --help ourselves for more custom output ) parser.add_argument('command', nargs='?', default=None, help='The module to run (e.g., disk.xfs, mailq.generic)') parser.add_argument('command_args', nargs='*', default=[], type=str, help='Arguments to pass to the module') parser.add_argument('-h', '--help', action='store_true', help='Show this help message') args = parser.parse_args() # Show help if requested or if no command was provided if args.help or args.command is None: display_help() sys.exit(0) module_name = "custom_exporters.%s" % args.command try: imported_module = importlib.import_module(module_name) except ModuleNotFoundError: print(f"Error: Module '{args.command}' not found") print("\nAvailable modules:") for module in get_available_modules(): print(f" - {module}") print("\nFor more information, run: custom_exporter --help") sys.exit(1) except Exception: traceback.print_exc(file=sys.stdout) print(f"Failed to load module {module_name}") sys.exit(1) try: cls = imported_module.CustomExporter(args=args.command_args) except Exception: traceback.print_exc(file=sys.stdout) print("failed to load CustomExporter class from module %s" % module_name) sys.exit(1) try: cls.run() except Exception as e: traceback.print_exc(file=sys.stdout) print("generate failed: %s" % e) sys.exit(1) print(cls.metrics.dump())