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

Source Code for Module ZenModel.IpInterface

  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__="""IpInterface 
 15   
 16  IpInterface is a collection of devices and subsystems that make 
 17  up a business function 
 18   
 19  $Id: IpInterface.py,v 1.59 2004/04/23 03:01:02 edahl Exp $""" 
 20   
 21  __version__ = "$Revision: 1.59 $"[11:-2] 
 22   
 23  import re 
 24  import copy 
 25  import logging 
 26  log = logging.getLogger("zen.IpInterface") 
 27   
 28  from Globals import Persistent 
 29  from Globals import DTMLFile 
 30  from Globals import InitializeClass 
 31  from Acquisition import aq_parent 
 32  from Acquisition import aq_base 
 33  from App.Dialogs import MessageDialog 
 34  from AccessControl import ClassSecurityInfo 
 35   
 36  from Products.ZenRelations.RelSchema import * 
 37   
 38  from IpAddress import IpAddress, findIpAddress 
 39  from Products.ZenUtils.Utils import localIpCheck, localInterfaceCheck  
 40  from Products.ZenUtils.IpUtil import * 
 41   
 42  from ConfmonPropManager import ConfmonPropManager 
 43  from OSComponent import OSComponent 
 44  from Products.ZenModel.Exceptions import * 
 45   
