1
2
3
4
5
6
7
8
9
10
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
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
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
112
120
121 security.declareProtected('View', 'primarySortKey')
123 """
124 Make sure that networks sort correctly
125 """
126 return ipToDecimal(self.id)
127
130
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
142 if name == 'netmask':
143 return self._netmask
144 else:
145 raise AttributeError( name )
146
147 security.declareProtected('Change Device', 'setIpAddress')
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()
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')
168 """
169 Return only the IP address
170 """
171 return ipunwrap(self.id)
172
173 security.declareProtected('View', 'getIpAddress')
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
182
183 security.declareProtected('View', 'getInterfaceName')
185 if self.interface():
186 return self.interface().name()
187 return "No Interface"
188
189 security.declareProtected('View', 'getDeviceName')
191 if self.interface():
192 return self.device().titleOrId()
193 return "No Device"
194
195 security.declareProtected('View', 'getNetworkName')
197 if self.network():
198 return self.network().getNetworkName()
199 return "No Network"
200
202 """
203 Used for indexing
204 """
205 if self.interface():
206 return self.interface().description
207
209 """
210 Used for indexing
211 """
212 if self.interface():
213 return self.interface().macaddress
214
215 security.declareProtected('View', 'getNetworkUrl')
217 if self.network():
218 return self.network().absolute_url()
219 return ""
220
221 security.declareProtected('View', 'getDeviceUrl')
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
234 """
235 Return the device for this IP
236 """
237 iface = self.interface()
238 if iface: return iface.device()
239 return None
240
244
248
250 """
251 The device id, for indexing purposes.
252 """
253 d = self.device()
254 if d: return d.id
255 else: return None
256
258 """
259 The interface id, for indexing purposes.
260 """
261 i = self.interface()
262 if i: return i.id
263 else: return None
264
266 """
267 The ipAddress id, for indexing purposes.
268 """
269 return self.getPrimaryId()
270
272 """
273 The network id, for indexing purposes.
274 """
275 n = self.network()
276 if n: return n.getPrimaryId()
277 else: return None
278
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
294
296 ip = self.getIpAddress()
297 if ip:
298 ip = ip.partition('/')[0]
299 return str(numbip(ip))
300
301 InitializeClass(IpAddress)
302