import requests import json import psutil import time def get_ip_address(): return "10.181.7.236" def get_system_uptime(): return int(time.time()-psutil.boot_time() ) def get_cpu_info(): cpu_cores = psutil.cpu_count(logical=False) # 物理CPU核心数 cpu_count = psutil.cpu_count() # 总CPU核心数(包括逻辑CPU) cpu_freq = psutil.cpu_freq().current # CPU频率 return { 'cpu_cores': cpu_cores, 'cpu_count': cpu_count, 'cpu_freq': cpu_freq, } def get_memory_info(): memory = psutil.virtual_memory() return { 'total_memory': memory.total, 'memory_usage': memory.percent, } def get_disk_info(path='/'): disk = psutil.disk_usage(path) disk_io = psutil.disk_io_counters() return { 'disk_total':disk.total, 'disk_usage': disk.percent, 'disk_read_speed': disk_io.read_bytes / (1024 ** 2), # 转换为MB/s 'disk_write_speed': disk_io.write_bytes / (1024 ** 2), # 转换为MB/s } def get_cpu_usage(): return psutil.cpu_percent(interval=1) def monitor_system(interval=60): url = "http://10.181.7.236:9988/api/resource/monitoring" headers = { 'Content-Type': 'application/json' } while 1: payload = json.dumps({ "system_ip" : get_ip_address(), "system_uptime" : get_system_uptime(), 'cpu_cores' : get_cpu_info()['cpu_cores'], 'cpu_count' : get_cpu_info()['cpu_count'], 'cpu_freq' : get_cpu_info()['cpu_freq'], 'cpu_usage' : get_cpu_usage(), 'memory_total' : get_memory_info()['total_memory']/(1024 ** 3), 'memory_usage' : get_memory_info()['memory_usage'], 'disk1_name' : '/', 'disk1_total' : get_disk_info()['disk_total']/(1024 ** 3), 'disk1_usage' : get_disk_info()['disk_usage'], 'disk1_read_speed' : get_disk_info()['disk_read_speed']/(1024 ** 2), 'disk1_write_speed' : get_disk_info()['disk_write_speed']/(1024 ** 2), 'disk2_name' : '/data', 'disk2_total' : get_disk_info('/data')['disk_total']/(1024 ** 3), 'disk2_usage' : get_disk_info('/data')['disk_usage'], 'disk2_read_speed' : get_disk_info('/data')['disk_read_speed']/(1024 ** 2), 'disk2_write_speed' : get_disk_info('/data')['disk_write_speed']/(1024 ** 2) }) response = requests.request("POST", url, headers=headers, data=payload) print(response.text) print(payload) time.sleep(interval) if __name__ == "__main__": monitor_system()