Пример #1
0
def get_users_stat(s3_servers):
    users_stat = {}
    try:
        rgwAdmin = RGWAdmin(settings.S3_ACCESS, settings.S3_SECRET,
                            s3_servers.pop(0), secure=False) 
        buckets_list = rgwAdmin.get_bucket()
        for bucket in buckets_list:
            try:
                bucket_stat = rgwAdmin.get_bucket(bucket)
                if bucket_stat["owner"] in users_stat:
                    if "rgw.main" in bucket_stat["usage"]:
                        users_stat[
                            bucket_stat["owner"]
                        ][bucket] = bucket_stat["usage"]["rgw.main"]
                    else:
                        users_stat[
                            bucket_stat["owner"]
                        ][bucket] = {}
                else:
                    if "rgw.main" in bucket_stat["usage"]:
                        users_stat[bucket_stat["owner"]] = {
                            bucket: bucket_stat["usage"]["rgw.main"]}
                    else:
                        users_stat[bucket_stat["owner"]] = {
                            bucket: {}}
            except:
                pass
        return users_stat
    except IndexError:
        return users_stat
    except TypeError:
        return get_users_stat(s3_servers)
    except exceptions.ServerDown:
        return get_users_stat(s3_servers)
Пример #2
0
def get_users_stat(s3_server):
    users_stat = defaultdict(lambda: [0, defaultdict(lambda: defaultdict(dict))])
    try:
        rgwAdmin = RGWAdmin(settings.S3_CRED['access_key'],
                            settings.S3_CRED['secret_key'],
                            s3_server, secure=False) 
        buckets_list = rgwAdmin.get_bucket()
        for bucket in buckets_list:
            try:
                bucket_stat = rgwAdmin.get_bucket(bucket)
                username = rgwAdmin.get_user(
                    bucket_stat["owner"]
                )["display_name"]
                
                if ':' in username:
                    group_name = username.split(":")[0].upper()
                else:
                    group_name = "_"
                users_stat[group_name][1][bucket_stat["owner"]][bucket] = (
                    bucket_stat["usage"]["rgw.main"]
                )
                users_stat[group_name][0] += bucket_stat[
                    "usage"
                ]["rgw.main"]["size_kb"]

            except:
                pass
        for i in users_stat:
            users_stat[i][0] *= 1024
            for j in users_stat[i][1]:
                users_stat[i][1][j].default_factory = None
            users_stat[i][1].default_factory = None
        return dict(users_stat)
    except:
        return dict(users_stat)
Пример #3
0
def get_users_stat(s3_servers):
    users_stat = {}
    try:
        rgwAdmin = RGWAdmin(settings.S3_ACCESS,
                            settings.S3_SECRET,
                            s3_servers.pop(0),
                            secure=False)
        buckets_list = rgwAdmin.get_bucket()
        for bucket in buckets_list:
            try:
                bucket_stat = rgwAdmin.get_bucket(bucket)
                if bucket_stat["owner"] in users_stat:
                    if "rgw.main" in bucket_stat["usage"]:
                        users_stat[bucket_stat["owner"]][bucket] = bucket_stat[
                            "usage"]["rgw.main"]
                    else:
                        users_stat[bucket_stat["owner"]][bucket] = {}
                else:
                    if "rgw.main" in bucket_stat["usage"]:
                        users_stat[bucket_stat["owner"]] = {
                            bucket: bucket_stat["usage"]["rgw.main"]
                        }
                    else:
                        users_stat[bucket_stat["owner"]] = {bucket: {}}
            except:
                pass
        return users_stat
    except IndexError:
        return users_stat
    except TypeError:
        return get_users_stat(s3_servers)
    except exceptions.ServerDown:
        return get_users_stat(s3_servers)
Пример #4
0
def updateBucketInfo():
    conn = pymysql.connect("localhost", "root", "PASSWORD", "rgw_info")
    cursor = conn.cursor()

    cursor.execute("CREATE TABLE IF NOT EXISTS user_info(user_id varchar(100) not null primary key ,"
                   "display_name varchar(100) ,max_buckets int ,bucketlist longtext not null,max_size_kb bigint ,"
                   "max_objects int ,user_quota_enable varchar(100) ,user_bucket_quota_enable varchar (100)) ")
    cursor.execute("CREATE TABLE IF NOT EXISTS bucket_info(bucket_id varchar(100) not null primary key ,"
                   " usaged varchar(100) ,usaged_gb int,"
                   "num_objects int ,bucket_max_size_gb int ,max_objects int ,bucket_quota_enable varchar(100) )")
    rgw = RGWAdmin(
        access_key='*******************',
        secret_key='*************************',
        server='**.**.**.**:7480',
        verify=False, secure=False)

    userlist = rgw.get_users()

    for i in range(len(userlist)):
        userinfo = rgw.get_user(uid=userlist[i])
        bucketlist = rgw.get_bucket(uid=userlist[i])
        userquota = rgw.get_user_quota(uid=userlist[i])
        userbucketquota = rgw.get_user_bucket_quota(uid=userlist[i])
        bucketlist_str = ",".join(bucketlist)
        print(bucketlist_str)
        user_sql = "replace into user_info values ('%s','%s','%s','%s','%s','%s','%s','%s') " \
                   % (userinfo['user_id'],
                      userinfo['display_name'],
                      userinfo['max_buckets'],
                      bucketlist_str,
                      userquota['max_size_kb'],
                      userquota['max_objects'],
                      userquota['enabled'],
                      userbucketquota['enabled'])
        cursor.execute(user_sql)
        conn.commit()
        print("userinfo:", userinfo)
        print("bucketlist:", bucketlist)
        for j in range(len(bucketlist)):
            bucketinfo = rgw.get_bucket(bucket=bucketlist[j])
            bucketquota = bucketinfo['bucket_quota']
            try:
                bucket_rgw = bucketinfo['usage']['rgw.main']
                print(bucket_rgw)
            except:
                bucket_rgw = {'size_kb_actual': 0, 'num_objects': 0}
            bucket_sql = "replace into bucket_info values ('%s','%s','%s','%s','%s','%s','%s') " \
                         % (bucketinfo['bucket'],
                            bucket_rgw['size_kb_actual'] / bucketquota['max_size_kb'],
                            bucket_rgw['size_kb_actual'] / 1024 / 1024,
                            bucket_rgw['num_objects'],
                            bucketquota['max_size_kb'] / 1024 / 1024,
                            bucketquota['max_objects'],
                            bucketquota['enabled'])
            cursor.execute(bucket_sql)
            conn.commit()

    conn.close()