Пример #1
0
 def update_report_status(self):
     """
     更新数据上报状态
     :return:
     """
     res = requests_get(
         QUERY_IP_REPORT_STATUS_URL, **{
             "biz_id": self.cc_biz_id,
             "data_id": self.data_id
         })
     if not res['result']:
         return
     for ip, ip_info in res['data'].items():
         if 'agent_status' not in ip_info:
             continue
         try:
             agent = AgentStatus.objects.get(ds_id=self.id, ip=ip)
             if agent.status in (DS_STATUS.CREATE, DS_STATUS.NORMAL,
                                 DS_STATUS.EXCEPTION):
                 agent.status = (DS_STATUS.NORMAL if ip_info['agent_status']
                                 == 1 else DS_STATUS.EXCEPTION)
                 if 'last_report_time' in ip_info:
                     agent.update_time = ip_info['last_report_time']
                 agent.save()
         except ObjectDoesNotExist:
             logger.warning('ip not exist. ds_id: {ds_id}, ip: {ip}'.format(
                 ds_id=self.id, ip=ip))
     # 更新数据源状态
     self.update_ds_status()
Пример #2
0
 def update_status(self):
     """
     更新接入状态
     :return:
     """
     # logger.info('update_status start')
     url = QUERY_IP_STATUS_URL
     res = requests_get(
         url, **{
             "biz_id": self.cc_biz_id,
             "data_id": self.data_id
         })
     # logger.info('update_status finish')
     if not res['result']:
         raise Exception('update_status false')
     for ip_info in res['data']:
         try:
             AgentStatus.objects.get(ds_id=self.id,
                                     ip=ip_info['ip']).update_status(
                                         ip_info['ip_op_result'])
         except ObjectDoesNotExist:
             logger.warning('ip not exist. ds_id: {ds_id}, ip: {ip}'.format(
                 ds_id=self.id, ip=ip_info['ip']))
     # 更新数据源的状态
     self.update_ds_status()
Пример #3
0
 def get_monitors(self):
     """
     获取关联监控项
     :return:
     """
     res = requests_get(
         QUERY_DS_MONITORS_URL,
         **{"stat_source_info__contains": self.result_table_id})
     return res
Пример #4
0
 def get_select_options(config_name):
     """
     获取下拉框选项
     :param config_name:
     :return: [{
                 display: "=",
                 value: "="
             }],
     """
     url = QUERY_OPTIONS_URL.format(
         config_name=CONFIG_NAME_MAP[config_name])
     return requests_get(url)['data']
Пример #5
0
def get_alarm_data(request, cc_biz_id):
    """获取告警列表

    :param request:
    :param cc_biz_id:
    :return:
    """
    def build_alarm_recent_data(data_list):
        """组装最近告警列表数据

        :param data_list:
        :return:
        """
        result_data_list = []
        for data in data_list:
            result_data_list.append({
                'id': data['id'],
                'alarm_attr_id': data['alarm_attr_id'],
                'source_time': data['source_time'],
                'level': data['level'],
                'status': data['user_status'],
                'alarm_content': json.loads(data['alarm_content'])
            })
        return result_data_list
    try:
        # 近期告警事件
        res = requests_get(
            ALARM_RECENT_URL,
            cc_biz_id=cc_biz_id,
            **json.loads(request.GET['params'])
        )
        if res['result']:
            res['data'] = build_alarm_recent_data(res['data']['results'])
    except Exception as e:
        logger.error(traceback.format_exc())
        res = {
            'result': False,
            'message': str(e),
            'data': []
        }
    return render_json(res)
