ViniCastilho

cdp-b2-t1-monitor-log.py

Jun 28th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. from concurrent.futures import ThreadPoolExecutor;
  2. import socket;
  3. import math;
  4. import os;
  5.  
  6. # /home/a221251782/Monitoramento/
  7. LOGFILEPATH = 'simula-log.log';
  8. SERVERHOST = 'localhost';
  9. SERVERPORT = 8191;
  10. MAXWORKERS = 4;
  11.  
  12. def sendlines(lnchunk):
  13. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sc:
  14. SERVER = (SERVERHOST, SERVERPORT);
  15. sc.connect(SERVER);
  16. separator = '\n';
  17. sc.sendall(separator.join(lnchunk).encode());
  18. #with:
  19. #def:
  20.  
  21. def monitorfile():
  22. lastsize = os.path.getsize(LOGFILEPATH);
  23. with open(LOGFILEPATH, 'r') as fh:
  24. fh.seek(0, os.SEEK_END);
  25. tpexec = ThreadPoolExecutor(max_workers=MAXWORKERS);
  26. while True:
  27. currsize = os.path.getsize(LOGFILEPATH);
  28. if currsize > lastsize:
  29. rlines = fh.readlines();
  30. chunksize = max(1, math.floor(len(rlines) / MAXWORKERS));
  31. for i in range(0, len(rlines), chunksize):
  32. tpexec.submit(sendlines, rlines[i:i+chunksize]);
  33. #for:
  34. lastsize = currsize;
  35. #if:
  36. #while:
  37. #with:
  38. #def:
  39.  
  40. if __name__ == '__main__': monitorfile();
Advertisement
Add Comment
Please, Sign In to add comment