class Check_Cert_Worker(object): def __init__(self): # 存储检查过的域名最近一次检查时间 self.worker = Redis_Queue(Webmoni_Check_Cert_Queue) self.radio = self.worker.subscribe() def start(self): """ 收听来自webmoni_check_cert频道的节目 :return: """ print('Check Cert Worker 已启动') while True: msg = self.radio.parse_response() program = eval(msg[2].decode()) pattern = program.get('pattern') if pattern == 0: # 节目模式=0 就检测所有域名 domain_list = DomainName.objects.filter(check_id=0).values('url') for row in domain_list: domain = row.get('url') c = Cert_check(domain) cert_info = c.py_check() DomainName.objects.filter(url=domain).update(cert_valid_date=cert_info['endDate'], cert_valid_days=cert_info['expire']) elif pattern == 1: # 节目模式=1 检测单个域名 domain = program.get('domain') c = Cert_check(domain) cert_info = c.py_check() DomainName.objects.filter(url=domain).update(cert_valid_date=cert_info['endDate'], cert_valid_days=cert_info['expire'])
class Send_Mail_Worker(object): def __init__(self): self.current = 0 self.now_data = {} self.worker = Redis_Queue(Webmoni_Send_Mail_Queue) self.radio = self.worker.subscribe() def start(self): print('Send Mail Worker 已启动') while True: msg = self.radio.parse_response() program = eval(msg[2].decode()) current_Timestamp = program.get('time') - program.get('time') % 300 if self.current == 0: self.current = current_Timestamp status = program.get('status') url_id = program.get('url_id') if self.current != current_Timestamp: self.send_mail(self.now_data) self.now_data.clear() self.current = current_Timestamp # 本次数据没有超过5分钟的情况 if self.current == current_Timestamp: if not self.now_data.get(program['domain']): self.now_data[program['domain']] = {'failtag': 0, 'areas': {}} if status != 100: self.now_data[program['domain']]['failtag'] += 1 self.now_data[program['domain']]['areas'][str(program.get('node'))] = program.get('total_time') def send_mail(self,last_data): node_info = {} for node in Node.objects.all(): node_info[str(node.id)] = node.node for domain, y in last_data.items(): domain_obj = DomainName.objects.get(url=domain) if domain_obj.warning == 0: if y['failtag'] >= int(len(y['areas']) / 2) + 1: DomainName.objects.filter(url=domain).update(status=99) content = '域名:{}<br>CDN:{}<br>项目:{}<br>'.format(domain, domain_obj.cdn, domain_obj.project_name) for area, val in y['areas'].items(): content += '{}:{}<br>'.format(node_info[area], val) content += '<a href="http://47.91.239.10:80/webmoni/Nowarning/{}/">点击不警告</a><br>'.format(domain) send_mail(domain, [Ming, Lin], content) else: DomainName.objects.filter(url=domain).update(status=100)
class Minion_Check_Worker(object): def __init__(self): self.worker = Redis_Queue('check_minion') self.radio = self.worker.subscribe() # 检测主机队列 def start(self): print('Minion Check Worker 已启动') while True: # 接收队列消息 msg = self.radio.parse_response() # 解析消息内容 msg = eval(msg[2]) time.sleep(10) if msg['add'] else None # 调用test.ping测试 minion_check = Test_ping() # 检测主机配置 grains = Grains() if msg['pattern'] == 1: # 检测主机 minion_check.get_status(msg['id']) if msg['pattern'] == 2: # 获取主机信息 grains.get_minion_items(msg['id'])
class Execute_PlayBook_Worker(object): def __init__(self): self.worker = Redis_Queue('state_execute') self.radio = self.worker.subscribe() self.client = client.LocalClient() def start(self): print('Execute PlayBook Worker 已启动') while True: # 接收队列消息 msg = self.radio.parse_response() # 解析消息内容 msg = eval(msg[2]) print('state_execute频道接收消息:', msg) # 执行状态 minion_state = Minion_state() result = minion_state.exe_sls(msg['number'], msg['minion_id_list'], msg['playbook_id']) jid = result[0] number = result[1] t = 1 # 判断异常 if result[0] not in msg['minion_id_list'] and type( jid) == int and jid > 0: # 持续查询,直到出现结果,或者连接超时 while not self.client.get_cache_returns(jid): time.sleep(1) if t == 1200: information = {'ERROR': 'Timeout'} break else: t += 1 else: information = self.client.get_cache_returns(jid) # 继续查询,直到全部主机执行结果全部得到 if len(information) != len( msg['minion_id_list']) and 'ERROR' not in information: while len(self.client.get_cache_returns(jid)) != len( msg['minion_id_list']): time.sleep(1) if t == 1200: information = self.client.get_cache_returns(jid) minion_id_obj = Accepted_minion.objects.in_bulk( msg['minion_id_list']).values() for minion_id in minion_id_obj: if str(minion_id) not in information: information[str(minion_id)] = { 'ret': ['Timeout'] } break else: t += 1 else: information = self.client.get_cache_returns(jid) minion_state.save_sls(number=number, information=information) print('任务完成,state_execute频道继续接收消息') else: information = { 'ERROR': '未选择主机或剧本' } if jid == 0 else { '不存在的主机': result[0] } minion_state.save_sls(number=number, information=information) print('任务异常,state_execute频道继续接收消息') continue