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 or net.netmask == 32: 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=None, filter='/'):
53 """ Depth-first search of the network tree emanating from 54 rootnode, returning (network, device) edges. 55 """ 56 if not pairs: 57 pairs = [] 58 if depth: 59 for node in _get_related(rootnode, filter): 60 sorted = _sortedpair(rootnode, node) 61 pair = [x.id for x in sorted] 62 if pair not in pairs: 63 pairs.append(pair) 64 yield sorted 65 for childnode in _get_related(node, filter): 66 for n in _get_connections( 67 childnode, depth-1, pairs, filter): 68 yield n
69
70 -def get_edges(rootnode, depth=1, withIcons=False, filter='/'):
71 """ Returns some edges """ 72 depth = int(depth) 73 g = _get_connections(rootnode, depth, [], filter) 74 def getColor(node): 75 if node.meta_type=='IpNetwork': 76 return '0xffffff' 77 summary = node.getEventSummary() 78 colors = '0xff0000 0xff8c00 0xffd700 0x00ff00 0x00ff00'.split() 79 color = '0x00ff00' 80 for i in range(5): 81 if summary[i][1]+summary[i][2]>0: 82 color = colors[i] 83 break 84 return color
85 for nodea, nodeb in g: 86 if withIcons: 87 yield ((nodea.id, nodea.getIconPath(), getColor(nodea)), 88 (nodeb.id, nodeb.getIconPath(), getColor(nodeb))) 89 else: 90 yield (nodea.id, nodeb.id) 91 109 110 111 187