Procházet zdrojové kódy

added serial check

tsi před 2 roky
rodič
revize
791ddab116
1 změnil soubory, kde provedl 48 přidání a 0 odebrání
  1. 48 0
      scripts/check_serial.py

+ 48 - 0
scripts/check_serial.py

@@ -0,0 +1,48 @@
+import os
+import re
+import requests
+
+# Telegram Configurations
+bot_token = '949332240:AAHNsQEmCW4it86Esa7F5o07XxwrotSM7s8'
+chat_id = '-914111351'
+
+# Path to the kernel log
+log_file = "/var/log/kern.log"
+
+# File to store last line read
+last_line_file = "/tmp/last_line_read.txt"
+
+# Error message to look for
+error_msg = "pl2303 ttyUSB0: usb_serial_generic_read_bulk_callback - urb stopped:"
+
+def send_telegram(bot_token, chat_id, message):
+    send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + chat_id + '&parse_mode=Markdown&text=' + message
+    response = requests.get(send_text)
+    print(response.json())
+    return response.json()
+
+def check_log_file(log_file, error_msg, last_line_file):
+    last_line_read = 0
+    if os.path.exists(last_line_file):
+        with open(last_line_file, "r") as f:
+            last_line_read = int(f.readline().strip())
+    
+    with open(log_file, "r") as f:
+        log_lines = f.readlines()
+    
+    # Check if log file was rotated
+    if len(log_lines) < last_line_read:
+        last_line_read = 0
+
+    for i, line in enumerate(log_lines[last_line_read:]):
+        if re.search(error_msg, line):
+            with open(last_line_file, "w") as f:
+                f.write(str(last_line_read + i))
+            return True
+    with open(last_line_file, "w") as f:
+        f.write(str(len(log_lines)))
+    return False
+
+if __name__ == "__main__":
+    if check_log_file(log_file, error_msg, last_line_file):
+        send_telegram(bot_token, chat_id, "Error detected: serial2usb")