监控服务器内存及CPU使用情况
监控内存及CPU使用情况 import psutil def get_memory_cpu_info ( ) : info = psutil . virtual_memory ( ) memory_cpu_info = { 'memory_total' : info . total , 'memory_available' : info . available , 'memory_used' : info . used , 'memory_free' : info . free , 'memory_percent' : info . percent , 'cup_used_percent' : psutil . cpu_percent ( ) } return memory_cpu_info if __name__ == '__main__' : res = get_memory_cpu_info ( ) print ( res ) Json文件写出 import json def write_to_file ( content , filename ) : with open ( filename , 'a' , encoding = 'utf-8' ) as f : f . write ( json . dumps ( content , ensure_ascii =