#! /usr/bin/env python#coding=utf-8
importpsutil#print psutil.test()
importfunctoolsimportyamlimportjsonimporttimeimportosfrom pylog importloggerdeflog(func):
@functools.wraps(func)def wrapper(*args, **kw):
logger.debug(u'---- Invoke : %s ----' , func.__name__)return func(*args, **kw)returnwrapperclassMonitor():
@logdef __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)
@logdef __loadProcess(self):
self.monitorproc=Nonetry:for p inpsutil.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=pbreak
if(self.monitorproc):
logger.info('Findprocess %s: id:%s', self.confprocname, self.monitorproc.pid)else:
logger.info('Do Not Find Porcess ! Please Check~!')exceptException, e:
logger.debug(e)returnself.monitorproc
@logdefloopControl(self):
logger.info('Begin while loop!')
finprocessloop= 1
while 1:try:whilefinprocessloop:if(not self.__loadProcess()):
time.sleep(self.noporcesssleeptime)continue
else:
finprocessloop=0
args= self.__getProcInfo()if args andargs[0]:
self.__checkProc(*args)else:
logger.info('Missing Process Control: %s !', self.confprocname)
finprocessloop= 1time.sleep(self.getprocinfospantime)exceptException, e:
logger.debug('loopControl.while :%s', e)
@logdef __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)exceptException, e:
logger.debug(e)return(False, 0, 0, 0, 0)
@logdef __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)ifisparmvalid:
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())exceptException, e:
logger.debug(e)if __name__ == '__main__':try:
m=Monitor()
m.loopControl()exceptException, e:
logger.debug(e)
python硬件监控 python监控模块
转载文章标签 python硬件监控 python屏幕监控的模块有哪些 ci json python 文章分类 Python 后端开发
-
Python 实现Linux Raid信息监控
python脚本监控linux raid
zabbix raid python -
python模块--Telnetlib模块
telnet模块
ci 用户名 for循环 -
python 数据监控模块 python开发监控系统
Python之路,Day20 - 分布式监控系统开发 本节内容为什么要做监控? 常用监控系统设计讨论监控系统架构设计监控表结构设计 为什么要做监控? –熟悉IT监控系统的设计原理 –开发一个简版的类Zabbi
python 数据监控模块 系统架构 数据库 python ico -
datagrip插件仓库
webpack用过哪些插件,使用webpack好处 html-webpack-plugin //生成html文件 extract-text-webpack-plugin //提取样式到单独的css文件 CommonsChunkPlugin //提取公共的JS库 (通过webpack.optimize.Commons
datagrip插件仓库 webpack json javascript ViewUI