全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

使用Python SDK访问云监控

最近更新时间:2023-01-03 11:58:44

目录


开发前准备

安装KS3 Python SDK

因为用户在查询某个Bucket的监控数据时,需要查询Bucket所在的Region,去云监控对应的服务地址查询相关的监控项,所以需要安装KS3 SDK。详见KS3 SDK for Python使用指南

安装云监控Python SDK

详见云监控 SDK说明

运行环境

支持Python2.6, 2.7 , 3.7 版本。

初始化配置

  1. 用户首先需要在金山云控制台申请安全凭证,安全凭证包括access_key_id和secret_access_key。access_key_id 是用于标识API调用者的身份,secret_access_key是用于加密签名字符串和服务器端验证签名字符串的密钥。secret_access_key 必须严格保管,避免泄露。获取安全凭证方法: 点击查看

  2. 通过文件配置及管理密钥,参考examples内示例:

所在位置: ‘/etc/kscore.cfg’ 或 ‘./.kscore.cfg’ 或 ‘C:\kscore.cfg’

注意: 使用相对路径时,需与运行目录保持一致。

[Credentials]
ks_access_key_id=<your ak>
ks_secret_access_key=<your sk>

注意:配置文件中ak和sk不要加引号

  1. 用户也可以在程序运行时配置:
from kscore.session import get_session
# 密钥
ACCESS_KEY_ID = "<your ak>"
SECRET_ACCESS_KEY = "<your sk>"

s = get_session()
client = s.create_client("monitor", "cn-beijing-6", use_ssl=True, ks_access_key_id=ACCESS_KEY_ID, ks_secret_access_key=SECRET_ACCESS_KEY)

Demo演示

from kscore.session import get_session
import json

if __name__ == "__main__":
    s = get_session()

    # 不同Region的bucket需要设置对应的参数去查询
    # 北京region cn-beijing-6
    # 上海region cn-shanghai-2
    # 香港region cn-hongkong-2
    # 俄罗斯region eu-east-1

    #GetMetricStatistics
    client = s.create_client("monitor", "cn-beijing-6", use_ssl=True)

    #获取一天的标准存储量总量
    m=client.get_metric_statistics(InstanceID="<YourBucketName>",Namespace="KS3",MetricName="ks3.bucket.capacity.total.sd",StartTime="2018-07-03T00:00:05Z",EndTime="2018-07-04T00:00:05Z",Period="86400",Aggregate="Max")
    print json.dumps(m,sort_keys=True,indent=4)

    #获取一天的标准存储量增量
    m=client.get_metric_statistics(InstanceID="<YourBucketName>",Namespace="KS3",MetricName="ks3.bucket.capacity.add.sd",StartTime="2018-03-18T00:00:00Z",EndTime="2018-03-19T00:00:00Z",Period="86400",Aggregate="Max")
    print json.dumps(m,sort_keys=True,indent=4)
    # 获取一天的标准存储量删除量
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.capacity.del.sd",StartTime="2018-03-18T00:00:00Z",EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的低频存储量总量
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.capacity.total.ia",StartTime="2018-03-18T00:00:00Z",EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的低频存储量增量
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.capacity.add.ia",StartTime="2018-03-18T00:00:00Z",EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的低频存储量删除量
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.capacity.del.ia",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的归档存储量总量
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.capacity.total.ar",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的归档存储量增量
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.capacity.add.ar",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的归档存储量删除量
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.capacity.del.ar",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的标准存储流量(外网下载流量+CDN回源流量)
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.flow.down.sd",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的标准存储外网流量(不包含CDN下载量的公网下行流量)
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.flow.onet.down.sd",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的标准存储CDN流量(下载)
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.flow.cdn.down.sd",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的低频存储流量(外网下载流量+CDN回源流量)
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.flow.down.ia",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的低频存储外网流量(不包含CDN下载量的公网下行流量)
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.flow.onet.down.ia",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的低频存储CDN流量(下载)
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.flow.cdn.down.ia",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的归档存储流量(外网下载流量+CDN回源流量)
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.flow.down.ar",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的归档存储外网流量(不包含CDN下载量的公网下行流量)
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.flow.onet.down.ar",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的归档存储CDN流量(下载)
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.flow.cdn.down.ar",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的标准存储的get请求数
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.getcount.sd",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的标准存储的put请求数
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.putcount.sd",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的低频存储的get请求数
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.getcount.ia",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的低频存储的put请求数
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.putcount.ia",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的归档get次数
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.getcount.ar",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的归档put次数
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.putcount.ar",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的低频存储的数据取回量
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.flow.up.ia",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
    # 获取一天的归档存储取回量
    m = client.get_metric_statistics(InstanceID="<YourBucketName>", Namespace="KS3", MetricName="ks3.bucket.flow.up.ar",StartTime="2018-03-18T00:00:00Z", EndTime="2018-03-19T00:00:00Z", Period="86400",Aggregate="Max")
    print json.dumps(m, sort_keys=True, indent=4)
文档导读
纯净模式常规模式

纯净模式

点击可全屏预览文档内容
文档反馈