# Rsyslog Server Side Template 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:**
```python
config = logsrvasst(514, 'udp')
print(config)
```
### output
```plaintext
# 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.
```python
print_suggestions(1234, 'tcp')
```
```plaintext
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.
```python
properties = load_properties('properties.json')
print(properties)
```
```json
[
{"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.
```python
property_name = get_property_name('hst', properties)
print(property_name)
```
#### Output example
```plaintext
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.
```python
components = extract_properties_and_constants('/var/log/src/hst-len/pme.log', properties)
print(components)
```
#### Output Example
```python
[
'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.
p`roperties` (list): The list of `properties`
Returns:
`str`: The generated rsyslog `template` as a `string`.
```python
template = generate_template('test', '/var/log/src/hst-len/pme.log', properties)
print(template)
```
#### Output Examples
```plaintext
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
```python
descriptions_json = show_property_descriptions(properties)
print(descriptions_json)
```
#### Output Example
```json
[
{
"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.
## Rsyslog Server Side Ruleset Configuration Generator
This Python function generates a rsyslog ruleset configuration string based on user inputs. It allows customization of the ruleset name, template name, port, protocol, and facility.severity pairs.
## Function Overview
### `create_rsyslog_ruleset(ruleset_name, template_name, port, protocol, facilities_severities)`
### `create_tls_rsyslog_ruleset(ruleset_name, template_name, port, tls_ca, tls_cert, tls_key, facilities_severities)`
Generates a rsyslog configuration string with the specified parameters.
#### Parameters
- **`ruleset_name`** (`str`): The name of the ruleset.
- **`template_name`** (`str`): The name of the template to use in the ruleset action.
- **`port`** (`int`): The port number to listen on.
- **`protocol`** (`str`): The protocol to use (`imtcp` for TCP or `imudp` for UDP).
- **`facilities_severities`** (`str`): A comma-separated list of `facility.severity` pairs.
- **`tls_ca`** (`str`): The TLS CA file path (`/etc/pki/certs/certificatesCA.pem`).
- **`tls_cert`** (`str`): The TLS certificate path (`/etc/pki/certs/certificates.pem`).
- **`tls_key`** (`str`): The TLS key path (`/etc/pki/certs/key.pem`).
#### Returns
- **`str`**: The generated rsyslog configuration.
#### Example Usage
```python
rsyslog_config = create_rsyslog_ruleset(ruleset_name, template_name, port, protocol, facilities.severities)
print(rsyslog_config)
```
```python
rsyslog_tls_config = create_tls_rsyslog_ruleset(ruleset_name, template_name, port, tls_ca, tls_cert, tls_key, facilities.severities)
print(rsyslog_tls_config)
```
# Overall usage of the entire package
```python
import logsrvasst as asst
# Load properties from JSON file
port = input("Enter port number:")
protocol = input("Enter protocol")
print(asst.logsrvasst(port,protocol))
if(port != 514):
asst.print_suggestions(port,protocol)
json_file_path = 'properties.json'
properties = asst.load_properties(json_file_path)
# Show property descriptions
property_descriptions_json = asst.show_property_descriptions(properties)
print("\nProperty descriptions in JSON format:")
# print(property_descriptions_json)
# User input
template_name = input("Enter the template name: ")
file_path = input("Enter the file path: ")
# Generate the rsyslog template
rsyslog_template = asst.generate_template(template_name, file_path, properties)
# Print the generated template
print("\nGenerated rsyslog template:")
print(rsyslog_template)
# ruleset generator
rsyslog_ruleset = create_rsyslog_ruleset('testRule', 'exampleTemplate', 514, 'protocol', '*.info,mail.crit')
print(rsyslog_ruleset)
# tls ruleset generator
tls_ruleset = create_tls_rsyslog_ruleset("Test_Rule","Test_template",514,"/etc/CA.pem","/etc/tls.pem","/etc/tls.key","local3.info,local4.*")
print(tls_ruleset)
```
#### THANK YOU
Raw data
{
"_id": null,
"home_page": "https://github.com/titas2003/logsrvasst.git",
"name": "logsrvasst",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": null,
"author": "Titas Majumder",
"author_email": "titas20031996@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/27/c8/ab0e38c11ccf1daa88dd4ecf827a9d3892f33d9a180f5053fccc0a43011f/logsrvasst-0.2.1.tar.gz",
"platform": null,
"description": "# Rsyslog Server Side Template Generator\n\n## Overview\n\nThis 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.\n\n## Functions\n\n### `logsrvasst(port, protocol)`\n\nGenerates an rsyslog server-side configuration file content based on the specified port and protocol.\n\n**Parameters:**\n\n- `port` (int): The port on which the rsyslog server will listen. Commonly, this is 514, but you can specify a different port.\n- `protocol` (str): The protocol used by the server, either `'tcp'` or `'udp'`.\n\n**Returns:**\n\n- `str`: The rsyslog configuration content.\n\n**Example:**\n\n```python\nconfig = logsrvasst(514, 'udp')\nprint(config)\n```\n\n### output\n\n```plaintext\n\n# rsyslog server configuration\n\n# Load the necessary modules\nmodule(load=\"imudp\") \ninput(type=\"imudp\" port=\"514\")\n# End of configuration\n```\n\n`print_suggestions(port, protocol)`\nPrints suggestions for managing SELinux and firewall rules if a non-default port is used.\n\nParameters:\n\nport (int): The port that was used in the configuration.\nprotocol (str): The protocol used in the configuration.\n\n```python\nprint_suggestions(1234, 'tcp')\n```\n\n```plaintext\nSince you are using a non-default port, consider running the following commands to update SELinux and firewall rules:\n# semanage port -a -t syslogd_port_t -p tcp 1234\n# firewall-cmd --permanent --add-port=1234/tcp\n# firewall-cmd --reload\n```\n\n`load_properties(file_path)`\nLoads property codes and descriptions from a JSON file.\n\nParameters:\n\n. `file_path` (str): The path to the JSON file containing property codes and descriptions.\n\nReturns:\n. `list`: A list of dictionaries, each representing a property with its code, name, and description.\n\n```python\nproperties = load_properties('properties.json')\nprint(properties)\n```\n\n```json\n[\n {\"code\": \"hst\", \"name\": \"hostname\", \"description\": \"The hostname of the machine where the log message was originated.\"},\n {\"code\": \"src\", \"name\": \"source\", \"description\": \"The IP address or hostname of the source of the log message.\"},\n ...\n]\n```\n\n`get_property_name(code, properties)`\nRetrieves the property name for a given code from the list of properties.\n\nParameters:\n\n`code` (str): The short-form code for the property.\n`properties` (list): The list of properties.\n\nReturns:\n\n`str`: The property name corresponding to the given `code`, or `None` if not found.\n\n```python\nproperty_name = get_property_name('hst', properties)\nprint(property_name)\n```\n\n#### Output example\n\n```plaintext\nhostname\n```\n\n`extract_properties_and_constants(file_path, properties)`\nExtracts properties and constants from the given file path. Codes are replaced with the corresponding property names.\n\nParameters:\n\n`file_path` (str): The file path to be parsed.\n`properties` (list): The list of properties.\n\nReturns:\n`list`: A list of template components, including `property` and `constant` entries.\n\n```python\ncomponents = extract_properties_and_constants('/var/log/src/hst-len/pme.log', properties)\nprint(components)\n```\n\n#### Output Example\n\n```python\n[\n 'constant(value=\"/\")',\n 'constant(value=\"var\")',\n 'constant(value=\"/\")',\n 'constant(value=\"log\")',\n 'constant(value=\"/\")',\n 'property(name=\"source\")',\n 'constant(value=\"/\")',\n 'property(name=\"hostname\")',\n 'constant(value=\"/\")',\n 'property(name=\"programname\")',\n 'constant(value=\".log\")'\n]\n```\n\n`generate_template(template_name, file_path, properties)`\nGenerates an rsyslog template based on the provided name and file path. The file path is parsed to replace codes with property names.\n\n#### Parameters\n\n`template_name` (str): The name of the template.\n\n`file_path` (str): The file path to be parsed.\n\np`roperties` (list): The list of `properties`\n\nReturns:\n`str`: The generated rsyslog `template` as a `string`.\n\n```python\ntemplate = generate_template('test', '/var/log/src/hst-len/pme.log', properties)\nprint(template)\n```\n\n#### Output Examples\n\n```plaintext\ntemplate(name=\"test\" type=\"list\") {\n constant(value=\"/\")\n constant(value=\"var\")\n constant(value=\"/\")\n constant(value=\"log\")\n constant(value=\"/\")\n property(name=\"source\")\n constant(value=\"/\")\n property(name=\"hostname\")\n constant(value=\"/\")\n property(name=\"programname\")\n constant(value=\".log\")\n}\n```\n\n`show_property_descriptions(properties)`\nReturns a JSON-formatted string with property codes and their descriptions.\n\n#### Parameters\n\n`properties` (list): The list of properties.\n\nReturns:\n`str`: A JSON formatted string containing the property codes and descriptions.\n\n##### Usage Example\n\n```python\ndescriptions_json = show_property_descriptions(properties)\nprint(descriptions_json)\n```\n\n#### Output Example\n\n```json\n[\n {\n \"code\": \"hst\",\n \"description\": \"The hostname of the machine where the log message was originated.\"\n },\n {\n \"code\": \"src\",\n \"description\": \"The IP address or hostname of the source of the log message.\"\n },\n ...\n]\n```\n\n### Usage\n\n1. Ensure that you have `Python 3.x` installed.\n2. Create a `properties.json` file with the required property codes and descriptions in the same directory as the script.\n3. Run the script using Python.\n\n## Rsyslog Server Side Ruleset Configuration Generator\n\nThis Python function generates a rsyslog ruleset configuration string based on user inputs. It allows customization of the ruleset name, template name, port, protocol, and facility.severity pairs.\n\n## Function Overview\n\n### `create_rsyslog_ruleset(ruleset_name, template_name, port, protocol, facilities_severities)`\n\n### `create_tls_rsyslog_ruleset(ruleset_name, template_name, port, tls_ca, tls_cert, tls_key, facilities_severities)`\n\nGenerates a rsyslog configuration string with the specified parameters.\n\n#### Parameters\n\n- **`ruleset_name`** (`str`): The name of the ruleset.\n- **`template_name`** (`str`): The name of the template to use in the ruleset action.\n- **`port`** (`int`): The port number to listen on.\n- **`protocol`** (`str`): The protocol to use (`imtcp` for TCP or `imudp` for UDP).\n- **`facilities_severities`** (`str`): A comma-separated list of `facility.severity` pairs.\n- **`tls_ca`** (`str`): The TLS CA file path (`/etc/pki/certs/certificatesCA.pem`).\n- **`tls_cert`** (`str`): The TLS certificate path (`/etc/pki/certs/certificates.pem`).\n- **`tls_key`** (`str`): The TLS key path (`/etc/pki/certs/key.pem`).\n\n#### Returns\n\n- **`str`**: The generated rsyslog configuration.\n\n#### Example Usage\n\n```python\n rsyslog_config = create_rsyslog_ruleset(ruleset_name, template_name, port, protocol, facilities.severities)\n print(rsyslog_config)\n```\n\n```python\n rsyslog_tls_config = create_tls_rsyslog_ruleset(ruleset_name, template_name, port, tls_ca, tls_cert, tls_key, facilities.severities)\n print(rsyslog_tls_config)\n```\n\n# Overall usage of the entire package\n\n```python\nimport logsrvasst as asst\n# Load properties from JSON file\nport = input(\"Enter port number:\")\nprotocol = input(\"Enter protocol\")\nprint(asst.logsrvasst(port,protocol))\nif(port != 514):\n asst.print_suggestions(port,protocol)\njson_file_path = 'properties.json'\nproperties = asst.load_properties(json_file_path)\n \n# Show property descriptions\nproperty_descriptions_json = asst.show_property_descriptions(properties)\nprint(\"\\nProperty descriptions in JSON format:\")\n# print(property_descriptions_json)\n \n# User input\ntemplate_name = input(\"Enter the template name: \")\nfile_path = input(\"Enter the file path: \")\n\n# Generate the rsyslog template\nrsyslog_template = asst.generate_template(template_name, file_path, properties)\n \n# Print the generated template\nprint(\"\\nGenerated rsyslog template:\")\nprint(rsyslog_template)\n# ruleset generator\nrsyslog_ruleset = create_rsyslog_ruleset('testRule', 'exampleTemplate', 514, 'protocol', '*.info,mail.crit')\nprint(rsyslog_ruleset)\n# tls ruleset generator\ntls_ruleset = create_tls_rsyslog_ruleset(\"Test_Rule\",\"Test_template\",514,\"/etc/CA.pem\",\"/etc/tls.pem\",\"/etc/tls.key\",\"local3.info,local4.*\")\nprint(tls_ruleset)\n```\n\n#### THANK YOU\n\n\n",
"bugtrack_url": null,
"license": null,
"summary": "Generate server-side configuration for rsyslog",
"version": "0.2.1",
"project_urls": {
"Homepage": "https://github.com/titas2003/logsrvasst.git"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "609560089a46d415ff4d9969e8aafe09cbbca9d4880f58681ea9c687381fdf0b",
"md5": "b3fcb66f3c4de2ae53dc851ee27c66bf",
"sha256": "af0db598a8531e602dfca1ebce4ad4a0bb5d98d961b85f2a76978b27f26cac50"
},
"downloads": -1,
"filename": "logsrvasst-0.2.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b3fcb66f3c4de2ae53dc851ee27c66bf",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 7006,
"upload_time": "2024-08-07T16:39:32",
"upload_time_iso_8601": "2024-08-07T16:39:32.052426Z",
"url": "https://files.pythonhosted.org/packages/60/95/60089a46d415ff4d9969e8aafe09cbbca9d4880f58681ea9c687381fdf0b/logsrvasst-0.2.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "27c8ab0e38c11ccf1daa88dd4ecf827a9d3892f33d9a180f5053fccc0a43011f",
"md5": "31c38ee6b382654d85c5d2c570f36722",
"sha256": "40d81deba7c4ea143513e065ea24f2cb8ddf193fff5fb3b25a90428b15bed109"
},
"downloads": -1,
"filename": "logsrvasst-0.2.1.tar.gz",
"has_sig": false,
"md5_digest": "31c38ee6b382654d85c5d2c570f36722",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 5874,
"upload_time": "2024-08-07T16:39:33",
"upload_time_iso_8601": "2024-08-07T16:39:33.746008Z",
"url": "https://files.pythonhosted.org/packages/27/c8/ab0e38c11ccf1daa88dd4ecf827a9d3892f33d9a180f5053fccc0a43011f/logsrvasst-0.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-07 16:39:33",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "titas2003",
"github_project": "logsrvasst",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "logsrvasst"
}