| 12345678910111213141516171819202122232425262728293031323334 |
- import subprocess
- import re
- import time
- disks = ['sda', 'sdb', 'sdc', 'sdd'] # List of disks to monitor
- while True:
- cmd = "iostat -xk 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))}
- for disk in disks:
- if disk in disk_info:
- read_bytes = disk_info[disk].get('rkB/s', '0.00')
- write_bytes = disk_info[disk].get('wkB/s', '0.00')
- if float(read_bytes) > 0 or float(write_bytes) > 0:
- print(f"R/W activity detected on /dev/{disk}:")
- print(f"Read bytes: {read_bytes}, Write bytes: {write_bytes}")
- time.sleep(1) # Adjust the frequency of checking disk activity (1 second in this example)
|