Python怎样绘制Crushmap分布图

发布时间:2021-12-04 14:40:50 作者:柒染
来源:亿速云 阅读:183

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分布图的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 在python中画正态分布图像的实例
  2. 怎么在python中绘制一个3维正态分布图

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python crushmap

上一篇:Bootstrap中的图片轮播效果怎么实现

下一篇:MySQL命名规范是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》