Forráskód Böngészése

second script using stats

root 2 éve
szülő
commit
d5b64145bb
2 módosított fájl, 77 hozzáadás és 9 törlés
  1. 27 9
      main.py
  2. 50 0
      main_2.py

+ 27 - 9
main.py

@@ -1,11 +1,14 @@
 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 -xk 1 2 " + " ".join(disks)
+    cmd = "iostat -k 1 2 " + " ".join(disks)
     process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
     output, _ = process.communicate()
 
@@ -22,13 +25,28 @@ while True:
             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('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)
+            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)

+ 50 - 0
main_2.py

@@ -0,0 +1,50 @@
+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)
+
+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
+    serial_port.write(byte_array)
+    time.sleep(0.1)  # Adjust the frequency of checking disk activity (1 second in this example)
+    prev_stats = current_stats