Openstack Ceilometer samples查询方法比较分析
麹权
2023-12-01
Ceilometer是OpenStack中的一个子项目,能把OpenStack内部发生的几乎所有的事件都收集起来,目前项目中主要使用它来进行性能监控。
Ceilometer samples查询分为简单查询和复杂查询,简单查询是get接口,操作检查,但查询不精确,复杂查询是post查询,查询语法复杂,可以精确查询
一 Ceilometer 简单查询
1.1 接口介绍:
url:
GET /v2/samples/(?P<counter_name>[0-9a-zA-Z\-\_]+)? ###counter_name是具体的统计项,例如cpu_util等
path Parameters:
q (list(Query)) – Filter rules for the samples to be returned.
limit (int) – Maximum number of samples to be returned.
return:
list(Sample)
简单查询接口有二个path可选参数,没有req body。其中 limit是返回值的最大个数,q是过滤规则,该参数是一个list,各规则间是and关系,现详细说明
path参数q 示例:
只返回资源id等于bd9431c1-8d69-4ad3-803a-8d4a6b89fd36的统计sample的q写法
{
"field": "resource_id",
"op": "eq",
"type": "string",
"value": "bd9431c1-8d69-4ad3-803a-8d4a6b89fd36"
}
示例说明:
field 规则针对的字段
Type: unicode
The name of the field to test
op 操作符小于、小于等于、等于、不等于、大于等于、大于
Type: Enum(lt, le, eq, ne, ge, gt)
The comparison operator. Defaults to ‘eq’.
type:value的类型
Type: unicode
The data type of value to compare against the stored data
value:比较值
Type: unicode
The value to compare against the stored data
1.2 Openstack ceilometerclient调用举例:
ceilometer client是Openstack项目提供的和ceilometer组件进行交互的python lib库,里面封装了api接口调用一些函数。
search_cond = [{"field": "resource_id", "op": "eq", "value": '1545fdfd5fsd55'}] 查询条件是一个list.该条件将查询出resource_id='1545fdfd5fsd55'的samples
ret = ceilometer.samples.list('cpu_util', q=search_cond, limit=1)
1.3 特点:
顾名思义简单查询,参数少、规则简单,使用该接口查询出来的结果往往不是太多,就是太少,不能精确的获取需要的数据。
二 Ceilometer 复杂查询
2.1 接口介绍:
Url :POST /v2/query/samples
path Parameters: 无
reqbody:
filter: string 过滤规则
orderby: string 排序参数
limit: int 最大返回数
filter语法:
filter := expression
expression := simple_expression| simple_in_expression | complex_expression
simple_expression := {simple_operator: {field_name: value} }
simple_in_expression := {in: {field_name: [value, value, ...]} }
simple_operator := = | != | < | <= | > | >= | in
complex_expression := {complex_operator: [expression, expression, ...]} | not_expression
not_expression := {not: expression}
complex_operator := and | or
示例:
POST request body of the query:
{
"filter" : "{\"and\":[{\"and\": [{\"=\": {\"counter_name\": \"cpu_util\"} }, {\">\": {\"counter_volume\": 0.23} }, {\"<\": {\"counter_volume\": 0.26} }]}, {\"or\": [{\"and\": [{\">\": {\"timestamp\": \"2013-12-01T18:00:00\"} }, {\"<\": {\"timestamp\": \"2013-12-01T18:15:00\"} }]}, {\"and\": [{\">\": {\"timestamp\": \"2013-12-01T18:30:00\"} }, {\"<\": {\"timestamp\": \"2013-12-01T18:45:00\"} }]}]}]}",
"orderby" : "[{\"counter_volume\": \"ASC\"}, {\"timestamp\": \"DESC\"}]",
"limit" : 4
}
2.2 Openstack ceilometerclient调用举例:
ceilometer client是Openstack项目提供的和ceilometer组件进行交互的python lib库,里面封装了api接口调用一些函数。
q_dict = {
"and": [
{
"and": [
{
"=": {
"counter_name": "cpu_util"
}
},
{
">": {
"counter_volume": 0.23
}
},
{
"<": {
"counter_volume": 0.26
}
}
]
},
{
"or": [
{
"and": [
{
">": {
"timestamp": "2013-12-01T18:00:00"
}
},
{
"<": {
"timestamp": "2013-12-01T18:15:00"
}
}
]
},
{
"and": [
{
">": {
"timestamp": "2013-12-01T18:30:00"
}
},
{
"<": {
"timestamp": "2013-12-01T18:45:00"
}
}
]
}
]
}
]
}
samples = ceilometer.query_samples.query(filter=json.dumps((q_dict)),limit=1)
2.3 特点:
语法复杂,查询精确