コード例 #1
0
def metric(instance_ids,
           period=300,
           name="CPUUtilization",
           namespace="acs_ecs_dashboard"):
    r = DescribeMetricListRequest()
    r.set_Namespace(namespace)
    r.set_MetricName(name)
    r.set_Dimensions([{"instanceId": iid} for iid in instance_ids])
    r.set_Period(period)
    return r
コード例 #2
0
ファイル: rds_metrics.py プロジェクト: gaotuan/SQL-Manager
def rds_metrics():
    print("begin rds_metrics: " +
          datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))  # 日期格式化
    # 这两行很重要,用来寻找项目根目录,os.path.dirname要写多少个根据要运行的python文件到根目录的层数决定
    # BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    # sys.path.append(BASE_DIR)

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settingConf.settings')
    django.setup()
    from core.models import CloudAccount, MetricMetaList, MetricList, grained

    try:
        accs = CloudAccount.objects.filter(is_delete='0').values(
            'access_key_id', 'access_key_secret')
        metrics = MetricMetaList.objects.filter(is_delete='0').values(
            'MetricName', 'Namespace')
        start_time = (int(round(time.time() * 1000)) - 5 * 60 * 1000)

        for acc in accs:
            client = AcsClient(acc.get('access_key_id'),
                               acc.get('access_key_secret'))
            request = DescribeMetricListRequest()
            request.set_accept_format('json')

            for metric in metrics:
                try:
                    request.set_MetricName(metric.get('MetricName'))
                    request.set_Namespace(metric.get('Namespace'))
                    request.set_StartTime(start_time)

                    response = client.do_action_with_exception(request)
                    res = json.loads(
                        json.loads(str(response,
                                       encoding="utf-8"))['Datapoints'])
                    # res = json.loads(json.loads(response.decode('utf8'))['Datapoints'])
                    instances = []
                    for r in res:
                        r['MetricName'] = metric.get('MetricName')
                        r['timestamp'] = round(r['timestamp'] / 1000)
                        MetricList.objects.create(**r)
                    #     instances.append(MetricList(**r))
                    # # MetricList.objects.bulk_create(instances, batch_size=None,ignore_conflicts=True)
                    # MetricList.objects.bulk_create(instances)
                except Exception as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
        print("done  rds_metrics: " +
              datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))  # 日期格式化
    except Exception as e:
        CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
コード例 #3
0
ファイル: iciclealiyun.py プロジェクト: BarryCui/cmp
 def fetch_bucket_usage(self):
     """
     返回该账号下的所有bucket容量信息的列表。
     获取bucket容量方法:
     使用reversed方法倒序遍历列表,把各种存储类型的使用量相加,每种存储类型只能加一次
     最终得到一个bucket的总容量/使用量
     """
     client = self.connect()
     request = DescribeMetricListRequest()
     request.set_accept_format('json')
     request.set_Namespace("acs_oss")
     request.set_MetricName("MeteringStorageUtilization")
     response = client.do_action_with_exception(request)
     list = ast.literal_eval(
         json.loads(str(response, encoding='utf-8'))['Datapoints'])
     return list
コード例 #4
0
 def yesterday_DiskUsed(self,Date,Bucketid):     #获取昨天的容量
     client = AcsClient(self.ak,self.sk,self.domain)
     request = DescribeMetricListRequest()
     request.set_accept_format('json')
     request.set_EndTime(Date + ' 08:00:00')
     request.set_StartTime(Date + ' 07:00:00')
     request.set_Namespace("acs_oss_dashboard")
     request.set_MetricName("MeteringStorageUtilization")
     request.set_Dimensions("{\'BucketName\':'%s'}" % Bucketid)
     response = client.do_action_with_exception(request)
     result = str(response, encoding='utf-8')
     data = json.loads(result)   #str转dict,eval不安全,exec无法转含有true,false字样的内容
     storage = json.loads(data["Datapoints"])    #再转一次
     capacity= storage[0]["Value"]
     return capacity
