Esempio n. 1
0
def analyze(request):
    """
    利用soar分析SQL
    :param request:
    :return:
    """
    text = request.POST.get('text')
    instance_name = request.POST.get('instance_name')
    db_name = request.POST.get('db_name')
    if not (text and instance_name and db_name):
        result = {"total": 0, "rows": []}
    else:
        soar = Soar()
        soar_test_dsn = SysConfig().get('soar_test_dsn')
        # 目标实例的连接信息
        instance_info = Instance.objects.get(instance_name=instance_name)
        online_dsn = "{user}:{pwd}@{host}:{port}/{db}".format(user=instance_info.user,
                                                              pwd=instance_info.raw_password,
                                                              host=instance_info.host,
                                                              port=instance_info.port,
                                                              db=db_name)
        args = {"report-type": "markdown",
                "query": '',
                "online-dsn": online_dsn,
                "test-dsn": soar_test_dsn,
                "allow-online-as-test": "false"}
        rows = generate_sql(text)
        for row in rows:
            args['query'] = row['sql'].replace('"', '\\"').replace('`', '').replace('\n', ' ')
            cmd_args = soar.generate_args2cmd(args=args, shell=True)
            row['report'] = soar.execute_cmd(cmd_args, shell=True)
        result = {"total": len(rows), "rows": rows}
    return HttpResponse(json.dumps(result, cls=ExtendJSONEncoder, bigint_as_string=True),
                        content_type='application/json')
Esempio n. 2
0
def analyze(request):
    """
    利用soar分析SQL
    :param request:
    :return:
    """
    text = request.POST.get('text')
    instance_name = request.POST.get('instance_name')
    db_name = request.POST.get('db_name')
    if not text:
        result = {"total": 0, "rows": []}
    else:
        soar = Soar()
        if instance_name != '' and db_name != '':
            try:
                instance_info = user_instances(
                    request.user,
                    db_type=['mysql']).get(instance_name=instance_name)
            except Instance.DoesNotExist:
                return JsonResponse({
                    'status': 1,
                    'msg': '你所在组未关联该实例!',
                    'data': []
                })
            soar_test_dsn = SysConfig().get('soar_test_dsn')
            # 获取实例连接信息
            online_dsn = "{user}:{pwd}@{host}:{port}/{db}".format(
                user=instance_info.user,
                pwd=instance_info.password,
                host=instance_info.host,
                port=instance_info.port,
                db=db_name)
        else:
            online_dsn = ''
            soar_test_dsn = ''
        args = {
            "report-type": "markdown",
            "query": '',
            "online-dsn": online_dsn,
            "test-dsn": soar_test_dsn,
            "allow-online-as-test": "false"
        }
        rows = generate_sql(text)
        for row in rows:
            args['query'] = row['sql']
            cmd_args = soar.generate_args2cmd(args=args, shell=True)
            stdout, stderr = soar.execute_cmd(cmd_args,
                                              shell=True).communicate()
            row['report'] = stdout if stdout else stderr
        result = {"total": len(rows), "rows": rows}
    return HttpResponse(json.dumps(result,
                                   cls=ExtendJSONEncoder,
                                   bigint_as_string=True),
                        content_type='application/json')
Esempio n. 3
0
def generate(request):
    """
    解析上传文件为SQL列表
    :param request:
    :return:
    """
    text = request.POST.get('text')
    if text is None:
        result = {"total": 0, "rows": []}
    else:
        rows = generate_sql(text)
        result = {"total": len(rows), "rows": rows}
    return HttpResponse(json.dumps(result, cls=ExtendJSONEncoder, bigint_as_string=True),
                        content_type='application/json')