35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
from flask import Flask, jsonify
|
|
import psutil
|
|
|
|
app = Flask(__name__)
|
|
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
|
|
|
|
def bytes_to_human_readable(bytes):
|
|
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
|
|
if bytes < 1024.0:
|
|
return f"{bytes:.2f} {unit}"
|
|
bytes /= 1024.0
|
|
|
|
def get_disk_info():
|
|
disk_info = []
|
|
partitions = psutil.disk_partitions()
|
|
for partition in partitions:
|
|
usage = psutil.disk_usage(partition.mountpoint)
|
|
disk_info.append({
|
|
'device': partition.device.replace('\\\\', '\\').rstrip('\\'),
|
|
#'mountpoint': partition.mountpoint,
|
|
#'fstype': partition.fstype,
|
|
'total': bytes_to_human_readable(usage.total),
|
|
'used': bytes_to_human_readable(usage.used),
|
|
'free': bytes_to_human_readable(usage.free),
|
|
'percent': usage.percent
|
|
})
|
|
return disk_info
|
|
|
|
@app.route('/disk', methods=['GET'])
|
|
def disk():
|
|
return jsonify(get_disk_info())
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5000)
|