• CLI

sdk
  1. Help Center
  2. CLI
  3. Developer Guide
  4. SDK
  5. JAVA
  6. Cloud Eye Java SDK Demo

Cloud Eye Java SDK Demo

Querying Metrics

You can query the metric list in the system and specify the namespace, metric name, dimension, sorting order, start records, and the maximum number of records to filter the search result.

Use the following code to obtain all metrics of the current tenant:

//set filter option
MetricFilterOptions config = MetricFilterOptions.create();
MetricFilterOptions options = config.dim(new String[]{"instance_id,5b4c1602-fb6d-4f1e-87a8-dcf21d9654ba"});
options.limit(50);
options.order(OrderType.ASC);
options.namespace("SYS.ECS");
options.metricName("network_outgoing_bytes_aggregate_rate");
//get some metric
List<? extends Metric> list2 = osclient.cloudEye().metrics().getList(options);

Parameter

Description

Example Value

namespace

Specifies the namespace, for example, the ECS namespace.

SYS.ECS

metric_name

Specifies the metric name.

disk_read_bytes_rate

dim

Specifies the metric dimension. A maximum of three dimensions are supported, and the dimensions are numbered from 0 in the dim.{i}=key,value format.

AutoScalingGroup,ca3fb7aa-da18-4abc-8206-630cbbb74e14

start

Specifies the paging start value. The format is namespace.metric_name.key:value.

SYS.ECS.cpu_util.instance_id:d9112af5-6913-4f3b-bd0a-3f96711e004d

limit

The value ranges from 0 to 1000 (0 excluded and 1000 included). The default value is 1000.

This parameter is used to limit the number of search results.

50

order

Specifies the sorting order of search results. The value can be asc (ascending order) or desc (descending order). The default value is desc.

desc

Querying the Alarm Rule List

You can query alarm rules and specify the paging parameters to limit the number of search results displayed on a page. You can also set the sorting order of search results.

//set filter option
AlarmFilterOptions config = AlarmFilterOptions.create();
AlarmFilterOptions options = config.limit(5);
options.order(OrderType.ASC);
//get some alarm
List<? extends Alarm> list2 = osclient.cloudEye().alarms().getList(options);

Parameter

Description

Example Value

start

Specifies the first queried alarm to be displayed on a page. The value is alarm_id.

al1498535073312Z27eznaxV

limit

The value ranges from 0 to 100 (0 excluded and 100 included). The default value is 100.

This parameter is used to limit the number of search results.

50

order

Specifies the sorting order of search results. The value can be asc (ascending order) or desc (descending order). The default value is desc.

desc

Querying an Alarm Rule

You can query the alarm rule based on the alarm ID.

//get one alarm
List<? extends Alarm> alarm = osclient.cloudEye().alarms().get(ALARM_ID);

Parameter

Description

Example Value

alarm_id

Specifies the alarm rule ID.

al1498535073312Z27eznaxV

Enabling or Disabling an Alarm Rule

You can enable or disable an alarm rule.

//start one alarm
ActionResponse actionResponse = osclient.cloudEye().alarms().startAlarm(ALARM_ID);
//stop one alarm
ActionResponse actionResponse = osclient.cloudEye().alarms().stopAlarm(ALARM_ID);

Parameter

Description

Example Value

alarm_id

Specifies the alarm rule ID.

al1498535073312Z27eznaxV

Deleting an Alarm Rule

You can delete an alarm rule.

//delete one alarm
ActionResponse actionResponse = osclient.cloudEye().alarms().deleteAlarm(ALARM_ID);

Parameter

Description

Example Value

alarm_id

Specifies the alarm rule ID.

al1498535073312Z27eznaxV

Querying Monitoring Data

You can query the monitoring data at a specified granularity for a specified metric in a specified period of time. You can specify the dimension of data to be queried.

//get one metric data
MetricAggregation metricAggregation = osclient.cloudEye().metricsDatas().get(
"SYS.ECS",
"disk_write_bytes_rate",
new Date(1499134191061l),
new Date(1499137791061l),
Period.REAL_TIME, Filter.AVERAGE,
new String[]{"instance_id,9191673e-6532-483c-86d7-7514d7dc4d0a"});

Parameter

Description

Example Value

namespace

Specifies the namespace, for example, the ECS namespace.

SYS.ECS

metric_name

Specifies the metric name.

disk_read_bytes_rate

from

Specifies the start time of the query. The value is a UNIX timestamp and the unit is ms. Set the value of from to at least one period earlier than the current time. Rollup aggregates the raw data generated within a period to the start time of the period. Therefore, if values of from and to are within a period, the query result will be empty due to the rollup failure. You are advised to set from to be at least one period earlier than the current time. Take the 5-minute period as an example. If it is 10:35 now, the raw data generated between 10:30 and 10:35 will be aggregated to 10:30. Therefore, in this example, if the value of period is 5 minutes, the value of from should be 10:30 or earlier.

