Ejemplo n.º 1
0
def collect_app_alive_failed_point_cron():
    with call_mysql.OpsMysqlClient() as cursor:
        sql = '''select `url`, `ops_cp_app_id` from urlooker.strategy where environment = 'release';'''
        cursor.execute(sql)
        result_set = cursor.fetchall()
    for app_obj in result_set:
        try:
            app_d_obj = AppDetail.objects.get(id=app_obj['ops_cp_app_id'])
            app_alive_graph_obj = AppAliveGraph(
                environment='release',
                app_id=app_obj['ops_cp_app_id'],
                check_api=app_obj['url'],
                hours=24)
            monitor_info_list = app_alive_graph_obj.monitor_info()
            failed_point_count = 0
            for mf in monitor_info_list[0]['values']:
                if isinstance(mf['value'], float):
                    if mf['value'] != 0:
                        logger.debug(
                            f'app alive 连接失败点数据: {app_d_obj.app_name} {mf}')
                        failed_point_count += 1
            AppAliveStatistics.objects.create(
                app_name=app_d_obj.app_name,
                failed_point_amount=failed_point_count)
        except Exception as e:
            logger.warning('收集app alive 统计数据出错, 未找到配置...')
Ejemplo n.º 2
0
 def __check_app_alive_exists(self, app_id, env) -> bool:
     sql = select_urlooker_app_alive(app_id=app_id, environment=env, select_args='id')
     with call_mysql.OpsMysqlClient(username=self.mysql_username,
                                    password=self.mysql_password,
                                    host=self.mysql_host,
                                    port=self.mysql_port,
                                    schema='urlooker') as cursor:
         cursor.execute(sql)
         result = cursor.fetchall()
     if result:
         return True
     return False
Ejemplo n.º 3
0
 def create(self, request, *args, **kwargs):
     serializer = self.serializer_class(data=request.data)
     serializer.is_valid(raise_exception=True)
     data = serializer.validated_data
     if data['schema'] not in self.specific_schema:
         return Response(status=status.HTTP_400_BAD_REQUEST,
                         data={'detail': 'schema 不在指定的数据库中'})
     with call_mysql.OpsMysqlClient(username=local_mysql_conf['username'],
                                    password=local_mysql_conf['password'],
                                    port=local_mysql_conf['port'],
                                    host=local_mysql_conf['host'],
                                    schema=data['schema']) as cursor:
         cursor.execute(data['query'])
         result_set = cursor.fetchall()
     return Response(result_set)
Ejemplo n.º 4
0
 def destroy(self, request, *args, **kwargs):
     with transaction.atomic():
         app_id = kwargs.get('pk')
         urlooker_id_sql = select_urlooker(app_id=app_id, select_args='id')
         with call_mysql.OpsMysqlClient(username=self.mysql_username,
                                        password=self.mysql_password,
                                        host=self.mysql_host,
                                        port=self.mysql_port,
                                        schema='urlooker') as cursor:
             cursor.execute(urlooker_id_sql)
             result_set = cursor.fetchall()
             falcon_endpoints = [f'''urlooker_{result['id']}''' for result in result_set]
         # 删除falcon graph 信息
         del_endpoint_to_graph(falcon_endpoints)
         sql_list = delete_urlooker_app_alive(app_id=app_id)
         # 删除 urlooker 信息
         with call_mysql.OpsMysqlClient(username=self.mysql_username,
                                        password=self.mysql_password,
                                        host=self.mysql_host,
                                        port=self.mysql_port,
                                        schema='urlooker') as cursor:
             for sql in sql_list:
                 cursor.execute(sql)
         return Response(status=status.HTTP_204_NO_CONTENT)
