Python怎樣繪制Crushmap分布圖

Python怎樣繪制Crushmap分布圖,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

目前創(chuàng)新互聯(lián)已為超過千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、玉門網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

原理

使用命令ceph report --format=json > crush.json導(dǎo)出json格式數(shù)據(jù)文件,之后使用pydot和graphviz實(shí)現(xiàn)繪圖。

使用說明

安裝依賴

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導(dǎo)出數(shù)據(jù)文件
    2. 每種類型bucket一個(gè)顏色,不夠自己去color_list里面添加,支持最多10級(jí)結(jié)構(gòu)
    3. 生成的文件默認(rèn)問png格式,文件保存在當(dāng)前目錄的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,執(zhí)行以下命令,成功會(huì)生成crushmap.png

user@demo$ python build_crushmap.py crush3.json
Sucessful, File = crushmap.png

看完上述內(nèi)容,你們掌握Python怎樣繪制Crushmap分布圖的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站題目:Python怎樣繪制Crushmap分布圖
本文網(wǎng)址:http://muchs.cn/article10/jojhdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)微信公眾號(hào)、企業(yè)建站外貿(mào)建站、ChatGPT網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司