import subprocess import re import time import serial serial_port = serial.Serial('/dev/ttyACM0', 115200, timeout=1) disks = ['sda', 'sdb', 'sdc', 'sdd'] # List of disks to monitor while True: cmd = "iostat -k 1 2 " + " ".join(disks) process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output, _ = process.communicate() lines = output.decode().split('\n') disk_info = {} current_disk = '' for line in lines: if line.startswith('Device'): headers = re.findall(r'\S+', line) elif line.startswith(tuple(disks)): io_data = re.findall(r'\S+', line) if len(io_data) >= len(headers): current_disk = io_data[0] disk_info[current_disk] = {headers[i]: io_data[i] for i in range(1, len(headers))} counter = 0 byte_array = bytearray() for disk in disks: if disk in disk_info: read_bytes = disk_info[disk].get('kB_read/s', '0.00') write_bytes = disk_info[disk].get('kB_wrtn/s', '0.00') if float(read_bytes) > 0: # serial_port.write(f"{hex(counter)}".encode()) print(counter) byte_array.append(counter) else: # serial_port.write(f"{hex(counter+1)".encode()) print(counter+1) byte_array.append(counter+1) if float(write_bytes) > 0: # serial_port.write(f"{hex(counter+2})".encode()) print(counter+2) byte_array.append(counter+2) else: # serial_port.write(f"{hex(counter+3)".encode()) print(counter+3) byte_array.append(counter+3) counter += 4 serial_port.write(byte_array) #time.sleep(0.1) # Adjust the frequency of checking disk activity (1 second in this example)