psuti是一个跨平台,能够轻松实现获取系统运行进程,系统利用率信息,主要用于系统监控,分析和限制系统资源及进程管理。
psutil为第三方模块,通过pip安装模块。
获取cpu信息
1.cpu信息
2.User Time:执行用户进程的时间百分比
3.System Time:执行内核进程和中断的时间百分比
4.Wait IO 由于IO等待而使CPU处于idle空闲状态的时间百分比
5.Idle,CPU处于idle状态的时间百分比
######获取cpu的物理个数
>>> psutil.cpu_count(logical=False)
######获取cpu的逻辑个数,默认logical=True
>>> psutil.cpu_count()
#######获取cpu负载的百分比
>>> psutil.cpu_percent()
#######获取cpu的相关信息
>>> psutil.cpu_times_percent()
scputimes(user=50.100000000000001, nice=0.0, system=0.10000000000000001, idle=49.700000000000003, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0)
获取内存信息
获取物理内存
>>> mem = psutil.virtual_memory() #######内存的相关信息
>>> mem
svmem(total=2104487936, available=1061302272, percent=49.600000000000001, used=2019471360, free=85016576, active=1381191680, inactive=335818752, buffers=289062912, cached=687222784)
>>> mem.total #########内存的总信息
2104487936
>>> mem.free ###########内存的空闲量
85016576
获取swap内存
>>> swap = psutil.swap_memory()
>>> swap
sswap(total=536862720, used=5496832, free=531365888, percent=1.0, sin=1499136, sout=7172096)
>>> swap.total #######虚拟内存总量
536862720
>>> swap.free ########剩余内存总量
531365888
获取磁盘信息
>>> disk = psutil.disk_partitions()
>>> disk ######所有的磁盘分区信息
[sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw'), sdiskpart(device='/dev/sdb1', mountpoint='/data', fstype='ext4', opts='rw')]
获取其中一块磁盘的详细信息
>>> psutil.disk_usage('/data') ##########磁盘详细信息
sdiskusage(total=52843966464, used=336404480, free=49823244288, percent=0.59999999999999998)
>>> psutil.disk_usage('/data').total ########磁盘总量
52843966464
>>> psutil.disk_usage('/data').free ##########磁盘空闲量
49823244288
获取磁盘总的IO信息
>>> psutil.disk_io_counters()
sdiskio(read_count=45666, write_count=3915128, read_bytes=595380224, write_bytes=28514291712, read_time=62136, write_time=15799360, read_merged_count=23385, write_merged_count=3045265, busy_time=3432934)
获取磁盘分的IO信息
>>> psutil.disk_io_counters(perdisk=True)
{'sdb1': sdiskio(read_count=338, write_count=87, read_bytes=2782208, write_bytes=909312, read_time=224, write_time=329, read_merged_count=341, write_merged_count=135, busy_time=435), 'sda2': sdiskio(read_count=409, write_count=78, read_bytes=3059712, write_bytes=7172096, read_time=340, write_time=2237, read_merged_count=338, write_merged_count=1672, busy_time=2155), 'sda1': sdiskio(read_count=44919, write_count=3914999, read_bytes=589538304, write_bytes=28506443776, read_time=61572, write_time=15796850, read_merged_count=22706, write_merged_count=3043479, busy_time=3430370)}
获取网卡信息,主要包括获取字节数,接受字节数,发送包数,接受包数
>>> psutil.net_io_counters() ########获取网络总IO信息,默认pernic=False
snetio(bytes_sent=11914780901, bytes_recv=17872222026, packets_sent=60947652, packets_recv=239415142, errin=0, errout=0, dropin=2260072, dropout=0)
>>> psutil.net_io_counters(pernic=True) #########获取网络每个网络接口的IO信息
{'lo': snetio(bytes_sent=356988, bytes_recv=356988, packets_sent=2811, packets_recv=2811, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=11914565009, bytes_recv=17871969714, packets_sent=60945546, packets_recv=239413594, errin=0, errout=0, dropin=2260072, dropout=0)}
获取系统用户信息
>>> psutil.users()
[suser(name='root', terminal='pts/1', host='192.168.8.151', started=1458716800.0), suser(name='root', terminal='pts/2', host='192.168.8.151', started=1458718720.0)]
获取系统的开机时间
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
'2015-06-02 16:31:35'
获取系统进程信息
>>> psutil.pids() ##########进程号
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 33, 34, 35, 36, 37, 38, 39, 45, 46, 217, 218, 338, 339, 340, 422, 596, 828, 833, 862, 863, 864, 910, 1070, 1082, 1090, 1102, 1118, 1134, 1137, 1146, 1148, 1149, 1151, 1153, 1155, 1156, 1158, 5133, 5134, 5135, 5136, 5137, 5138, 5139, 5140, 5142, 5144, 5148, 5149, 5150, 5270, 5341, 5342, 6984, 7687, 10349, 10352, 13533, 14007, 14099, 15458, 18432, 23404, 30964, 30966, 31044, 31045, 31048, 31200, 31541]
>>> p = psutil.Process(31541) ##########生成进程实例
>>> p.name() #########进程名
'httpd'
>>> p.pid #########获取pid
31541
>>> p.ppid() #########获取父进程pid
1
>>> p.parent() ########获取父进程,不存在则返回None
<psutil.Process(pid=1, name='init') at 139889475650512>
>>> p.exe() #######进程bin路径
'/usr/local/apache/bin/httpd'
>>> p.cwd() ###########进程工作的绝对路径
'/'
>>> p.username() ########那个用户运行的进程
'root'
>>> p.status() ##########进程状态
'sleeping'
>>> p.create_time() ##########进程创建时间
1457429059.79
>>> datetime.datetime.fromtimestamp(p.create_time()).strftime("%Y-%m-%d %H:%M:%S")
'2016-03-08 17:24:19'
>>> p.uids() ##########进程uid信息
puids(real=0, effective=0, saved=0)
>>> p.gids() ##########进程gid信息
pgids(real=0, effective=0, saved=0)
>>> p.cpu_times() ###########进程cpu时间信息
pcputimes(user=16.559999999999999, system=4.4100000000000001, children_user=196.94, children_system=39.619999999999997)
>>> p.memory_percent() #################进程内存利用率百分比
0.85092965816839916
>>> p.memory_info() ################进程内存使用相信信息
pmem(rss=17907712, vms=280260608, shared=7897088, text=430080, lib=0, data=9900032, dirty=0)
>>> p.io_counters() ########进程io信息
pio(read_count=515693, write_count=48326, read_bytes=618496, write_bytes=6672384)
>>> p.num_threads() #########进程开始线程数
1
>>> p.connections() #############返回打开进程socket的namedutples列表,包括fs,family,laddr等信息
[pconn(fd=4, family=10, type=1, laddr=('::', 80), raddr=(), status='LISTEN')]