This topic describes how to use Alibaba Cloud SDK for Java to query the monitoring information and custom data of an Elastic Compute Service (ECS) instance.

Prerequisites

Before you begin, ensure that the following requirements are met:
  • You must have an Alibaba Cloud account and an AccessKey pair (AccessKey ID and AccessKey secret) to use Alibaba Cloud SDK for Java. You can create and view your AccessKey pair on the AccessKey Management page in the Alibaba Cloud Management Console.
  • You have installed Alibaba Cloud SDK for Java. For more information about SDK versions, see Alibaba Cloud SDK.
    <dependencies>
        <! -- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <! -- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-ecs-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ecs</artifactId>
            <version>4.17.4</version>
        </dependency>
    </dependencies>

Sample code

This section shows the sample request code:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526.DescribeInstanceMonitorDataRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeInstanceMonitorDataResponse;
import com.aliyuncs.ecs.model.v20140526.DescribeUserDataRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeUserDataResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.List;

/**
 * DescribeInstanceMonitorData    Queries all monitoring information of an ECS instance.
 * DescribeUserData    Queries the custom data of an ECS instance.
 */
public class Demo{

    // The ID of the corresponding instance.
    private static String instanceId = "i-bp13p7vlcb1uih******";
    // The beginning of the time range to query. Specify the time in the ISO 8601 standard in YYYY-MM-DDThh:mm:ssZ format. It must be in UTC time. If the value of seconds (ss) is not 00, the time is rounded up to the next minute.
    private static String startTime = "2019-11-11T07:00:00Z";
    // The end of the time range to query. Specify the time in the ISO 8601 standard in YYYY-MM-DDThh:mm:ssZ format. It must be in UTC. If the value of seconds (ss) is not 00, the time is rounded up to the next minute.
    private static String endTime = "2019-11-11T07:30:00Z";

    public static void main(String[] args) throws ClientException {

        IAcsClient client = Initialization();
        // Query all monitoring information of an ECS instance.
        List<DescribeInstanceMonitorDataResponse.InstanceMonitorData> monitorDataList = DescribeInstanceMonitorData(client);
        // Process the returned monitoring information. Process the data based on your requirements.
        System.out.println(new Gson().toJson(monitorDataList));
        // Query the custom data of an ECS instance.
        String userData = DescribeUserData(client);
        // Process the returned instance custom data. Process the data based on your requirements.
        System.out.println(userData);

    }

    /**
     * DescribeUserData    Queries the custom data of an ECS instance.
     */
    private static String DescribeUserData(IAcsClient client) {
        DescribeUserDataRequest request = new DescribeUserDataRequest();
        request.setInstanceId(instanceId);
        try {
            DescribeUserDataResponse response = client.getAcsResponse(request);
            System.out.println("--------------------The custom data of the ECS instance was queried.--------------------");
            return response.getUserData();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            return null;
        }
    }

    /**
     * DescribeInstanceMonitorData    Queries all monitoring information of an ECS instance.
     */
    private static List<DescribeInstanceMonitorDataResponse.InstanceMonitorData> DescribeInstanceMonitorData(IAcsClient client) {
        DescribeInstanceMonitorDataRequest request = new DescribeInstanceMonitorDataRequest();
        // The ID of the corresponding instance.
        request.setInstanceId(instanceId);
        // The beginning of the time range to query. Specify the time in the ISO 8601 standard in YYYY-MM-DDThh:mm:ssZ format. It must be in UTC. If the value of seconds (ss) is not 00, the time is rounded up to the next minute.
        request.setStartTime(startTime);
        // The end of the time range to query. Specify the time in the ISO 8601 standard in YYYY-MM-DDThh:mm:ssZ format. It must be in UTC. If the value of seconds (ss) is not 00, the time is rounded up to the next minute.
        request.setEndTime(endTime);
        try {
            DescribeInstanceMonitorDataResponse response = client.getAcsResponse(request);
            System.out.println("--------------------All monitoring information of the ECS instance was queried.--------------------");
            return response.getMonitorData();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            return null;
        }
    }

    /**
     * Initialization  Initializes common request parameters.
     */
    private static IAcsClient Initialization() {
        // Initialize request parameters.
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>", // The ID of the region.
                "<your-access-key-id>", // The AccessKey ID.
                "<your-access-key-secret>"); // The AccessKey secret.
        return new DefaultAcsClient(profile);
    }
}

Output

This section shows the sample success response:
--------------------All monitoring information of the ECS instance was queried.--------------------
{
    "requestId": "8B2933EB-2E12-41D8-A5A2-A6DC23424040", 
    "monitorData": [
        {
            "instanceId": "i-bp13p7vlcb1ui******", 
            "cPU": 8, 
            "intranetRX": 1192, 
            "intranetTX": 720, 
            "intranetBandwidth": 24, 
            "internetRX": 0, 
            "internetTX": 0, 
            "internetBandwidth": 0, 
            "timeStamp": "2019-11-11T07:01:00Z"
        }
    ]
}
--------------------The custom data of the ECS instance is queried.--------------------
IyEvYmluL3No-UserData