psutil api文档:

http://pythonhosted.org/psutil/

api 测试

#! /usr/bin/env python
# coding=utf-8 import psutil # CPU-> Examples # print psutil.cpu_times()
# print psutil.cpu_count()
# print psutil.cpu_count(logical=False)
#
# for x in range(3):
# print psutil.cpu_percent(interval=1)
# print psutil.cpu_percent(interval=1, percpu=True)
# print psutil.cpu_times_percent(interval=1, percpu=False) # Memory-> Examples: # print psutil.virtual_memory()
# print psutil.swap_memory() # Disks-> Examples: # print psutil.disk_partitions()
# print psutil.disk_usage('/')
# print psutil.disk_io_counters(perdisk=False) # Networks-> Examples: # print psutil.net_io_counters(pernic=True)
# print psutil.net_connections() # Other system info-> Examples: # print psutil.users()
# print psutil.boot_time() # Process Management-> Examples: print psutil.pids() for i in psutil.pids():
p = psutil.Process(i)
# print p.name(), p.cpu_percent(interval=1.0) # print p.name()
# print p.cmdline()
# print p.exe()
# print p.cwd()
# print p.status()
# print p.username()
# print p.create_time() # print p.terminal()
# print p.uids()
# print p.gids() # print p.cpu_times()
# print p.cpu_percent(interval=1.0) # print p.cpu_affinity()
# print p.cpu_affinity([0]) # print p.memory_percent()
# print p.memory_info()
# print p.ext_memory_info()
# print p.memory_maps()
# print p.io_counters()
# print p.open_files()
# print p.connections()
# print p.num_threads()
# print p.num_fds()
# print p.threads()
# print p.num_ctx_switches()
# print p.nice()
# print p.nice(10) # print p.ionice(psutil.IOPRIO_CLASS_IDLE) # IO priority (Win and Linux only)
# print p.ionice()
# print p.rlimit(psutil.RLIMIT_NOFILE, (5, 5)) # set resource limits (Linux only)
# print p.rlimit(psutil.RLIMIT_NOFILE) # print p.suspend()
# print p.resume()
# print p.terminate()
# print p.wait(timeout=3) print psutil.test()

配置:

process:
name: ProxyTool.exe
path: E:\Project\ProxyTool.exe rules: p_cpu_percent: 100
#t_cpu_percent: 20
#cpu_times: 30
#num_threads: 15
#connections: 20 noporcesssleeptime: 3
getprocinfotimespan: 3
cpupercentinterval: 1

config.yaml

转换exe

