python学习

Python学习打call第六十五天:Python获取阿里云产品

2019-04-19  本文已影响2人  暖A暖

1.阿里云服务器

2.通过阿里云官方的Python SDK API获取阿里云监控数据指标

3.安装阿里云Python SDK, 且获取监控数据

确认已经安装了python,然后使用pip命令行安装两个依赖

pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-cms
from aliyunsdkcore import client
from aliyunsdkcms.request.v20180308 import QueryMetricListRequest
import time
from datetime import datetime, timedelta
import json
from pprint import pprint

# 返回最近三分钟的时间戳, 毫秒单位,必须是整数类型
def get_start_timestamp():
    start_datetime = datetime.now() - timedelta(minutes=3)
    start_timestamp = int(start_datetime.timestamp()*1000)
    return start_timestamp

# 初始化客户端,填写ak, secret, region_id
clt = client.AcsClient('LTAIxdvbuz2MD74Z','I2b7O1UJmkjfgHVC4i7Atb8gGXwAbU','cn-shenzhen')
# 初始化请求对象
request = QueryMetricListRequest.QueryMetricListRequest()
# 设置响应类型
request.set_accept_format('json')
# 设置项目
request.set_Project('acs_ecs_dashboard')
# 设置获取的指标的key, 这里是CPU的使用率
request.set_Metric('CPUUtilization')
# 获取3分钟以前的时间戳
start_time = get_start_timestamp()
# 设置起始时间
request.set_StartTime(start_time)
# 设置结束时间
request.set_EndTime(int(datetime.now().timestamp() * 1000))
# 设置实例ID
request.set_Dimensions("{'instanceId':'i-wz98bynewgl7gu3jqqb3'}")
# 添加异常打印
result = clt.do_action_with_exception(request)
# 反序列化
result = json.loads(result)
# 打印结果
pprint(result)
from aliyunsdkcore import client
from aliyunsdkcms.request.v20180308 import QueryMetricListRequest
import time
from datetime import datetime, timedelta
import json
from pprint import pprint

def get_start_timestamp():
    start_datetime = datetime.now() - timedelta(minutes=3)
    start_timestamp = int(start_datetime.timestamp()*1000)
    return start_timestamp
clt = client.AcsClient('LTAIxdvbuz2MD74Z','I2b7O1UJmkjfgHVC4i7Atb8gGXwAbU','cn-shenzhen')
request = QueryMetricListRequest.QueryMetricListRequest()
request.set_accept_format('json')
request.set_Project('acs_ecs_dashboard')

# 设置获取的指标的key, 这里是CPU的使用率
request.set_Metric('DiskReadIOPS')

start_time = get_start_timestamp()
request.set_StartTime(start_time)
request.set_EndTime(int(datetime.now().timestamp() * 1000))
request.set_Dimensions("{'instanceId':'i-wz98bynewgl7gu3jqqb3'}")
result = clt.do_action_with_exception(request)
result = json.loads(result)
pprint(result)

4.获取第三方的监控指标

5.预设监控项参考

参考:https://www.9xkd.com/user/plan-view.html?id=3232777786

上一篇 下一篇

猜你喜欢

热点阅读