コード例 #5
0
def getapidata(instanceId, set_MetricName):
    client = AcsClient('LTAI4**********JWM94Wqb', 'sWyYH**********fqMHhRc')
    #此处替换为真实的阿里云API秘钥授权信息
    request = DescribeMetricListRequest()
    request.set_accept_format('json')
    request.set_MetricName(set_MetricName)
    request.set_Namespace("acs_bandwidth_package")
    request.set_Dimensions("{\"instanceId\":\"" + instanceId + "\"}")
    request.set_Length("10")
    response = client.do_action_with_exception(request)
    return (str(response, encoding='utf-8'))
コード例 #6
0
def getapidata(instanceId, set_MetricName, geographicSpanId, localRegionId,
               oppositeRegionId):
    client = AcsClient('LTAI4**********JWM94Wqb', 'sWyYH**********fqMHhRc')
    #此处替换为真实的阿里云API秘钥授权信息
    request = DescribeMetricListRequest()
    request.set_accept_format('json')
    request.set_MetricName(set_MetricName)
    request.set_Namespace("acs_cen")
    if set_MetricName == "InternetOutRateByConnectionRegion":
        request.set_Dimensions("{\"cenId\":\"" + instanceId +
                               "\",\"geographicSpanId\":\"" +
                               geographicSpanId + "\",\"localRegionId\":\"" +
                               localRegionId + "\",\"oppositeRegionId\":\"" +
                               oppositeRegionId + "\"}")
    if set_MetricName == "LatencyByConnectionRegion":
        request.set_Dimensions("{\"src_region_id\":\"" + localRegionId +
                               "\",\"dst_region_id\":\"" + oppositeRegionId +
                               "\"}")
    request.set_Length("10")
    response = client.do_action_with_exception(request)
    return (str(response, encoding='utf-8'))
コード例 #7
0
def get_ali_oss_totle_size(ak, sk, region, bucketname):
    onehours = datetime.timedelta(hours=12)
    starttime = datetime.datetime.now() - onehours

    nowtime = datetime.datetime.now()

    client = AcsClient(ak, sk, region)

    request = DescribeMetricListRequest()
    request.set_accept_format('json')

    request.set_MetricName("MeteringStorageUtilization")
    request.set_Namespace("acs_oss")
    request.set_Period("43200")
    request.set_StartTime(starttime)
    request.set_EndTime(nowtime)
    request.set_Dimensions("{\"BucketName\":" + bucketname + "}")

    response = client.do_action_with_exception(request)

    data_dict = json.loads(response)
    total_size = json.loads(data_dict.get("Datapoints"))
    try:
        if len(total_size) > 0:
            ali_total_size_all = 0
            for i in total_size:
                ali_total_size = round(
                    i.get('MeteringStorageUtilization') / 1024, 3)  # 单位kb
                ali_total_size_all = int(ali_total_size) + int(
                    ali_total_size_all)
            return ali_total_size_all
        else:
            ali_total_size = 0
            return ali_total_size
    except IndexError:
        print "except %s,%s" % (region, bucketname)
        ali_total_size = 0
        return ali_total_size
コード例 #8
0
def get_server_monitor(instance, MetricName, end):
    request = DescribeMetricListRequest()
    request.set_accept_format('json')
    request.set_Namespace('acs_ecs_dashboard')
    request.set_MetricName(MetricName)  # cpu_idle
    request.set_EndTime(str(datetime.now())[:19])
    request.set_StartTime(str(datetime.now() - timedelta(minutes=30))[:19])
    response = client.do_action_with_exception(request)
    response = json.loads(str(response, encoding='utf-8'))
    return ([[
        time.strftime("%Y-%m-%d %H:%M:%S",
                      time.localtime(data['timestamp'] / 1000)),
        data['Minimum'], data['Maximum'], data['Average'], data["instanceId"]
    ] for data in eval(response["Datapoints"])])
コード例 #9
0
def getapidata(instanceId,set_MetricName,vbrInstanceId):
	client = AcsClient('LTAI4**********JWM94Wqb', 'sWyYH**********fqMHhRc')
	#此处替换为真实的阿里云API秘钥授权信息
	request = DescribeMetricListRequest()
	request.set_accept_format('json')
	request.set_MetricName(set_MetricName)
	request.set_Namespace("acs_cen")
	request.set_Dimensions("{\"cenId\":\""+instanceId+"\",\"vbrInstanceId\":\""+vbrInstanceId+"\"}")
	#request.set_Dimensions("{\"cenId\":\"cen-25ju1ijg6i4g0ts26c\",\"vbrInstanceId\":\"vbr-j6c7tx9xr68bc3sm85zaq\"}")
	request.set_Length("10")
	response = client.do_action_with_exception(request)
	return (str(response, encoding='utf-8'))
