python 获取系统基础信息

系统基础信息包括 Linux基本性能,块设备,网卡接口,系统信息,网络地址库等信息。

使用的是psutil模块。

安装方法十分简单   pip install psutil  就好



 内存信息

查看物理内存的total值和物理内存的used值。

>>> import psutil
>>> mem = psutil.virtual_memory()
>>> print 'total:',mem.total/1024/1024,'Mb',' used:',mem.used/1024/1024,'Mb'
total: 3774 Mb  used: 1043 Mb

不仅仅total和used。其他的也可以获取到。

>>> mem
svmem(total=3958308864, available=2556866560, percent=35.4, used=1094393856, free=1518125056, active=891387904, inactive=927453184, buffers=40960, cached=1345748992, shared=11018240)
>>> mem.free/1024/1024
1447
>>> psutil.swap_memory()
sswap(total=2147479552, used=6463488, free=2141016064, percent=0.3, sin=471040, sout=6836224)


 CPU信息

Linux操作系统下CPU的利用率有以下几个部分。

User Time,执行用户进程的事件百分比

System Time,执行内核进程和中断的时间百分比

Wait IO,由于IO等待而使CPU处于idle(空闲)状态的时间百分比

Idle,CPU处于idle的时间百分比

下面是显示CPU状态的

>>> psutil.cpu_times()
scputimes(user=7846.72, nice=27.52, system=2248.4, idle=42798.1, iowait=187.2, irq=0.0, softirq=28.21, steal=0.0, guest=0.0, guest_nice=0.0)

显示每一个CPU状态

>>> psutil.cpu_times(percpu=True)
[scputimes(user=3963.92, nice=14.5, system=1133.28, idle=21375.0, iowait=93.69, irq=0.0, softirq=13.62, steal=0.0, guest=0.0, guest_nice=0.0), scputimes(user=3883.61, nice=13.01, system=1115.68, idle=21487.44, iowait=93.51, irq=0.0, softirq=14.6, steal=0.0, guest=0.0, guest_nice=0.0)]

获取单项数据

>>> psutil.cpu_times().user
7850.13

获取CPU的核数

>>> psutil.cpu_count()
2
>>> psutil.cpu_count(logical=False)
2

 磁盘信息

  在磁盘利用率方面,一般关注的是磁盘的利用率及IO信息。其中磁盘的利用率使用psutil.disk_usage方法获取。IO的其他信息读取如下。
读IO数    read_count()
写IO数  write_count()
IO读字节数  read_bytes()
IO写字节数  write_bytes()
磁盘读时间  read_time()
磁盘写时间  write_time()
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/mapper/centos-root', mountpoint='/', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'), sdiskpart(device='/dev/sr0', mountpoint='/run/media/root/CentOS 7 x86_64', fstype='iso9660', opts='ro,nosuid,nodev,relatime,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500')]
>>> 
>>> 
>>> psutil.disk_usage('/')
sdiskusage(total=61696114688, used=20093308928, free=41602805760, percent=32.6)
>>> 
>>> 
>>> psutil.disk_io_counters()
sdiskio(read_count=50217, write_count=17009, read_bytes=4081980416, write_bytes=167176192, read_time=1126392, write_time=1281732, read_merged_count=25, write_merged_count=696, busy_time=360413)
>>> 
>>> 
>>> psutil.disk_io_counters(perdisk=True)
{'sdb1': sdiskio(read_count=1325, write_count=350, read_bytes=81351680, write_bytes=12537856, read_time=22693, write_time=46455, read_merged_count=1, write_merged_count=38, busy_time=16554), 'sr0': sdiskio(read_count=22, write_count=0, read_bytes=270336, write_bytes=0, read_time=58, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=58), 'sda2': sdiskio(read_count=23669, write_count=6774, read_bytes=1945483776, write_bytes=69984768, read_time=535274, write_time=521397, read_merged_count=24, write_merged_count=658, busy_time=165491), 'sda1': sdiskio(read_count=310, write_count=2063, read_bytes=29149184, write_bytes=2130944, read_time=8584, write_time=1088, read_merged_count=0, write_merged_count=0, busy_time=6811), 'dm-0': sdiskio(read_count=24763, write_count=7822, read_bytes=2024631808, write_bytes=82522624, read_time=558691, write_time=712792, read_merged_count=0, write_merged_count=0, busy_time=170591), 'dm-1': sdiskio(read_count=128, write_count=0, read_bytes=1093632, write_bytes=0, read_time=1092, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=908)}
>>> 

 网络信息

  系统的网络信息与磁盘IO类似。涉及的重点也是 bytes_sent(发送字节数)、bytes_recv(接收字节数)、packets_sent(发送数据包)、packets_recv(接收数据包)

>>> psutil.net_io_counters()
snetio(bytes_sent=1932224, bytes_recv=8510735, packets_sent=10540, packets_recv=21391, errin=0, errout=0, dropin=0, dropout=0)
>>> 
>>> 
>>> psutil.net_io_counters(pernic=True)
{'eno16777736': snetio(bytes_sent=18985, bytes_recv=805201, packets_sent=230, packets_recv=9673, errin=0, errout=0, dropin=0, dropout=0), 'ens38': snetio(bytes_sent=1901551, bytes_recv=7701193, packets_sent=10302, packets_recv=11802, errin=0, errout=0, dropin=0, dropout=0), 'lo': snetio(bytes_sent=14488, bytes_recv=14488, packets_sent=54, packets_recv=54, errin=0, errout=0, dropin=0, dropout=0), 'virbr0-nic': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'virbr0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}

 其他信息

 获取用户登陆,开机时间。

>>> psutil.users()
[suser(name='root', terminal=':0', host='root', started=1510042496.0, pid=3371), suser(name='root', terminal='pts/0', host='root', started=1510042880.0, pid=4448)]
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
'2017-11-07 16:12:30'


 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值