Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to draw Crushmap Distribution Map by Python

Shulou Source: shulou.com Published: 2022-05-31 22:16:36 10月02日 Update

Python how to draw Crushmap distribution map, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Principle

Use the command ceph report-- format=json > crush.json to export the json format data file, and then use pydot and graphviz to implement the drawing.

Use instructions to install dependent pip install pydotpip install graphviz script code #-*-coding: utf-8-*-import pydotfrom graphviz import Digraphimport jsonimport sysclass build_crushmap_graphviz (): "" 1. Use the command ceph report-- format=json > crush.json to export the data file 2. 0. Each type of bucket one color, not enough to add their own color_list, support up to 10 levels of structure 3. The generated file is in png format by default. The file is saved in the crushmap.png "" def _ _ init__ (self) of the current directory: 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 self.dot.graph_attr 2160 'self.dot.graph_attr [' resolution'] = '100' self.dot.graph_attr [' bb'] = '0pime 4pia 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 (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 (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 (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 (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) use case

Save the above script as build_crushmap.py, execute the following command, and crushmap.png will be generated successfully.

User@demo$ python build_crushmap.py crush3.jsonSucessful, File = crushmap.png after reading the above, have you mastered how Python draws the Crushmap distribution map? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Files commands distribution maps content data methods more formats scripts problems generation success helplessness for this not enough code instructions for use reasons principles for this Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno vpn Shulou Tech Info Apple