python运维--psutil

来源:互联网 发布:监视网络流量软件 编辑:程序博客网 时间:2024/06/06 01:25

psutil(Python system and process utilities)是跨平台的进程管理和系统工具的python库,可以查询处理系统的CPU,内存,硬盘,网络等信息,用于系统资源的监控,分析,以及对进程进行管理。以下为系统的CPU和内存信息。

安装psutil

# wget https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.gz# tar -zxvf psutil-2.1.3.tar.gz# cd psutil-2.1.3/# python setup.py install

cpu相关:

>>> psutil.cpu_times()scputimes(user=2533.42, nice=2.72, system=589.09, idle=27799.25, iowait=209.42, irq=0.0, softirq=0.49, steal=0.0, guest=0.0, guest_nice=0.0)
>>> psutil.cpu_times().user2600.07
>>> psutil.cpu_count()        #CPU逻辑个数4>>> psutil.cpu_count(logical=False)       #CPU 物理个数1

内存:

shell 获取内存总量,已用内存

$ free -m|grep Mem|awk '{print $2}'3697$ free -m|grep Mem|awk '{print $3}'1200
使用psutil:

>>> import psutil>>> mem = psutil.virtual_memory()>>> memsvmem(total=3876986880L, available=2884190208L, percent=25.6, used=1675534336L, free=2201452544L, active=864583680, inactive=583118848, buffers=1032192L, cached=681705472)>>> mem.total,mem.used(3876986880L, 1675534336L)>>> 

内存的总大小:total
可以用来的分配的内存: available,free+ buffers +cached
 已经用掉内存的百分比 (total - available) / total
空闲未被分配的内存 free,Linux下不包括buffers和cached

>>> psutil.swap_memory()sswap(total=4160745472L, used=0L, free=4160745472L, percent=0.0, sin=0, sout=0)
swap分区的信息

1 0
原创粉丝点击