46 -def manage_addIpInterface(context, id, userCreated, REQUEST = None):
47 """make a device""" 48 d = IpInterface(id) 49 context._setObject(id, d) 50 d = context._getOb(id) 51 d.interfaceName = id 52 if userCreated: d.setUserCreateFlag() 53 if REQUEST is not None: 54 REQUEST['RESPONSE'].redirect(context.absolute_url() 55 +'/manage_main')
56 57 addIpInterface = DTMLFile('dtml/addIpInterface',globals()) 58 59
60 -class IpInterface(OSComponent):
61 """IpInterface object""" 62 63 portal_type = meta_type = 'IpInterface' 64 65 manage_editIpInterfaceForm = DTMLFile('dtml/manageEditIpInterface', 66 globals()) 67 68 # catalog to find interfaces that should be pinged 69 # indexes are id and description 70 #default_catalog = 'interfaceSearch' 71 72 ifindex = '0' 73 interfaceName = '' 74 macaddress = "" 75 type = "" 76 description = "" 77 mtu = 0 78 speed = 0 79 adminStatus = 0 80 operStatus = 0 81 _ipAddresses = [] 82 83 84 _properties = OSComponent._properties + ( 85 {'id':'ips', 'type':'lines', 'mode':'w', 'setter':'setIpAddresses'}, 86 {'id':'interfaceName', 'type':'string', 'mode':'w'}, 87 {'id':'ifindex', 'type':'string', 'mode':'w'}, 88 {'id':'macaddress', 'type':'string', 'mode':'w'}, 89 {'id':'type', 'type':'string', 'mode':'w'}, 90 {'id':'description', 'type':'string', 'mode':'w'}, 91 {'id':'mtu', 'type':'int', 'mode':'w'}, 92 {'id':'speed', 'type':'long', 'mode':'w'}, 93 {'id':'adminStatus', 'type':'int', 'mode':'w'}, 94 {'id':'operStatus', 'type':'int', 'mode':'w'}, 95 ) 96 97 _relations = OSComponent._relations + ( 98 ("os", ToOne(ToManyCont,"Products.ZenModel.OperatingSystem","interfaces")), 99 ("ipaddresses", ToMany(ToOne,"Products.ZenModel.IpAddress","interface")), 100 ("iproutes", ToMany(ToOne,"Products.ZenModel.IpRouteEntry","interface")), 101 ) 102 103 zNoPropertiesCopy = ('ips','macaddress') 104 105 localipcheck = re.compile(r'^127.|^0.').search 106 localintcheck = re.compile(r'^lo0').search 107 108 defaultIgnoreTypes = ('Other', 'softwareLoopback', 'CATV MAC Layer') 109 110 factory_type_information = ( 111 { 112 'id' : 'IpInterface', 113 'meta_type' : 'IpInterface', 114 'description' : """Arbitrary device grouping class""", 115 'icon' : 'IpInterface_icon.gif', 116 'product' : 'ZenModel', 117 'factory' : 'manage_addIpInterface', 118 'immediate_view' : 'viewIpInterface', 119 'actions' : 120 ( 121 { 'id' : 'status' 122 , 'name' : 'Status' 123 , 'action' : 'viewIpInterface' 124 , 'permissions' : ('View',) 125 }, 126 { 'id' : 'perfConf' 127 , 'name' : 'Template' 128 , 'action' : 'objTemplates' 129 , 'permissions' : ("Change Device", ) 130 }, 131 { 'id' : 'viewHistory' 132 , 'name' : 'Modifications' 133 , 'action' : 'viewHistory' 134 , 'permissions' : ('View',) 135 }, 136 ) 137 }, 138 ) 139 140 security = ClassSecurityInfo() 141
142 - def __init__(self, id, title = None):
143 OSComponent.__init__(self, id, title) 144 self._ipAddresses = []
145 146 147 security.declareProtected('View', 'viewName')
148 - def viewName(self):
149 """Use the unmagled interface name for display""" 150 return self.interfaceName.rstrip('\x00') #Bogus fix for MS names
151 name = primarySortKey = viewName 152 153
154 - def _setPropValue(self, id, value):
155 """override from PerpertyManager to handle checks and ip creation""" 156 self._wrapperCheck(value) 157 if id == 'ips': 158 self.setIpAddresses(value) 159 else: 160 setattr(self,id,value)
161 #if id == 'macaddress': self.index_object() 162 163
164 - def manage_editProperties(self, REQUEST):
165 """override from propertiyManager so we can trap errors""" 166 try: 167 return ConfmonPropManager.manage_editProperties(self, REQUEST) 168 except IpAddressError, e: 169 return MessageDialog( 170 title = "Input Error", 171 message = e.args[0], 172 action = "manage_main")
173 174
175 - def __getattr__(self, name):
176 if name == 'ips': 177 return self.getIpAddresses() 178 else: 179 raise AttributeError, name
180 181
182 - def _prepIp(self, ip, netmask=24):
183 """Split ips in the format 1.1.1.1/24 into ip and netmask. 184 Default netmask is 24. 185 """ 186 iparray = ip.split("/") 187 if len(iparray) > 1: 188 ip = iparray[0] 189 checkip(ip) 190 netmask = maskToBits(iparray[1]) 191 return ip, netmask
192 193
194 - def addIpAddress(self, ip, netmask=24):
195 """Add an ip to the ipaddresses relationship on this interface. 196 """ 197 ip, netmask = self._prepIp(ip, netmask) 198 #see if ip exists already and link it to interface 199 ipobj = findIpAddress(self, ip) 200 if ipobj: 201 dev = ipobj.device() 202 if dev and dev != self.device(): 203 log.warn("Adding IP Address %s to %s found it on device %s", 204 ip, self.getId(), dev.getId()) 205 self.ipaddresses.addRelation(ipobj) 206 #never seen this ip make a new one in correct subnet 207 else: 208 ipobj = self.getDmdRoot("Networks").createIp(ip, netmask) 209 self.ipaddresses.addRelation(ipobj)
210 211
212 - def addLocalIpAddress(self, ip, netmask=24):
213 """Add a locally stored ip. Ips like 127./8 are maintained locally. 214 """ 215 (ip, netmask) = self._prepIp(ip, netmask) 216 ip = ip + '/' + str(netmask) 217 if not self._ipAddresses: self._ipAddresses = [] 218 if not ip in self._ipAddresses: 219 self._ipAddresses = self._ipAddresses + [ip,]
220 221
222 - def setIpAddresses(self, ips):
223 """Set a list of ipaddresses in the form 1.1.1.1/24 on to this 224 interface. If networks for the ips don't exist they will be created. 225 """ 226 if type(ips) == type(''): ips = [ips,] 227 if not ips: 228 self.removeRelation('ipaddresses') 229 else: 230 ipids = self.ipaddresses.objectIdsAll() 231 localips = copy.copy(self._ipAddresses) 232 for ip in ips: 233 if localIpCheck(self, ip) or localInterfaceCheck(self, self.id): 234 if not ip in localips: 235 self.addLocalIpAddress(ip) 236 else: 237 localips.remove(ip) 238 else: 239 #if not ipFromIpMask(ip) in ipids: 240 rawip = ipFromIpMask(ip) 241 ipmatch = filter(lambda x: x.find(rawip) > -1, ipids) 242 if not ipmatch: 243 self.addIpAddress(ip) 244 elif len(ipmatch) == 1: 245 ipids.remove(ipmatch[0]) 246 else: 247 pass # THIS WOULD BE BAD!! -EAD 248 249 #delete ips that are no longer in use 250 for ip in ipids: 251 ipobj = self.ipaddresses._getOb(ip) 252 self.removeRelation('ipaddresses', ipobj) 253 for ip in localips: 254 self._ipAddresses.remove(ip)
255 256
257 - def removeIpAddress(self, ip):
258 """Remove an ipaddress from this interface. 259 """ 260 for ipobj in self.ipaddresses(): 261 if ipobj.id == ip: 262 self.ipaddresses.removeRelation(ipobj) 263 return
264 265
266 - def getIp(self):
267 """Return the first ip for this interface in the form: 1.1.1.1. 268 """ 269 if self.ipaddresses.countObjects(): 270 return self.ipaddresses()[0].getIp() 271 elif len(self._ipAddresses): 272 return self._ipAddresses[0].split('/')[0]
273 274
275 - def getIpSortKey(self):
276 """Return the ipaddress as a 32bit integter for sorting purposes. 277 """ 278 if self.ipaddresses.countObjects(): 279 return self.ipaddresses()[0].primarySortKey() 280 elif len(self._ipAddresses): 281 return numbip(self._ipAddresses[0].split('/')[0])
282 283
284 - def getIpAddress(self):
285 """Return the first ipaddress with its netmask ie: 1.1.1.1/24. 286 """ 287 if self.ipaddresses.countObjects(): 288 return self.ipaddresses()[0].getIpAddress() 289 elif len(self._ipAddresses): 290 return self._ipAddresses[0]
291 292
293 - def getIpAddressObj(self):
294 """Return the first real ipaddress object or None if none are found. 295 """ 296 if len(self.ipaddresses()): 297 return self.ipaddresses()[0]
298 299
300 - def getIpAddressObjs(self):
301 """Return a list of the ip objects on this interface.""" 302 retval=[] 303 for ip in self.ipaddresses.objectValuesAll(): 304 retval.append(ip) 305 for ip in self._ipAddresses: 306 retval.append(ip) 307 return retval
308 309
310 - def getIpAddresses(self):
311 """Return list of ip addresses as strings in the form 1.1.1.1/24. 312 """ 313 return map(str, self.getIpAddressObjs())
314 315
316 - def getNetwork(self):
317 """Return the network for the first ip on this interface. 318 """ 319 if self.ipaddresses.countObjects(): 320 return self.ipaddresses()[0].network()
321 322
323 - def getNetworkName(self):
324 """Return the network name for the first ip on this interface. 325 """ 326 net = self.getNetwork() 327 if net: return net.getNetworkName() 328 return ""
329 330 344 345 363 364 365 security.declareProtected('View', 'getInterfaceName')
366 - def getInterfaceName(self):
367 """Return the name of this interface. 368 """ 369 if self.interfaceName: return self.interfaceName 370 elif self.viewName(): return self.viewName() 371 else: return "None"
372 373 security.declareProtected('View', 'getInterfaceMacaddress')
374 - def getInterfaceMacaddress(self):
375 """Return the mac address of this interface. 376 """ 377 return self.macaddress
378 379
380 - def getRRDTemplateName(self):
381 """Return the interface type as the target type name. 382 """ 383 return self.prepId(self.type or "Unknown")
384 385
386 - def getRRDTemplates(self):
387 default = self.getRRDTemplateByName(self.getRRDTemplateName()) 388 if default: 389 return [default] 390 return [self.getRRDTemplateByName('ethernetCsmacd')]
391 392
393 - def snmpIgnore(self):
394 """Ignore interface that are operationally down. 395 """ 396 return self.operStatus > 1
397 398
399 - def niceSpeed(self):
400 if not self.speed: 401 return 'Unknown' 402 speed = self.speed 403 for unit in ('bps', 'Kbps', 'Mbps', 'Gbps'): 404 if speed < 1000: break 405 speed /= 1000 406 return "%.1f%s" % (speed, unit)
407 408
409 - def manage_beforeDelete(self, item, container):
410 """Unindex this interface after it is deleted. 411 """ 412 if (item == self or item == self.device() 413 or getattr(item, "_operation", -1) < 1): 414 OSComponent.manage_beforeDelete(self, item, container)
415 416 417 418 InitializeClass(IpInterface) 419