main.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import subprocess
  2. import re
  3. import time
  4. import serial
  5. serial_port = serial.Serial('/dev/ttyACM0', 115200, timeout=1)
  6. disks = ['sda', 'sdb', 'sdc', 'sdd'] # List of disks to monitor
  7. while True:
  8. cmd = "iostat -k 1 2 " + " ".join(disks)
  9. process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  10. output, _ = process.communicate()
  11. lines = output.decode().split('\n')
  12. disk_info = {}
  13. current_disk = ''
  14. for line in lines:
  15. if line.startswith('Device'):
  16. headers = re.findall(r'\S+', line)
  17. elif line.startswith(tuple(disks)):
  18. io_data = re.findall(r'\S+', line)
  19. if len(io_data) >= len(headers):
  20. current_disk = io_data[0]
  21. disk_info[current_disk] = {headers[i]: io_data[i] for i in range(1, len(headers))}
  22. counter = 0
  23. byte_array = bytearray()
  24. for disk in disks:
  25. if disk in disk_info:
  26. read_bytes = disk_info[disk].get('kB_read/s', '0.00')
  27. write_bytes = disk_info[disk].get('kB_wrtn/s', '0.00')
  28. if float(read_bytes) > 0:
  29. # serial_port.write(f"{hex(counter)}".encode())
  30. print(counter)
  31. byte_array.append(counter)
  32. else:
  33. # serial_port.write(f"{hex(counter+1)".encode())
  34. print(counter+1)
  35. byte_array.append(counter+1)
  36. if float(write_bytes) > 0:
  37. # serial_port.write(f"{hex(counter+2})".encode())
  38. print(counter+2)
  39. byte_array.append(counter+2)
  40. else:
  41. # serial_port.write(f"{hex(counter+3)".encode())
  42. print(counter+3)
  43. byte_array.append(counter+3)
  44. counter += 4
  45. serial_port.write(byte_array)
  46. #time.sleep(0.1) # Adjust the frequency of checking disk activity (1 second in this example)