Python运维插件——psutil


1、psutil简介
psutil (python system and process utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network) in Python. It is useful mainly for system monitoring, profiling and limiting process resources and management of running processes. It implements many functionalities offered by command line tools such as: ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iostat, iotop, uptime, pidof, tty, taskset, pmap. It currently supports Linux, Windows, OSX, FreeBSD and Sun Solaris, both 32-bit and 64-bitarchitectures, with Python versions from 2.6 to 3.5 (users of Python 2.4 and 2.5 may use 2.1.3version). PyPy is also known to work.
中文翻译:
psutil(Python System and process utilities,Python系统和进程工具包)是一款用Python实现的跨平台的获取运行程序和系统利用率(CPU、内存、磁盘、网络)相关信息的库。主要对系统监控、分析及减少进程资源和管理运程序有帮助。它实现了许多命令行的功能,诸如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap。目前支持linux、windows、OSX,FreeBSD和Sun Solaris,同时支持32位和64位架构,支持2.6到3.5版本的Python(使用Python2.4和2.5版本的用户请使用本软件2.1.3版本)。PyPy已知可以运行。
psutil有自己的参考文档:http://pythonhosted.org/psutil/,如果遇到一些问题而没有现成的方案参考,请查阅官方参考文档。
2、安装
psutil并没有集成在任何版本的Python库中,需要自行安装。
下面以lcentos为例安装
2、1 安装前准备
首先要确保系统安装有gcc和python-devel,如果没有,可以通过yum安装
 
$ sudo yum install gcc python-devel
2、2 通过源码安装
安装gcc和python-devel后,下载源码包进行安装
$ wget https://pypi.python.org/packages/source/p/psutil/psutil-3.2.1.tar.gz
$ tar -zxf  psutil-3.2.1.tar.gz
$ cd psutil-3.2.1
$ python setup.py install
2、3 验证
安装完成后,进入python,导入psutil库,即可使用其组件功能。
$ python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.cpu_times()
scputimes(user=3961.46, nice=169.729, system=2150.659, idle=16900.540, iowait=629.59, irq=0.0, softirq=19.42, steal=0.0, guest=0, nice=0.0)
3、psutil各个组件介绍(时间紧迫待完善)
3、1 系统信息类
CPU
内存
磁盘
网络
其他系统信息

评论关闭