75 lines
2.2 KiB
Python
75 lines
2.2 KiB
Python
import logging
|
|
import os
|
|
|
|
from django.template.loader import render_to_string
|
|
|
|
from events.constants import ELK_URL, ELK_LOGIN, ELK_PASS
|
|
from inputs.constants import LOGSTASH_CONFIG_DIR
|
|
from inputs.enums import LogInputType
|
|
from inputs.models import LogInputArmaIF, LogInputEndpoint
|
|
|
|
_log = logging.getLogger(__name__)
|
|
|
|
|
|
def update_arma_config(arma: LogInputArmaIF):
|
|
input = arma.input
|
|
|
|
if input.type != LogInputType.ARMAIF:
|
|
_log.error(f'Call update_arma_config with input type {input.type}')
|
|
return
|
|
|
|
if not input.config_file_name:
|
|
input.config_file_name = get_config_name(input)
|
|
input.save()
|
|
|
|
context = {
|
|
"pk": input.pk,
|
|
"port": arma.port,
|
|
"elastic_url": ELK_URL,
|
|
"elastic_login": ELK_LOGIN,
|
|
"elastic_pass": ELK_PASS,
|
|
"adjust_datetime": input.adjust_datetime,
|
|
}
|
|
|
|
config = render_to_string("vector/config/armaif.toml", context)
|
|
update_config(input, config)
|
|
|
|
|
|
def update_endpoint_config(endpoint: LogInputEndpoint):
|
|
input = endpoint.input
|
|
|
|
if input.type != LogInputType.ENDPOINT:
|
|
_log.error(f'Call update_endpoint_config with input type {input.type}')
|
|
return
|
|
|
|
if not input.config_file_name:
|
|
input.config_file_name = get_config_name(input)
|
|
input.save()
|
|
|
|
context = {
|
|
"pk": input.pk,
|
|
"port": endpoint.port,
|
|
"elastic_url": ELK_URL,
|
|
"elastic_login": ELK_LOGIN,
|
|
"elastic_pass": ELK_PASS,
|
|
"adjust_datetime": input.adjust_datetime,
|
|
}
|
|
config = render_to_string(
|
|
'vector/config/endpoint.toml', context)
|
|
update_config(input, config)
|
|
|
|
|
|
def update_config(input, config_content: str):
|
|
""" Write new config content to correct file"""
|
|
with open(os.path.join(LOGSTASH_CONFIG_DIR, input.config_file_name), 'w') as f:
|
|
# with open(os.path.join('/usr/src/app/public/vector', input.config_file_name), 'w') as f:
|
|
f.write(config_content)
|
|
|
|
|
|
def get_config_name(log_input) -> str:
|
|
if log_input.type == LogInputType.ARMAIF:
|
|
return f'armaif_{log_input.pk}.toml'
|
|
elif log_input.type == LogInputType.ENDPOINT:
|
|
return f'endpoint_{log_input.pk}.toml'
|
|
else:
|
|
raise RuntimeError(f"Can't made config name for type {log_input.type}")
|