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 def read_diskstats(): with open('/proc/diskstats', 'r') as file: lines = file.readlines() disk_stats = {} for line in lines: data = line.split() if len(data) >= 14: if data[2] in disks: disk_stats[data[2]] = { 'reads': int(data[3]), 'writes': int(data[7]) } return disk_stats prev_stats = read_diskstats() current_stats = read_diskstats() print(current_stats) prev_data = bytearray() while True: current_stats = read_diskstats() byte_array = bytearray() counter = 0 for disk in disks: read_diff = current_stats[disk]['reads'] - prev_stats[disk]['reads'] write_diff = current_stats[disk]['writes'] - prev_stats[disk]['writes'] if read_diff > 0: # print(counter) byte_array.append(counter) else: # print(counter+1) byte_array.append(counter+1) if write_diff > 0: # print(counter+2) byte_array.append(counter+2) else: # print(counter+3) byte_array.append(counter+3) counter += 4 if byte_array != prev_data: serial_port.write(byte_array) prev_data = byte_array time.sleep(0.1) # Adjust the frequency of checking disk activity (1 second in this example) prev_stats = current_stats