psutil api文档:
http://pythonhosted.org/psutil/
配置:
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
时间: 2024-10-07 22:32:52