Example #1
0
 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
Example #2
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
Example #3
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'))
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'))
Example #5
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"])
Example #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'))
Example #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