系统性能信息模块 psutil:
参考:https://github.com/giampaolo/psutil
安装psutil模块:
1
|
[root@kurol ~]
# python36 -m easy_install -i http://pypi.douban.com/simple/ psutil
|
1、获取系统性能信息:
1.1、获取CPU信息:
1
|
import
psutil
|
获取CPU完整信息:
1
|
>>> psutil.cpu_times() scputimes(user
=
60984.989999999998
, nice
=
27.280000000000001
, system
=
37572.639999999999
, idle
=
6605536.1100000003
, iowait
=
88463.169999999998
, irq
=
0.53000000000000003
, softirq
=
151.34
, steal
=
0.0
, guest
=
0.0
)
|
获取单项数据信息(用户user的CPU时间比):
1
|
>>> psutil.cpu_times().user
60985.209999999999
|
获取CPU的逻辑个数,默认logical=True:
1
|
>>> psutil.cpu_count()
1
|
获取CPU的物理个数:
1
|
>>> psutil.cpu_count(logical
=
False
)
1
|
1.2、获取内存信息:
1
|
In [
1
]:
import
psutil
|
获取内存完整信息:
1
|
In [
2
]: mem
=
psutil.virtual_memory() In [
3
]: mem Out[
3
]: svmem(total
=
1044832256
, available
=
357302272
, percent
=
65.8
, used
=
554692608
, free
=
69898240
, active
=
732987392
, inactive
=
152940544
, buffers
=
121593856
, cached
=
298647552
, shared
=
278528
)
|
获取内存总数:
1
|
In [
4
]: mem.total Out[
4
]:
1044832256
|
获取空闲内存数:
1
|
In [
5
]: mem.free Out[
5
]:
69898240
|
获取SWAP分区信息:
1
|
In [
6
]: psutil.swap_memory() Out[
6
]: sswap(total
=
0
, used
=
0
, free
=
0
, percent
=
0
, sin
=
0
, sout
=
0
)
|
1.3、获取磁盘信息:
获取磁盘完整信息:
1
|
In [
7
]: psutil.disk_partitions() Out[
7
]: [sdiskpart(device
=
'/dev/vda1'
, mountpoint
=
'/'
, fstype
=
'ext3'
, opts
=
'rw,noatime,acl,user_xattr'
)]
|
获取分区(参数)的使用方法
1
|
In [
8
]: psutil.disk_usage(
'/'
) Out[
8
]: sdiskusage(total
=
21136797696
, used
=
3255058432
, free
=
16808058880
, percent
=
16.2
)
|
获取硬盘总的IO个数、读写信息
1
|
In [
9
]: psutil.disk_io_counters() Out[
9
]: sdiskio(read_count
=
3147178
, write_count
=
9242170
, read_bytes
=
88165640192
, write_bytes
=
293065478144
, read_time
=
38673564
, write_time
=
428113153
, read_merged_count
=
41555
, write_merged_count
=
62306169
, busy_time
=
66422434
)
|
获取单个分区IO的个数、读写信息
1
|
In [
10
]: psutil.disk_io_counters(perdisk
=
True
) Out[
10
]: {
'vda1'
: sdiskio(read_count
=
3147178
, write_count
=
9242205
, read_bytes
=
88165640192
, write_bytes
=
293065760768
, read_time
=
38673564
, write_time
=
428113482
, read_merged_count
=
41555
, write_merged_count
=
62306203
, busy_time
=
66422585
)}
|
1.4、获取网络信息:
获取网络总的IO信息,默认pernic=False:
1
|
In [
11
]: psutil.net_io_counters() Out[
11
]: snetio(bytes_sent
=
7783736637
, bytes_recv
=
3332052537
, packets_sent
=
17192882
, packets_recv
=
26834127
, errin
=
0
, errout
=
0
, dropin
=
0
, dropout
=
0
)
|
获取pernic=True输出每个网络接口的IO信息:
1
|
In [
12
]: psutil.net_io_counters(pernic
=
False
) Out[
12
]: snetio(bytes_sent
=
7783790463
, bytes_recv
=
3332100618
, packets_sent
=
17193280
, packets_recv
=
26834627
, errin
=
0
, errout
=
0
, dropin
=
0
, dropout
=
0
)
|
1.5、获取其他系统信息:
获取当前登录系统的用户:
1
|
In [
13
]: psutil.users() Out[
13
]: [suser(name
=
'root'
, terminal
=
'tty1'
, host
=
'
', started=1492050176.0), suser(name='
root
', terminal='
pts
/
3
', host='
127.0
.
0.1
', started=1496363136.0), suser(name='
root
', terminal='
pts
/
5
', host='
127.0
.
0.1
', started=1496386560.0), suser(name='
root
', terminal='
pts
/
6
', host='
183.19
.
153.161
', started=1496634240.0), suser(name='
root
', terminal='
pts
/
7
', host='
183.19
.
153.161
', started
=
1496645632.0
)]
|
获取开机时间,以Linux时间戳格式返回:
1
|
In [
14
]:
import
psutil,datetime In [
15
]: psutil.boot_time() Out[
15
]:
1489823731.0
|
获取开机时间,转换成自然时间格式返回:
1
|
In [
19
]: datetime.datetime.fromtimestamp(psutil.boot_time()).strftime(
"%Y-%m-%d %H:%M:%S"
) Out[
19
]:
'2017-03-18 15:55:31'
|
2、系统进程管理方法:
psutil模块在获取进程信息方面也停工了很好的支持。
2.1、获取进程信息:
列出所有进程PID
1
|
In [
10
]: psutil.pids() Out[
10
]: [
1
,
2
, ......
32381
,
32407
]
|
实例化一个Process对象,参数为一进程PID
1
|
In [
11
]: p
=
psutil.Process(
8843
)
|
获取进程名:
1
|
In [
12
]: p.name() Out[
12
]:
'httpd'
|
获取bin路径:
1
|
In [
13
]: p.exe() Out[
13
]:
'/usr/sbin/httpd'
|
获取进程工作路径绝对路径:
1
|
In [
14
]: p.cwd() Out[
14
]:
'/'
|
获取进程状态:
1
|
In [
15
]: p.status() Out[
15
]:
'sleeping'
|
获取进程创建时间,时间戳格式:
1
|
In [
16
]: p.create_time() Out[
16
]:
1492649546.17
|
获取uid信息:
1
|
In [
17
]: p.uids() Out[
17
]: puids(real
=
0
, effective
=
0
, saved
=
0
)
|
获取gid信息:
1
|
In [
18
]: p.gids() Out[
18
]: pgids(real
=
0
, effective
=
0
, saved
=
0
)
|
获取获取进程CPU亲和度,如要设置进程CPU亲和度,将CPU号作为参数即可
1
|
In [
19
]: p.cpu_affinity() Out[
19
]: [
0
]
|
获取CPU时间信息,包括user、system两个CPU时间
1
|
In [
20
]: p.cpu_times() Out[
20
]: pcputimes(user
=
34.49
, system
=
83.82
, children_user
=
14932.68
, children_system
=
1664.72
)
|
获取内存利用率
1
|
In [
21
]: p.memory_percent() Out[
21
]:
1.3066181601499103
|
获取进程内存rss、vms信息
1
|
In [
22
]: p.memory_info() Out[
22
]: pmem(rss
=
13651968
, vms
=
309997568
, shared
=
7004160
, text
=
344064
, lib
=
0
, data
=
5300224
, dirty
=
0
)
|
获取IO信息,包括读写IO数及字节数
1
|
In [
23
]: p.io_counters() Out[
23
]: pio(read_count
=
829019264
, write_count
=
1492003
, read_bytes
=
2957004800
, write_bytes
=
4966547456
, read_chars
=
6639699389927
, write_chars
=
6973819808
)
|
获取打开进程socket的namedutples列表,包括fs、family、laddr等信息
1
|
In [
24
]: p.connections() Out[
24
]: [pconn(fd
=
3
, family
=
<AddressFamily.AF_INET:
2
>,
type
=
<SocketKind.SOCK_STREAM:
1
>, laddr
=
(
'0.0.0.0'
,
80
), raddr
=
(), status
=
'LISTEN'
)]
|
获取进程开启的线程数
1
|
In [
26
]: p.num_threads() Out[
26
]:
1
|
2.2、popen类的使用
psutil提供的popen类的作用:获取用户启动的应用程序进程信息,以便跟踪程序进程的运行状态。
1
|
In [
1
]:
import
psutil In [
2
]:
from
subprocess
import
PIPE
|
通过psutil的Popen方法启动的应用程序,可以跟踪该程序运行的所有相关信息。
1
|
In [
3
]: p
=
psutil.Popen([
"/usr/bin/python3"
,
"-c"
,
"print('hello')"
],stdout
=
PIPE) In [
4
]: p.name() Out[
4
]:
'python3'
In [
5
]: p.username() Out[
5
]:
'root'
In [
6
]: p.communicate() Out[
6
]: (b
'hello\n'
,
None
) In [
7
]: p.cpu_time()
#得到进程运行的CPU时间
|