使用Java SDK访问云监控

最近更新时间:2019-01-14 10:10:55

目录


开发前准备

安装KS3 Java SDK

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

安装云监控Python SDK

详见云监控 SDK说明

运行环境

适用于Java 5 以上开发环境

Demo演示

import org.apache.log4j.Logger;

import com.ksc.auth.AWSCredentials;
import com.ksc.auth.BasicAWSCredentials;
import com.ksc.monitor.KSCMonitorClient;
import com.ksc.monitor.model.GetMetricStatisticsRequest;
import com.ksc.monitor.model.GetMetricStatisticsResponse;

/**
 * Created by cutedandan on 2017/7/3.
 */
public class ks3Size {
    private static final Logger log = Logger.getLogger(ks3Size.class);

    public static void main(String[] args){
        String ak = "<your ak>";
        String sk = "<your sk>";

        AWSCredentials credentials = new BasicAWSCredentials(ak, sk);
        KSCMonitorClient client = new KSCMonitorClient(credentials);

        # 不同Region的bucket需要设置对应的参数去查询
        # 北京region monitor.cn-beijing-6.api.ksyun.com
        # 上海region monitor.cn-shanghai-2.api.ksyun.com
        # 杭州region monitor.cn-shanghai-2.api.ksyun.com
        # 香港region monitor.cn-hongkong-2.api.ksyun.com
        # 俄罗斯region monitor.eu-east-1.api.ksyun.com
        client.setEndpoint("monitor.cn-beijing-6.api.ksyun.com");

        String bucket="<YourBucketName>";
        String startTime="2017-09-04T016:00:00Z";
        String endTime="2017-09-05T016:00:00Z";
        //获取标准存储的总量
        GetMetricStatisticsRequest request=new GetMetricStatisticsRequest();
        request.setNamespace("KS3");      //固定设置为KS3
        request.setInstanceId(bucket);   // 设置对应要查询的bucket name
        request.setMetricName("ks3.bucket.capacity.total.sd");  //要查询的参数
        request.setStartTime(startTime);   // 设置起始和截止时间为查询7月2日的数据
        request.setEndTime(endTime);
        request.setPeriod(24*60*60);
        request.setAggregate("Max");
        request.setVersion("2010-05-25");

        GetMetricStatisticsResponse result = client.getMetricStatistics(request);
        String lable = result.getGetMetricStatisticsResult().getLabel();

        System.out.println("result: " + result);
        System.out.println("lable: " + lable);
        System.out.println("Datapoints: " + result.getGetMetricStatisticsResult().getDatapoints().get(0).getMax());

        // 获取一天中标准存储的增量
        request = new GetMetricStatisticsRequest();
        request.setNamespace("KS3");
        request.setInstanceId(bucket);
        request.setMetricName("ks3.bucket.capacity.add.sd");
        request.setStartTime(startTime);
        request.setEndTime(endTime);
        request.setPeriod(24*60*60);
        request.setAggregate("Max");
        request.setVersion("2010-05-25");

        result= client.getMetricStatistics(request);
        lable = result.getGetMetricStatisticsResult().getLabel();

        System.out.println("result: " + result);
        System.out.println("lable: " + lable);
        System.out.println("Datapoints: " + result.getGetMetricStatisticsResult().getDatapoints().get(0).getMax());

        //获取一天中标准存储的删除量
        request = new GetMetricStatisticsRequest();
        request.setNamespace("KS3");
        request.setInstanceId(bucket);
        request.setMetricName("ks3.bucket.capacity.del.sd");
        request.setStartTime(startTime);
        request.setEndTime(endTime);
        request.setPeriod(24*60*60);
        request.setAggregate("Max");
        request.setVersion("2010-05-25");

        result= client.getMetricStatistics(request);
        lable = result.getGetMetricStatisticsResult().getLabel();

        System.out.println("result: " + result);
        System.out.println("lable: " + lable);
        System.out.println("Datapoints: " + result.getGetMetricStatisticsResult().getDatapoints().get(0).getMax());

        System.out.println(result.getGetMetricStatisticsResult().getDatapoints().get(0).getTimestamp());
    }
}

金山云,开启您的云计算之旅

注册有礼