import datetime
import psutil
############1.memory#############################
mem=psutil.virtual_memory()
print("总内存为:",mem.total,"\n","使用内存为:",mem.used,"\n","内存free:",mem.free)
print("所有内存信息:",mem)
print("交换分区信息",psutil.swap_memory())
#############2.cpu###########################
cpu_login_number=psutil.cpu_count() ####CPU逻辑个数
print(cpu_login_number)
cpu_info=psutil.cpu_stats()
print(cpu_info)
################3.disk===================
disk_partition=psutil.disk_partitions() #######all disk partitions info
print("报有硬盘分区情况",disk_partition)
print("C盘使用情况",psutil.disk_usage("C:\\"))
print("硬盘总的IO个数",psutil.disk_io_counters())
print("获取单个分区IO个数、读写情况",psutil.disk_io_counters(perdisk=True))
#############4.网络信息#################
print("网络总的IO INFO",psutil.net_io_counters()) ######默认pernic=False
print("每个网络接口的IO 信息",psutil.net_io_counters(pernic=True))
###########5.users time pid################
print("Login User",psutil.users()) #当前login user
print("boot time:",psutil.boot_time())
print("boot time swich",datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))
print("所有进程PID",psutil.pids()) ####
p=psutil.Process(1716) ####实例化一个进程对像,参数为一进程pid
print(p.name())
print(p.status())
#print(p.exe())
#print(p.cwd())
#print(p.uids,p.gids())
print(p.memory_info())
print(p.num_threads())
#######################6. popen获取用户启动的应用程序进程信息,便于追踪程序进程的运行状态
from subprocess import PIPE
#p2=psutil.Popen(["/usr/bin/python","-c","print('hello')"],stdout=PIPE)
#print(p2.name())
#print(p2.username())