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

Source Code for Module ZenUtils.NetworkTree

  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  from Products.ZenModel.Link import ILink 
 15   
16 -def _fromDeviceToNetworks(dev):
17 for iface in dev.os.interfaces(): 18 for ip in iface.ipaddresses(): 19 net = ip.network() 20 if net is None: 21 continue 22 else: 23 yield net
24
25 -def _fromNetworkToDevices(net, devclass):
26 for ip in net.ipaddresses(): 27 dev = ip.device() 28 if dev is None: 29 continue 30 dcp = dev.getDeviceClassPath() 31 if not ( dcp.startswith(devclass) or 32 dcp.startswith('/Network/Router')): 33 continue 34 else: 35 yield dev
36 45
46 -def _sortedpair(x,y):
47 l = [x,y] 48 cmpf = lambda x,y:int(x.meta_type=='Device')-int(y.meta_type=='Device') 49 l.sort(cmpf) 50 return tuple(l)
51
52 -def _get_connections(rootnode, depth=1, pairs=[], filter='/'):
53 """ Depth-first search of the network tree emanating from 54 rootnode, returning (network, device) edges. 55 """ 56 if depth: 57 for node in _get_related(rootnode, filter): 58 sorted = _sortedpair(rootnode, node) 59 pair = [x.id for x in sorted] 60 if pair not in pairs: 61 pairs.append(pair) 62 yield sorted 63 for childnode in _get_related(node, filter): 64 for n in _get_connections( 65 childnode, depth-1, pairs, filter): 66 yield n
67
68 -def get_edges(rootnode, depth=1, withIcons=False, filter='/'):
69 """ Returns some edges """ 70 depth = int(depth) 71 g = _get_connections(rootnode, depth, [], filter) 72 def getColor(node): 73 if node.meta_type=='IpNetwork': 74 return '0xffffff' 75 summary = node.getEventSummary() 76 colors = '0xff0000 0xff8c00 0xffd700 0x00ff00 0x00ff00'.split() 77 color = '0x00ff00' 78 for i in range(5): 79 if summary[i][1]+summary[i][2]>0: 80 color = colors[i] 81 break 82 return color
83 for nodea, nodeb in g: 84 if withIcons: 85 yield ((nodea.id, nodea.getIconPath(), getColor(nodea)), 86 (nodeb.id, nodeb.getIconPath(), getColor(nodeb))) 87 else: 88 yield (nodea.id, nodeb.id) 89 107 108 109 164