Пример #6
0
def get_alarm_num_data(request, cc_biz_id, period, group_field,
                       recent_intervals):
    """获取告警数量统计

    :param request:
    :param cc_biz_id:
    :param period: 统计周期 [day 按天统计|hour 按小时统计]
    :param group_field: 聚合字段 按此字段group by, 例如按告警级别[level]
    :param recent_intervals: 返回从当前时间向前recent_intervals个周期的数据
    :return:
    """
    try:
        res = requests_get(ALARM_COUNTS_URL,
                           cc_biz_id=cc_biz_id,
                           period=period,
                           group_field=group_field,
                           recent_intervals=int(recent_intervals) - 1)
    except Exception as e:
        logger.error(traceback.format_exc())
        res = {'result': False, 'message': u"查询接口失败:%s" % e, 'data': []}
    return render_json(res)
Пример #7
0
def get_alarm_num_data(request, cc_biz_id, period, group_field, begin_time,
                       end_time):
    """获取告警数量统计

    :param request:
    :param cc_biz_id:
    :param period: 统计周期 [day 按天统计|hour 按小时统计]
    :param group_field: 聚合字段 按此字段group by, 例如按告警级别[level]
    :param begin_time: YYYY-MM-DD HH:mm:SS
    :param end_time: YYYY-MM-DD HH:mm:SS
    :return:
    """
    try:
        res = requests_get(ALARM_COUNTS_URL, cc_biz_id=cc_biz_id,
                           period=period, group_field=group_field,
                           time_key__gte=begin_time, time_key__lte=end_time)
    except Exception as e:
        logger.error(traceback.format_exc())
        res = {
            'result': False,
            'message': u"查询接口失败:%s" % e,
            'data': []
        }
    return render_json(res)
Пример #8
0
def get_event_data(request, cc_biz_id, event_type, begin_time, end_time, page):
    """获取事件列表数据

    :param request:
    :param cc_biz_id:
    :param event_type:
        最近频繁告警事件:alwaysAlert
        近期告警事件:recentAlert
        最近操作事件:recentOperate
    :param begin_time: %Y-%m-%d %H:%M:%S
    :param end_time: %Y-%m-%d %H:%M:%S
    :param page:
    :return:
    """
    def build_alarm_recent_data(data_list):
        """组装最近告警列表数据

        :param data_list:
        :return:
        """
        result_data_list = []
        for data in data_list:
            result_data_list.append({
                'id':
                data['id'],
                'source_time':
                data['source_time'],
                'level':
                data['level'],
                'alarm_content':
                json.loads(data['alarm_content'])
            })
        return result_data_list

    try:
        if event_type == ALARM_EVENT_TYPE.alwaysAlert:
            # 最近频繁告警事件
            res = requests_get(ALARM_FREQ_URL,
                               cc_biz_id=cc_biz_id,
                               time_key__gte=begin_time,
                               time_key__lte=end_time,
                               page=page,
                               page_size=10)
        elif event_type == ALARM_EVENT_TYPE.recentAlert:
            # 近期告警事件
            res = requests_get(ALARM_RECENT_URL,
                               cc_biz_id=cc_biz_id,
                               source_time__gte=begin_time,
                               source_time__lte=end_time,
                               page=page,
                               page_size=10,
                               ordering='-source_time')
            if res['result']:
                res['data'] = build_alarm_recent_data(res['data']['results'])
        elif event_type == ALARM_EVENT_TYPE.recentOperate:
            # 操作事件
            config_types = ('Monitor', 'MonitorCondition',
                            'MonitorConditionConfig', 'AlarmDef')
            data_list = OperateRecord.objects.filter(
                biz_id=cc_biz_id,
                config_type__in=config_types,
                operate_time__gte=begin_time,
                operate_time__lte=end_time,
                ).exclude(operate_desc='')\
                .order_by('-operate_time')\
                .values('config_type', 'operator', 'operate', 'operate_desc',
                        'operate_time', 'operator_name', 'config_title')
            data_list = OperateRecord.build_operate_record_data(
                data_list, int(page))
            OperateRecord.update_chname(data_list)
            res = {'result': True, 'message': 'ok', 'data': data_list}
        else:
            raise Exception(u"非法请求。event_type: %s" % event_type)
    except Exception as e:
        logger.error(traceback.format_exc())
        res = {'result': False, 'message': str(e), 'data': []}
    return render_json(res)