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
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
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
94
95
96
104
105
106 security.declareProtected('View', 'primarySortKey')
108 """make sure that networks sort correctly"""
109 return numbip(self.id)
110
111
114
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
125 if name == 'netmask':
126 return self._netmask
127 else:
128 raise AttributeError( name )
129
130
131 security.declareProtected('Change Device', 'setIpAddress')
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()
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')
152 """return only the ip"""
153 return self.id
154
155
156 security.declareProtected('View', 'getIpAddress')
158 """return the ip with its netmask in the form 1.1.1.1/24"""
159 return self.id + "/" + str(self._netmask)
160
161
164
165
166 security.declareProtected('View', 'getInterfaceName')
168 if self.interface():
169 return self.interface().name()
170 return "No Interface"
171
172
173 security.declareProtected('View', 'getNetworkName')
175 if self.network():
176 return self.network().getNetworkName()
177 return "No Network"
178
179
180 security.declareProtected('View', 'getNetworkUrl')
182 if self.network():
183 return self.network().absolute_url()
184 return ""
185
186
187 security.declareProtected('View', 'getDeviceUrl')
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
200 """Reuturn the device for this ip for DeviceResultInt"""
201 int = self.interface()
202 if int: return int.device()
203 return None
204
205
212
213
218
219
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
231
235
237 """
238 The device id, for indexing purposes.
239 """
240 d = self.device()
241 if d: return d.id
242 else: return None
243
245 """
246 The interface id, for indexing purposes.
247 """
248 i = self.interface()
249 if i: return i.id
250 else: return None
251
253 """
254 The ipAddress id, for indexing purposes.
255 """
256 return self.getPrimaryId()
257
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