Added disk support
This commit is contained in:
parent
e748f29932
commit
ac5b4aab6e
1 changed files with 54 additions and 2 deletions
56
main.py
Normal file → Executable file
56
main.py
Normal file → Executable file
|
|
@ -1,6 +1,48 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
import json
|
import json
|
||||||
|
from threading import Thread, Lock
|
||||||
from flask import Flask, jsonify
|
from flask import Flask, jsonify
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from datetime import datetime
|
||||||
|
import time
|
||||||
|
import psutil
|
||||||
|
|
||||||
|
class Disk:
|
||||||
|
def __init__(self, values, updated) -> None:
|
||||||
|
self.sdiskio = values
|
||||||
|
self.updated = updated
|
||||||
|
self.read_rate = 0
|
||||||
|
self.read_rate = 0
|
||||||
|
|
||||||
|
def update(self, values, updated) -> None:
|
||||||
|
time_diff = updated - self.updated
|
||||||
|
read_diff = values.read_bytes - self.sdiskio.read_bytes
|
||||||
|
wrtn_diff = values.write_bytes - self.sdiskio.write_bytes
|
||||||
|
|
||||||
|
self.read_rate = read_diff/time_diff.total_seconds()
|
||||||
|
self.wrtn_rate = wrtn_diff/time_diff.total_seconds()
|
||||||
|
|
||||||
|
self.sdiskio = values
|
||||||
|
self.updated = updated
|
||||||
|
|
||||||
|
disk_dict = dict()
|
||||||
|
mutex = Lock()
|
||||||
|
def disk_io():
|
||||||
|
disks = psutil.disk_io_counters(perdisk=True)
|
||||||
|
updated = datetime.now()
|
||||||
|
|
||||||
|
with mutex:
|
||||||
|
for disk in disks:
|
||||||
|
if disk not in disk_dict:
|
||||||
|
disk_dict[disk] = Disk(disks[disk], updated)
|
||||||
|
else:
|
||||||
|
disk_dict[disk].update(disks[disk], updated)
|
||||||
|
|
||||||
|
def disk_thread_targ():
|
||||||
|
print("Disk thread started.")
|
||||||
|
while True:
|
||||||
|
disk_io()
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
def remove_empty_lists(item):
|
def remove_empty_lists(item):
|
||||||
if isinstance(item, list):
|
if isinstance(item, list):
|
||||||
|
|
@ -13,7 +55,10 @@ def remove_empty_lists(item):
|
||||||
else:
|
else:
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
print("App started.")
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
t = Thread(target=disk_thread_targ)
|
||||||
|
t.start()
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def hello():
|
def hello():
|
||||||
|
|
@ -61,7 +106,14 @@ def temps():
|
||||||
@app.route("/api/disks")
|
@app.route("/api/disks")
|
||||||
def disks():
|
def disks():
|
||||||
out = '{"disks":[' + subprocess.getoutput("df -P --total -x squashfs | awk \'BEGIN {printf\"{\\\"discarray\\\":[\"}{if($1==\"Filesystem\")next;if(a)printf\",\";printf\"{\\\"mount\\\":\\\"\"$6\"\\\",\\\"size\\\":\\\"\"$2\"\\\",\\\"used\\\":\\\"\"$3\"\\\",\\\"avail\\\":\\\"\"$4\"\\\",\\\"use%\\\":\\\"\"$5\"\\\"}\";a++;}END{print\"]}\";}\'")
|
out = '{"disks":[' + subprocess.getoutput("df -P --total -x squashfs | awk \'BEGIN {printf\"{\\\"discarray\\\":[\"}{if($1==\"Filesystem\")next;if(a)printf\",\";printf\"{\\\"mount\\\":\\\"\"$6\"\\\",\\\"size\\\":\\\"\"$2\"\\\",\\\"used\\\":\\\"\"$3\"\\\",\\\"avail\\\":\\\"\"$4\"\\\",\\\"use%\\\":\\\"\"$5\"\\\"}\";a++;}END{print\"]}\";}\'")
|
||||||
out += "," + subprocess.run(["iostat", "-d", "-o", "JSON"], capture_output = True, text = True).stdout + "]}"
|
|
||||||
|
dict_dict = dict()
|
||||||
|
with mutex:
|
||||||
|
for disk in disk_dict:
|
||||||
|
dict_dict[disk] = dict(disk_dict[disk].__dict__)
|
||||||
|
dict_dict[disk].pop('updated')
|
||||||
|
|
||||||
|
out += "," + json.dumps(dict_dict) + "]}"
|
||||||
return jsonify(remove_empty_lists(json.loads(out)))
|
return jsonify(remove_empty_lists(json.loads(out)))
|
||||||
|
|
||||||
@app.route("/api/memory")
|
@app.route("/api/memory")
|
||||||
|
|
@ -100,4 +152,4 @@ def network():
|
||||||
elif "inet6" in line:
|
elif "inet6" in line:
|
||||||
lan_dict[interface]["inet6"] = line.split()[1].split("/")[0]
|
lan_dict[interface]["inet6"] = line.split()[1].split("/")[0]
|
||||||
|
|
||||||
return jsonify({"wan": wan_out, "lan": lan_dict})
|
return jsonify({"wan": wan_out, "lan": lan_dict})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue