angolo40 / mikrocata2selks

Mikrotik + Selks (Suricata) + Telegram + TZSP on Debian 12
GNU General Public License v3.0
71 stars 16 forks source link

rewrited read_json function #6

Closed bekhzad-khamidullaev closed 10 months ago

bekhzad-khamidullaev commented 1 year ago

systemd crashed every minute, I found this error:

Traceback (most recent call last):
  File "/usr/local/bin/mikrocataTZSP0.py", line 403, in <module>
    main()
  File "/usr/local/bin/mikrocataTZSP0.py", line 386, in main
    notifier.loop()
  File "/usr/local/lib/python3.9/dist-packages/pyinotify.py", line 1376, in loop
    self.process_events()
  File "/usr/local/lib/python3.9/dist-packages/pyinotify.py", line 1275, in process_events
    self._default_proc_fun(revent)
  File "/usr/local/lib/python3.9/dist-packages/pyinotify.py", line 910, in __call__
    return _ProcessEvent.__call__(self, event)
  File "/usr/local/lib/python3.9/dist-packages/pyinotify.py", line 630, in __call__
    return meth(event)
  File "/usr/local/bin/mikrocataTZSP0.py", line 77, in process_IN_MODIFY
    add_to_tik(read_json(FILEPATH))
  File "/usr/local/bin/mikrocataTZSP0.py", line 114, in read_json
    alerts = [ujson.loads(line) for line in f.readlines()]
  File "/usr/local/bin/mikrocataTZSP0.py", line 114, in <listcomp>
    alerts = [ujson.loads(line) for line in f.readlines()]
ujson.JSONDecodeError: Unmatched '"' when decoding 'string'

I rewrote the read_json function and now the script works stably without errors:

 def read_json(fpath):
    global last_pos

    try:
        with open(fpath, "r") as f:
            f.seek(last_pos)
            alerts = []

            for line in f.readlines():
                try:
                    alerts.append(ujson.loads(line))
                except ujson.JSONDecodeError as e:
                    print(f"JSON decoding error: {e}\nLine: {line}")

            last_pos = f.tell()
            return alerts

    except FileNotFoundError:
        print(f"[Mikrocata] File: {fpath} not found. Retrying in 10 seconds..")
        sleep(10)
angolo40 commented 1 year ago

Hello and thanks.... Wich distro are u using? Python version?

bekhzad-khamidullaev commented 1 year ago

Hello! i am using ubuntu server 22.04.1 and python 3.9

angolo40 commented 10 months ago

Check 2.1. Thanks mate