openwrt: server side
This commit is contained in:
parent
2960f9f09a
commit
5d739c3e9d
@ -85,14 +85,15 @@ class BotsDatabase(MySQLDatabase):
|
|||||||
def get_openwrt_logs(self,
|
def get_openwrt_logs(self,
|
||||||
filter_text: str,
|
filter_text: str,
|
||||||
min_id: int,
|
min_id: int,
|
||||||
|
access_point: int,
|
||||||
limit: int = None) -> List[OpenwrtLogRecord]:
|
limit: int = None) -> List[OpenwrtLogRecord]:
|
||||||
tz = pytz.timezone('Europe/Moscow')
|
tz = pytz.timezone('Europe/Moscow')
|
||||||
with self.cursor(dictionary=True) as cursor:
|
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:
|
if limit is not None:
|
||||||
sql += f" LIMIT {limit}"
|
sql += f" LIMIT {limit}"
|
||||||
|
|
||||||
cursor.execute(sql, (f'%{filter_text}%', min_id))
|
cursor.execute(sql, (access_point, f'%{filter_text}%', min_id))
|
||||||
data = []
|
data = []
|
||||||
for row in cursor.fetchall():
|
for row in cursor.fetchall():
|
||||||
data.append(OpenwrtLogRecord(
|
data.append(OpenwrtLogRecord(
|
||||||
|
@ -30,11 +30,14 @@ limit = 10
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def main(mac: str, title: str) -> int:
|
def main(mac: str,
|
||||||
|
title: str,
|
||||||
|
ap: int) -> int:
|
||||||
db = BotsDatabase()
|
db = BotsDatabase()
|
||||||
|
|
||||||
data = db.get_openwrt_logs(filter_text=mac,
|
data = db.get_openwrt_logs(filter_text=mac,
|
||||||
min_id=state['last_id'],
|
min_id=state['last_id'],
|
||||||
|
access_point=ap,
|
||||||
limit=config['openwrt_log_analyzer']['limit'])
|
limit=config['openwrt_log_analyzer']['limit'])
|
||||||
if not data:
|
if not data:
|
||||||
return 0
|
return 0
|
||||||
@ -45,20 +48,23 @@ def main(mac: str, title: str) -> int:
|
|||||||
max_id = log.id
|
max_id = log.id
|
||||||
|
|
||||||
text = '\n'.join(map(lambda s: str(s), data))
|
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
|
return max_id
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
config.load('openwrt_log_analyzer')
|
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'],
|
state = SimpleState(file=state_file,
|
||||||
default={'last_id': 0})
|
default={'last_id': 0})
|
||||||
|
|
||||||
max_last_id = 0
|
max_last_id = 0
|
||||||
for name, mac in config['devices'].items():
|
for name, mac in config['devices'].items():
|
||||||
last_id = main(mac, title=name)
|
last_id = main(mac, title=name, ap=ap)
|
||||||
if last_id > max_last_id:
|
if last_id > max_last_id:
|
||||||
max_last_id = last_id
|
max_last_id = last_id
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user