#! /usr/bin/env python
# coding=utf-8 '''
Created on 2015.10.12 @author: ryhan
''' import os # 以下代码解决输出乱码问题
import sys
# print sys.getdefaultencoding()
reload(sys)
sys.setdefaultencoding('utf-8')
# print sys.getdefaultencoding() Py_Installer_Path='D:\pyinstaller-develop'
Py_FilePATH = "%s\\" % (os.path.dirname(os.path.realpath(__file__)),)
Py_FileList=['MyProcMonitor'] # print Py_FilePATH os.chdir(Py_Installer_Path) for fname in Py_FileList: #cmd='python pyinstaller.py --upx-dir=D:\pyinstaller-develop\upx391w -F %s%s.py' % (Py_FilePATH,fname)
#upx.exe 放入到python安装路径下 如果不想使用upx,需要添加参数 --noupx
cmd='python pyinstaller.py -F %s%s.py' % (Py_FilePATH,fname)
print cmd
os.system(cmd) cmd='copy /y %s\%s\dist\%s.exe %s' % (Py_Installer_Path,fname,fname,Py_FilePATH)
print cmd
os.system(cmd)

BulidExe

主程:

#! /usr/bin/env python
# coding=utf-8 import psutil
# print psutil.test() import functools
import yaml
import json
import time
import os from pylog import logger def log(func):
@functools.wraps(func)
def wrapper(*args, **kw): logger.debug(u'---- Invoke : %s ----' , func.__name__) return func(*args, **kw)
return wrapper class Monitor(): @log
def __init__(self): self.confd = yaml.load(file('config.yaml'))
logger.debug('yaml:%s', self.confd) if(self.confd == None or self.confd.get('process') == None or self.confd.get('rules') == None or len(self.confd.get('rules')) == 0):
raise ValueError('please check config.yaml~! (key: confprocess or rules)') self.confprocname = self.confd.get('process', '{}').get('name', '')
self.confprocpath = self.confd.get('process', '{}').get('path', '')
self.confrules = self.confd.get('rules') self.noporcesssleeptime = self.confd.get('noporcesssleeptime', 3)
self.getprocinfospantime = self.confd.get('getprocinfotimespan', 1)
self.cpupercentinterval = self.confd.get('cpupercentinterval', 1) @log
def __loadProcess(self): self.monitorproc = None try:
for p in psutil.process_iter():
# pinfo = p.as_dict(attrs=['pid', 'name']) # for pid in psutil.pids():
# p = psutil.Process(pid) if p.name() == self.confprocname:
self.monitorproc = p
break if(self.monitorproc):
logger.info('Findprocess %s: id:%s ', self.confprocname, self.monitorproc.pid)
else:
logger.info('Do Not Find Porcess ! Please Check~!') except Exception, e:
logger.debug(e) return self.monitorproc @log
def loopControl(self): logger.info('Begin while loop!') finprocessloop = 1 while 1:
try:
while finprocessloop:
if(not self.__loadProcess()):
time.sleep(self.noporcesssleeptime)
continue
else:
finprocessloop = 0 args = self.__getProcInfo()
if args and args[0]:
self.__checkProc(*args)
else:
logger.info('Missing Process Control: %s !', self.confprocname)
finprocessloop = 1 time.sleep(self.getprocinfospantime) except Exception, e:
logger.debug('loopControl.while :%s', e) @log
def __getProcInfo(self): try:
p = self.monitorproc
pinf = {} pinf['id'] = p.pid
pinf['name'] = p.name()
# pinf['exe'] = p.exe()
pinf['num_threads'] = p.num_threads()
pinf['num_handles'] = p.num_handles()
pinf['threads'] = p.threads()
pinf['connections'] = p.connections()
pinf['memory_percent'] = p.memory_percent()
pinf['memory_info'] = p.memory_info()
pinf['cpu_affinity'] = p.cpu_affinity()
pinf['cpu_times'] = p.cpu_times()
pinf['p_cpu_percent'] = p.cpu_percent(interval=self.cpupercentinterval)
pinf['t_cpu_percent'] = psutil.cpu_percent(interval=self.cpupercentinterval)
pinf['cpu_count_real'] = psutil.cpu_count()
pinf['cpu_count_logical'] = psutil.cpu_count(logical=False) cpu_count_real = pinf['cpu_count_real']
cpu_count_logical = pinf['cpu_count_logical']
p_cpu_percent = pinf['p_cpu_percent']
t_cpu_percent = pinf['t_cpu_percent'] logger.debug('pinfo:%s', pinf)
# logger.debug('p_cpu_percent:%s', p_cpu_percent)
# logger.debug('t_cpu_percent:%s', t_cpu_percent) return (True, p_cpu_percent, t_cpu_percent, cpu_count_real, cpu_count_logical) except Exception, e:
logger.debug(e)
return (False, 0, 0, 0, 0) @log
def __checkProc(self, isparmvalid, proc_cpu_percent, total_cpu_percent, cpu_count_real, cpu_count_logical): try: logger.debug('args => pid:%s, pname:%s, isparmvalid:%s, p_u_percent:%s,p_u_t_percent:%s, t_u_percent:%s, u_r_count:%s, u_l_count:%s'\
, self.monitorproc.pid, self.monitorproc.name(), isparmvalid, proc_cpu_percent, proc_cpu_percent / cpu_count_real, total_cpu_percent, cpu_count_real, cpu_count_logical) if isparmvalid: conf_p_cpu_percent = self.confrules.get('p_cpu_percent', 100) if proc_cpu_percent > conf_p_cpu_percent:
# p.send_signal(signal.SIGTERM)
logger.info('judge=> proc_cpu_percent[%s] > conf_p_cpu_percent[%s]. Now kill %s %s ', proc_cpu_percent, conf_p_cpu_percent, self.monitorproc.pid, self.monitorproc.name())
self.monitorproc.terminate()
else:
logger.info('judge=> proc_cpu_percent[%s] < conf_p_cpu_percent[%s]. Keep Watch %s %s ', proc_cpu_percent, conf_p_cpu_percent, self.monitorproc.pid, self.monitorproc.name()) except Exception, e:
logger.debug(e) if __name__ == '__main__': try:
m = Monitor()
m.loopControl() except Exception, e:
logger.debug(e)

MyProcMonitor

日志:

#! /usr/bin/env python
# coding=utf-8 import logging
import logging.handlers # NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL
# CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET # logging初始化工作
# logging.basicConfig() # create logger
logger = logging.getLogger('tst')
logger.setLevel(logging.DEBUG) # create console handler and set level to debug
consolehandler = logging.StreamHandler()
consolehandler.setLevel(logging.INFO) # filehandler = logging.handlers.RotatingFileHandler('run.log', maxBytes=1024 * 1024, backupCount=5)
filehandler = logging.handlers.TimedRotatingFileHandler('run', when='H', interval=1, backupCount=1)
filehandler.suffix = "%Y%m%d-%H%M.log"
filehandler.setLevel(logging.DEBUG) # create formatter
formatter = logging.Formatter('%(asctime)s - %(message)s') # add formatter to handler
consolehandler.setFormatter(formatter)
filehandler.setFormatter(formatter) # 为handler添加formatter # add handler to logger
logger.addHandler(consolehandler)
logger.addHandler(filehandler)

pylog

Python进程监控-MyProcMonitor的更多相关文章

  1. 【321】python进程监控:psutil

    参考:Python进程监控-MyProcMonitor 参考:Python3.6 安装psutil 模块和功能简介 参考:psutil module (Download files) 参考:廖雪峰 - ...

  2. linux 进程监控

    linux 进程监控 supervise Supervise是daemontools的一个工具,可以用来监控管理unix下的应用程序运行情况,在应用程序出现异常时,supervise可以重新启动指定程 ...

  3. python进程池剖析(一)

    python中两个常用来处理进程的模块分别是subprocess和multiprocessing,其中subprocess通常用于执行外部程序,比如一些第三方应用程序,而不是Python程序.如果需要 ...

  4. Mac下Supervisor进程监控管理工具的安装与配置

    Supervisor是一个类 unix 操作系统下的进程监控管理工具. Supervisor是由 Python 写成,可用 Python 的包安装管理工具 pip(Python Package Ind ...

  5. python系统监控及邮件发送

    python系统监控及邮件发送   #psutil模块是一个跨平台库,能轻松实现获取系统运行的进程和系统利用率   import psutil                              ...

  6. python——进程基础

    我们现在都知道python的多线程是个坑了,那么多进程在这个时候就变得很必要了.多进程实现了多CPU的利用,效率简直棒棒哒~~~ 拥有一个多进程程序: #!/usr/bin/env python #- ...

  7. 【SFTP】使用Jsch实现Sftp文件下载-支持断点续传和进程监控

    参考上篇文章: <[SFTP]使用Jsch实现Sftp文件下载-支持断点续传和进程监控>:http://www.cnblogs.com/ssslinppp/p/6248763.html  ...

  8. python多线程监控指定目录

    import win32file import tempfile import threading import win32con import os dirs=["C:\\WINDOWS\ ...

  9. 使用gdb调试Python进程

    使用gdb调试Python进程 有时我们会想调试一个正在运行的Python进程,或者一个Python进程的coredump.例如现在遇到一个mod_wsgi的进程僵死了,不接受请求,想看看究竟是运行到 ...

随机推荐

  1. BZOJ1001 BeiJing2006 狼抓兔子 【网络流-最小割】*

    BZOJ1001 BeiJing2006 狼抓兔子 Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较 ...

  2. [MEF]第05篇 MEF的目录(Catalog)筛选

    一.演示概述本示例演示如何使用MEF提供的目录(Catalog)的扩展机制实现可过滤导出部件的自定义目录类.主要是通过继承ComposablePartCatalog基类,并实现接口INotifyCom ...

  3. grpc gateway 使用以及docker compose 集成

    1. grpc gateway 安装 参考,比较简单,有需要的依赖可以参考相资料 mkdir tmp cd tmp git clone https://github.com/google/protob ...

  4. (转)Linux查看CPU,硬盘,内存的大小

         分类: linux(21)  在Linux的桌面版本中,查看这些东西的确很方便,有图形化的工具可以使用.但是在Linux服务器版上,或者远程ssh连接的时候,就没有图形化的界面可以操作了.此 ...

  5. c# 启动关闭sql服务

    static void Main(string[] args) { ServiceController sc = new ServiceController("MSSQL$SQLEXPRES ...

  6. session 与 cookie 区别

    一.Session的概念 Session 是存放在服务器端的,类似于Session结构来存放用户数据,当浏览器 第一次发送请求时,服务器自动生成了一个Session和一个Session ID用来唯一标 ...

  7. Java--mysql实现分页查询--分页显示

    当数据库中数据条数过多时,一个页面就不能显示,这是要设置分页查询,首先要使用的是数据库sql语句的limit条件实现分组查询sql语句大概形式为: select * from table limit ...

  8. li布局问题

    问题示意,好多网站一般都有这种布局,如 问题主要原因,第一个li没有margin-left 其余有(这里只考虑一排的情况) 第一种解决方式: <!DOCTYPE html> <htm ...

  9. kubernetes 核心原理

    3.1 K8s API Server 原理分析 K8s API server核心提供对各种资源对象的增.删.改.查以及Watch等HTTPRest接口,是集群内各个模块之间数据交互和通信的中心枢纽,是 ...

  10. MySQL性能测试工具之mysqlslap

    MySQL性能测试工具之mysqlslap [日期:2014-10-05] 来源:Linux社区  作者:tongcheng [字体:大 中 小]   --转自Linux社区:http://www.l ...