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