python获取阿里云CDN带宽.md
目前阿里云CDN有了独立的SDK,因此既可以使用云监控接口,也可以使用CDN接口。
一、阿里云CMS监控接口
from aliyunsdkcore import client
from aliyunsdkcms.request.v20170301 import QueryMetricListRequest
import time
clt = client.AcsClient('ACCESS_KEY','SECRET_KEY','cn-beijing')
request = QueryMetricListRequest.QueryMetricListRequest()
request.set_accept_format('json')
request.set_Project('acs_cdn')
request.set_Metric('BPS')
start_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()-3600))
#start_time = "2016-10-25 10:00:00"
timestamp_start = int(time.mktime(time.strptime(start_time, "%Y-%m-%d %H:%M:%S"))) * 1000
request.set_StartTime(timestamp_start)
request.set_Dimensions("{'instanceId':'js.lenovo.com.cn'}")
request.set_Period('300')
#result = clt.do_action(request)
result = clt.do_action_with_exception(request)
print result
二、阿里云CDN接口
#!/usr/bin/env python
#coding=utf-8
import sys
import time
import json
from aliyunsdkcore import client
from aliyunsdkcdn.request.v20141111 import DescribeCdnMonitorDataRequest
clt = client.AcsClient('ACCESS_KEY','SECRET_KEY','cn-beijing')
request = DescribeCdnMonitorDataRequest.DescribeCdnMonitorDataRequest()
request.set_accept_format('json')
# 注意时间格式,精确到分钟,UTC时间
start_time = time.strftime('%Y-%m-%dT%H:%M:00Z',time.gmtime(time.time()-3600))
end_time = time.strftime('%Y-%m-%dT%H:%M:00Z',time.gmtime(time.time()))
#domain = sys.argv[1]
domain = 'js.lenovo.com.cn'
#时间段不设置,默认取最近24小时数据
request.set_StartTime(start_time)
request.set_EndTime(end_time)
request.set_DomainName(domain)
# 发起请求
response = clt.do_action_with_exception(request)
#print(response)
# json处理,打印
datadict=json.loads(response)
print datadict['MonitorDatas']['CDNMonitorData'][-1]['BytesPerSecond']
三、坑坑坑
时间戳,能戳死个人
有的是YYYY-MM-DD’T’HH:mmZ,如2011-05-30T03:29Z
有的是YYYY-MM-DD’T’HH:mm:SSZ,如2011-05-30T03:29:00Z
文档上写的不一定对,自己尝试才知道
四、参考资料
https://api.aliyun.com/?spm=a2c1g.8271268.10000.2.sKRmRW
转载请注明:IPCPU-网络之路 » python获取阿里云CDN带宽