def get(self, req_data): form = MonitorAlertDashGetForm(ImmutableMultiDict(req_data)) if not form.validate(): raise Status400(form.errors) form_data = form.data.copy() region = form_data.pop('region') ser_instance = self.db_session.query(MonitorRegion).get(region) if not ser_instance: raise Status403('region id is invalid') server_list = [i.ip for i in ser_instance._serverips] form_data['broadband'] = form_data['broadband'].split(',') origin_data = es_client.get_region_record( settings.opt['ELASTICSEARCH']['INDEX'], servers=server_list, **form_data) for item in origin_data: if form_data['datatype'] == 'delay': item['level'] = MonitorAlertHandler.set_level( form_data['datatype'], delay=item['delay']) elif form_data['datatype'] == 'loss_rate': item['level'] = MonitorAlertHandler.set_level( form_data['datatype'], loss_rate=item['loss_rate']) else: item['level'] = MonitorAlertHandler.set_level( form_data['datatype'], item['delay'], item['loss_rate']) return {'return': 0, 'message': 'success', 'instance': origin_data}
def get(self, req_data): form = MonitorTraceDetailForm(ImmutableMultiDict(req_data)) if not form.validate(): raise Status400(form.errors) get_data = es_trace.get_detail_record( settings.opt['ELASTICSEARCH']['TRACEINDEX'], **form.data) max_delay = get_data['instance'].get('delay_top', self.max_delay) for item in get_data['instance']['traceroute']: if item['ip'] == '***': item['county'] = u'未知'.encode('utf-8') item['broadband'] = u'未知'.encode('utf-8') item['delay'] = round(item['delay'], 2) elif MonitorTraceDetailHandler.check_ipv4(item['ip']): ip_info = get_ipaddr_info(item['ip']) item['county'] = ip_info['county'] item['broadband'] = ip_info['broadband'] item['delay'] = round(item['delay'], 2) else: item['ip'] = '***' item['county'] = u'未知'.encode('utf-8') item['broadband'] = u'未知'.encode('utf-8') item['delay'] = max_delay if item['delay'] == 0 or item['delay'] > max_delay: item['delay'] = max_delay for item in MonitorTraceDetailHandler.delay_check_list: if get_data['instance'].get(item, 0.0) == 0.0: get_data['instance'][item] = max_delay get_data.update({'result': 0, 'message': 'success'}) return get_data
def get(self, req_data): form = MonitorAlertDashDetailForm(ImmutableMultiDict(req_data)) if not form.validate(): raise Status400(form.errors) form_data = form.data.copy() region = form_data.pop('region') ser_instance = self.db_session.query(MonitorRegion).get(region) if not ser_instance: raise Status403('region id is invalid') server_list = [i.ip for i in ser_instance._serverips] # logger.debug('{0}'.format(form_data)) form_data['broadband'] = form_data['broadband'].split(',') origin_data = es_client.get_province_record( settings.opt['ELASTICSEARCH']['INDEX'], servers=server_list, **form_data) # logger.debug('{0}'.format(origin_data)) ip_total = 0 avg_delay, avg_loss = 0, 0 for item in origin_data['county_net']: ip_total = ip_total + item['counter'] for item in origin_data['county_net']: avg_delay = avg_delay + item['delay'] * item['counter'] / ip_total avg_loss = avg_loss + item['loss_rate'] * item['counter'] / ip_total item['ip_perc'] = item['counter'] / ip_total * 100 return { 'return': 0, 'message': 'success', 'ip_total': ip_total, 'avg_delay': avg_delay, 'avg_loss': avg_loss, 'update_time': origin_data['updatetime'], 'instance': origin_data['county_net'] }
def get(self, req_data): form = MonitorTraceForm(ImmutableMultiDict(req_data)) if not form.validate(): raise Status400(form.errors) get_data = es_trace.get_record( settings.opt['ELASTICSEARCH']['TRACEINDEX'], **form.data) get_data.update({'result': 0, 'message': 'success'}) return get_data
def put(self, req_data): if not req_data.has_key('id'): raise Status400('id is required') form = MonitorRegionPutForm(data=ImmutableMultiDict(req_data)) if not form.validate(): raise Status400(form.errors) result = MonitorRegion._validate(self.db_session, req_data['id'], **req_data) instance = result['instance'] if instance is None: raise Status403(result['message']) instance._save(self.db_session) return { 'id': '{}'.format(instance.id), 'result': 0, 'message': 'success' }
def get(self, req_data): form = MonitorUserQuerySearchForm(ImmutableMultiDict(req_data)) if not form.validate(): raise Status400(form.errors) search_data = {k: v for k, v in form.data.iteritems() if v != u''} get_data = es_account.get_record( settings.opt['ELASTICSEARCH']['ACCOUNTINDEX'], **search_data) get_data.update({'result': 0, 'message': 'success'}) return get_data
def post(self, req_data): if not req_data.has_key('id'): raise Status400('id is required') instance = self.db_session.query(MonitorAlertLog).get( req_data['id'][0]) if not instance: raise Status400('id not exists') if instance.state != 'open': raise Status400('state is not open') ret = self.make_confirm(instance.trigger, instance.serverip, instance.province, instance.county, instance.broadband, instance.appid, 'confirm') if not ret: return {'result': -1, 'message': 'success', 'id': req_data['id']} instance.state = 'confirm' instance.content_detail = u'{}<br/>告警已经确认。'.format( instance.content_detail).encode('utf8') instance._save(self.db_session, update=True) return {'result': 0, 'message': 'success', 'id': req_data['id']}
def get(self, req_data): if not req_data.has_key('id'): raise Status400('id is required') result = MonitorRegion._validate(self.db_session, req_data['id']) if result['instance'] is None: raise Status403(result['message']) serialized = [ AlchemyEncoder().parse_sqlalchemy_object(item) for item in result['instance']._serverips ] return {'instance': serialized, 'result': 0, 'message': 'success'}
def delete(self, req_data): if not req_data.has_key('id'): raise Status400('id is required') result = MonitorRegion._validate(self.db_session, req_data['id']) instance = result['instance'] if instance is None: raise Status403(result['message']) instance._delete(self.db_session) return { 'id': '{}'.format(instance.id), 'result': 0, 'message': 'success' }
def get(self, req_data): form = MonitorUserQueryDetailForm(ImmutableMultiDict(req_data)) if not form.validate(): raise Status400(form.errors) get_data = es_query.get_detail_record( settings.opt['ELASTICSEARCH']['QUERYINDEX'], **form.data) # region_name = 'unknown' # if get_data: # server_inst = self.db_session.query(MonitorServerIp).filter( # MonitorServerIp.ip==get_data['target_ip']).first() # if server_inst: # region_name = server_inst._region.name get_data.update({'result': 0, 'message': 'success'}) return get_data
def post(self, req_data): form = MonitorServerIpForm(ImmutableMultiDict(req_data)) if not form.validate(): raise Status400(form.errors) result = MonitorServerIp._validate(self.db_session, **req_data) instance = result['instance'] if instance is None: raise Status403(result['message']) instance._save(self.db_session) return { 'instance': AlchemyEncoder().parse_sqlalchemy_object(instance), 'result': 0, 'message': 'success' }
def delete(self, req_data): if not req_data.has_key('id'): raise Status400('id is required') result = MonitorTrigger._validate(self.db_session, req_data['id']) instance = result['instance'] if instance is None: raise Status403(result['message']) if len(instance._rule._triggers) == 1: # 删除整条告警规则 for i in instance._rule._actions: i._delete(self.db_session, commit=False) instance._rule._delete(self.db_session, commit=False) instance._delete(self.db_session) return { 'id': '{}'.format(instance.id), 'result': 0, 'message': 'success', }
def delete(self, req_data): if not req_data.has_key('id'): raise Status400('id is required') failed = [] success = [] for ip_id in req_data['id']: result = MonitorServerIp._validate(self.db_session, ip_id) instance = result['instance'] if instance is None: failed.append(ip_id) else: success.append(ip_id) instance._delete(self.db_session) return { 'result': 0, 'message': 'success', 'success': success, 'failed': failed }
def get(self, req_data): if not req_data.has_key('id'): raise Status400('id is required') instance = self.db_session.query(MonitorAlertLog).get( req_data['id'][0]) if not instance: raise Status403('alert log id is invalid') origin_data = AlchemyEncoder().parse_sqlalchemy_object(instance) key_list = [ 'start_time', 'region', 'state', 'content_detail', 'rule_name', 'broadband', 'id' ] ret_data = { key: value for key, value in origin_data.iteritems() if key in key_list } if origin_data['state'] == 'close': stop_time = datetime.datetime.strptime(origin_data['stop_time'], '%Y-%m-%d %H:%M:%S') start_time = datetime.datetime.strptime(origin_data['start_time'], '%Y-%m-%d %H:%M:%S') if stop_time < start_time: time_delta = start_time - stop_time else: time_delta = stop_time - start_time ret_data['duration'] = time_delta.seconds / 60 else: now = datetime.datetime.now() start_time = datetime.datetime.strptime(origin_data['start_time'], '%Y-%m-%d %H:%M:%S') time_delta = now - start_time ret_data['duration'] = time_delta.seconds / 60 if origin_data['county'] == '*': ret_data['county'] = origin_data['province'] else: ret_data['county'] = '{0}{1}'.format(origin_data['province'], origin_data['county']) return { 'result': 0, 'message': 'success', 'instance': ret_data, }
def get(self, req_data): form = MonitorAlertLogGetForm(ImmutableMultiDict(req_data)) if not form.validate(): raise Status400(form.errors) if form.data['state'] == 'all': data_list = self.db_session.query(MonitorAlertLog).filter( MonitorAlertLog.start_time > form.data['starttime'], MonitorAlertLog.start_time < form.data['stoptime'], ).all() else: data_list = self.db_session.query(MonitorAlertLog).filter( MonitorAlertLog.start_time > form.data['starttime'], MonitorAlertLog.start_time < form.data['stoptime'], MonitorAlertLog.state == form.data['state'], ).all() serialized = [] for item in data_list: rec = { 'uuid': item.id, 'state': item.state, 'starttime': item.start_time, 'content': item.content, 'region': item.region, 'dest': u'{0}{1}'.format(item.province, item.county), } start_time = datetime.datetime.strptime(item.start_time, '%Y-%m-%d %H:%M:%S') stop_time = datetime.datetime.strptime(item.stop_time, '%Y-%m-%d %H:%M:%S') if item.state == 'close': if stop_time > start_time: rec['duration'] = (stop_time - start_time).seconds / 60 else: rec['duration'] = (start_time - stop_time).seconds / 60 else: rec['duration'] = (datetime.datetime.now() - start_time).seconds / 60 serialized.append(rec) return {'instance': serialized, 'result': 0, 'message': 'success'}
def get(self, req_data): form = TraceAllForm(ImmutableMultiDict(req_data)) if not form.validate(): raise Status400(form.errors) get_data = es_trace.get_all_record( settings.opt['ELASTICSEARCH']['TRACEINDEX'], **form.data) ip_dict = {} ip_list = [] for item in get_data: for data in item['traceroute']: find_delay = lambda x, y: y if x == 0.0 else x if MonitorTraceDetailHandler.check_ipv4(data['ip']): if data['ip'] not in ip_list: ip_dict[data['ip']] = { 'delay': find_delay(data['delay'], item['delay_top']), 'counter': 1 } ip_list.append(data['ip']) else: ip_dict[data['ip']]['delay'] = ip_dict[data['ip']]['delay'] + \ find_delay(data['delay'], item['delay_top']) ip_dict[data['ip']]['counter'] = ip_dict[ data['ip']]['counter'] + 1 ip_ret = get_ipaddr_list(ip_list) instance = [{ 'ip': k, 'delay': v['delay'] / v['counter'], 'broadband': ip_ret[k]['broadband'], 'country': ip_ret[k]['country'], 'county': ip_ret[k]['county'], } for k, v in ip_dict.iteritems() if ip_ret[k]['country'] != '局域网'.encode() and ip_ret[k]['county'] != '局域网'.encode()] ret = {'result': 0, 'message': 'success', 'instance': instance} return ret
def get(self, req_data): form = MonitorHeartbeatForm(ImmutableMultiDict(req_data)) if not form.validate(): raise Status400(form.errors) get_data = es_heart.get_record( settings.opt['ELASTICSEARCH']['HEARTINDEX'], **form.data) if len(get_data['dlist']) == 0: return { 'result': 0, 'message': 'success', 'interval': 0, 'instance': [], 'trace_list': [], } # 检查时间断点 sorted_data = sorted(get_data['dlist'], cmp=lambda x, y: cmp(x['time'], y['time'])) mseconds = form.data['duration'] * 1000 ret_data = [] s_flag, count_flag = 0, 0.0 interval = get_data['interval'] sorted_len = len(sorted_data) last_flag = False while count_flag <= mseconds: now = form.data['starttime'] + datetime.timedelta( seconds=count_flag / 1000) s_flag_time = sorted_data[s_flag]['time'] delay_len = len(sorted_data[s_flag]['delay_list']) delta = sorted_data[s_flag]['interval'] * delay_len / 100 time_flag = datetime.datetime.strptime( s_flag_time, '%Y-%m-%d %H:%M:%S') - datetime.timedelta(seconds=delta * 0.1) # logger.debug('xxxxxxxxxxxxxxxxxxxxxxxxxxx') # logger.debug('{0} --- {1} --- {2}'format(s_flag_time, sorted_data[s_flag]['interval'], delay_len)) # logger.debug('{0} --- {1}'.format(now, time_flag)) # logger.debug('{0} --- {1} --- {2}'.format(count_flag, mseconds, len(ret_data))) # logger.debug('xxxxxxxxxxxxxxxxxxxxxxxxxxx') if time_flag < now: if s_flag == sorted_len - 1 and last_flag is True: ret_data.append({ 'loss_rate': '-', 'delay': '-', }) count_flag = count_flag + interval else: tmp_time = datetime.datetime.strptime( s_flag_time, '%Y-%m-%d %H:%M:%S') - now tmp_len = tmp_time.seconds * 1000 / sorted_data[s_flag][ 'interval'] for j in range(tmp_len): if sorted_data[s_flag]['delay_list'][j] == 0: sorted_data[s_flag]['delay_list'][j] = get_data[ 'delay_top'] ret_data.append({ 'loss_rate': sorted_data[s_flag]['loss_rate'], 'delay': sorted_data[s_flag]['delay_list'][j] }) for jj in range( (sorted_data[s_flag]['interval'] - interval) / interval): ret_data.append({ 'loss_rate': '-', 'delay': '-', }) s_flag = s_flag + 1 if s_flag == sorted_len: last_flag = True s_flag = s_flag - 1 count_flag = count_flag + tmp_len * sorted_data[s_flag][ 'interval'] elif time_flag == now: for j in sorted_data[s_flag]['delay_list']: if j == 0: j = get_data['delay_top'] ret_data.append({ 'loss_rate': sorted_data[s_flag]['loss_rate'], 'delay': j }) for jj in range( (sorted_data[s_flag]['interval'] - interval) / interval): ret_data.append({ 'loss_rate': '-', 'delay': '-', }) s_flag = s_flag + 1 if s_flag == sorted_len: last_flag = True s_flag = s_flag - 1 count_flag = count_flag + delta * 100 else: ret_data.append({ 'loss_rate': '-', 'delay': '-', }) count_flag = count_flag + interval # 新增主动检测匹配功能 start_time = form.data['starttime'] stop_time = start_time + datetime.timedelta( seconds=form.data['duration']) trace_time = es_trace.get_record( settings.opt['ELASTICSEARCH']['TRACEINDEX'], userid=form.data['user_id'], appid=form.data['appid'], starttime=start_time, stoptime=stop_time, ) trace_list = [{ 'pos': MonitorHeartbeatHandler.pos_from_time( datetime.datetime.strptime(t, '%Y-%m-%d %H:%M:%S'), start_time, interval), 'timestamp': t } for t in trace_time['checktimes']] return { 'result': 0, 'message': 'success', 'interval': interval, 'instance': ret_data, 'trace_list': trace_list, }