python 获取linux的内存信息
1、安装psutil源码
https://pypi.python.org/pypi?:action=display&name=psutil#downloads
找到psutil-5.2.2.tar.gz 点击即可下载
shell # tar xzvf psutil-5.2.2.tar.gz
shell # cd psutil-5.2.2
shell # python setup.py install
2、使用psutil获取内存信息
终端输入python
shell # python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> mem = psutil.virtual_memory()
>>> mem
svmem(total=8254787584, available=3015909376, percent=63.5, used=4780199936, free=272994304, active=5022015488, inactive=2267688960, buffers=147845120, cached=3053748224, shared=199192576)
>>>mem.total,mem.used #总内存,内存使用率
(8254787584, 4780199936) #然后依次类推
>>> psutil.swap_memory()
sswap(total=4160745472, used=463339520, free=3697405952, percent=11.1, sin=8634368, sout=472358912) #swap 信息
>>> psutil.swap_memory().used #swap 使用,后面的可以以此类推
463339520
3、使用psutil获取cpu信息
shell # python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.cpu_times_percent()
scputimes(user=0.5, nice=0.0, system=0.29999999999999999, idle=93.900000000000006, iowait=5.2000000000000002, irq=0.0, softirq=0.0, steal=0.0, guest=0.0)
>>> psutil.cpu_times_percent().user #user 占用cpu的时间比,以此类推
>>> psutil.cpu_count() #获取cpu的逻辑个数
4
4、使用psutil获取磁盘disk信息
>>> psutil.disk_partitions()
[sdiskpart(device=‘/dev/mapper/vg_zabbixserver161-lv_root‘, mountpoint=‘/‘, fstype=‘ext4‘, opts=‘rw‘), sdiskpart(device=‘/dev/sda1‘, mountpoint=‘/boot‘, fstype=‘ext4‘, opts=‘rw‘)] #获取磁盘分区信息
>>> psutil.disk_partitions()[0] #获取磁盘第一个分区
sdiskpart(device=‘/dev/mapper/vg_zabbixserver161-lv_root‘, mountpoint=‘/‘, fstype=‘ext4‘, opts=‘rw‘)
>>> psutil.disk_partitions()[0].mountpoint #获取磁盘第一个分区的挂载点
‘/‘
>>> psutil.disk_usage(‘/‘) #第一个分区的使用信息
sdiskusage(total=37525069824, used=19492302848, free=16119746560, percent=54.700000000000003)
>>> psutil.disk_usage(‘/‘).total #第一个分区的总使用量
37525069824
本文出自 “chuck的博客” 博客,请务必保留此出处http://chuckzeng.blog.51cto.com/10524728/1942153
原文地址:http://chuckzeng.blog.51cto.com/10524728/1942153