本来这周打算基于glaphite来对系统服务进行监控,但是中间一直有个地方没有搞明白,所以至于glaphite的web页面监控可能要推迟到下周与朋友相见!
今天就和大家聊聊一个非常不错的Python下系统信息模块pustil
简介
psutil是一个跨平台库,主要应用于系统监控,分析和限制系统资源及进程的管理;
可以轻松的获取如下系统信息:https://pypi.python.org/pypi/psutil
-
CPU
-
内存
-
磁盘
-
网络
-
进程
安装
1
2
3
4
|
# wget https://pypi.python.org/packages/source/p/psutil/psutil-2.2.0.tar.gz --no-check-certificate
# tar xf psutil-2.2.0.tar.gz
# cd psutil-2.2.0
# python setup.py install
|
获取系统性能信息
(1)CPU信息
-
User Time:执行用户进程的时间百分比
-
System Time:执行内核进程和中断的时间百分比
-
Wait IO:由于IO等待而使CPU处于idle(空闲)状态的时间百分比
-
idle:CPU处于idle状态的时间百分比
先来看看psutil模块中对cpu有哪些方法可以使用
1
2
3
|
>>>
import
psutil
>>> psutil.cpu_ (键盘tab键 命令补全)
psutil.cpu_count( psutil.cpu_percent( psutil.cpu_times( psutil.cpu_times_percent(
|
分述psutil模块中cpu方法的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#获取CPU的物理个数
>>> psutil.cpu_count(logical
=
False
)
#获取CPU的逻辑个数,默认logical=True
>>> psutil.cpu_count()
#获取cpu负载的百分比
>>> psutil.cpu_percent()
0.10000000000000001
#获取cpu的完整信息,需要显示所有逻辑cpu信息
>>> psutil.cpu_times()
scputimes(user
=
3.2999999999999998
, nice
=
0.0
, system
=
16.329999999999998
, idle
=
8322.0400000000009
, iowait
=
21.93
, irq
=
0.46999999999999997
, softirq
=
0.54000000000000004
, steal
=
0.0
, guest
=
0.0
)
#获取单项数据信息,如用户user的CPU时间比
>>> psutil.cpu_times().user
3.2999999999999998
#时间间隔,是否以列表形式输出
>>> psutil.cpu_times_percent(interval
=
2
,percpu
=
True
)
[scputimes(user
=
0.0
, nice
=
0.0
, system
=
0.0
, idle
=
100.0
, iowait
=
0.0
, irq
=
0.0
, softirq
=
0.0
, steal
=
0.0
, guest
=
0.0
)]
|
(2)内存信息
-
内存总数total
-
已使用的内存数used
-
空闲内存数free
-
缓冲区使用数buffers
-
缓存使用数cache
-
交换分区使用数swap
先来看看psutil模块中对memory有哪些方法可以使用
1
2
3
4
|
>>> mem
=
psutil.virtual_memory()
>>> mem.(键盘tab键 命令补全)
mem.active mem.buffers mem.count mem.inactive mem.percent mem.used
mem.available mem.cached mem.free mem.index mem.total
|
分述psutil模块中memory方法的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#获取获取内存完整信息
>>> mem
svmem(total
=
1960640512L
, available
=
1861410816L
, percent
=
5.0999999999999996
, used
=
158261248L
, free
=
1802379264L
, active
=
55451648
, inactive
=
35868672
, buffers
=
13492224L
, cached
=
45539328
)
#获取内存总数
>>> mem.total
1960640512L
#获取使用内存
>>> mem.used
158261248L
#获取空闲内存
>>> mem.free
1802379264L
#获取swap分区信息
>>> psutil.swap_memory()
sswap(total
=
4160741376L
, used
=
0L
, free
=
4160741376L
, percent
=
0.0
, sin
=
0
, sout
=
0
)
|
(3)硬盘信息
-
利用率
-
IO
先来看看psutil模块中对disk有哪些方法可以使用
1
2
|
>>> psutil.dis
psutil.disk_io_counters psutil.disk_partitions psutil.disk_usage
|
分述psutil模块中disk方法的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#获取磁盘的完整信息
>>> psutil.disk_partitions()
[sdiskpart(device
=
'/dev/mapper/VolGroup-lv_root'
, mountpoint
=
'/'
, fstype
=
'ext4'
, opts
=
'rw'
), sdiskpart(device
=
'/dev/sda1'
, mountpoint
=
'/boot'
, fstype
=
'ext4'
, opts
=
'rw'
)]
#获取分区的使用情况
>>> psutil.disk_usage(
'/'
)
sdiskusage(total
=
16521789440
, used
=
1433333760
, free
=
14249177088
, percent
=
8.6999999999999993
)
>>> psutil.disk_usage(
'/boot'
)
sdiskusage(total
=
507744256
, used
=
33196032
, free
=
448333824
, percent
=
6.5
)
#获取硬盘总的IO数
>>> psutil.disk_io_counters()
sdiskio(read_count
=
7295
, write_count
=
7190
, read_bytes
=
116201472
, write_bytes
=
38065152
, read_time
=
123500
, write_time
=
33952
)
#获取单个分区IO个数 读写信息
>>> psutil.disk_io_counters(perdisk
=
True
)
{
'dm-1'
: sdiskio(read_count
=
297
, write_count
=
0
, read_bytes
=
1216512
, write_bytes
=
0
, read_time
=
534
, write_time
=
0
),
'sda2'
: sdiskio(read_count
=
2462
, write_count
=
2543
, read_bytes
=
56902656
, write_bytes
=
19034112
, read_time
=
29130
, write_time
=
11733
),
'dm-0'
: sdiskio(read_count
=
3859
, write_count
=
4647
, read_bytes
=
55251968
, write_bytes
=
19034112
, read_time
=
93096
, write_time
=
22216
),
'sda1'
: sdiskio(read_count
=
677
, write_count
=
6
, read_bytes
=
2830336
, write_bytes
=
21504
, read_time
=
740
, write_time
=
5
)}
|
(4)网络信息
-
发送字节数
-
接收字节数
-
发送数据包数
-
接收数据包数
先来看看psutil模块中对network有哪些方法可以使用
1
2
|
>>> psutil.net
psutil.net_connections psutil.net_io_counters psutil.network_io_counters
|
分述psutil模块中nework方法的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#获取网络总的IO信息,默认pernic=False
>>> psutil.net_io_counters()
snetio(bytes_sent
=
910776
, bytes_recv
=
13499265
, packets_sent
=
8757
, packets_recv
=
15556
, errin
=
0
, errout
=
0
, dropin
=
0
, dropout
=
0
)
#获取每个网络接口的IO信息
>>> psutil.net_io_counters(pernic
=
True
)
{
'lo'
: snetio(bytes_sent
=
0
, bytes_recv
=
0
, packets_sent
=
0
, packets_recv
=
0
, errin
=
0
, errout
=
0
, dropin
=
0
, dropout
=
0
),
'eth2'
: snetio(bytes_sent
=
911296
, bytes_recv
=
13405208
, packets_sent
=
8761
, packets_recv
=
14795
, errin
=
0
, errout
=
0
, dropin
=
0
, dropout
=
0
),
'eth1'
: snetio(bytes_sent
=
1788
, bytes_recv
=
98301
, packets_sent
=
18
, packets_recv
=
803
, errin
=
0
, errout
=
0
, dropin
=
0
, dropout
=
0
)}
#获取所有接口的网卡信息
>>> psutil.net_connections()
[sconn(fd
=
3
, family
=
2
,
type
=
1
, laddr
=
(
'0.0.0.0'
,
22
), raddr
=
(), status
=
'LISTEN'
, pid
=
1110
), sconn(fd
=
12
, family
=
2
,
type
=
1
, laddr
=
(
'127.0.0.1'
,
25
), raddr
=
(), status
=
'LISTEN'
, pid
=
1186
), sconn(fd
=
3
, family
=
2
,
type
=
1
, laddr
=
(
'192.168.10.26'
,
22
), raddr
=
(
'192.168.10.1'
,
54342
), status
=
'ESTABLISHED'
, pid
=
1794
), sconn(fd
=
4
, family
=
10
,
type
=
1
, laddr
=
(
'::'
,
22
), raddr
=
(), status
=
'LISTEN'
, pid
=
1110
), sconn(fd
=
13
, family
=
10
,
type
=
1
, laddr
=
(
'::1'
,
25
), raddr
=
(), status
=
'LISTEN'
, pid
=
1186
), sconn(fd
=
6
, family
=
2
,
type
=
2
, laddr
=
(
'0.0.0.0'
,
68
), raddr
=
(), status
=
'NONE'
, pid
=
1745
), sconn(fd
=
6
, family
=
2
,
type
=
2
, laddr
=
(
'0.0.0.0'
,
68
), raddr
=
(), status
=
'NONE'
, pid
=
1643
)]
#获取指定接口的网卡信息
>>> psutil.net_connections(kind
=
'inet4'
)
[sconn(fd
=
3
, family
=
2
,
type
=
1
, laddr
=
(
'0.0.0.0'
,
22
), raddr
=
(), status
=
'LISTEN'
, pid
=
1110
), sconn(fd
=
12
, family
=
2
,
type
=
1
, laddr
=
(
'127.0.0.1'
,
25
), raddr
=
(), status
=
'LISTEN'
, pid
=
1186
), sconn(fd
=
3
, family
=
2
,
type
=
1
, laddr
=
(
'192.168.10.26'
,
22
), raddr
=
(
'192.168.10.1'
,
54342
), status
=
'ESTABLISHED'
, pid
=
1794
), sconn(fd
=
6
, family
=
2
,
type
=
2
, laddr
=
(
'0.0.0.0'
,
68
), raddr
=
(), status
=
'NONE'
, pid
=
1745
), sconn(fd
=
6
, family
=
2
,
type
=
2
, laddr
=
(
'0.0.0.0'
,
68
), raddr
=
(), status
=
'NONE'
, pid
=
1643
)]
|
(5)其它系统信息
-
用户登录
-
开机时间
具体操作列子如下:
1
2
3
4
5
6
7
8
9
|
#获取当前登录系统的用户信息
>>> psutil.users()
[suser(name
=
'root'
, terminal
=
'tty1'
, host
=
'
', started=1427459328.0), suser(name='
root
', terminal='
pts
/
0
', host='
192.168
.
10.1
', started=1427459456.0), suser(name='
root
', terminal='
pts
/
1
', host='
192.168
.
10.1
', started=1427459456.0), suser(name='
root
', terminal='
pts
/
2
', host='
192.168
.
10.1
', started
=
1427459584.0
)]
>>>
len
(psutil.users())
4
#获取系统开机时间并格式化输出
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime(
'%Y-%m-%d %H:%M:%S'
)
'2015-03-27 20:27:36'
|
系统进程管理方法
(1)进程信心
-
进程PID
-
单个进程名称
-
路径
-
状态
-
系统资源利用率
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
31
32
|
>>>
import
psutil
>>> psutil.pids() 列出所有进程的PID
[
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
,
31
,
32
,
37
,
38
,
40
,
41
,
71
,
199
,
203
,
218
,
219
,
220
,
325
,
327
,
344
,
345
,
364
,
417
,
569
,
764
,
765
,
817
,
1042
,
1058
,
1070
,
1110
,
1186
,
1194
,
1197
,
1209
,
1211
,
1213
,
1215
,
1217
,
1219
,
1228
,
1229
,
1231
,
1298
,
1643
,
1745
,
1794
,
1796
,
1829
,
1846
,
2004
,
2039
,
2154
,
2175
]
>>> p
=
psutil.Process(
2154
) 实例化一个Process对象,参数为一个进程的PID
>>> p.name() 进程名
'python'
>>> p.exe() 进程
bin
路径
'/usr/bin/python'
>>> p.cwd() 进程工作目录绝对路径
'/root'
>>> p.status() 进程状态
'running'
>>> p.create_time() 进程创建时间、时间戳格式
1427469528.49
>>> 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
=
0.059999999999999998
, system
=
0.050000000000000003
)
>>> p.cpu_affinity() get进程CPU亲和度
[
0
]
>>> p.memory_percent() 进程内存利用率
0.43265534645853532
>>> p.memory_info() 进程内存rss、vms信息
pmem(rss
=
8486912
, vms
=
185946112
)
>>> p.io_counters() 进程IO信息,包括读写IO数及字节数
pio(read_count
=
2206
, write_count
=
1286
, read_bytes
=
40960
, write_bytes
=
0
)
>>> p.connections() 返回打开进程socket的namedutples列表
[]
>>> p.num_threads() 进程开启的线程数
1
|
(2)popen类的使用
psutil提供了popen类的作用是获取用户启动的应用程序进程信息,以便跟踪程序进程的运行状态。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#定义
>>>
import
psutil
>>>
from
subprocess
import
PIPE
>>> p
=
psutil.Popen([
'/usr/bin/python'
,
'-c'
,
'print("hello world.")'
],stdout
=
PIPE)
#获取
>>> p.name()
'python'
>>> p.username()
'root'
>>> p.communicate()
(
'hello world.\n'
,
None
)
>>> p.cpu_times()
pcputimes(user
=
0.01
, system
=
0.0
)
|
补充:2015-03-28
psutil大小单位一般都采用字节,查看起来很不舒服!那么我们就用雅观的方式来显示内存的信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
首先定义单位换算字典
unit
=
{
'b'
:
1
,
'k'
:
2
*
*
10
,
'm'
:
2
*
*
20
,
'g'
:
2
*
*
30
}
统计内存的总数并换算单位
>>>
import
psutil
>>> mem
=
psutil.virtual_memory()
>>> mem.total
/
unit[
'm'
]
#以M的单位显示,并保留整数
1869L
>>> mem.total
/
unit[
'g'
]
#以G的单位显示,并保留整数
1L
>>>
print
'%.2f'
%
(
float
(mem.total)
/
unit[
'm'
])
#保留两位小数
1869.81
>>>
print
'%.2f'
%
(
float
(mem.total)
/
unit[
'g'
])
#保留两位小数
1.83
|
-
如果朋友看过我前篇博文IT资产管理,可能还会记得那篇搜集主机硬件信息都是基于读取文件的方式进行截取或匹配等手段来获取信息,用起来确实有点麻烦,如果朋友看明白了这篇文章,可能用那种方式10行完成的代码用这个模块2行就可以实现了。这就是psutil的强大之处。
-
所谓万世追究其根源,psutil的强大无非就是在底层封装了一些库,我们直接调用就可以了,实质上也是通过读取文件的形式来获取的。
好了,今天的psutil就先到这里,如果大家对psutil感兴趣,更多的功能可以参考psutil官方文档
本文转自zys467754239 51CTO博客,原文链接:http://blog.51cto.com/467754239/1625819,如需转载请自行联系原作者