51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Python获取Linux系统性能信息

1)CPU信息

{#1373-1542183461083}[root@localhost ~]# python

{#3686-1542183470122}Python 2.7.5 (default, Jul 13 2018, 13:06:57)

{#3624-1542183470122}[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2

{#4390-1542183470122}Type "help", "copyright", "credits" or "license" for more information.

{#1060-1542183470122}>>> import psutil

{#9279-1542183796352}#使用cpu_times方法获取CPU完整信息

{#7028-1542183470122}>>> psutil.cpu_times()

{#9596-1542183470122}scputimes(user=14571.83, nice=10.13, system=10459.3, idle=9390763.99, iowait=716.03, irq=0.0, softirq=3.8, steal=0.0, guest=0.0, guest_nice=0.0)

{#1498-1542183470122}#获取单项数据信息,如用户user的时间比

{#4681-1542183539382}>>> psutil.cpu_times().user

{#7089-1542183470122}14571.87

{#6070-1542183567467}#获取CPU的逻辑个数,默认logical=True4

{#8945-1542183470122}>>> psutil.cpu_count()

{#9256-1542183470122}1

{#9054-1542183611946}#获取CPU的物理个数

{#5963-1542183470122}>>> psutil.cpu_count(logical=False)

{#3392-1542183470122}1

{#7691-1542183459423}2)内存信息

{#9390-1542183636890}>>> import psutil

{#3978-1542183786730}#使用psutil.virtual_memory方法获取内存完整信息

{#3966-1542183720054}>>> mem = psutil.virtual_memory()

{#5780-1542183720054}>>> mem

{#8281-1542183720054}svmem(total=1928695808L, available=1699635200L, percent=11.9, used=1617731584L, free=310964224L, active=733216768, inactive=700944384, buffers=194965504L, cached=1193705472)

{#3015-1542183809019}#获取内存总数

{#6633-1542183720054}>>> mem.total

{#4968-1542183720054}1928695808L

{#9096-1542183812812}#获取空闲内存数

{#1723-1542183720054}>>> mem.free

{#6412-1542183720054}310964224L

{#8074-1542183814298}#获取swap分区信息

{#7130-1542183720054}>>> psutil.swap_memory()

{#7739-1542183720054}sswap(total=0L, used=0L, free=0L, percent=0.0, sin=0, sout=0)

{#8333-1542183745622}3)磁盘信息

{#5182-1542183965402}#使用psutil.disk_partitions方法获取磁盘完整信息

{#6682-1542183861946}>>> psutil.disk_partitions()

{#8873-1542183950769}[sdiskpart(device='/dev/vda1', mountpoint='/', fstype='ext4', opts='rw,relatime,data=ordered')]

{#5857-1542183999786}#使用psutil.disk_usage方法获取分区(参数)的使用情况

{#5785-1542183950769}>>> psutil.disk_usage('/')

{#6181-1542183950769}sdiskusage(total=42139451392, used=3013500928, free=36961796096, percent=7.2)

{#9573-1542184001529}#使用psutil.disk_io_counters方法获取磁盘总的IO个数、读写信息

{#9290-1542183950769}>>> psutil.disk_io_counters()

{#3194-1542183950769}sdiskio(read_count=26141, write_count=1716089, read_bytes=474932224, write_bytes=13267562496, read_time=109538, write_time=15169214)

{#8160-1542184005006}#"perdisk=True"参数获取单个分区IO个数、读写信息

{#3017-1542183950769}>>> psutil.disk_io_counters(perdisk=True)

{#7651-1542183950769}{'vda1': sdiskio(read_count=26141, write_count=1716089, read_bytes=474932224, write_bytes=13267562496, read_time=109538, write_time=15169214)}

{#3384-1542183961530}4)网络信息

{#4691-1542184215852}#使用psutil.net_io_counters获取网络总的IO信息,默认pernic=False

{#1680-1542184146460}>>> psutil.net_io_counters()

{#5991-1542184205454}snetio(bytes_sent=287203066, bytes_recv=887595622, packets_sent=2422790, packets_recv=2704748, errin=0, errout=0, dropin=0, dropout=0)

{#8128-1542184251881}#pernic=True输出每个网络接口的IO信息

{#8711-1542184205454}>>> psutil.net_io_counters(pernic=True)

{#3234-1542184205454}{'lo': snetio(bytes_sent=502615, bytes_recv=502615, packets_sent=2200, packets_recv=2200, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=286703163, bytes_recv=887096509, packets_sent=2420612, packets_recv=2702590, errin=0, errout=0, dropin=0, dropout=0)}

{#6256-1542184205454}>>>

{#5653-1542184212724}5)其他系统信息

{#1411-1542184413538}#使用psutil.users方法返回当前登录系统的用户信息

{#3856-1542184290410}>>> psutil.users()

{#3125-1542184411840}[suser(name='root', terminal='tty1', host='', started=1536040960.0), suser(name='root', terminal='pts/1', host='14.155.220.101', started=1542177536.0), suser(name='root', terminal='pts/2', host='14.155.220.101', started=1542181760.0)]

{#2560-1542184416443}#转换成自然时间格式

{#4989-1542184411840}>>> import psutil, datetime

{#3995-1542184411840}>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")

{#3313-1542184411840}'2018-07-28 12:50:26'

{#9999-1542184523534}#使用psutil.boot_time方法获取开机时间,以Linux时间戳格式返回

{#3414-1542184411840}>>> psutil.boot_time()

{#4736-1542184520557}1532753426.0


继续阅读

历史上的今天

11 月
14

赞(0)
未经允许不得转载:工具盒子 » Python获取Linux系统性能信息