您好,登錄后才能下訂單哦!
Python怎樣繪制Crushmap分布圖,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
使用命令ceph report --format=json > crush.json導出json格式數據文件,之后使用pydot和graphviz實現繪圖。
pip install pydot pip install graphviz
# -*- coding: utf-8 -*- import pydot from graphviz import Digraph import json import sys class build_crushmap_graphviz(): """ 1. 使用命令ceph report --format=json > crush.json導出數據文件 2. 每種類型bucket一個顏色,不夠自己去color_list里面添加,支持最多10級結構 3. 生成的文件默認問png格式,文件保存在當前目錄的crushmap.png """ def __init__(self): self.graph = pydot.Dot('ceph_crushmap', graph_type='digraph') self.dot = Digraph(comment='CrushMap', node_attr={'shape': 'record', 'height': '.1'}) self.dot.graph_attr['size'] = '4096,2160' self.dot.graph_attr['resolution'] = '100' self.dot.graph_attr['bb'] = '0,0,4,8' self.dot.format = 'png' self.color_list = ["maroon", "pink", "khaki", "orange", "purple", "yellow", "cyan", "beige", "red"] self.save_name = "crushmap" def build(self, crushmap_file): try: with open(crushmap_file) as data_file: data = json.load(data_file) for i in range(len(data['crushmap']['devices'])): self.dot.node(str(data['crushmap']['devices'][i]['id']), 'device: ' + data['crushmap']['devices'][i]['name'], {'style': 'filled', 'fillcolor': 'green'}) tmp_list = [] color_dict = {} for i in range(len(data['crushmap']['buckets'])): if data['crushmap']['buckets'][i]['type_name'] in tmp_list: color_ = color_dict[data['crushmap']['buckets'][i]['type_name']] else: tmp_list.append(data['crushmap']['buckets'][i]['type_name']) color_ = self.color_list.pop() color_dict[data['crushmap']['buckets'][i]['type_name']] = color_ self.dot.node(str(data['crushmap']['buckets'][i]['id']), data['crushmap']['buckets'][i]['type_name'] + ': ' + data['crushmap']['buckets'][i]['name'], {'style': 'filled', 'fillcolor': color_}) edges_list = [] for i in range(len(data['crushmap']['buckets'])): for j in range(len(data['crushmap']['buckets'][i]['items'])): self.dot.edge(str(data['crushmap']['buckets'][i]['id']), str(data['crushmap']['buckets'][i]['items'][j]['id'])) edges_list.append( str(data['crushmap']['buckets'][i]['id']) + str(data['crushmap']['buckets'][i]['items'][j]['id'])) self.dot.render(self.save_name) print "Sucessful, File = {}.{}".format(self.save_name,self.dot.format) except: print "Faild!" if __name__ == '__main__': file_path = sys.argv[1] crush_make = build_crushmap_graphviz() crush_make.build(file_path)
保存上面的腳本為build_crushmap.py,執行以下命令,成功會生成crushmap.png
user@demo$ python build_crushmap.py crush3.json Sucessful, File = crushmap.png
看完上述內容,你們掌握Python怎樣繪制Crushmap分布圖的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。