diff options
author | zsdc <taras@vyos.io> | 2022-06-06 21:33:17 +0300 |
---|---|---|
committer | zsdc <taras@vyos.io> | 2022-06-06 21:33:17 +0300 |
commit | 74454c341a57aab66151b9ef2488eb72a306c002 (patch) | |
tree | 806e55b157da50db24d544800696019dc139a59c /src | |
parent | 0bbe8d655df4bc50d70aeef2d3199a73e0af9f03 (diff) | |
download | vyos-1x-74454c341a57aab66151b9ef2488eb72a306c002.tar.gz vyos-1x-74454c341a57aab66151b9ef2488eb72a306c002.zip |
event-handler: T3083: Optimized event-handler
* Removed dynamic generating for systemd unit
* Optimized configuration file deleting process
* Added exceptions handlers to event-handler script to protect service
from most obvious potential troubles
* Improved logging
* Moved pattern compilation outside a messages loop to avoid extra operations
* Added signal handlers for proper systemd integration
Diffstat (limited to 'src')
-rwxr-xr-x | src/conf_mode/system_event_handler.py | 57 | ||||
-rwxr-xr-x | src/helpers/vyos-event-handler.py | 55 | ||||
-rwxr-xr-x | src/system/vyos-event-handler.py | 120 | ||||
-rw-r--r-- | src/systemd/vyos-event-handler.service | 11 |
4 files changed, 158 insertions, 85 deletions
diff --git a/src/conf_mode/system_event_handler.py b/src/conf_mode/system_event_handler.py index 8efc816cb..9f91e99fc 100755 --- a/src/conf_mode/system_event_handler.py +++ b/src/conf_mode/system_event_handler.py @@ -15,22 +15,17 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import json -import os +from pathlib import Path from vyos.config import Config -from vyos.configdict import node_changed -from vyos.util import cmd from vyos.util import call -from vyos.template import render from vyos import ConfigError from vyos import airbag -airbag.enable() +airbag.enable() -systemd_dir = '/etc/systemd/system' -systemd_service = 'vyos-event-handler' -service_path = f'{systemd_dir}/{systemd_service}.service' -event_conf = '/run/vyos-event-handler.conf' +service_name = 'vyos-event-handler' +service_conf = Path(f'/run/{service_name}.conf') def get_config(config=None): @@ -40,41 +35,43 @@ def get_config(config=None): conf = Config() base = ['system', 'event-handler'] - event = conf.get_config_dict(base, get_first_key=True, no_tag_node_value_mangle=True) + config = conf.get_config_dict(base, + get_first_key=True, + no_tag_node_value_mangle=True) - return event + return config -def verify(event): + +def verify(config): # bail out early - looks like removal from running config - if not event: + if not config: return None - for name, event_config in event.items(): + for name, event_config in config.items(): if 'pattern' not in event_config or 'script' not in event_config: - raise ConfigError(f'Event-handler "pattern and script" are mandatory!') + raise ConfigError( + 'Event-handler: both pattern and script items are mandatory!') -def generate(event): - if not event: - return None - conf_json = json.dumps(event, indent = 4) - with open(event_conf, 'w') as f: - f.write(conf_json) +def generate(config): + if not config: + # Remove old config and return + service_conf.unlink(missing_ok=True) + return None - render(service_path, 'event-handler/systemd_event_handler_service.j2', event) + # Write configuration file + conf_json = json.dumps(config, indent=4) + service_conf.write_text(conf_json) return None -def apply(event): - call('systemctl daemon-reload') - if event: - call(f'systemctl restart {systemd_service}.service') + +def apply(config): + if config: + call(f'systemctl restart {service_name}.service') else: - call(f'systemctl stop {systemd_service}.service') + call(f'systemctl stop {service_name}.service') - for f in [service_path, event_conf]: - if os.path.isfile(f): - os.unlink(f) if __name__ == '__main__': try: diff --git a/src/helpers/vyos-event-handler.py b/src/helpers/vyos-event-handler.py deleted file mode 100755 index 018a752f4..000000000 --- a/src/helpers/vyos-event-handler.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env python3 -# -# Copyright (C) 2022 VyOS maintainers and contributors -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 2 or later as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -import argparse -import select -import re -import json - -from sys import argv -from os import getpid -from systemd import journal -from vyos.util import call - - -parser = argparse.ArgumentParser() -parser.add_argument("-c", "--config", action="store", help="Path to even-handler configuration", required=True) - -args = parser.parse_args() -config = args.config -data = journal.Reader() -data.seek_tail() -data.get_previous() -p = select.poll() -p.register(data, data.get_events()) -my_pid = getpid() - -with open(config, 'r') as f: - config = json.load(f) - - -if __name__ == '__main__': - while p.poll(): - if data.process() != journal.APPEND: - continue - for entry in data: - message = entry['MESSAGE'] - for name, event_config in config.items(): - pattern = re.compile(rf'{event_config["pattern"]}') - script = event_config['script'] - if message != "" and entry['_PID'] != my_pid and pattern.match(message): - call(script) - journal.send(f'Pattern found: {event_config["pattern"]}, script executed: {script}') diff --git a/src/system/vyos-event-handler.py b/src/system/vyos-event-handler.py new file mode 100755 index 000000000..f4b793dbe --- /dev/null +++ b/src/system/vyos-event-handler.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2022 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import argparse +import select +import re +import json +from os import getpid +from pathlib import Path +from signal import signal, SIGTERM, SIGINT +from systemd import journal +from sys import exit +from vyos.util import call + +# Identify this script +my_pid = getpid() +my_name = Path(__file__).stem + + +# handle termination signal +def handle_signal(signal_type, frame): + if signal_type == SIGTERM: + journal.send('Received SIGTERM signal, stopping normally', + SYSLOG_IDENTIFIER=my_name) + if signal_type == SIGINT: + journal.send('Received SIGINT signal, stopping normally', + SYSLOG_IDENTIFIER=my_name) + exit(0) + + +# Execute script safely +def script_run(pattern: str, script_path: str) -> None: + try: + call(script_path) + journal.send( + f'Pattern found: "{pattern}", script executed: "{script_path}"', + SYSLOG_IDENTIFIER=my_name) + except Exception as err: + journal.send( + f'Pattern found: "{pattern}", failed to execute script "{script_path}": {err}', + SYSLOG_IDENTIFIER=my_name) + + +# iterate trough regexp items +def analyze_message(message: str, regex_patterns: dict) -> None: + for pattern_compiled, pattern_config in regex_patterns.items(): + if pattern_compiled.match(message): + script_run(pattern_config['pattern_raw'], + pattern_config['pattern_script']) + + +if __name__ == '__main__': + # Parse command arguments and get config + parser = argparse.ArgumentParser() + parser.add_argument('-c', + '--config', + action='store', + help='Path to even-handler configuration', + required=True, + type=Path) + + args = parser.parse_args() + try: + config_path = Path(args.config) + config = json.loads(config_path.read_text()) + except Exception as err: + print( + f'Configuration file "{config_path}" does not exist or malformed: {err}' + ) + exit(1) + + # Prepare for proper exitting + signal(SIGTERM, handle_signal) + signal(SIGINT, handle_signal) + + # Set up journal connection + data = journal.Reader() + data.seek_tail() + data.get_previous() + p = select.poll() + p.register(data, data.get_events()) + + # Prepare compiled regex objects + patterns = {} + for name, event_config in config.items(): + pattern_raw = f'{event_config["pattern"]}' + pattern_compiled = re.compile(rf'{event_config["pattern"]}') + pattern_config = { + pattern_compiled: { + 'pattern_raw': pattern_raw, + 'pattern_script': event_config['script'] + } + } + patterns.update(pattern_config) + + journal.send(f'Started with configuration: {config}', + SYSLOG_IDENTIFIER=my_name) + + while p.poll(): + if data.process() != journal.APPEND: + continue + for entry in data: + message = entry['MESSAGE'] + pid = entry['_PID'] + # Skip empty messages and messages from this process + if message and pid != my_pid: + analyze_message(message, patterns) diff --git a/src/systemd/vyos-event-handler.service b/src/systemd/vyos-event-handler.service new file mode 100644 index 000000000..6afe4f95b --- /dev/null +++ b/src/systemd/vyos-event-handler.service @@ -0,0 +1,11 @@ +[Unit] +Description=VyOS event handler +After=network.target vyos-router.service + +[Service] +Type=simple +Restart=always +ExecStart=/usr/bin/python3 /usr/libexec/vyos/system/vyos-event-handler.py --config /run/vyos-event-handler.conf + +[Install] +WantedBy=multi-user.target |