Skip to main content

Generate server-side configuration for rsyslog

Project description

Rsyslog Server Side Configuration Generator

Overview

This Python script generates rsyslog server-side configuration templates and assists with managing SELinux and firewall rules. It can also display property codes and descriptions in JSON format. The script includes functions to generate configurations, handle ports and protocols, and translate file path codes into property names.

Functions

logsrvasst(port, protocol)

Generates an rsyslog server-side configuration file content based on the specified port and protocol.

Parameters:

  • port (int): The port on which the rsyslog server will listen. Commonly, this is 514, but you can specify a different port.
  • protocol (str): The protocol used by the server, either 'tcp' or 'udp'.

Returns:

  • str: The rsyslog configuration content.

Example:

config = logsrvasst(514, 'udp')
print(config)

output

# rsyslog server configuration

# Load the necessary modules
module(load="imudp") 
input(type="imudp" port="514")
# End of configuration

print_suggestions(port, protocol) Prints suggestions for managing SELinux and firewall rules if a non-default port is used.

Parameters:

port (int): The port that was used in the configuration. protocol (str): The protocol used in the configuration.

print_suggestions(1234, 'tcp')
Since you are using a non-default port, consider running the following commands to update SELinux and firewall rules:
# semanage port -a -t syslogd_port_t -p tcp 1234
# firewall-cmd --permanent --add-port=1234/tcp
# firewall-cmd --reload

load_properties(file_path) Loads property codes and descriptions from a JSON file.

Parameters:

. file_path (str): The path to the JSON file containing property codes and descriptions.

Returns: . list: A list of dictionaries, each representing a property with its code, name, and description.

properties = load_properties('properties.json')
print(properties)
[
    {"code": "hst", "name": "hostname", "description": "The hostname of the machine where the log message was originated."},
    {"code": "src", "name": "source", "description": "The IP address or hostname of the source of the log message."},
    ...
]

get_property_name(code, properties) Retrieves the property name for a given code from the list of properties.

Parameters:

code (str): The short-form code for the property. properties (list): The list of properties.

Returns:

str: The property name corresponding to the given code, or None if not found.

property_name = get_property_name('hst', properties)
print(property_name)

Output example:

hostname

extract_properties_and_constants(file_path, properties) Extracts properties and constants from the given file path. Codes are replaced with the corresponding property names.

Parameters:

file_path (str): The file path to be parsed. properties (list): The list of properties.

Returns: list: A list of template components, including property and constant entries.

components = extract_properties_and_constants('/var/log/src/hst-len/pme.log', properties)
print(components)

Output Example:

[
    'constant(value="/")',
    'constant(value="var")',
    'constant(value="/")',
    'constant(value="log")',
    'constant(value="/")',
    'property(name="source")',
    'constant(value="/")',
    'property(name="hostname")',
    'constant(value="/")',
    'property(name="programname")',
    'constant(value=".log")'
]

generate_template(template_name, file_path, properties) Generates an rsyslog template based on the provided name and file path. The file path is parsed to replace codes with property names.

Parameters:

template_name (str): The name of the template.

file_path (str): The file path to be parsed.

properties (list): The list of properties

Returns: str: The generated rsyslog template as a string.

template = generate_template('test', '/var/log/src/hst-len/pme.log', properties)
print(template)

Output Example:

template(name="test" type="list") {
    constant(value="/")
    constant(value="var")
    constant(value="/")
    constant(value="log")
    constant(value="/")
    property(name="source")
    constant(value="/")
    property(name="hostname")
    constant(value="/")
    property(name="programname")
    constant(value=".log")
}

show_property_descriptions(properties) Returns a JSON-formatted string with property codes and their descriptions.

Parameters:

properties (list): The list of properties.

Returns: str: A JSON formatted string containing the property codes and descriptions.

Usage Example:
descriptions_json = show_property_descriptions(properties)
print(descriptions_json)

Output Example:

[
    {
        "code": "hst",
        "description": "The hostname of the machine where the log message was originated."
    },
    {
        "code": "src",
        "description": "The IP address or hostname of the source of the log message."
    },
    ...
]

Usage

  1. Ensure that you have Python 3.x installed.
  2. Create a properties.json file with the required property codes and descriptions in the same directory as the script.
  3. Run the script using Python.

THANK YOU

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logsrvasst-0.1.0.tar.gz (4.2 kB view hashes)

Uploaded Source

Built Distribution

logsrvasst-0.1.0-py3-none-any.whl (5.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page