Package ZenUtils :: Module Graphics
[hide private]
[frames] | no frames]

Source Code for Module ZenUtils.Graphics

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13   
14 -class NetworkGraph(object):
15 ''' 16 This class is a wrapper for pydot functionality. It provides a means of 17 graphically representing nodes in a tree. 18 '''
19 - def __init__(self, device=None, node=None, parentName=None):
20 if device: 21 raise NotImplemented 22 self.node = None 23 elif node: 24 self.node = node 25 self.parentName = parentName 26 self.format = 'png'
27
28 - def simpleProcessNodes(self, node=None, parentName=None, doNets=False, 29 doDevices=False):
30 ''' 31 This method processes the child nodes of the passed node, and then its 32 children. It returns a list of tuples where the tuples represent a 33 simple parent-child relationship. 34 ''' 35 if not node: 36 node = self.node 37 if not parentName: 38 parentName = self.parentName 39 edges = [] 40 if doNets and hasattr(node, 'nets'): 41 edges.extend([ (parentName, x.ip) for x in node.nets if x.ip != 'default']) 42 for childNode in node.children: 43 childName = '%s' % (childNode.pj.hostname) 44 if parentName: 45 edges.append((parentName, childName)) 46 moreEdges = self.simpleProcessNodes(childNode, childName, doNets=doNets) 47 edges.extend(moreEdges) 48 return edges
49
50 - def complexProcessNodes(self):
51 ''' 52 This method (will) processes nodes and builds the graphs node at a time 53 with actual pydot objects (as opposed to building a graph from a list 54 of edges). This allows for custom presentation changes based on node 55 attributes. 56 ''' 57 raise NotImplemented
58 59
60 - def setGraphFromEdges(self, edges, directed=True):
61 import os, os.path 62 from Utils import zenPath 63 config = zenPath('lib/graphviz/config') 64 if not os.path.exists(config) or os.path.getsize(config) == 0: 65 os.system(zenPath("bin/dot") + " -c") 66 import pydot 67 graph = pydot.graph_from_edges(edges, directed=directed) 68 graph.ranksep = '1.5' 69 graph.format = self.format 70 graph.bgcolor = '#EEEEEE' 71 # the following don't seem to be working right now 72 graph.fillcolor = '#5A6F8F' 73 graph.fontcolor = '#FFFFFF' 74 graph.fontsize = '10.0' 75 graph.fontname = 'Helvetica' 76 graph.style = 'filled' 77 self.graph = graph
78
79 - def prepare(self, nodeProcessing='simple', edges=None):
80 if nodeProcessing == 'simple': 81 processNodes = self.simpleProcessNodes 82 if not edges: 83 edges = processNodes(doNets=self.withNetworks) 84 self.setGraphFromEdges(edges) 85 else: 86 raise NotImplemented 87 processNodes = self.complexProcessNodes
88 89
90 - def write(self, fdOrPath, format='png'):
91 if hasattr(fdOrPath, 'write'): 92 data = self.graph.create(format=format) 93 fdOrPath.write(data) 94 elif isinstance(fdOrPath, str): 95 self.graph.write(fdOrPath) 96 else: 97 raise TypeError, "Unknown parameter for filename or file handle."
98
99 - def render(self, format='png', withNetworks=False):
100 ''' 101 This will render an image format suitable for display in a browser. 102 ''' 103 self.withNetworks = withNetworks 104 self.prepare() 105 return self.graph.create(format=format)
106 107 ''' 108 scp /tmp/graph_from_edges_dot.svg [email protected]: 109 110 * Give a device name 111 * Find device and get node 112 * input node 113 * output to given file or stdout 114 * turn on/off networks 115 * turn on/off devices 116 * look at nets[0].pingjobs -- should be the list of things to ping on that 117 * network 118 119 processNode should allow one to indicate whether or not networks, devices, 120 etc., should be displayed 121 ''' 122