阿里云OSS的Bucket容量大小采集

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

# 获取阿里云云监控中 OSS 中的bucket 的bucket大小

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from datetime import datetime
import json
import oss2
import time
import requests

accessKeyId = "XXXXXXXXXX"
accessSecret = "XXXXXXXXXXXX"


# 接口调用
def QueryMetricList():
    #client = AcsClient(accessKeyId, accessSecret, 'default')
    client = AcsClient(accessKeyId, accessSecret)

    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('metrics.aliyuncs.com')
    request.set_method('POST')
    request.set_version('2018-03-08')
    request.set_action_name('QueryMetricList')

    request.add_query_param('Metric', 'MeteringStorageUtilization')
    request.add_query_param('Project', 'acs_oss')

    response = client.do_action_with_exception(request)

    json_data = str(response, encoding='utf-8')
    oss_data_dict = json.loads(json_data, encoding='utf8')
    return oss_data_dict

# 时间戳转换成标准时间
def get_date(timestamp):
    time_local = time.localtime(timestamp)
    covert_time = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
    return covert_time

# 获取整点时间戳 + 转换为阿里云时间戳
def get_integral_time_timestamp(hour):
    hour_stamp = datetime.now().replace(hour=hour,minute=0, second=0, microsecond=0).timestamp() * 1000
    return hour_stamp

# 字节单位换算
def bytes_conversion(number):
    symbols = ('K','M','G','T','P','E','Z','Y')
    prefix = dict()
    for i,s in enumerate(symbols):
        prefix[s] = 1<<(i+1) *10
    for s in reversed(symbols):
        if int(number) >= prefix[s]:
            value = float(number) / prefix[s]
            return '%.2f%s' %(value,s)
    return "%sB" %number


def get_oss_list():

    oss_data_dict = QueryMetricList()
    oss_data_str=oss_data_dict["Datapoints"]
    oss_data_list = json.loads(oss_data_str,encoding='utf8')
    return oss_data_list


def get_oss_backets(oss_data_list, timestamp, BucketName):
    for oss_bucket in oss_data_list:
        if oss_bucket['timestamp']==timestamp and oss_bucket['BucketName']==BucketName:
            return oss_bucket

def sendmessage(content):
    url = 'https://oapi.dingtalk.com/robot/send?access_token=XXXXXXXXXXXXXXXX'

    HEADERS = {
        "Content-Type": "application/json ;charset=utf-8 "
    }
    message = content
    String_textMsg = {
        "msgtype": "text",
        "text": {"content": message},
    }
    String_textMsg = json.dumps(String_textMsg)
    res = requests.post(url, data=String_textMsg, headers=HEADERS)


def Output(oss_data_list):

    temp = []

    # 采集时间点的时间戳
    current_timestamp = get_integral_time_timestamp(0)
    # 30天前的时间戳   2592000000 = 3600 * 24 * 30 * 1000
    before_timestamp = current_timestamp - 1728000000

    all_bucket_names = [ bucketname['BucketName'] for bucketname in oss_data_list]
    all_bucket_name = list(set(all_bucket_names))

    for bucketname in all_bucket_name:
        data = {}
        current_oss_bucket = get_oss_backets(oss_data_list, current_timestamp, bucketname)
        before_oss_bucket = get_oss_backets(oss_data_list, before_timestamp, bucketname)
        time_interval = ("%s 至 %s" % (get_date(int(current_timestamp / 1000)), get_date(int(before_timestamp / 1000))))
        data["采集时间段"] = time_interval
        data["Bucket"] = bucketname
        MeteringStorageUtilization= current_oss_bucket["MeteringStorageUtilization"] - before_oss_bucket["MeteringStorageUtilization"]
        data["Bucket大小差值"]= bytes_conversion(MeteringStorageUtilization)
        temp.append(data)
    return temp

if __name__ == '__main__':
    oss_data_list=get_oss_list()
    # temp=get_oss_backets(oss_data_list,1546959600000.0, "fhn213")
    # for k,v in temp.items():
    #     print(k,v,type(v))

    temp=Output(oss_data_list)
    print(temp)

    # sendmessage(temp)


posted @ 2019-01-25 12:02  McSiberiaWolf  阅读(1454)  评论(0编辑  收藏  举报