Ejemplo n.º 5
0
 def create(self, request, *args, **kwargs):
     serializer = self.serializer_class(data=request.data)
     serializer.is_valid(raise_exception=True)
     data_list = json.loads(serializer.data['data'])
     ret_list = []
     print(data_list)
     for data_obj in data_list:
         with call_mysql.OpsMysqlClient(
                 username=local_mysql_conf['username'],
                 password=local_mysql_conf['password'],
                 host=local_mysql_conf['host'],
                 port=local_mysql_conf['port'],
                 schema='urlooker'
         ) as cursor:
             sql = select_urlooker_app_alive(app_id=data_obj['app_id'], environment=data_obj['env'],
                                             check_api=data_obj['check_api'], select_args='max_step')
             cursor.execute(sql)
             result = cursor.fetchone()
             if result is not None:
                 data_obj['is_alarm'] = result['max_step']
             else:
                 data_obj['is_alarm'] = 0
         try:
             app_alive_graph_obj = AppAliveGraph(app_id=data_obj['app_id'],
                                                 environment=data_obj['env'],
                                                 check_api=data_obj['check_api'],
                                                 hours=0.1)
             # 直接获取前一分钟的监控数据
             data_obj['alive_data'] = app_alive_graph_obj.monitor_info()[0]['values'][-2]
             ret_list.append(data_obj)
         except Exception as e:
             data_obj['alive_data'] = {
                 "timestamp": datetime2timestamp(),
                 "value": 2
             }
             ret_list.append(data_obj)
             logger.warning('获取最新app alive 监控数据错误')
             logger.exception(e)
     """ret_dict 数据格式
     {
         "1000": {
             "timestamp": 1557222000,
             "value": 0
         },
         ...
     }
     """
     return Response(ret_list)
Ejemplo n.º 6
0
 def create(self, request, *args, **kwargs):
     with transaction.atomic():
         serializer = self.serializer_class(data=request.data)
         serializer.is_valid(raise_exception=True)
         data = serializer.validated_data
         app_id = data['app_id']
         app_detail_obj = AppDetail.objects.get(id=app_id)
         try:
             insert_sql_list = self.__get_app_alive_api(app_detail_obj)
         except Exception as e:
             logger.exception(e)
             return Response({'detail': 'app 关联信息不完整。'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         with call_mysql.OpsMysqlClient(username=self.mysql_username,
                                        password=self.mysql_password,
                                        host=self.mysql_host,
                                        port=self.mysql_port,
                                        schema='urlooker') as cursor:
             for insert_sql in insert_sql_list:
                 cursor.execute(insert_sql)
         return Response({'code': 0}, status=status.HTTP_201_CREATED)
Ejemplo n.º 7
0
    def __get_urlooker_id(self):
        result = []
        sql = f'''select id, ip, idc, url from strategy
where environment = '{self.env}' and ops_cp_app_id = {self.app_id} and url = '{self.check_api}';'''
        with call_mysql.OpsMysqlClient(username=local_mysql_conf['username'],
                                       password=local_mysql_conf['password'],
                                       host=local_mysql_conf['host'],
                                       port=local_mysql_conf['port'],
                                       schema='urlooker') as cursor:
            cursor.execute(sql)
            result = cursor.fetchall()
        ret_result = {}
        for strategy_obj in result:
            ret_result[strategy_obj['id']] = {
                'ip': strategy_obj['ip'],
                'idc': strategy_obj['idc'],
                'url': strategy_obj['url']
            }

        return ret_result
Ejemplo n.º 8
0
 def update(self, request, *args, **kwargs):
     with transaction.atomic():
         serializer = self.serializer_class(data=request.data)
         serializer.is_valid(raise_exception=True)
         data = serializer.validated_data
         app_id = data.get('app_id', None)
         env = data.get('env', None)
         is_alarm = data.get('is_alarm', None)
         check_api = data.get('check_api', 0)
         sql = update_urlooker_app_alive(app_id=app_id,
                                         environment=env,
                                         check_api=check_api,
                                         update_kwargs=f'max_step = {is_alarm}')
         logger.debug(sql)
         with call_mysql.OpsMysqlClient(username=self.mysql_username,
                                        password=self.mysql_password,
                                        host=self.mysql_host,
                                        port=self.mysql_port,
                                        schema='urlooker') as cursor:
             cursor.execute(sql)
         return Response({'code': 0}, status=status.HTTP_200_OK)