@tony-yin
2017-10-10T21:16:40.000000Z
字数 2313
阅读 724
Magicloud
Script
docprofile.py
:提供cprofiler
的装饰器接口
import cProfile
import pstats
import os
def do_cprofile(filename):
"""
Decorator for function profiling.
"""
def wrapper(func):
def profiled_func(*args, **kwargs):
print filename
# Flag for do profiling or not.
DO_PROF = os.getenv("PROFILING")
if DO_PROF:
profile = cProfile.Profile()
profile.enable()
result = func(*args, **kwargs)
profile.disable()
# Sort stat by internal time.
sortby = "cum"
ps = pstats.Stats(profile).sort_stats(sortby)
ps.dump_stats(filename)
else:
result = func(*args, **kwargs)
return result
return profiled_func
return wrapper
create_osd.py
:批量创建osd
的脚本,目前支持批量创建20个或者以下数量的osd
,默认/dev/sda
和/dev/sdb
各10
个,并且journal
分区采用预设分区,分区类型为单一分区,缓存为空,参数较为简单,仅供测试使用,如果有其他需求,需要适当修改代码
使用方法: python create_osd.py osd1 20
// 起始osd, 数量
from mcs3 import storage
import sys
first_osd = sys.argv[1]
num = sys.argv[2]
first_number = int(first_osd[3:])
count = 1
for osd in range(first_number, int(num) + first_number)
sv_name = 'osd' + str(osd)
data_dev = '/dev/sda' + str(osd) if count <=10 else '/dev/sdb' + str(osd - 10)
journal_dev = ''
cache_dev = ''
host = '10.16.100.99'
storage.add_storage_volume_single(sv_name, data_dev, journal_dev, cache_dev, _host=host)
count += 1
enable_osd.py
:admin
层启用osd
的后端脚本,一些参数都是默认设置好的,所以有需要改变的要修改部分hardcode
需要注意的是这段代码不会等待后端所有启用工作执行完毕才结束,而是调用后就直接结束脚本执行了
import mcs3.config
from mcs3.cluster import ClusterManager, IfaceRoleChangeInUse, IfaceRoleNotDefined
from mcs3.license import *
from mcs3.node_management import prepare_enable_osd, enable_osd
from mcs3.remote import TASK_TICKET, ASYNC_MODE
import sys
from docprofile import do_cprofile
@do_cprofile('./enable_osd.out')
def test():
ip = '10.16.100.99'
cluster_if = ''
public_if = 'eth0'
first_osd = sys.argv[1]
num = sys.argv[2]
svs = []
for osd in range(int(first_osd[3:]), int(num)+int(first_osd[3:])):
svs.append('osd' + str(osd))
print svs
mgr = ClusterManager()
all_osds = mgr.list_hosts_in_pool("default")
cluster_ip = mcs3.config.Mcs3CephConfig().get_storage_ip()
lic_str = get_license_key(cluster_ip)
if not lic_str:
lic_str = get_pre_license()
if not lic_str:
raise LicenseNotFound()
set_license_key(lic_str, ip)
prepare_enable_osd(cluster_if, public_if, svs, _host=ip)
enable_osd(svs, True,
_host=ip,
_async=ASYNC_MODE.WAIT_UPDATED,
_ticket=TASK_TICKET.ENABLE_OSD)
if __name__ == '__main__':
test()
pstat.py
:用于处理生成的cprofiler
文件,做一些排序,截取和打印等工作,默认按culativetime
排序,打印前100
条记录,有其他需求的可以适当修改参数
import pstats, sys
file = sys.argv[1]
p = pstats.Stats(file)
p.strip_dirs().sort_stats('cum').print_stats(100)