Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1125618
  • 博文数量: 170
  • 博客积分: 1603
  • 博客等级: 上尉
  • 技术积分: 1897
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 15:54
文章分类

全部博文(170)

文章存档

2016年(27)

2015年(21)

2014年(27)

2013年(21)

2012年(7)

2011年(67)

我的朋友

分类: Python/Ruby

2015-07-23 17:00:06

创建Procss类,3506是pid
 p = psutil.Process(3506)

p里的内容应该不是固定的,实际进程消失的情况下调用实例内的函数会抛出psutil.NoSuchProcess错误(至少0.6版的process_iter()返回的Procss实例会如此)

获取当前连接
>>> a =  p.connections()
>>> print type(a[0])
<class 'psutil._common.pconn'>
>>> print a
[pconn(fd=5, family=2, type=1, laddr=('172.20.0.93', 2677), raddr=(), status='LISTEN')]

获取打开的文件
>>> print p.open_files()   
[popenfile(path='/var/log/middleware/info.log', fd=1), popenfile(path='/var/log/middleware/errlog.log', fd=2), popenfile(path='/var/log/middleware/info.log', fd=3), popenfile(path='/var/log/middleware/errlog.log', fd=4)]

 上述方法可以解决我原来在论坛里没人回答的问题

点击(此处)折叠或打开

  1. # -*- coding: UTF-8 -*-
  2. __author__ = 'loliz_000'

  3. import psutil

  4. PSUTIL_INTERVAL_TIME = 0.1

  5. #返回服务器内存与cpu信息
  6. def get_mem_and_cpu_info(process=None):
  7.     if process is None:
  8.         try:
  9.             #获取内存与cpu信息,这里的类变量赋值后不随系统内的状况变化而变化
  10.             #需要获取系统最新的状态需要再次调用对应函数返回相应类
  11.             mem_info = psutil.virtual_memory()
  12.             cpu_info = psutil.cpu_times_percent(PSUTIL_INTERVAL_TIME)
  13.         except:
  14.             return 'get mem or cpu info from systme error'
  15.         #内存信息
  16.         total = mem_info.total
  17.         free = mem_info.free
  18.         buffers = mem_info.buffers
  19.         cached = mem_info.cached
  20.         percent = mem_info.percent
  21.         #used = total - (free + buffers + cached)
  22.         #used_with_cache = mem_info.used
  23.         #cpu占用信息
  24.         cpu_count = psutil.cpu_count()
  25.         cpu_user = cpu_info.user
  26.         cpu_system = cpu_info.system
  27.         cpu_iowait = cpu_info.iowait
  28.         cpu_irq = cpu_info.irq
  29.         cpu_idle = cpu_info.idle
  30.         mem_line = "Mem used:%s%%|free:%d|buffers:%d|cached:%d|total:%d\t" % (
  31.             percent,
  32.             free/1048576,buffers/1048576,cached/1048576,
  33.             int(total/1048576)
  34.         )
  35.         cpu_line = 'Cpu count:%d|us:%f|sys:%f|iow:%f|irq:%f|idl:%f' % \
  36.                    (cpu_count, cpu_user, cpu_system, cpu_iowait, cpu_irq,cpu_idle)
  37.         del mem_info
  38.         del cpu_info
  39.         return mem_line + cpu_line
  40.     elif process is isinstance(process, psutil.Process):
  41.         pass
  42.         try:
  43.             cpu_info = process.cpu_percent(PSUTIL_INTERVAL_TIME)
  44.             mem_info = process.memory_info()
  45.         except:
  46.             return 'get mem or cpu info from process error'
  47.     else:
  48.         return 'process argv is not psutil.Process'


#psutil 0.6和3.1.1的目前可以知道写法不一样的地方有



点击(此处)折叠或打开

  1. #检查进程是否运行
  2. for process in psutil.process_iter():
  3.     #psutil获取对应信息,必须catch NoSuchProcess,
  4.     try:
  5.         if psutil.__version__ == '3.1.1':
  6.             proc_name = process.name()
  7.             proc_exe = process.exe()
  8.             proc_cmd = process.cmdline()
  9.             proc_gid= process.gids()[1]
  10.             #>>> print p.gids()
  11.             #pgids(real=0, effective=0, saved=0)
  12.             proc_uid = process.uids()[1]
  13.             #>>> print p.uids()
  14.             #puids(real=0, effective=0, saved=0)
  15.             proc_username = process.username()
  16.         else:
  17.             proc_name = process.name
  18.             proc_exe = process.exe
  19.             proc_cmd = process.cmdline
  20.             proc_gid= process.gids[1]
  21.             proc_uid = process.uids[1]
  22.             proc_username = process.username
  23.     #获取的的进程信息可能是已经结束的进程,直接跳过
  24.     except psutil.NoSuchProcess:
  25.         continue
  26.     except:
  27.         break

阅读(1330) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~