From 2960f9f09a9991e342a676999e498b08affa4dff Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Thu, 18 May 2023 05:12:35 +0300 Subject: openwrt: home side changes --- src/home/api/web_api_client.py | 6 ++++-- src/home/database/bots.py | 7 ++++--- src/openwrt_logger.py | 21 +++++++++++++-------- src/web_api.py | 4 +++- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/home/api/web_api_client.py b/src/home/api/web_api_client.py index ca9a9ee..6677182 100644 --- a/src/home/api/web_api_client.py +++ b/src/home/api/web_api_client.py @@ -66,9 +66,11 @@ class WebAPIClient: }) def log_openwrt(self, - lines: List[Tuple[int, str]]): + lines: List[Tuple[int, str]], + access_point: int): return self._post('log/openwrt/', { - 'logs': stringify(lines) + 'logs': stringify(lines), + 'ap': access_point }) def get_sensors_data(self, diff --git a/src/home/database/bots.py b/src/home/database/bots.py index 99befc0..26fb170 100644 --- a/src/home/database/bots.py +++ b/src/home/database/bots.py @@ -37,13 +37,14 @@ class BotsDatabase(MySQLDatabase): self.commit() def add_openwrt_logs(self, - lines: List[Tuple[datetime, str]]): + lines: List[Tuple[datetime, str]], + access_point: int): now = datetime.now() with self.cursor() as cursor: for line in lines: time, text = line - cursor.execute("INSERT INTO openwrt (log_time, received_time, text) VALUES (%s, %s, %s)", - (time.strftime(datetime_fmt), now.strftime(datetime_fmt), text)) + cursor.execute("INSERT INTO openwrt (log_time, received_time, text, ap) VALUES (%s, %s, %s, %s)", + (time.strftime(datetime_fmt), now.strftime(datetime_fmt), text, access_point)) self.commit() def add_sound_hits(self, diff --git a/src/openwrt_logger.py b/src/openwrt_logger.py index 098c049..0c5df28 100755 --- a/src/openwrt_logger.py +++ b/src/openwrt_logger.py @@ -2,16 +2,15 @@ import os from datetime import datetime +from typing import Tuple, List +from argparse import ArgumentParser from home.config import config from home.database import SimpleState from home.api import WebAPIClient -from typing import Tuple, List - -log_file = '/var/log/openwrt.log' f""" This script is supposed to be run by cron every 5 minutes or so. -It looks for new lines in {log_file} and sends them to remote server. +It looks for new lines in log file and sends them to remote server. OpenWRT must have remote logging enabled (UDP; IP of host this script is launched on; port 514) @@ -41,16 +40,22 @@ def parse_line(line: str) -> Tuple[int, str]: if __name__ == '__main__': - config.load('openwrt_logger') + parser = ArgumentParser() + parser.add_argument('--file', type=str, required=True, + help='openwrt log file') + parser.add_argument('--access-point', type=int, required=True, + help='access point number') + + arg = config.load('openwrt_logger', parser=parser) state = SimpleState(file=config['simple_state']['file'], default={'seek': 0, 'size': 0}) - fsize = os.path.getsize(log_file) + fsize = os.path.getsize(arg.file) if fsize < state['size']: state['seek'] = 0 - with open(log_file, 'r') as f: + with open(arg.file, 'r') as f: if state['seek']: # jump to the latest read position f.seek(state['seek']) @@ -75,4 +80,4 @@ if __name__ == '__main__': lines.append((0, line)) api = WebAPIClient() - api.log_openwrt(lines) + api.log_openwrt(lines, arg.access_point) diff --git a/src/web_api.py b/src/web_api.py index c00c372..0ddc6bd 100755 --- a/src/web_api.py +++ b/src/web_api.py @@ -154,8 +154,10 @@ class WebAPIServer(http.HTTPServer): try: logs = data['logs'] + ap = int(data['ap']) except KeyError: logs = '' + ap = 0 # validate it logs = json.loads(logs) @@ -173,7 +175,7 @@ class WebAPIServer(http.HTTPServer): line[1] )) - BotsDatabase().add_openwrt_logs(lines) + BotsDatabase().add_openwrt_logs(lines, ap) return self.ok() async def GET_recordings_list(self, req: http.Request): -- cgit v1.2.3