parent
ff9cdf1b1b
commit
e71cb72faf
@ -1,14 +0,0 @@
|
|||||||
from flask import Blueprint, jsonify
|
|
||||||
import psutil
|
|
||||||
|
|
||||||
cpu_bp = Blueprint('cpu', __name__)
|
|
||||||
|
|
||||||
@cpu_bp.route('/cpu', methods=['GET'])
|
|
||||||
def get_cpu():
|
|
||||||
cpu_times = psutil.cpu_times_percent(interval=1, percpu=False)
|
|
||||||
return jsonify({
|
|
||||||
'user': cpu_times.user,
|
|
||||||
'system': cpu_times.system,
|
|
||||||
'idle': cpu_times.idle,
|
|
||||||
'iowait': cpu_times.iowait,
|
|
||||||
})
|
|
@ -0,0 +1,6 @@
|
|||||||
|
import psutil
|
||||||
|
|
||||||
|
def get_cpu_percent():
|
||||||
|
cpu_percent = psutil.cpu_percent(interval=1)
|
||||||
|
print(f'CPU usage: {cpu_percent}%')
|
||||||
|
return cpu_percent
|
@ -1,14 +0,0 @@
|
|||||||
from flask import Blueprint, jsonify
|
|
||||||
import psutil
|
|
||||||
|
|
||||||
io_bp = Blueprint('io', __name__)
|
|
||||||
|
|
||||||
@io_bp.route('/io', methods=['GET'])
|
|
||||||
def get_io():
|
|
||||||
io_counters = psutil.disk_io_counters()
|
|
||||||
return jsonify({
|
|
||||||
'read_count': io_counters.read_count,
|
|
||||||
'write_count': io_counters.write_count,
|
|
||||||
'read_bytes': io_counters.read_bytes,
|
|
||||||
'write_bytes': io_counters.write_bytes,
|
|
||||||
})
|
|
@ -0,0 +1,6 @@
|
|||||||
|
import psutil
|
||||||
|
|
||||||
|
def get_io_percent():
|
||||||
|
disk_usage = psutil.disk_usage('/')
|
||||||
|
print(f'Disk usage: {disk_usage.percent}%')
|
||||||
|
return disk_usage.percent
|
@ -1,15 +0,0 @@
|
|||||||
from flask import Blueprint, jsonify
|
|
||||||
import psutil
|
|
||||||
|
|
||||||
memory_bp = Blueprint('memory', __name__)
|
|
||||||
|
|
||||||
@memory_bp.route('/memory', methods=['GET'])
|
|
||||||
def get_memory():
|
|
||||||
virtual_memory = psutil.virtual_memory()
|
|
||||||
return jsonify({
|
|
||||||
'total': virtual_memory.total,
|
|
||||||
'available': virtual_memory.available,
|
|
||||||
'percent': virtual_memory.percent,
|
|
||||||
'used': virtual_memory.used,
|
|
||||||
'free': virtual_memory.free,
|
|
||||||
})
|
|
@ -0,0 +1,6 @@
|
|||||||
|
import psutil
|
||||||
|
|
||||||
|
def get_memory_percent():
|
||||||
|
virtual_memory = psutil.virtual_memory()
|
||||||
|
print(f'Memory usage: {virtual_memory.percent}%')
|
||||||
|
return virtual_memory.percent
|
@ -0,0 +1,74 @@
|
|||||||
|
import psutil
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
log_file_path = '/data/ww/py_sys_monitor/net_speed'
|
||||||
|
|
||||||
|
def ensure_log_file_exists():
|
||||||
|
if not os.path.exists(log_file_path):
|
||||||
|
os.makedirs(os.path.dirname(log_file_path), exist_ok=True)
|
||||||
|
with open(log_file_path, 'w') as f:
|
||||||
|
now = datetime.now()
|
||||||
|
for i in range(12):
|
||||||
|
past_time = (now - timedelta(hours=i)).replace(minute=0, second=0, microsecond=0).strftime('%Y-%m-%d %H:%M:%S')
|
||||||
|
f.write(f"{past_time},0,0\n")
|
||||||
|
|
||||||
|
ensure_log_file_exists()
|
||||||
|
|
||||||
|
def convert_to_mbps(bytes_per_second):
|
||||||
|
return bytes_per_second * 8 / (1024 * 1024) # Convert to Megabits per second (Mbps)
|
||||||
|
|
||||||
|
def log_network_speed():
|
||||||
|
while True:
|
||||||
|
net_io_start = psutil.net_io_counters()
|
||||||
|
time.sleep(3600) # Sleep for 1 hour
|
||||||
|
net_io_end = psutil.net_io_counters()
|
||||||
|
|
||||||
|
bytes_sent_per_second = (net_io_end.bytes_sent - net_io_start.bytes_sent) / 3600
|
||||||
|
bytes_recv_per_second = (net_io_end.bytes_recv - net_io_start.bytes_recv) / 3600
|
||||||
|
|
||||||
|
upload_speed_mbps = convert_to_mbps(bytes_sent_per_second)
|
||||||
|
download_speed_mbps = convert_to_mbps(bytes_recv_per_second)
|
||||||
|
|
||||||
|
# Get the current time and round down to the nearest hour
|
||||||
|
now = datetime.now()
|
||||||
|
current_time = now.replace(minute=0, second=0, microsecond=0).strftime('%Y-%m-%d %H:%M:%S')
|
||||||
|
|
||||||
|
with open(log_file_path, 'a') as f:
|
||||||
|
f.write(f"{current_time},{upload_speed_mbps},{download_speed_mbps}\n")
|
||||||
|
|
||||||
|
def get_last_12_hours_speed():
|
||||||
|
speeds = []
|
||||||
|
now = datetime.now()
|
||||||
|
|
||||||
|
# Generate the last 12 hours' timestamps
|
||||||
|
timestamps = [(now - timedelta(hours=i)).replace(minute=0, second=0, microsecond=0).strftime('%Y-%m-%d %H:%M:%S') for i in range(12)]
|
||||||
|
timestamp_speed_map = {timestamp: {'upload_speed_mbps': 0, 'download_speed_mbps': 0} for timestamp in timestamps}
|
||||||
|
|
||||||
|
with open(log_file_path, 'r') as f:
|
||||||
|
lines = deque(f, maxlen=12) # Read only the last 12 lines
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
timestamp, upload_speed, download_speed = line.strip().split(',')
|
||||||
|
if timestamp in timestamp_speed_map:
|
||||||
|
timestamp_speed_map[timestamp] = {
|
||||||
|
'upload_speed_mbps': float(upload_speed),
|
||||||
|
'download_speed_mbps': float(download_speed)
|
||||||
|
}
|
||||||
|
|
||||||
|
for timestamp in timestamps:
|
||||||
|
speeds.append({
|
||||||
|
'timestamp': timestamp,
|
||||||
|
'upload_speed_mbps': timestamp_speed_map[timestamp]['upload_speed_mbps'],
|
||||||
|
'download_speed_mbps': timestamp_speed_map[timestamp]['download_speed_mbps']
|
||||||
|
})
|
||||||
|
|
||||||
|
return speeds
|
||||||
|
|
||||||
|
# Start the thread to log network speed
|
||||||
|
thread = threading.Thread(target=log_network_speed)
|
||||||
|
thread.daemon = True
|
||||||
|
thread.start()
|
@ -1,11 +0,0 @@
|
|||||||
from flask import Blueprint, jsonify
|
|
||||||
import psutil
|
|
||||||
|
|
||||||
processes_bp = Blueprint('processes', __name__)
|
|
||||||
|
|
||||||
@processes_bp.route('/processes', methods=['GET'])
|
|
||||||
def get_processes():
|
|
||||||
processes = []
|
|
||||||
for proc in psutil.process_iter(['pid', 'name', 'username']):
|
|
||||||
processes.append(proc.info)
|
|
||||||
return jsonify(processes)
|
|
@ -0,0 +1,7 @@
|
|||||||
|
import psutil
|
||||||
|
|
||||||
|
def get_processes():
|
||||||
|
processes = []
|
||||||
|
for proc in psutil.process_iter(['pid', 'name', 'username']):
|
||||||
|
processes.append(proc.info)
|
||||||
|
return processes
|
@ -0,0 +1,25 @@
|
|||||||
|
from flask import Blueprint, jsonify
|
||||||
|
import app.cpu_usage as cpu_usage
|
||||||
|
import app.io_usage as io_usage
|
||||||
|
import app.memory_usage as memory_usage
|
||||||
|
import app.network as network_usage
|
||||||
|
|
||||||
|
route_bp = Blueprint('route', __name__)
|
||||||
|
|
||||||
|
# 资源使用情况
|
||||||
|
@route_bp.route('/sys/resource', methods=['GET'])
|
||||||
|
def get_resource():
|
||||||
|
cpu_percent = cpu_usage.get_cpu_percent()
|
||||||
|
io_percent = io_usage.get_io_percent()
|
||||||
|
memory_percent = memory_usage.get_memory_percent()
|
||||||
|
return jsonify({
|
||||||
|
'cpu_percent': cpu_percent,
|
||||||
|
'io_percent': io_percent,
|
||||||
|
'memory_percent': memory_percent,
|
||||||
|
})
|
||||||
|
|
||||||
|
# 获取过去12小时网络传输速度
|
||||||
|
@route_bp.route('/sys/network_speed', methods=['GET'])
|
||||||
|
def get_network():
|
||||||
|
return jsonify(network_usage.get_last_12_hours_speed())
|
||||||
|
|
Loading…
Reference in new issue