您的位置:首页 > 编程语言 > Python开发

【python运维】获取系统性能信息

2017-12-05 15:53 447 查看
1、获取cpu信息
user 执行用户进程的时间百分比
system 执行内核进程和中断的时间百分比
iowait 由于IO等待而使CPU处于idle状态时间百分比
idle cpu处于idle状态的时间百分比

获取cpu利用率信息
>>> from psutil import *
>>> cpu_times()
scputimes(user=57.369999999999997, nice=68.689999999999998, system=139.47999999999999, idle=19649.23, iowait=237.56999999999999, irq=6.9299999999999997, softirq=38.399999999999999, steal=0.0)
也可单个获取,如
>>> cpu_times().user
58.649999999999999

获取cpu的逻辑个数
>>> cpu
4000
_count()
2
获取cpu的物理个数
>>> cpu_count(logical=False)

2、获取内存信息
total 内存总数
used 已使用的内存数
free 空闲内存数
buffers 缓冲使用数
cache 缓存使用数
1、Buffer(缓冲区)是系统两端处理速度平衡(从长时间尺度上看)时使用的。它的引入是为了减小短期内突发I/O的影响,起到流量整形的作用。比如生产者——消费者问题,他们产生和消耗资源的速度大体接近,加一个buffer可以抵消掉资源刚产生/消耗时的突然变化。
2、Cache(缓存)则是系统两端处理速度不匹配时的一种折衷策略。因为CPU和memory之间的速度差异越来越大,所以人们充分利用数据的局部性(locality)特征,通过使用存储系统分级(memory
hierarchy)的策略来减小这种差异带来的影响。
获取内存信息
>>> virtual_memory()
svmem(total=4247584768L, available=4000800768L, percent=5.7999999999999998, used=1446277120L, free=2801307648L, active=467664896, inactive=894840832, buffers=126816256L, cached=1072676864)

获取swap信息
>>> swap_memory()
sswap(total=5268037632L, used=0L, free=5268037632L, percent=0.0, sin=0, sout=0)
当swap开始使用(也就是used不再等于0L),就说明系统的内存资源紧俏。根据这一特性可以设置一个阈值,从而优化系统管理。

3、获取磁盘信息
write_count 写IO数
read_bytes IO读字节数
write_bytes IO写字节数
read_time 磁盘读时间

获取磁盘分区
>>> disk_partitions()
[sdiskpart(device='/dev/mapper/VolGroup00-LogVol00', mountpoint='/', fstype='ext3', opts='rw'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='ext3', opts='rw')]
获取磁盘挂载点的使用情况
>>> disk_usage('/')
sdiskusage(total=15569174528L, used=4420149248L, free=10345398272L, percent=28.399999999999999)
>>> disk_usage('/boot')
sdiskusage(total=103512064L, used=26875904L, free=71291904L, percent=26.0)
获取硬盘总的IO个数
>>> disk_io_counters()
sdiskio(read_count=129345, write_count=246908, read_bytes=2128687616, write_bytes=1804669952, read_time=1002474, write_time=57171470)
获取单个分区IO个数
>>> disk_io_counters(perdisk=True);
{'dm-1': sdiskio(read_count=72, write_count=0, read_bytes=294912, write_bytes=0, read_time=925, write_time=0), 'sda2': sdiskio(read_count=55779, write_count=26632, read_bytes=1063876096, write_bytes=902647808,
read_time=329091, write_time=3917564), 'dm-0': sdiskio(read_count=73369, write_count=220373, read_bytes=1063437312, write_bytes=902647808, read_time=671953, write_time=53253991), 'sda1': sdiskio(read_count=125, write_count=9, read_bytes=1079296, write_bytes=13312,
read_time=505, write_time=66)}

4、获取网络信息
bytes_recv 接收字节数
bytes_sent 发送字节数
packets_sent 数据包数
packets_recv 接收数据包数

获取网络总的IO信息(默认pernic=False)
>>> net_io_counters()
snetio(bytes_sent=3887165, bytes_recv=19521516, packets_sent=8312, packets_recv=12915, errin=0, errout=0, dropin=0, dropout=0)
获取每个网络接口的IO信息
>>> net_io_counters(pernic=True)
{'sit0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'lo': snetio(bytes_sent=3447480, bytes_recv=3447480, packets_sent=1575,
packets_recv=1575, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=439685, bytes_recv=16074036, packets_sent=6737, packets_recv=11340, errin=0, errout=0, dropin=0, dropout=0)}

5、获取其它系统信息
获取当前用户的信息
>>> users()
[suser(name='root', terminal=':0', host='', started=1512392971.0), suser(name='root', terminal='pts/1', host='localhost', started=1512392981.0), suser(name='root', terminal='pts/2', host='localhost',
started=1512393057.0), suser(name='root', terminal='pts/3', host='localhost', started=1512394322.0), suser(name='root', terminal='pts/4', host='localhost', started=1512394505.0)]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux python 运维