目前分類:openstack (3)

瀏覽方式: 標題列表 簡短摘要

 

 

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)

 

 

 

 

 

文章標籤

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

使用python撰寫程式後,開始安裝到系統,這中間還是大有學問的。

一般的做法是包成package如下圖,執行python setup install. 你的程式就可以放到OS上指定的目錄,之後就可以用import的方式當作lib使用。

Screen Shot 2014-08-27 at 上午10.06.19  

這方法是寫python程式時大家最常用到的方法。

 

一個大問題是,source code還在系統中,對於你是做一個enterprise的產品來說,這中間還需要一些事情要做

1. 變成deb檔(用ubuntu為範例):

在deploy整個系統時,變成deb檔是能夠很有效管理你的deploy程序

2. 刪除py檔,保留pyc檔

系統中,你的程式碼將會受到保護,不會讓人看到你的Source Code.

 

如何做:

在ubuntu 14.04的server中,安裝所需要的套件

apt-get install -y ruby1.9.3 build-essential

gem install fpm -v 1.1.0

 

除此之外你還需要已經做好的package,如上圖

整個目錄結構與目錄中的檔案可能是這樣

Screen Shot 2014-08-27 at 上午10.06.45  

 

產生一個Makefile如下,並放到主目錄中

#!/usr/bin/make -f

 

NAME=flysatellite

 

all: clean package

 

package:

        fpm -s python -t deb -a amd64 -d python \

        -x '**/*.py' -x '**/test' -x '**/tests' \

        --deb-upstart upstart/fs-flysatellite.upstart \

        --python-package-name-prefix fs \

        setup.py

 

clean:

        rm -rf dist build fs-$(NAME)*.deb usr *.egg-info

 

 

接著你可以執行make

你會看到產生出來的deb檔 fs-flysatellite_0.05_amd64.deb

Screen Shot 2014-08-27 at 上午10.16.43  

很簡單吧

裝看看

dpkg -i fs-flysatellite_0.05_amd64.deb

裝完後看看安裝的目錄為怎樣

Screen Shot 2014-08-27 at 上午10.13.39  

 

完成了

你的source code只剩下 .pyc檔

Screen Shot 2014-08-27 at 上午10.13.52   

opensource 是你最好的朋友

文章標籤

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

更新kernel > 3.10

sudo update-pciids && sudo update-usbids

apt-get purge linux-image-* linux-headers-*

apt-get install linux-image-generic-lts-saucy linux-headers-generic-lts-saucy
uname -a

安裝bcache

add-apt-repository ppa:g2p/storage
apt-get update
apt-get install bcache-tools
make-bcache -C /dev/sdd -B /dev/sdb

其中sdd為ssd或傳統硬碟或memory, sdb 為傳統硬碟。

mkfs.ext4 /dev/bcache0

從新開機

看看 /dev/bcache0是否還在

mount /dev/bcache0 /mnt/testssd

開始試用吧

文章標籤

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