Python获取Linux系统性能信息

小柒博客 Python1 97.2K3字数 2645阅读8分49秒阅读模式

1)CPU信息

[root@localhost ~]# python

Python 2.7.5 (default, Jul 13 2018, 13:06:57)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import psutil

#使用cpu_times方法获取CPU完整信息

>>> psutil.cpu_times()

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)

#获取单项数据信息,如用户user的时间比

>>> psutil.cpu_times().user

14571.87

#获取CPU的逻辑个数,默认logical=True4

>>> psutil.cpu_count()

1

#获取CPU的物理个数

>>> psutil.cpu_count(logical=False)

1

2)内存信息

>>> import psutil

#使用psutil.virtual_memory方法获取内存完整信息

>>> mem = psutil.virtual_memory()

>>> mem

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

#获取内存总数

>>> mem.total

1928695808L

#获取空闲内存数

>>> mem.free

310964224L

#获取swap分区信息

>>> psutil.swap_memory()

sswap(total=0L, used=0L, free=0L, percent=0.0, sin=0, sout=0)

3)磁盘信息

#使用psutil.disk_partitions方法获取磁盘完整信息

>>> psutil.disk_partitions()

[sdiskpart(device='/dev/vda1', mountpoint='/', fstype='ext4', opts='rw,relatime,data=ordered')]

#使用psutil.disk_usage方法获取分区(参数)的使用情况

>>> psutil.disk_usage('/')

sdiskusage(total=42139451392, used=3013500928, free=36961796096, percent=7.2)

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

>>> psutil.disk_io_counters()

sdiskio(read_count=26141, write_count=1716089, read_bytes=474932224, write_bytes=13267562496, read_time=109538, write_time=15169214)

#"perdisk=True"参数获取单个分区IO个数、读写信息

>>> psutil.disk_io_counters(perdisk=True)

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

4)网络信息

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

>>> psutil.net_io_counters()

snetio(bytes_sent=287203066, bytes_recv=887595622, packets_sent=2422790, packets_recv=2704748, errin=0, errout=0, dropin=0, dropout=0)

#pernic=True输出每个网络接口的IO信息

>>> psutil.net_io_counters(pernic=True)

{'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)}

>>>

5)其他系统信息

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

>>> psutil.users()

[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)]

#转换成自然时间格式

>>> import psutil, datetime

>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")

'2018-07-28 12:50:26'

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

>>> psutil.boot_time()

1532753426.0


若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!

继续阅读
Wechat
微信扫一扫,加我!
weinxin
微信公众号
微信扫一扫,关注我!
weinxin
Python最后更新:2024-1-25
小柒博客
  • 本文由 小柒博客 发表于 2018年11月14日22:04:09
  • 声明:本站所有文章,如无特殊说明或标注,本站文章均为原创。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。转载请务必保留本文链接:https://www.yangxingzhen.com/1614.html
    • 运维老司机
      运维老司机 6

      亲测,写的不错,感谢博主 :razz:

    匿名

    发表评论

    匿名网友
    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    拖动滑块以完成验证