1
2
3
4
5
6
7
8
9
10
11 __doc__ = """IpAddress
12
13 IpAddress represents a device residing on an IP network.
14 """
15
16 import socket
17 import logging
18 log = logging.getLogger("zen.IpAddress")
19
20
21 from ManagedEntity import ManagedEntity
22
23 from ipaddr import IPAddress
24
25 from AccessControl import ClassSecurityInfo
26 from Globals import DTMLFile
27 from Globals import InitializeClass
28 import zope.interface
29 from Products import Zuul
30 from Products.Zuul.interfaces import IInfo
31 from Products.ZenUtils.jsonutils import json
32 from Products.Zuul.utils import allowedRolesAndUsers
33 from Products.ZenModel.interfaces import IIndexed
34 from Products.ZenModel.Linkable import Layer3Linkable
35 from Products.ZenRelations.RelSchema import ToOne, ToMany, ToManyCont
36 from Products.ZenUtils.IpUtil import maskToBits, checkip, ipToDecimal, netFromIpAndNet, \
37 ipwrap, ipunwrap, ipunwrap_strip
38 from Products.ZenModel.Exceptions import WrongSubnetError
39 from Products.ZenUtils.IpUtil import numbip
40
41
49
50
51 addIpAddress = DTMLFile('dtml/addIpAddress',globals())
52
53
54 -class IpAddress(ManagedEntity, Layer3Linkable):
55 """IpAddress object"""
56 zope.interface.implements(IIndexed)
57
58 event_key = portal_type = meta_type = 'IpAddress'
59
60 default_catalog = 'ipSearch'
61
62 version = 4
63
64 _properties = (
65 {'id':'netmask', 'type':'string', 'mode':'w', 'setter':'setNetmask'},
66 {'id':'ptrName', 'type':'string', 'mode':'w'},
67 {'id':'version', 'type':'int', 'mode':'w'},
68 )
69 _relations = ManagedEntity._relations + (
70 ("network", ToOne(ToManyCont,"Products.ZenModel.IpNetwork","ipaddresses")),
71 ("interface", ToOne(ToMany,"Products.ZenModel.IpInterface","ipaddresses")),
72 ("clientroutes", ToMany(ToOne,"Products.ZenModel.IpRouteEntry","nexthop")),
73 )
74
75 factory_type_information = (
76 {
77 'id' : 'IpAddress',
78 'meta_type' : 'IpAddress',
79 'description' : """Ip Address Class""",
80 'icon' : 'IpAddress_icon.gif',
81 'product' : 'ZenModel',
82 'factory' : 'manage_addIpAddress',
83 'immediate_view' : 'viewIpAddressOverview',
84 'actions' :
85 (
86 { 'id' : 'overview'
87 , 'name' : 'Overview'
88 , 'action' : 'viewIpAddressOverview'
89 , 'permissions' : ( "View", )
90 },
91 )
92 },
93 )
94
95 security = ClassSecurityInfo()
96
108
116
117 security.declareProtected('View', 'primarySortKey')
119 """
120 Make sure that networks sort correctly
121 """
122 return ipToDecimal(self.id)
123
126
128 """
129 Override from PerpertyManager to handle checks and IP creation
130 """
131 self._wrapperCheck(value)
132 if id == 'netmask':
133 self.setNetmask(value)
134 else:
135 setattr(self,id,value)
136
138 if name == 'netmask':
139 return self._netmask
140 else:
141 raise AttributeError( name )
142
143 security.declareProtected('Change Device', 'setIpAddress')
145 """
146 Set the IP address. Use the format 1.1.1.1/24 to also set the netmask
147 """
148 iparray = ip.split("/")
149 if len(iparray) > 1:
150 ip = iparray[0]
151 self._netmask = maskToBits(iparray[1])
152 checkip(ip)
153 aqself = self.primaryAq()
154 network = aqself.aq_parent
155 netip = netFromIpAndNet(ip, network.netmask)
156 if netip == network.id:
157 network._renameObject(aqself.id, ipwrap(ip))
158 else:
159 raise WrongSubnetError(
160 "IP %s is in a different subnet than %s" % (ipunwrap(ip), ipunwrap(self.id)) )
161
162 security.declareProtected('View', 'getIp')
164 """
165 Return only the IP address
166 """
167 return ipunwrap(self.id)
168
169 security.declareProtected('View', 'getIpAddress')
171 """
172 Return the IP with its netmask in the form 1.1.1.1/24
173 """
174 return ipunwrap(self.id) + "/" + str(self._netmask)
175
178
179 security.declareProtected('View', 'getInterfaceName')
181 if self.interface():
182 return self.interface().name()
183 return "No Interface"
184
185 security.declareProtected('View', 'getDeviceName')
187 if self.interface():
188 return self.device().titleOrId()
189 return "No Device"
190
191 security.declareProtected('View', 'getNetworkName')
193 if self.network():
194 return self.network().getNetworkName()
195 return "No Network"
196
198 """
199 Used for indexing
200 """
201 if self.interface():
202 return self.interface().description
203
205 """
206 Used for indexing
207 """
208 if self.interface():
209 return self.interface().macaddress
210
211 security.declareProtected('View', 'getNetworkUrl')
213 if self.network():
214 return self.network().absolute_url()
215 return ""
216
217 security.declareProtected('View', 'getDeviceUrl')
219 """
220 Get the primary URL path of the device to which this IP
221 is associated. If no device return the URL to the IP itself.
222 """
223 d = self.device()
224 if d:
225 return d.getPrimaryUrlPath()
226 else:
227 return self.getPrimaryUrlPath()
228
230 """
231 Return the device for this IP
232 """
233 iface = self.interface()
234 if iface: return iface.device()
235 return None
236
240
244
246 """
247 The device id, for indexing purposes.
248 """
249 d = self.device()
250 if d: return d.id
251 else: return None
252
254 """
255 The interface id, for indexing purposes.
256 """
257 i = self.interface()
258 if i: return i.id
259 else: return None
260
262 """
263 The ipAddress id, for indexing purposes.
264 """
265 return self.getPrimaryId()
266
268 """
269 The network id, for indexing purposes.
270 """
271 n = self.network()
272 if n: return n.getPrimaryId()
273 else: return None
274
276 ip = self.getIpAddress()
277 if ip:
278 ip = ip.partition('/')[0]
279 return str(numbip(ip))
280
281 InitializeClass(IpAddress)
282