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

Source Code for Module ZenModel.IpAddress

  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__="""IpAddress 
 15   
 16  IpAddress represents a device residing on an IP network. 
 17   
 18  $Id: IpAddress.py,v 1.42 2004/04/15 00:54:14 edahl Exp $""" 
 19   
 20  __version__ = "$Revision: 1.42 $"[11:-2] 
 21   
 22  import socket 
 23  import logging 
 24  log = logging.getLogger("zen.IpAddress") 
 25   
 26  #base classes for IpAddress 
 27  from ManagedEntity import ManagedEntity 
 28   
 29   
 30  from AccessControl import ClassSecurityInfo 
 31  from Globals import DTMLFile 
 32  from Globals import InitializeClass 
 33  from OFS.FindSupport import FindSupport 
 34  from Acquisition import aq_parent 
 35   
 36  from Products.ZenRelations.RelSchema import * 
 37   
 38  from Products.ZenUtils.IpUtil import * 
 39  from Products.ZenUtils.Utils import getObjByPath 
 40   
 41  from Products.ZenModel.Exceptions import * 
 42   
43 -def manage_addIpAddress(context, id, netmask=24, REQUEST = None):
44 """make a IpAddress""" 45 ip = IpAddress(id, netmask) 46 context._setObject(ip.id, ip) 47 if REQUEST is not None: 48 REQUEST['RESPONSE'].redirect(context.absolute_url() 49 +'/manage_main')
50 51
52 -def findIpAddress(context, ip):
53 """find an ip from base. base should be Networks root found through aq""" 54 searchCatalog = context.Networks.ipSearch 55 ret = searchCatalog({'id':ip}) 56 if len(ret) > 1: 57 raise IpAddressConflict, "IP address conflict for IP: %s" % ip 58 if ret: 59 ipobj = getObjByPath(searchCatalog.getPhysicalRoot(), 60 ret[0].getPrimaryId) 61 return ipobj
62 63 64 addIpAddress = DTMLFile('dtml/addIpAddress',globals()) 65 66
67 -class IpAddress(ManagedEntity):
68 """IpAddress object""" 69 event_key = portal_type = meta_type = 'IpAddress' 70 71 default_catalog = 'ipSearch' 72 73 _properties = ( 74 {'id':'netmask', 'type':'string', 'mode':'w', 'setter':'setNetmask'}, 75 {'id':'ptrName', 'type':'string', 'mode':'w'}, 76 ) 77 _relations = ManagedEntity._relations + ( 78 ("network", ToOne(ToManyCont,"Products.ZenModel.IpNetwork","ipaddresses")), 79 ("interface", ToOne(ToMany,"Products.ZenModel.IpInterface","ipaddresses")), 80 ("clientroutes", ToMany(ToOne,"Products.ZenModel.IpRouteEntry","nexthop")), 81 ) 82 83 factory_type_information = ( 84 { 85 'id' : 'IpAddress', 86 'meta_type' : 'IpAddress', 87 'description' : """Ip Address Class""", 88 'icon' : 'IpAddress_icon.gif', 89 'product' : 'ZenModel', 90 'factory' : 'manage_addIpAddress', 91 'immediate_view' : 'viewIpAddressOverview', 92 'actions' : 93 ( 94 { 'id' : 'overview' 95 , 'name' : 'Overview' 96 , 'action' : 'viewIpAddressOverview' 97 , 'permissions' : ( "View", ) 98 }, 99 ) 100 }, 101 ) 102 103 security = ClassSecurityInfo() 104
105 - def __init__(self, id, netmask=24):
106 checkip(id) 107 ManagedEntity.__init__(self, id) 108 self._netmask = maskToBits(netmask) 109 self.ptrName = None
110 #self.setPtrName() 111 112
113 - def setPtrName(self):
114 try: 115 data = socket.gethostbyaddr(self.id) 116 if data: self.ptrName = data[0] 117 except socket.error, e: 118 self.ptrName = "" 119 log.warn("%s: %s", self.id, e)
120 121 122 security.declareProtected('View', 'primarySortKey')
123 - def primarySortKey(self):
124 """make sure that networks sort correctly""" 125 return numbip(self.id)
126 127
128 - def setNetmask(self, value):
129 self._netmask = maskToBits(value)
130
131 - def _setPropValue(self, id, value):
132 """override from PerpertyManager to handle checks and ip creation""" 133 self._wrapperCheck(value) 134 if id == 'netmask': 135 self.setNetmask(value) 136 else: 137 setattr(self,id,value)
138 139
140 - def __getattr__(self, name):
141 if name == 'netmask': 142 return self._netmask 143 else: 144 raise AttributeError, name
145 146 147 security.declareProtected('Change Device', 'setIpAddress')
148 - def setIpAddress(self, ip):
149 """set the ipaddress can be in the form 1.1.1.1/24 to also set mask""" 150 iparray = ip.split("/") 151 if len(iparray) > 1: 152 ip = iparray[0] 153 self._netmask = maskToBits(iparray[1]) 154 checkip(ip) 155 aqself = self.primaryAq() #set aq path 156 network = aqself.aq_parent 157 netip = getnetstr(ip, network.netmask) 158 if netip == network.id: 159 network._renameObject(aqs.id, ip) 160 else: 161 raise WrongSubnetError, \ 162 "Ip %s is in a different subnet than %s" % (ip, self.id)
163 164 165 166 security.declareProtected('View', 'getIp')
167 - def getIp(self):
168 """return only the ip""" 169 return self.id
170 171 172 security.declareProtected('View', 'getIpAddress')
173 - def getIpAddress(self):
174 """return the ip with its netmask in the form 1.1.1.1/24""" 175 return self.id + "/" + str(self._netmask)
176 177
178 - def __str__(self):
179 return self.getIpAddress()
180 181 182 security.declareProtected('View', 'getInterfaceName')
183 - def getInterfaceName(self):
184 if self.interface(): 185 return self.interface().name() 186 return "No Interface"
187 188 189 security.declareProtected('View', 'getNetworkName')
190 - def getNetworkName(self):
191 if self.network(): 192 return self.network().getNetworkName() 193 return "No Network"
194 195 196 security.declareProtected('View', 'getNetworkUrl')
197 - def getNetworkUrl(self):
198 if self.network(): 199 return self.network().absolute_url() 200 return ""
201 202 203 security.declareProtected('View', 'getDeviceUrl')
204 - def getDeviceUrl(self):
205 """Get the primary url path of the device in which this ip 206 is associated with. If no device return url to the ip itself. 207 """ 208 d = self.device() 209 if d: 210 return d.getPrimaryUrlPath() 211 else: 212 return self.getPrimaryUrlPath()
213 214
215 - def device(self):
216 """Reuturn the device for this ip for DeviceResultInt""" 217 int = self.interface() 218 if int: return int.device() 219 return None
220 221
222 - def manage_afterAdd(self, item, container):
223 """ 224 Device only propagates afterAdd if it is the added object. 225 """ 226 super(IpAddress,self).manage_afterAdd(item, container) 227 self.index_object()
228 229
230 - def manage_afterClone(self, item):
231 """Not really sure when this is called.""" 232 super(IpAddress,self).manage_afterClone(item) 233 self.index_object()
234 235
236 - def manage_beforeDelete(self, item, container):
237 """ 238 Device only propagates beforeDelete if we are being deleted or copied. 239 Moving and renaming don't propagate. 240 """ 241 super(IpAddress,self).manage_beforeDelete(item, container) 242 self.unindex_object()
243 244 245 InitializeClass(IpAddress) 246