From 5d739c3e9d73e13ad836df904a88debacce31db0 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Thu, 18 May 2023 05:28:36 +0300 Subject: openwrt: server side --- src/home/database/bots.py | 5 +++-- src/openwrt_log_analyzer.py | 28 +++++++++++++++++----------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/home/database/bots.py b/src/home/database/bots.py index 26fb170..cde48b9 100644 --- a/src/home/database/bots.py +++ b/src/home/database/bots.py @@ -85,14 +85,15 @@ class BotsDatabase(MySQLDatabase): def get_openwrt_logs(self, filter_text: str, min_id: int, + access_point: int, limit: int = None) -> List[OpenwrtLogRecord]: tz = pytz.timezone('Europe/Moscow') with self.cursor(dictionary=True) as cursor: - sql = "SELECT * FROM openwrt WHERE text LIKE %s AND id > %s" + sql = "SELECT * FROM openwrt WHERE ap=%s AND text LIKE %s AND id > %s" if limit is not None: sql += f" LIMIT {limit}" - cursor.execute(sql, (f'%{filter_text}%', min_id)) + cursor.execute(sql, (access_point, f'%{filter_text}%', min_id)) data = [] for row in cursor.fetchall(): data.append(OpenwrtLogRecord( diff --git a/src/openwrt_log_analyzer.py b/src/openwrt_log_analyzer.py index bdfeb6c..a55aaf8 100755 --- a/src/openwrt_log_analyzer.py +++ b/src/openwrt_log_analyzer.py @@ -30,11 +30,14 @@ limit = 10 """ -def main(mac: str, title: str) -> int: +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 @@ -45,22 +48,25 @@ def main(mac: str, title: str) -> int: max_id = log.id text = '\n'.join(map(lambda s: str(s), data)) - telegram.send_message(f'{title}\n\n' + text) + telegram.send_message(f'{title} (AP #{ap})\n\n' + text) return max_id if __name__ == '__main__': config.load('openwrt_log_analyzer') + for ap in config['aps']: + state_file = config['simple_state']['file'] + state_file = state_file.replace('.txt', f'-{ap}.txt') - state = SimpleState(file=config['simple_state']['file'], - default={'last_id': 0}) + state = SimpleState(file=state_file, + default={'last_id': 0}) - max_last_id = 0 - for name, mac in config['devices'].items(): - last_id = main(mac, title=name) - if last_id > max_last_id: - max_last_id = last_id + 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 + if max_last_id: + state['last_id'] = max_last_id -- cgit v1.2.3