系统性能信息模块 psutil
1安装:
在 /tmp 下创建一个 soft 目录,设置为 777 权限
# sudo mkdir soft
# sudo chmod -R 777 soft/
# wget https://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.tar.gz --no-check-certificate
# tar -xzvf psutil-2.0.0.tar.gz
# cd psutil-2.0.0/
# python setup.py install
running install
running build
running build_py
creating build
creating build/lib.linux-i686-2.7
creating build/lib.linux-i686-2.7/psutil
copying psutil/_psbsd.py -> build/lib.linux-i686-2.7/psutil
copying psutil/__init__.py -> build/lib.linux-i686-2.7/psutil
copying psutil/_pslinux.py -> build/lib.linux-i686-2.7/psutil
copying psutil/_common.py -> build/lib.linux-i686-2.7/psutil
copying psutil/_compat.py -> build/lib.linux-i686-2.7/psutil
copying psutil/_psposix.py -> build/lib.linux-i686-2.7/psutil
copying psutil/_pssunos.py -> build/lib.linux-i686-2.7/psutil
copying psutil/_psosx.py -> build/lib.linux-i686-2.7/psutil
copying psutil/_pswindows.py -> build/lib.linux-i686-2.7/psutil
running build_ext
building '_psutil_linux' extension
creating build/temp.linux-i686-2.7
creating build/temp.linux-i686-2.7/psutil
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c psutil/_psutil_linux.c -o build/temp.linux-i686-2.7/psutil/_psutil_linux.o
psutil/_psutil_linux.c:12:20: 致命错误: Python.h:没有那个文件或目录
编译中断。
error: command 'gcc' failed with exit status 1
gao@gao-VirtualBox:/tmp/soft/psutil-2.0.0$
解决方法是安装python-dev,这是Python的头文件和静态库包:
sudo apt-get install python-dev
gao@gao-VirtualBox:/tmp/soft/psutil-2.0.0$ sudo python setup.py install
running install
running build
running build_py
running build_ext
building '_psutil_linux' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c psutil/_psutil_linux.c -o build/temp.linux-i686-2.7/psutil/_psutil_linux.o
psutil/_psutil_linux.c: 在函数‘init_psutil_linux’中:
psutil/_psutil_linux.c:475:5: 警告: 隐式常量转换溢出 [-Woverflow]
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-i686-2.7/psutil/_psutil_linux.o -o build/lib.linux-i686-2.7/_psutil_linux.so
building '_psutil_posix' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c psutil/_psutil_posix.c -o build/temp.linux-i686-2.7/psutil/_psutil_posix.o
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-i686-2.7/psutil/_psutil_posix.o -o build/lib.linux-i686-2.7/_psutil_posix.so
running install_lib
copying build/lib.linux-i686-2.7/_psutil_linux.so -> /usr/local/lib/python2.7/dist-packages
creating /usr/local/lib/python2.7/dist-packages/psutil
copying build/lib.linux-i686-2.7/psutil/_psbsd.py -> /usr/local/lib/python2.7/dist-packages/psutil
copying build/lib.linux-i686-2.7/psutil/__init__.py -> /usr/local/lib/python2.7/dist-packages/psutil
copying build/lib.linux-i686-2.7/psutil/_pslinux.py -> /usr/local/lib/python2.7/dist-packages/psutil
copying build/lib.linux-i686-2.7/psutil/_common.py -> /usr/local/lib/python2.7/dist-packages/psutil
copying build/lib.linux-i686-2.7/psutil/_compat.py -> /usr/local/lib/python2.7/dist-packages/psutil
copying build/lib.linux-i686-2.7/psutil/_psposix.py -> /usr/local/lib/python2.7/dist-packages/psutil
copying build/lib.linux-i686-2.7/psutil/_pssunos.py -> /usr/local/lib/python2.7/dist-packages/psutil
copying build/lib.linux-i686-2.7/psutil/_psosx.py -> /usr/local/lib/python2.7/dist-packages/psutil
copying build/lib.linux-i686-2.7/psutil/_pswindows.py -> /usr/local/lib/python2.7/dist-packages/psutil
copying build/lib.linux-i686-2.7/_psutil_posix.so -> /usr/local/lib/python2.7/dist-packages
byte-compiling /usr/local/lib/python2.7/dist-packages/psutil/_psbsd.py to _psbsd.pyc
byte-compiling /usr/local/lib/python2.7/dist-packages/psutil/__init__.py to __init__.pyc
byte-compiling /usr/local/lib/python2.7/dist-packages/psutil/_pslinux.py to _pslinux.pyc
byte-compiling /usr/local/lib/python2.7/dist-packages/psutil/_common.py to _common.pyc
byte-compiling /usr/local/lib/python2.7/dist-packages/psutil/_compat.py to _compat.pyc
byte-compiling /usr/local/lib/python2.7/dist-packages/psutil/_psposix.py to _psposix.pyc
byte-compiling /usr/local/lib/python2.7/dist-packages/psutil/_pssunos.py to _pssunos.pyc
byte-compiling /usr/local/lib/python2.7/dist-packages/psutil/_psosx.py to _psosx.pyc
byte-compiling /usr/local/lib/python2.7/dist-packages/psutil/_pswindows.py to _pswindows.pyc
running install_egg_info
Writing /usr/local/lib/python2.7/dist-packages/psutil-2.0.0.egg-info
gao@gao-VirtualBox:/tmp/soft/psutil-2.0.0$ python
Python 2.7.3 (default, Dec 18 2014, 19:03:52)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> mem = psutil.virtual_memory()
>>> mem.total,mem.used
(1543016448L, 1417523200L)
>>> psutil.cpu_times()
scputimes(user=210.94, nice=21.38, system=350.13, idle=4838.61, iowait=170.52, irq=0.04, softirq=12.23, steal=0.0, guest=0.0, guest_nice=0.0)
>>> psutil.cpu_times().user
211.09
>>> psutil.cpu_count()
2
>>> psutil.cpu_count(logicl=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "psutil/_common.py", line 93, in wrapper
ret = cache[key] = fun(*args, **kwargs)
TypeError: cpu_count() got an unexpected keyword argument 'logicl'
>>> psutil.cpu_count(logical=False)
1
>>> psutil.swap_memory()
sswap(total=941617152L, used=43753472L, free=897863680L, percent=4.6, sin=1798144, sout=44019712)
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,errors=remount-ro')]
>>> psutil.disk_usage('/')
sdiskusage(total=20207816704L, used=6999846912L, free=12181467136L, percent=34.6)
>>> psutil.disk_io_counters()
sdiskio(read_count=46584, write_count=9553, read_bytes=1286282240, write_bytes=452259840, read_time=1815956, write_time=1128332)
>>> psutil.disk_io_counters(perdisk=True)
{'sr0': sdiskio(read_count=0, write_count=0, read_bytes=0, write_bytes=0, read_time=0, write_time=0), 'sda5': sdiskio(read_count=570, write_count=1655, read_bytes=2580480, write_bytes=44019712, read_time=2044, write_time=6312), 'sda2': sdiskio(read_count=2, write_count=0, read_bytes=6144, write_bytes=0, read_time=284, write_time=0), 'sda1': sdiskio(read_count=46012, write_count=7913, read_bytes=1283695616, write_bytes=408367104, read_time=1813628, write_time=1122020)}
>>> psutil.net_io_counters()
snetio(bytes_sent=4250815, bytes_recv=55347639, packets_sent=42311, packets_recv=66116, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.net_io_counters(pernic=True)
{'lo': snetio(bytes_sent=448, bytes_recv=448, packets_sent=6, packets_recv=6, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=4252009, bytes_recv=55387805, packets_sent=42314, packets_recv=66600, errin=0, errout=0, dropin=0, dropout=0)}
>>> psutil.users()
[suser(name='guest-rUsrlk', terminal='tty7', host='', started=1426638715.0), suser(name='gao', terminal='pts/1', host=':1.0', started=1426639871.0), suser(name='gao', terminal='tty8', host='', started=1426639747.0)]
>>> psutil.boot_time()
1426638680.0
>>> datetime.boot_time()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'datetime' is not defined
>>> import datetime
>>> datetime.boot_time()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'boot_time'
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
'2015-03-18 08:31:20'
>>>
https://github.com/giampaolo/psutil
http://psutil.redathedocs.org/en/latest