def service_handle(status_dic,template_dic,custom_monitor_list=None): alert_dic={} #1处理模板中的主机服务项 for k,v in template_dic.items(): monitor_host_list = set(v['host_list']) for monitor_host in set(v['host_list']): if status_dic.has_key(monitor_host): for service in v['template'].service_list.values(): if status_dic[monitor_host]['result_values'].has_key(service['name']): try: s = alert_handle.service_handle(service, status_dic[monitor_host]['result_values'][service['name']] ) if len(s) !=0: alert_list.append(s) except KeyError: alert_list.append({"NoValidServiceData":(service,"service not exist in client data")} ) else: pass ''' if len(monitor_host.custom_services.values()): for custom_service in monitor_host.custom_services.values(): if status_dic[monitor_host]['result_values'].has_key(custom_service['name']): try: s = alert_handle.service_handle(custom_service, status_dic[monitor_host]['result_values'][custom_service['name']] ) if len(s) !=0: alert_list.append(s) except KeyError: alert_list.append({"NoValidServiceData":(custom_service,"service not exist in client data")} ) else: pass ''' else: print 'host not in this template' #2处理一般服务项 if custom_monitor_list is not None: for custom_host in custom_monitor_list(): if status_dic.has_key(monitor_host): for service in custom_host.custom_services.values(): if status_dic[monitor_host]['result_values'].has_key(service['name']): try: s = alert_handle.service_handle(service, status_dic[monitor_host]['result_values'][service['name']] ) if len(s) !=0: alert_list.append(s) except KeyError: alert_list.append({"NoValidServiceData":(service,"service not exist in client data")} ) else: pass else: print 'host not custom service'
def status_handler(status_dic,host_dic): alert_dic={} #if host_dic.has_key(status_dic[]) #解析处理本server内的各主机监控字典,分析状态字典的内容 for h_k,one_host_dic in host_dic.items(): alert_list=[] #one_host_dic=host_dic[h_k] #1分析某台主机的是否返回了结果数据 print "\033[32;1mhost:%s\033[0m"%(h_k) if status_dic.has_key(h_k): #处理监控数据存入redis中??? status_dic[h_k]['inredis_time']=time.time() #存入到本地redis r=redis_connector.get_redis() r.lpush(h_k,status_dic[h_k]) #print r.lpop(h_k) #2分析该主机的监控服务项 if len(one_host_dic['service']): #状态字典是否获取了监控项的状态 if len(status_dic[h_k]['result_values']): #得到服务项的监控间隔 for service_k in one_host_dic['service'].keys(): #print service_k,one_host_dic['service'][service_k] #测试输出 #状态字典中是否有主机的服务项 if status_dic[h_k]['result_values'].has_key(service_k): try: #主机存在该监控服务项,用monitor_data_handle.py进行具体处理 alert_info = alert_handle.service_handle(one_host_dic['service'][service_k], status_dic[h_k]['result_values'][service_k] ) if len(s) !=0: alert_list.append(alert_info) else:pass except KeyError: print "service %s not exist in client data"%(service_k) alert_list.append({"NoValidServiceData":(service_k,"service not exist in client data")} ) else: print 'host:%s ,service:% is not exist'%(h_k,service_k) alert_list.append({"NoValidServiceData":(service_k,"service not exist in client data")} ) else: print 'host: %s has no get result data'%(one_host_dic['hostname']) alert_list.append('host: %s has no get result data'%(one_host_dic['hostname'])) else: print '%s host has no service....'%(h_k) #1没有收集到某主机h_k的状态信息 else: alert_list.append('host: %s has not get monitoring status data...'%(h_k)) print 'host: %s has not get monitoring status data...'%(one_host_dic['hostname']) #为某个主机添加警告列表 alert_dic[h_k]=alert_list #2分析需要得到监控状态的信息 #一个处理监控项中是否有该结果信息, #另一个是处理在规定的时间间隔中,某主机的监控信息是否返回 for s_k in status_dic.keys(): #状态主机的hostname alert_list=[] if s_k in host_dic.keys(): for service_k in status_dic[s_k]['result_values'].keys(): if service_k in host_dic[s_k]['service'].keys(): pass else: print "host %s service %s is not find in db..." %(s_k,service_k) alert_list.append("host %s service %s is not find in db..." %(s_k,service_k)) else: print "%s host is not find in monitor db...." %(s_k) alert_list.append("%s host is not find in monitor db...." %(s_k)) alert_dic[s_k]=alert_list #报警信息存入到redis中,proxy端放到远程的里面。 alert_dic['TimeStamp'] = time.time() print alert_dic if conf.proxy_server: r=redis_connector.get_redis('10.168.0.218',6379,0) else: r=redis_connector.get_redis() r['TempTriAquaeAlertList'] = json.dumps(alert_dic) #redis_connector.r['TempTriAquaeAlertList'] = json.dumps(alert_dic) '''