close

 

 

beautiful printing for curl

 

add  | python -m json.tool

 

to check which meters that ceilometer supported.

 

http://zqfan.github.io/assets/doc/ceilometer-havana-api-v2.html

 

to show whole system disk write byte using a statistics

 

curl -H 'X-Auth-Token: 90872f53cf534bf98292693ffe31c1ac' -H 'Content-Type: application/json' http://localhost:8777/v2/meters/disk.write.bytes/statistics

 

you can change disk.write.bytes to

 

http://www.openstack.cn/p861.html

 

such as cpu_util …..

 

take avg or sum

 

curl -H 'X-Auth-Token: 90872f53cf534bf98292693ffe31c1ac' -H 'Content-Type: application/json' http://localhost:8777/v2/meters/cpu_util/statistics?aggregate.func=avg



get resource information

 

curl -H 'X-Auth-Token: 90872f53cf534bf98292693ffe31c1ac' -H 'Content-Type: application/json' http://localhost:8777/v2/resources/f8a524a1-11c2-4620-bffa-bcc8f5014793

 

will show tenant_id ……

 

get project cpu_util information

 

curl -H 'X-Auth-Token: 90872f53cf534bf98292693ffe31c1ac' -H 'Content-Type: application/json'  "http://localhost:8777/v2/meters/cpu_util/statistics?q.field=timestamp&q.op=ge&q.value=2014-08-20T13:34:17&q.field=project_id&q.op=eq&q.value=9aff044df2d44839bd36ee98e08ffa88"

 

query for a instance

 

curl -H 'X-Auth-Token: 90872f53cf534bf98292693ffe31c1ac' -H 'Content-Type: application/json'  "http://localhost:8777/v2/meters/cpu_util/statistics?q.field=timestamp&q.op=ge&q.value=2014-08-20T13:34:17&q.field=resource_id&q.op=eq&q.value=664615f2-c9b2-4c5b-b3df-68e9f5616a1b"



An example for query filter need “   ”

 

curl -X GET -H 'X-Auth-Token: 90872f53cf534bf98292693ffe31c1ac' "http://localhost:8777/v2/meters/instance?q.field=metadata.event_type&q.value=compute.instance.exists"

 

curl -H 'X-Auth-Token: 90872f53cf534bf98292693ffe31c1ac' -H 'Content-Type: application/json'  "http://localhost:8777/v2/meters/cpu_util/statistics?q.field=timestamp&q.op=ge&q.value=2014-08-20T13:34:17"

 

for multiple query:

 

curl -H 'X-Auth-Token: 90872f53cf534bf98292693ffe31c1ac' -H 'Content-Type: application/json'  "http://localhost:8777/v2/meters/cpu_util/statistics?q.field=timestamp&q.op=ge&q.value=2014-08-20T13:34:17&q.field=timestamp&q.op=le&q.value=2014-08-30T13:00:00"

 

simple test in bash

 

token='90872f53cf534bf98292693ffe31c1ac'
curl -H "X-Auth-Token: $token" -H 'Content-Type: application/json' http://localhost:8777/v2/meters/network.incoming.packets/statistics

 

python code to provide you a billing method for a given tenant including statistics of diskio and networkio and cpu usage.

 

from six.moves.urllib import parse
from httplib2 import Http
import json
admin_token='90872f53cf534bf98292693ffe31c1ac'

def build_url(path, q, params=None):
   '''This converts from a list of dicts and a list of params to
      what the rest api needs, so from:
   "[{field=this,op=le,value=34},{field=that,op=eq,value=foo}],
    ['foo=bar','sna=fu']"
   to:
   "?q.field=this&q.op=le&q.value=34&
     q.field=that&q.op=eq&q.value=foo&
     foo=bar&sna=fu"
   '''
   if q:
       query_params = {'q.field': [],
                       'q.value': [],
                       'q.op': [],
                       'q.type': []}

       for query in q:
           for name in ['field', 'op', 'value', 'type']:
               query_params['q.%s' % name].append(query.get(name, ''))

       # Transform the dict to a sequence of two-element tuples in fixed
       # order, then the encoded string will be consistent in Python 2&3.
       new_qparams = sorted(query_params.items(), key=lambda x: x[0])
       path += "?" + parse.urlencode(new_qparams, doseq=True)

       if params:
           for p in params:
               path += '&%s' % p
   elif params:
       path += '?%s' % params[0]
       for p in params[1:]:
           path += '&%s' % p
   return path

def build_path(meter_name, meter_type='statistics'):
   #path ='http://localhost:8777/v2/meters/cpu_util/statistics'
   path ='http://localhost:8777/v2/meters/%s/%s'%(meter_name, meter_type)
   return path
def get_metaname_stat(meta_name, project_id):
   #path = build_path('cpu_util')
   path = build_path(meta_name)
   q=[{"field":"timestamp","op":"ge","value":"2014-09-29T13:34:17","type":"None"},
   {'field':'project_id','op':'eq','value':project_id}]
   full_path= build_url(path,q)

   h = Http()
   headers = {'Content-type': 'application/json','X-Auth-Token':'%s'%admin_token}
   esp, content = h.request("%s"%full_path, headers=headers)
   datas = json.loads(content)

   for data in datas:
       print '%s usage %s for this tenant %s'%(meta_name, data['sum'],project_id)


project_id='5bee4ac21f2844439b76665ba65ee952'
get_metaname_stat('cpu_util', project_id)
get_metaname_stat('disk.write.bytes', project_id)
get_metaname_stat('disk.read.bytes', project_id)
get_metaname_stat('network.incoming.bytes', project_id)
get_metaname_stat('network.outgoing.bytes', project_id)

 

 

 

 

 

arrow
arrow

    太空梭創業日誌 發表在 痞客邦 留言(0) 人氣()