summaryrefslogtreecommitdiff
path: root/src/openwrt_log_analyzer.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/openwrt_log_analyzer.py')
-rw-r--r--src/openwrt_log_analyzer.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/openwrt_log_analyzer.py b/src/openwrt_log_analyzer.py
new file mode 100644
index 0000000..f6d6413
--- /dev/null
+++ b/src/openwrt_log_analyzer.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python3
+from home.config import config
+from home.database import BotsDatabase, SimpleState
+from home.util import send_telegram
+
+"""
+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
+"""
+
+
+def main(mac: str, title: str) -> int:
+ db = BotsDatabase()
+
+ data = db.get_openwrt_logs(filter_text=mac,
+ min_id=state['last_id'],
+ 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))
+ send_telegram(f'<b>{title}</b>\n\n' + text)
+
+ return max_id
+
+
+if __name__ == '__main__':
+ config.load('openwrt_log_analyzer')
+
+ state = SimpleState(file=config['simple_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
+
+ if max_last_id:
+ state['last_id'] = max_last_id