Help Center> > Developer Guide > Python >CES Python SDK Demo

CES Python 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:

query = {
"namespace": "SYS.ECS",
"metric_name": "cpu_util",
"dimensions": [{
"name": "instance_id",
"value": "d9112af5-6913-4f3b-bd0a-3f96711e004d"
}],
"order": "desc",
"marker": "SYS.ECS.cpu_util.instance_id:9f31d05a-76d5-478a-b864-b1b5e8708482",
"limit": 10
}
# get some metric
metrics = conn.cloud_eye.metrics(**query)
Table 1 Parameter description

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

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 (-).

"dimensions": [{

"name": "instance_id",

"value": "33328f02-3814-422e-b688-bfdba93d4050"

},

{

"name": "server_id",

"value": "57899009-3814-422e-b688-bfdba93d4050"

}

]

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.

The default value is desc.

asc: ascending order

desc: descending order

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.

query = {
"limit": 1,
"marker": "last-alarm-id",
"order": "desc"
}
# get some alarm
for alarm in conn.cloud_eye.alarms(**query):
logging.info(alarm)
Table 2 Parameter description

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.

The default value is desc.

asc: ascending order

desc: descending order

desc

Querying an Alarm Rule

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

# plain ID
alarm = conn.cloud_eye.get_alarm("some-alarm-id")
# Instance with ID
alarm = conn.cloud_eye.get_alarm(alarm.Alarm(id="some-alarm-id"))
Table 3 Parameter description

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 alarm
conn.cloud_eye.enable_alarm("some-alarm-id")
or
conn.cloud_eye.enable_alarm(alarm.Alarm(id="some-alarm-id"))
# stop alarm
conn.cloud_eye.disable_alarm("some-alarm-id")
or
conn.cloud_eye.disable_alarm(alarm.Alarm(id="some-alarm-id"))
Table 4 Parameter description

Parameter

Description

Example Value

alarm_id

Specifies the alarm rule ID.

al1498535073312Z27eznaxV

Deleting an Alarm Rule

You can delete an alarm rule.

conn.cloud_eye.delete_alarm("some-alarm-id")
or
conn.cloud_eye.delete_alarm(alarm.Alarm(id="some-alarm-id"))
Table 5 Parameter description

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.

def get_epoch_time(datetime_):
if datetime_:
seconds = time.mktime(datetime_.timetuple())
return int(seconds) * 1000
else:
return None
now = datetime.datetime.now()
_to = now
_from = now - datetime.timedelta(minutes=5)
query = {
"namespace": "MINE.APP",
"metric_name": "cpu_util",
"from": get_epoch_time(_from),
"to": get_epoch_time(_to),
"period": 300,
"filter": "average",
"dimensions": [{
"name": "instance_id",
"value": "33328f02-3814-422e-b688-bfdba93d4050"
}]
}
for aggregation in conn.cloud_eye.metric_aggregations(**query):
logging.info(aggregation)
Table 6 Parameter description

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

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 (-).

"dimensions": [{

"name": "instance_id",

"value": "33328f02-3814-422e-b688-bfdba93d4050"

},

{

"name": "server_id",

"value": "57899009-3814-422e-b688-bfdba93d4050"

}

]

Adding Monitoring Data

You can add one or multiple pieces of monitoring data.

def get_epoch_time(datetime_):
if datetime_:
seconds = time.mktime(datetime_.timetuple())
return int(seconds) * 1000
else:
return None

now = datetime.datetime.now()
collect_time_1 = now
collect_time_2 = now - datetime.timedelta(minutes=5)
data = [
{
"metric": {
"namespace": "MINE.APP",
"dimensions": [
{
"name": "instance_id",
"value": "33328f02-3814-422e-b688-bfdba93d4050"
}
],
"metric_name": "cpu_util"
},
"ttl": 604800,
"collect_time": get_epoch_time(collect_time_1),
"value": 60,
"unit": "%"
},
{
"metric": {
"namespace": "MINE.APP",
"dimensions": [
{
"name": "instance_id",
"value": "33328f02-3814-422e-b688-bfdba93d4050"
}
],
"metric_name": "cpu_util"
},
"ttl": 604800,
"collect_time": get_epoch_time(collect_time_2),
"value": 70,
"unit": "%"
}
]
conn.cloud_eye.add_metric_data(data)
Table 7 Parameter description

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 (-).

"dimensions": [{

"name": "instance_id",

"value": "33328f02-3814-422e-b688-bfdba93d4050"

},

{

"name": "server_id",

"value": "57899009-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.

quotas = conn.cloud_eye.quotas()
for quota in quotas:
logging.info(quota)