openwrt: server side

This commit is contained in:
Evgeny Zinoviev 2023-05-18 05:28:36 +03:00
parent 2960f9f09a
commit 5d739c3e9d
2 changed files with 20 additions and 13 deletions

View File

@ -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(

View File

@ -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'<b>{title}</b>\n\n' + text)
telegram.send_message(f'<b>{title} (AP #{ap})</b>\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