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

Source Code for Module Products.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 or (at your 
  8  # option) any later version as published by 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  from ipaddr import IPAddress 
 27   
 28  from AccessControl import ClassSecurityInfo 
 29  from Globals import DTMLFile 
 30  from Globals import InitializeClass 
 31  import zope.interface 
 32  from Products import Zuul 
 33  from Products.Zuul.interfaces import IInfo 
 34  from Products.ZenUtils.jsonutils import json 
 35  from Products.Zuul.utils import allowedRolesAndUsers 
 36  from Products.ZenModel.interfaces import IIndexed 
 37  from Products.ZenModel.Linkable import Layer3Linkable 
 38  from Products.ZenRelations.RelSchema import ToOne, ToMany, ToManyCont 
 39  from Products.ZenUtils.IpUtil import maskToBits, checkip, ipToDecimal, netFromIpAndNet, \ 
 40                                       ipwrap, ipunwrap, ipunwrap_strip 
 41  from Products.ZenModel.Exceptions import WrongSubnetError 
 42  from Products.ZenUtils.IpUtil import numbip 
 43   
 44   
45 -def manage_addIpAddress(context, id, netmask=24, REQUEST = None):
46 """make an IpAddress""" 47 ip = IpAddress(id, netmask) 48 context._setObject(ip.id, ip) 49 if REQUEST is not None: 50 REQUEST['RESPONSE'].redirect(context.absolute_url() 51 +'/manage_main')
52 53 54 addIpAddress = DTMLFile('dtml/addIpAddress',globals()) 55 56
57 -class IpAddress(ManagedEntity, Layer3Linkable):
58 """IpAddress object""" 59 zope.interface.implements(IIndexed) 60 61 event_key = portal_type = meta_type = 'IpAddress' 62 63 default_catalog = 'ipSearch' 64 65 version = 4 66 detailKeys = ('device', 'interface', 'macAddress', 'interfaceDescription') 67 68 _properties = ( 69 {'id':'netmask', 'type':'string', 'mode':'w', 'setter':'setNetmask'}, 70 {'id':'ptrName', 'type':'string', 'mode':'w'}, 71 {'id':'version', 'type':'int', 'mode':'w'}, 72 ) 73 _relations = ManagedEntity._relations + ( 74 ("network", ToOne(ToManyCont,"Products.ZenModel.IpNetwork","ipaddresses")), 75 ("interface", ToOne(ToMany,"Products.ZenModel.IpInterface","ipaddresses")), 76 ("clientroutes", ToMany(ToOne,"Products.ZenModel.IpRouteEntry","nexthop")), 77 ) 78 79 factory_type_information = ( 80 { 81 'id' : 'IpAddress', 82 'meta_type' : 'IpAddress', 83 'description' : """Ip Address Class""", 84 'icon' : 'IpAddress_icon.gif', 85 'product' : 'ZenModel', 86 'factory' : 'manage_addIpAddress', 87 'immediate_view' : 'viewIpAddressOverview', 88 'actions' : 89 ( 90 { 'id' : 'overview' 91 , 'name' : 'Overview' 92 , 'action' : 'viewIpAddressOverview' 93 , 'permissions' : ( "View", ) 94 }, 95 ) 96 }, 97 ) 98 99 security = ClassSecurityInfo() 100
101 - def __init__(self, id, netmask=24):
102 checkip(id) 103 ManagedEntity.__init__(self, ipwrap(id)) 104 ipobj = IPAddress(ipunwrap_strip(id)) 105 if ipobj.version == 6: 106 # No user-definable subnet masks for IPv6 107 netmask = 64 108 self._netmask = maskToBits(netmask) 109 self.ptrName = None 110 self.title = ipunwrap(id) 111 self.version = ipobj.version
112
113 - def setPtrName(self):
114 try: 115 data = socket.gethostbyaddr(ipunwrap(self.id)) 116 if data: self.ptrName = data[0] 117 except socket.error, e: 118 self.ptrName = "" 119 log.warn("%s: %s", self.title, e)
120 121 security.declareProtected('View', 'primarySortKey')
122 - def primarySortKey(self):
123 """ 124 Make sure that networks sort correctly 125 """ 126 return ipToDecimal(self.id)
127
128 - def setNetmask(self, value):
129 self._netmask = maskToBits(value)
130
131 - def _setPropValue(self, id, value):
132 """ 133 Override from PerpertyManager to handle checks and IP creation 134 """ 135 self._wrapperCheck(value) 136 if id == 'netmask': 137 self.setNetmask(value) 138 else: 139 setattr(self,id,value)
140
141 - def __getattr__(self, name):
142 if name == 'netmask': 143 return self._netmask 144 else: 145 raise AttributeError( name )
146 147 security.declareProtected('Change Device', 'setIpAddress')
148 - def setIpAddress(self, ip):
149 """ 150 Set the IP address. Use the format 1.1.1.1/24 to also set the netmask 151 """ 152 iparray = ip.split("/") 153 if len(iparray) > 1: 154 ip = iparray[0] 155 self._netmask = maskToBits(iparray[1]) 156 checkip(ip) 157 aqself = self.primaryAq() #set aq path 158 network = aqself.aq_parent 159 netip = netFromIpAndNet(ip, network.netmask) 160 if netip == network.id: 161 network._renameObject(aqself.id, ipwrap(ip)) 162 else: 163 raise WrongSubnetError( 164 "IP %s is in a different subnet than %s" % (ipunwrap(ip), ipunwrap(self.id)) )
165 166 security.declareProtected('View', 'getIp')
167 - def getIp(self):
168 """ 169 Return only the IP address 170 """ 171 return ipunwrap(self.id)
172 173 security.declareProtected('View', 'getIpAddress')
174 - def getIpAddress(self):
175 """ 176 Return the IP with its netmask in the form 1.1.1.1/24 177 """ 178 return ipunwrap(self.id) + "/" + str(self._netmask)
179
180 - def __str__(self):
181 return self.getIpAddress()
182 183 security.declareProtected('View', 'getInterfaceName')
184 - def getInterfaceName(self):
185 if self.interface(): 186 return self.interface().name() 187 return "No Interface"
188 189 security.declareProtected('View', 'getDeviceName')
190 - def getDeviceName(self):
191 if self.interface(): 192 return self.device().titleOrId() 193 return "No Device"
194 195 security.declareProtected('View', 'getNetworkName')
196 - def getNetworkName(self):
197 if self.network(): 198 return self.network().getNetworkName() 199 return "No Network"
200
201 - def getInterfaceDescription(self):
202 """ 203 Used for indexing 204 """ 205 if self.interface(): 206 return self.interface().description
207
208 - def getInterfaceMacAddress(self):
209 """ 210 Used for indexing 211 """ 212 if self.interface(): 213 return self.interface().macaddress
214 215 security.declareProtected('View', 'getNetworkUrl')
216 - def getNetworkUrl(self):
217 if self.network(): 218 return self.network().absolute_url() 219 return ""
220 221 security.declareProtected('View', 'getDeviceUrl')
222 - def getDeviceUrl(self):
223 """ 224 Get the primary URL path of the device to which this IP 225 is associated. If no device return the URL to the IP itself. 226 """ 227 d = self.device() 228 if d: 229 return d.getPrimaryUrlPath() 230 else: 231 return self.getPrimaryUrlPath()
232
233 - def device(self):
234 """ 235 Return the device for this IP 236 """ 237 iface = self.interface() 238 if iface: return iface.device() 239 return None
240
241 - def index_object(self, idxs=None):
242 super(IpAddress, self).index_object(idxs) 243 self.index_links()
244
245 - def unindex_object(self):
246 self.unindex_links() 247 super(IpAddress, self).unindex_object()
248
249 - def deviceId(self):
250 """ 251 The device id, for indexing purposes. 252 """ 253 d = self.device() 254 if d: return d.id 255 else: return None
256
257 - def interfaceId(self):
258 """ 259 The interface id, for indexing purposes. 260 """ 261 i = self.interface() 262 if i: return i.id 263 else: return None
264
265 - def ipAddressId(self):
266 """ 267 The ipAddress id, for indexing purposes. 268 """ 269 return self.getPrimaryId()
270
271 - def networkId(self):
272 """ 273 The network id, for indexing purposes. 274 """ 275 n = self.network() 276 if n: return n.getPrimaryId() 277 else: return None
278
279 - def allowedRolesAndUsers(self):
280 """ 281 Returns all the users and groups that 282 have permission for this ip address. 283 Used for indexing. 284 """ 285 return allowedRolesAndUsers(self)
286
287 - def details(self):
288 dinfo = IInfo(self) 289 fields = self.detailKeys 290 details = dict() 291 for field in fields: 292 details[field] = Zuul.marshal(getattr(dinfo, field), ('name','uid')) 293 return json(details)
294
295 - def ipAddressAsInt(self):
296 ip = self.getIpAddress() 297 if ip: 298 ip = ip.partition('/')[0] 299 return str(numbip(ip))
300 301 InitializeClass(IpAddress) 302