Package ZenModel :: Module IpRouteEntry
[hide private]
[frames] | no frames]

Source Code for Module ZenModel.IpRouteEntry

  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  __doc__="""RouteEntry 
 15   
 16  RouteEntry represents a group of devices 
 17   
 18  $Id: IpRouteEntry.py,v 1.12 2004/04/12 16:33:15 edahl Exp $""" 
 19   
 20  __version__ = "$Revision: 1.12 $"[11:-2] 
 21   
 22  import re 
 23   
 24  from Globals import DTMLFile 
 25  from Globals import InitializeClass 
 26  from AccessControl import ClassSecurityInfo 
 27   
 28  from Products.ZenUtils.Utils import localIpCheck, prepId 
 29  from Products.ZenRelations.RelSchema import * 
 30   
 31  from IpAddress import findIpAddress 
 32   
 33  from OSComponent import OSComponent 
 34   
 35  import logging 
 36  log = logging.getLogger("zen.IpRouteEntry") 
 37   
38 -def manage_addIpRouteEntry(context, dest, routemask, nexthopid, interface, 39 routeproto, routetype, userCreated=None, REQUEST = None):
40 """make a IpRouteEntry""" 41 if not routemask: 42 routemask = 0 43 else: 44 routemask = int(routemask) 45 dest = '%s/%s' % (dest, routemask) 46 id = prepId(dest) 47 d = IpRouteEntry(id) 48 context._setObject(id, d) 49 d = context._getOb(id) 50 d.setTarget(dest) 51 d.setNextHopIp(nexthopid) 52 d.setInterfaceName(interface) 53 if userCreated: d.setUserCreateFlag() 54 setattr(d, 'routeproto', routeproto) 55 setattr(d, 'routetype', routetype) 56 setattr(d, 'routemask', routemask) 57 58 if REQUEST is not None: 59 REQUEST['RESPONSE'].redirect(context.absolute_url() 60 +'/manage_main')
61 62 addIpRouteEntry = DTMLFile('dtml/addIpRouteEntry',globals()) 63
64 -class IpRouteEntry(OSComponent):
65 """IpRouteEntry object""" 66 67 meta_type = 'IpRouteEntry' 68 69 # we don't monitor routes 70 monitor = False 71 72 _nexthop = "" 73 _target = "" 74 _targetobj = None 75 routetype = "" 76 routeproto = "" 77 routemask = 0 78 routeage = 0 79 metric1 = 0 80 metric2 = 0 81 metric3 = 0 82 metric4 = 0 83 metric5 = 0 84 85 _properties = ( 86 {'id':'routemask', 'type':'string', 'mode':''}, 87 {'id':'nexthopip', 'type':'string', 88 'mode':'', 'setter':'setNextHopIp'}, 89 {'id':'routeproto', 'type':'string', 'mode':''}, 90 {'id':'routeage', 'type':'string', 'mode':''}, 91 {'id':'routetype', 'type':'string', 'mode':''}, 92 {'id':'metric1', 'type':'int', 'mode':''}, 93 {'id':'metric2', 'type':'int', 'mode':''}, 94 {'id':'metric3', 'type':'int', 'mode':''}, 95 {'id':'metric4', 'type':'int', 'mode':''}, 96 {'id':'metric5', 'type':'int', 'mode':''}, 97 ) 98 _relations = OSComponent._relations + ( 99 ("os", ToOne(ToManyCont,"Products.ZenModel.OperatingSystem","routes")), 100 ("interface", ToOne(ToMany,"Products.ZenModel.IpInterface","iproutes")), 101 ("nexthop", ToOne(ToMany,"Products.ZenModel.IpAddress","clientroutes")), 102 ("target", ToOne(ToMany,"Products.ZenModel.IpNetwork","clientroutes")), 103 ) 104 105 security = ClassSecurityInfo() 106 107 ipcheck = re.compile(r'^127\.|^0\.0\.|^169\.254\.|^224\.').search 108 109
110 - def __getattr__(self, name):
111 if name == 'nexthopip': 112 return self.getNextHopIp() 113 else: 114 raise AttributeError, name
115 116 117 security.declareProtected('View', 'getNextHopDeviceLink') 126 127 136 137 138 security.declareProtected('View', 'getNextHopIp')
139 - def getNextHopIp(self):
140 """Return our next hop ip (as string) if stored as object or locally. 141 """ 142 ip = self._nexthop 143 ipobj = self.nexthop() 144 if ipobj: ip = ipobj.id 145 return ip
146 147
148 - def getNextHopDevice(self):
149 """Return the device to which this route points. 150 """ 151 ipobj = self.nexthop() 152 if ipobj: return ipobj.device()
153 154 155 security.declareProtected('View', 'getInterfaceName')
156 - def getInterfaceName(self):
157 """Return the interface name for this route as a string. 158 If no interface is found return 'No Interface'. 159 """ 160 if self.interface(): 161 return self.interface().name() 162 return "No Interface"
163 164 165 security.declareProtected('Change Device', 'setNextHopIp')
166 - def setNextHopIp(self, nextHopIp):
167 """if the nexthop is a 127. or 0. address store locally 168 else link to it in the network hierarchy""" 169 if localIpCheck(self, nextHopIp) or not nextHopIp: 170 self._nexthop = nextHopIp 171 else: 172 ip = findIpAddress(self, nextHopIp) 173 if not ip: 174 netmask = 24 175 int = self.interface() 176 if int: 177 intip = int.getIpAddressObj() 178 if intip: netmask = intip.netmask 179 ip = self.getDmdRoot("Networks").createIp(nextHopIp, netmask) 180 self.addRelation('nexthop', ip)
181 182
183 - def matchTarget(self, ip):
184 """Does this route target match the ip passed. 185 """ 186 if self.target(): return self.target().hasIp(ip)
187 188
189 - def setTarget(self, netip):
190 """Set this route target netip in the form 10.0.0.0/24. 191 """ 192 netid, netmask = netip.split('/') 193 if localIpCheck(self, netip) or netmask == '0': 194 self._target = netip 195 else: 196 net = self.getDmdRoot("Networks").createNet(netip) 197 self.target.addRelation(net)
198 199
200 - def getTarget(self):
201 """Return the route target ie 0.0.0.0/0. 202 """ 203 if self.target(): 204 return self.target().getNetworkName() 205 else: 206 return self._target
207 208
209 - def getTargetIp(self):
210 """Return the target network Ip ie: 10.2.1.0 211 """ 212 return self.getTarget().split("/")[0]
213 214 222 223 224 security.declareProtected('Change Device', 'setInterfaceIndex')
225 - def setInterfaceIndex(self, ifindex):
226 for int in self.os().interfaces(): 227 if int.ifindex == ifindex: break 228 else: 229 int = None 230 if int: self.interface.addRelation(int) 231 else: log.warn("interface index:%s not found", ifindex)
232 233
234 - def getInterfaceIndex(self):
235 int = self.interface() 236 if int: return int.ifindex
237 238 239 security.declareProtected('Change Device', 'setInterfaceName')
240 - def setInterfaceName(self, intname):
241 int = self.os().interfaces._getOb(intname,None) 242 if int: self.interface.addRelation(int) 243 else: log.warn("interface '%s' not found", intname)
244 245
246 - def getInterfaceIp(self):
247 int = self.interface() 248 if int: return int.getIp() 249 return ""
250 251 252 InitializeClass(IpRouteEntry) 253