Random GELF Event Generator for Graylog

  • Share this:

Code introduction


This function generates a random GELF event and sends it to a specified Graylog server. It uses the GelfMessage and TCPTransport classes from Graylog to create and send the event.


Technology Stack : graylog2, graypy

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from graylog2 import __version__ as graylog_version
from graypy import pygelf
from graypy import formatter
from graypy import transport

def random_graylog_event_generator(host, port, facility='local0', severity='info'):
    """
    This function generates a random GELF event and sends it to a Graylog server.
    """
    # Create a formatter for the GELF event
    gelf_formatter = formatter.GelfFormatter(facility=facility, severity=severity)

    # Create a transport for sending the GELF event
    transport_instance = transport.TCPTransport(host=host, port=port)

    # Generate a random GELF event
    event = pygelf.GelfMessage(
        version='1.1',
        host='localhost',
        short_message=f'Random event {random.randint(1, 100)}',
        additional_fields={'user': 'test_user'}
    )

    # Send the GELF event to the Graylog server
    transport_instance.send(event)

    return True