class MonitorServer(object): def __init__(self, ip, port): self.ip = ip self.port = port self.hosts = serialize.all_host_configs() self.redis = RedisHelper() def handle(self): redis_sub = self.redis.subscribe() redis_sub.parse_response() while True: msg = redis_sub.parse_response() # print('recv:', msg) action_process.action_process(self, msg) # 传入 self 实例本身, 和订阅的数据 print('----waiting for new msg ---') # received data for host, val in self.hosts['hosts'].items(): print(host, val) def run(self): print('----starting monitor server----') self.handle() def process(self): pass
class MonitorServer(object): def __init__(self,ip,port): self.ip = ip self.port = port self.hosts = serialize.all_host_configs() self.redis = RedisHelper() def handle(self): redis_sub = self.redis.subscribe() #redis_sub.parse_response() while True: msg = redis_sub.parse_response() print 'recv:',msg action_process.action_process(self,msg)#把self传过去的好处是,在另一个模块中也可以调用这个模块中已经生成的实例,而不需要再重新实例化出一个新的实例,相当于把实例传过去 print '---waiting for new msg---' for host,val in self.hosts['hosts'].items(): print host,val def run(self): print '---starting monitor server---' self.handle() def process(self): pass
class MonitorServer(object): def __init__(self, ip, port): #本机IP self.ip = ip #本机端口 self.port = port #self.hosts = serialize.all_host_configs() self.redis = RedisHelper() def handle(self): redis_sub = self.redis.subscribe() #redis_sub.parse_response() while True: msg = redis_sub.parse_response() #收到消息就打印,没有就堵塞 print('recv:', msg) #收到丢给action_process action_process.action_process(self, msg) print('-----waiting for new msg------') for host, val in self.hosts['hosts'].items(): print(host, val) def run(self): print('--------starting monitor server -------') self.handle() def process(self): pass
class MonitorServer(object): def __init__(self, ip, port): self.ip = ip self.port = port self.hosts = serialize.flush_all_host_configs_into_redis() self.redis = RedisHelper() def handle(self): redis_sub = self.redis.subscribe() #redis_sub.parse_response() while True: msg = redis_sub.parse_response() print 'recv:', msg action_process.action_process(self, msg) print '----waiting for new msg----' # received data for host, val in self.hosts['HostConfig'].items(): print host, val def run(self): print '----starting monitor server----' self.handle() def process(self): pass
class MonitorServer(object): def __init__(self,ip,port): self.ip = ip self.port = port self.hosts = serialize.host_config_serialize() self.redis = RedisHelper() def handle(self): redis_sub = self.redis.subscribe() while True: msg = redis_sub.parse_response() print 'recv:',msg action_process.action_process(self,msg) print '---waiting for new msg ---' for host,val in self.hosts['hosts'].items(): print host,val def run(self): self.handle() def process(self): pass
class MonitorServer(object): #创建主的类,调用连接Redis&调用serialize序列化,把监控模板上传至Redis def __init__(self): self.r = RedisHelper() self.save_configs() self.sub = self.r.subscribe() def start(self): self.monitor_data_processing() while True: client_data = json.loads(self.sub.parse_response()[2]) client_data['last_update'] = time.time() #print client_data business_type = client_data.keys()[0] action_process.action_process(self,business_type,client_data) ''' self.r.set('ServiceData::%s:%s' % (client_data['report_monitor_data']['ip_address'], client_data['report_monitor_data']['service_name']), client_data) ''' #数据存入到Redis中并且存储的相同数据只保留一条,数据不断刷新 def monitor_data_processing(self): #启用多进程处理任务 p = multiprocessing.Process(target=self.monitor_data_handling,) p.daemon = True #设置为dameon模式,主进程挂掉之后,关闭进程 p.start() def monitor_data_handling(self):#所有的数据逻辑处理方法 print '''---starting a new process to deal with monitor data ---''' while True: client_data = self.r.keys("ServiceData::*") for i in client_data: host_ip,host_server = i.split('::')[1].split(':') action_process.action_work(self,i,host_server) self.r.del_key(i) print i,'is work done' def save_configs(self): serialize.push_config_toredis(self,hosts.monitored_groups)#这里把self传过去,在push_config_toredis中即可调用实例
#!/usr/bin/env python # -*- coding:utf-8 -*- ''''订阅方''' import sys reload(sys) sys.setdefaultencoding('utf-8') from redishelper import RedisHelper obj = RedisHelper() redis_sub = obj.subscribe(channel='liyanliang') while True: msg = redis_sub.parse_response() print('接收:'.decode(), msg)
class MonitorServer(object): def __init__(self, ip, port): self.ip = ip self.port = port self.hosts = serialize.all_host_configs() self.redis = RedisHelper() def get_config(self, host): applied_services = [] config_list = {host: {}} for group in monitored_groups: if host in group.hosts: applied_services.extend( group.services) #后面追加列表,group.services是一台主机所有配置的服务 applied_services = set(applied_services) #去重,列表转集合去掉重复项 for service in applied_services: service = service() #把类实例化 config_list[host][service.name] = service.triggers #print config_list return config_list[host] def handle(self): redis_sub = self.redis.subscribe() value_dic = {} for host in self.hosts['hosts'].keys(): value_dic[host] = { 'MemUsage': [], 'iowait': [], 'idle': [], 'load1': [] } #用来存数据 print value_dic while True: msg = redis_sub.parse_response() #print 'recv:',msg action_process.action_process( self, msg) #self是将本实例传给action_process方法,action_process不再需要导入redis print '----waiting for new msg ---' #received data for host, val in self.hosts['hosts'].items(): if val is not None: #print host, val #get config configs = self.get_config(host) for service_name, val2 in val.items(): print "hahaha", configs[service_name] c_time_stamp = val2['time_stamp'] data = val2['data'] #print "dadada",data time_pass_since_last_recv = time.time() - c_time_stamp #print "Time pass:"******"\033[41;1m Service %s has no data for %ss\033[0m" % ( service_name, time_pass_since_last_recv) else: if 'MemUsage' in data.keys(): key = 'MemUsage' key_value = int(data['MemUsage']) * 100 / int( data['MemTotal']) print key, ": ", key_value value_dic[host][key].append(float(key_value)) else: for key in configs[service_name].keys(): key_value = data[key] print key, ": ", key_value value_dic[host][key].append( float(key_value)) for key in configs[service_name].keys(): if len(value_dic[host][key]) > 5: #只保留5个数,测试用 del value_dic[host][key][0] configs[service_name][key]['func']( configs[service_name][key]['warning'], configs[service_name][key]['operator'], value_dic[host][key][-6:-1]) #times = int(configs[service_name][key]['minutes'])*60/ print value_dic def run(self): print '----starting monitor server----' self.handle() def process(self): pass
__author__ = "那位先生Beer" from redishelper import RedisHelper obj = RedisHelper() redis_sub = obj.subscribe() while True: #不停地接收消息 msg = redis_sub.parse_response() #这才是真正的接收消息 print(msg)
#! /usr/bin/env python # coding:utf-8 """ redis 订阅端 """ from redishelper import RedisHelper import time r=RedisHelper() recv = r.subscribe() while True: print(r.subscribe())
#!/usr/bin/env python # -*- coding: UTF-8 -*- __author__ = "Sigai" from redishelper import RedisHelper subscriber = RedisHelper() sub = subscriber.subscribe() while True: msg = sub.parse_response() print(msg)