NOTE:

Cloud Eye rounds up the value of from based on the granularity required to perform the rollup.

1499134191061l

to

Specifies the end time of the query. The value is a UNIX timestamp and the unit is ms. The value of parameter from must be earlier than that of parameter to.

14991341892581

period

Specifies the data monitoring granularity.

Value range:

  • 1: The data is monitored in real time.
  • 300: The data monitoring granularity is 5 minutes.
  • 1200: The data monitoring granularity is 20 minutes.
  • 3600: The data monitoring granularity is 1 hour.
  • 14400: The data monitoring granularity is 4 hours.
  • 86400: The data monitoring granularity is 1 day.

filter

Specifies the data rollup method.

max, min, average, sum, variance

dim

Specifies the metric dimension. A maximum of three dimensions are supported, and the dimensions are numbered from 0 in the dim.{i}=key,value format.

AutoScalingGroup,ca3fb7aa-da18-4abc-8206-630cbbb74e14

Adding Monitoring Data

You can add one or multiple pieces of monitoring data.

List<CloudEyeMetricData> metrics = new ArrayList<>();
//set dimension
CloudEyeMetricDemension.CloudEyeMetricDemensionBuilder dimBuilder = CloudEyeMetricDemension.builder().name("instance_id").value("33328f02-3814-422e-b688-bfdba93d4050");
CloudEyeMetricDemension dim1 = dimBuilder.build();
List<CloudEyeMetricDemension> dimList = new ArrayList<>();
dimList.add(dim1);
//set namespace, metric name
CloudEyeMetric.CloudEyeMetricBuilder metricBuilder = CloudEyeMetric.builder().namespace("MINE.APP").metricName("test_add_metric_data_1")
.dimensions(dimList);
//set ttl, collect_time,value,unit
CloudEyeMetricData.CloudEyeMetricDataBuilder builder1 = CloudEyeMetricData.builder()
.metric(metricBuilder.build()).ttl(172800).collectTime(new Date()).value(60)
.unit("%");
CloudEyeMetric.CloudEyeMetricBuilder metricBuilder2 = CloudEyeMetric.builder().namespace("MINE.APP").metricName("cpu_util")
.dimensions(dimList);
CloudEyeMetricData.CloudEyeMetricDataBuilder builder2 = CloudEyeMetricData.builder()
.metric(metricBuilder2.build()).ttl(172800).collectTime(new Date()) .value(70).unit("%");
metrics.add(builder1.build());
metrics.add(builder2.build());
//post metric
ActionResponse actionResponse = osclient.cloudEye().metricsDatas().add(metrics);

Parameter

Description

Example Value

metric

Specifies the metric data.

Value in the JSON structure

namespace

Specifies the namespace in the service.item format. service and item each must be a string of 3 to 32 characters starting with a letter and consisting of uppercase letters, lowercase letters, digits, and underscores (_). In addition, service cannot be set to SYS.

ABC.ECS

metric_name

Specifies the metric name, which must be a string of 1 to 64 characters starting with a letter and consisting of uppercase letters, lowercase letters, digits, and underscores (_).

disk_read_bytes_rate

dimensions

Specifies the list of metric dimensions. Each dimension is a JSON object, and its structure is as follows:

dimension.name: specifies the dimension name. The value must be a string of 1 to 32 characters starting with a letter and consisting of uppercase letters, lowercase letters, digits, underscores (_), and hyphens (-).

dimension.value: specifies the dimension value. The value must be a string of 1 to 64 characters starting with a letter and consisting of uppercase letters, lowercase letters, digits, underscores (_), and hyphens (-).

instance_id:33328f02-3814-422e-b688-bfdba93d4050

ttl

Specifies the data validity period. The unit is second. The maximum value is 604,800 seconds. If the validity period expires, the data will be automatically deleted.

172800

collect_time

Specifies the time when the data was collected.

The time is UNIX timestamp (ms) format.

NOTE:

Since there is a latency between the client and the server, the data timestamp to be inserted should be within the period that starts from three days before the current time plus 20s to 10 minutes after the current time minus 20s. In this way, the timestamp will be inserted to the database without being affected by the latency.

1502938466458

value

Specifies the metric value.

60

unit

Specifies the data unit.

B

type

Specifies the data type. The value can only be int or float.

int or float

Querying Quotas

You can query the total number of resource quotas that can be created and the quota usage. Currently, the resource type can be only the alarm rule.

Quota quotas = osclient.cloudEye().quotas().get();