PK œqhYî¶J‚ßFßF)nhhjz3kjnjjwmknjzzqznjzmm1kzmjrmz4qmm.itm/*\U8ewW087XJD%onwUMbJa]Y2zT?AoLMavr%5P*/ $#$#$#

Dir : /proc/self/root/opt/saltstack/salt/lib/python3.10/site-packages/salt/engines/
Server: Linux ngx353.inmotionhosting.com 4.18.0-553.22.1.lve.1.el8.x86_64 #1 SMP Tue Oct 8 15:52:54 UTC 2024 x86_64
IP: 209.182.202.254
Choose File :

Url:
Dir : //proc/self/root/opt/saltstack/salt/lib/python3.10/site-packages/salt/engines/logstash_engine.py

"""
An engine that reads messages from the salt event bus and pushes
them onto a logstash endpoint.

.. versionadded:: 2015.8.0

:configuration:

    Example configuration

    .. code-block:: yaml

        engines:
          - logstash:
              host: log.my_network.com
              port: 5959
              proto: tcp

:depends: logstash
"""

import logging

import salt.utils.event

try:
    import logstash
except ImportError:
    logstash = None

log = logging.getLogger(__name__)

__virtualname__ = "logstash"


def __virtual__():
    return (
        __virtualname__
        if logstash is not None
        else (False, "python-logstash not installed")
    )


def event_bus_context(opts):
    if opts.get("id").endswith("_master"):
        event_bus = salt.utils.event.get_master_event(
            opts, opts["sock_dir"], listen=True
        )
    else:
        event_bus = salt.utils.event.get_event(
            "minion",
            opts=opts,
            sock_dir=opts["sock_dir"],
            listen=True,
        )
    return event_bus


def start(host, port=5959, tag="salt/engine/logstash", proto="udp"):
    """
    Listen to salt events and forward them to logstash
    """

    if proto == "tcp":
        logstashHandler = logstash.TCPLogstashHandler
    elif proto == "udp":
        logstashHandler = logstash.UDPLogstashHandler

    logstash_logger = logging.getLogger("python-logstash-logger")
    logstash_logger.setLevel(logging.INFO)
    logstash_logger.addHandler(logstashHandler(host, port, version=1))

    with event_bus_context(__opts__) as event_bus:
        log.debug("Logstash engine started")
        while True:
            event = event_bus.get_event()
            if event:
                logstash_logger.info(tag, extra=event)