From f29e139cbb7e4a4d539cba6e894ef4a6acd312d6 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Wed, 31 May 2023 09:22:00 +0300 Subject: WIP: big refactoring --- src/openwrt_log_analyzer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/openwrt_log_analyzer.py') diff --git a/src/openwrt_log_analyzer.py b/src/openwrt_log_analyzer.py index d31c3bf..35b755f 100755 --- a/src/openwrt_log_analyzer.py +++ b/src/openwrt_log_analyzer.py @@ -54,7 +54,7 @@ def main(mac: str, if __name__ == '__main__': - config.load('openwrt_log_analyzer') + config.load_app('openwrt_log_analyzer') for ap in config['openwrt_log_analyzer']['aps']: state_file = config['simple_state']['file'] state_file = state_file.replace('.txt', f'-{ap}.txt') -- cgit v1.2.3 From 3790c2205396cf860738f297e6ddc49cd2b2a03f Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 10 Jun 2023 22:29:24 +0300 Subject: new config: port openwrt_logger and webapiclient --- src/openwrt_log_analyzer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/openwrt_log_analyzer.py') diff --git a/src/openwrt_log_analyzer.py b/src/openwrt_log_analyzer.py index 35b755f..c1c4fbe 100755 --- a/src/openwrt_log_analyzer.py +++ b/src/openwrt_log_analyzer.py @@ -59,7 +59,7 @@ if __name__ == '__main__': state_file = config['simple_state']['file'] state_file = state_file.replace('.txt', f'-{ap}.txt') - state = SimpleState(file=state_file, + state = SimpleState(name=state_file, default={'last_id': 0}) max_last_id = 0 -- cgit v1.2.3 From f3b9d50496257d87757802dfb472b5ffae11962c Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 10 Jun 2023 22:44:31 +0300 Subject: new config: port openwrt_log_analyzer --- src/openwrt_log_analyzer.py | 66 ++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 30 deletions(-) (limited to 'src/openwrt_log_analyzer.py') diff --git a/src/openwrt_log_analyzer.py b/src/openwrt_log_analyzer.py index c1c4fbe..96023cd 100755 --- a/src/openwrt_log_analyzer.py +++ b/src/openwrt_log_analyzer.py @@ -1,33 +1,39 @@ #!/usr/bin/env python3 import home.telegram as telegram -from home.config import config +from home.telegram.config import TelegramChatsConfig +from home.util import validate_mac_address +from typing import Optional +from home.config import config, AppConfigUnit from home.database import BotsDatabase, SimpleState -""" -config.toml example: -[simple_state] -file = "/home/user/.config/openwrt_log_analyzer/state.txt" - -[mysql] -host = "localhost" -database = ".." -user = ".." -password = ".." - -[devices] -Device1 = "00:00:00:00:00:00" -Device2 = "01:01:01:01:01:01" - -[telegram] -chat_id = ".." -token = ".." -parse_mode = "HTML" - -[openwrt_log_analyzer] -limit = 10 -""" +class OpenwrtLogAnalyzerConfig(AppConfigUnit): + @classmethod + def schema(cls) -> Optional[dict]: + return { + 'database_name': {'type': 'string', 'required': True}, + 'devices': { + 'type': 'dict', + 'keysrules': {'type': 'string'}, + 'valuesrules': { + 'type': 'string', + 'check_with': validate_mac_address + } + }, + 'limit': {'type': 'integer'}, + 'telegram_chat': {'type': 'string'}, + 'aps': { + 'type': 'list', + 'schema': {'type': 'integer'} + } + } + + @staticmethod + def custom_validator(data): + chats = TelegramChatsConfig() + if data['telegram_chat'] not in chats: + return ValueError(f'unknown telegram chat {data["telegram_chat"]}') def main(mac: str, @@ -48,18 +54,18 @@ def main(mac: str, max_id = log.id text = '\n'.join(map(lambda s: str(s), data)) - telegram.send_message(f'{title} (AP #{ap})\n\n' + text) + telegram.send_message(f'{title} (AP #{ap})\n\n' + text, config.app_config['telegram_chat']) return max_id if __name__ == '__main__': - config.load_app('openwrt_log_analyzer') - for ap in config['openwrt_log_analyzer']['aps']: - state_file = config['simple_state']['file'] - state_file = state_file.replace('.txt', f'-{ap}.txt') + config.load_app(OpenwrtLogAnalyzerConfig) + for ap in config.app_config['aps']: + dbname = config.app_config['database_name'] + dbname = dbname.replace('.txt', f'-{ap}.txt') - state = SimpleState(name=state_file, + state = SimpleState(name=dbname, default={'last_id': 0}) max_last_id = 0 -- cgit v1.2.3 From b0bf43e6a272d42a55158e657bd937cb82fc3d8d Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 10 Jun 2023 23:02:34 +0300 Subject: move files, rename home package to homekit --- src/openwrt_log_analyzer.py | 78 --------------------------------------------- 1 file changed, 78 deletions(-) delete mode 100755 src/openwrt_log_analyzer.py (limited to 'src/openwrt_log_analyzer.py') diff --git a/src/openwrt_log_analyzer.py b/src/openwrt_log_analyzer.py deleted file mode 100755 index 96023cd..0000000 --- a/src/openwrt_log_analyzer.py +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/env python3 -import home.telegram as telegram - -from home.telegram.config import TelegramChatsConfig -from home.util import validate_mac_address -from typing import Optional -from home.config import config, AppConfigUnit -from home.database import BotsDatabase, SimpleState - - -class OpenwrtLogAnalyzerConfig(AppConfigUnit): - @classmethod - def schema(cls) -> Optional[dict]: - return { - 'database_name': {'type': 'string', 'required': True}, - 'devices': { - 'type': 'dict', - 'keysrules': {'type': 'string'}, - 'valuesrules': { - 'type': 'string', - 'check_with': validate_mac_address - } - }, - 'limit': {'type': 'integer'}, - 'telegram_chat': {'type': 'string'}, - 'aps': { - 'type': 'list', - 'schema': {'type': 'integer'} - } - } - - @staticmethod - def custom_validator(data): - chats = TelegramChatsConfig() - if data['telegram_chat'] not in chats: - return ValueError(f'unknown telegram chat {data["telegram_chat"]}') - - -def main(mac: str, - title: str, - ap: int) -> int: - db = BotsDatabase() - - data = db.get_openwrt_logs(filter_text=mac, - min_id=state['last_id'], - access_point=ap, - limit=config['openwrt_log_analyzer']['limit']) - if not data: - return 0 - - max_id = 0 - for log in data: - if log.id > max_id: - max_id = log.id - - text = '\n'.join(map(lambda s: str(s), data)) - telegram.send_message(f'{title} (AP #{ap})\n\n' + text, config.app_config['telegram_chat']) - - return max_id - - -if __name__ == '__main__': - config.load_app(OpenwrtLogAnalyzerConfig) - for ap in config.app_config['aps']: - dbname = config.app_config['database_name'] - dbname = dbname.replace('.txt', f'-{ap}.txt') - - state = SimpleState(name=dbname, - default={'last_id': 0}) - - max_last_id = 0 - for name, mac in config['devices'].items(): - last_id = main(mac, title=name, ap=ap) - if last_id > max_last_id: - max_last_id = last_id - - if max_last_id: - state['last_id'] = max_last_id -- cgit v1.2.3