コード例 #10
0
def GetMetric(inst, name, region, startTime, endTime, period):
    client = AcsClient(AccessKeyId, AccessSecret, region)

    request = DescribeMetricListRequest()
    request.set_accept_format('json')

    request.set_StartTime(startTime)
    request.set_EndTime(endTime)
    request.set_Dimensions("{\"instanceId\":\"" + inst + "\"}")
    request.set_Period(period)
    request.set_Namespace("acs_ecs_dashboard")
    request.set_MetricName(name)

    response = client.do_action_with_exception(request)
    # python2:  print(response)
    res = json.loads(response.decode('utf-8'), encoding="utf-8")

    if res["Success"] == True:
        return json.loads(res["Datapoints"])
コード例 #11
0
ファイル: iciclealiyun.py プロジェクト: BarryCui/cmp
 def fetch_traffic_send(self):
     """
     返回当月bucket的出方向公网流量
     获取某个bucket入方向流量的方法:
     筛选该bucket的InternetSend键对应的值并求和
     """
     client = self.connect()
     request = DescribeMetricListRequest()
     request.set_accept_format('json')
     request.set_Namespace("acs_oss")
     #request.set_StartTime('%s-%s-01 00:00:00' % (year, month))
     request.set_StartTime('%s 00:00:00' % self.start_time)
     #request.set_EndTime('%s 23:59:59' % today)
     request.set_EndTime('%s 23:59:59' % self.end_time)
     request.set_MetricName("InternetSend")
     response = client.do_action_with_exception(request)
     list = ast.literal_eval(
         json.loads(str(response, encoding='utf-8'))['Datapoints'])
     return list
コード例 #12
0
ファイル: iciclealiyun.py プロジェクト: BarryCui/cmp
 def fetch_total_req(self):
     """
     返回所有bucket当月请求数的列表。
     获取当月请求数方法:
     把列表中特定BucketName的每个字典的TotalRequestCount键对应值相加得到当月所有请求数
     """
     client = self.connect()
     request = DescribeMetricListRequest()
     request.set_accept_format('json')
     request.set_Namespace("acs_oss")
     #request.set_StartTime('%s-%s-01 00:00:00' % (year, month))
     request.set_StartTime('%s 00:00:00' % self.start_time)
     #request.set_EndTime('%s 23:59:59' % today)
     request.set_EndTime('%s 23:59:59' % self.end_time)
     request.set_MetricName("TotalRequestCount")
     response = client.do_action_with_exception(request)
     list = ast.literal_eval(
         json.loads(str(response, encoding='utf-8'))['Datapoints'])
     return list
コード例 #13
0
ファイル: tools.py プロジェクト: cuiliang0302/myblog
 def metricInfo(self):
     startTime = (
         datetime.datetime.now() -
         datetime.timedelta(minutes=10)).strftime("%Y-%m-%d %H:%M:%S")
     try:
         print(self.accessKeyId)
         print(self.accessSecret)
         print(self.location)
         print(self.instanceId)
         print(self.metric)
         client = AcsClient(self.accessKeyId, self.accessSecret,
                            self.location)
         request = DescribeMetricListRequest()
         request.set_accept_format('json')
         request.set_StartTime(startTime)
         instance = "{\"instanceId\":\"" + self.instanceId + "\"}"
         request.set_Dimensions(instance)
         request.set_Period("60")
         request.set_Namespace("acs_ecs_dashboard")
         request.set_MetricName(self.metric)
         response = client.do_action_with_exception(request)
         result = str(response, encoding='utf-8')
         result_dict = json.loads(result)
         print(result_dict)
         if result_dict['Code'] == '200':
             data = eval(result_dict['Datapoints'][1:-1])
             return data[0]["Average"]
     except Exception as e:
         print(e)
         value = 0.00
         return value