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 import zope.interface
31
32 from Products.ZenModel.interfaces import IIndexed
33 from Products.ZenModel.Linkable import Layer3Linkable
34 from Products.ZenRelations.RelSchema import *
35 from Products.ZenUtils.IpUtil import *
36 from Products.ZenModel.Exceptions import *
37
45
46
47 addIpAddress = DTMLFile('dtml/addIpAddress',globals())
48
49
50 -class IpAddress(ManagedEntity, Layer3Linkable):
51 """IpAddress object"""
52 zope.interface.implements(IIndexed)
53
54 event_key = portal_type = meta_type = 'IpAddress'
55
56 default_catalog = 'ipSearch'
57
58 _properties = (
59 {'id':'netmask', 'type':'string', 'mode':'w', 'setter':'setNetmask'},
60 {'id':'ptrName', 'type':'string', 'mode':'w'},
61 )
62 _relations = ManagedEntity._relations + (
63 ("network", ToOne(ToManyCont,"Products.ZenModel.IpNetwork","ipaddresses")),
64 ("interface", ToOne(ToMany,"Products.ZenModel.IpInterface","ipaddresses")),
65 ("clientroutes", ToMany(ToOne,"Products.ZenModel.IpRouteEntry","nexthop")),
66 )
67
68 factory_type_information = (
69 {
70 'id' : 'IpAddress',
71 'meta_type' : 'IpAddress',
72 'description' : """Ip Address Class""",
73 'icon' : 'IpAddress_icon.gif',
74 'product' : 'ZenModel',
75 'factory' : 'manage_addIpAddress',
76 'immediate_view' : 'viewIpAddressOverview',
77 'actions' :
78 (
79 { 'id' : 'overview'
80 , 'name' : 'Overview'
81 , 'action' : 'viewIpAddressOverview'
82 , 'permissions' : ( "View", )
83 },
84 )
85 },
86 )
87
88 security = ClassSecurityInfo()
89
95
96
97
105
106
107 security.declareProtected('View', 'primarySortKey')
109 """make sure that networks sort correctly"""
110 return numbip(self.id)
111
112
115
117 """override from PerpertyManager to handle checks and ip creation"""
118 self._wrapperCheck(value)
119 if id == 'netmask':
120 self.setNetmask(value)
121 else:
122 setattr(self,id,value)
123
124
126 if name == 'netmask':
127 return self._netmask
128 else:
129 raise AttributeError( name )
130
131
132 security.declareProtected('Change Device', 'setIpAddress')
134 """set the ipaddress can be in the form 1.1.1.1/24 to also set mask"""
135 iparray = ip.split("/")
136 if len(iparray) > 1:
137 ip = iparray[0]
138 self._netmask = maskToBits(iparray[1])
139 checkip(ip)
140 aqself = self.primaryAq()
141 network = aqself.aq_parent
142 netip = getnetstr(ip, network.netmask)
143 if netip == network.id:
144 network._renameObject(aqself.id, ip)
145 else:
146 raise WrongSubnetError(
147 "Ip %s is in a different subnet than %s" % (ip, self.id) )
148
149
150
151 security.declareProtected('View', 'getIp')
153 """return only the ip"""
154 return self.id
155
156
157 security.declareProtected('View', 'getIpAddress')
159 """return the ip with its netmask in the form 1.1.1.1/24"""
160 return self.id + "/" + str(self._netmask)
161
162
165
166
167 security.declareProtected('View', 'getInterfaceName')
169 if self.interface():
170 return self.interface().name()
171 return "No Interface"
172
173
174 security.declareProtected('View', 'getNetworkName')
176 if self.network():
177 return self.network().getNetworkName()
178 return "No Network"
179
180
181 security.declareProtected('View', 'getNetworkUrl')
183 if self.network():
184 return self.network().absolute_url()
185 return ""
186
187
188 security.declareProtected('View', 'getDeviceUrl')
190 """Get the primary url path of the device in which this ip
191 is associated with. If no device return url to the ip itself.
192 """
193 d = self.device()
194 if d:
195 return d.getPrimaryUrlPath()
196 else:
197 return self.getPrimaryUrlPath()
198
199
201 """Reuturn the device for this ip for DeviceResultInt"""
202 int = self.interface()
203 if int: return int.device()
204 return None
205
209
213
215 """
216 The device id, for indexing purposes.
217 """
218 d = self.device()
219 if d: return d.id
220 else: return None
221
223 """
224 The interface id, for indexing purposes.
225 """
226 i = self.interface()
227 if i: return i.id
228 else: return None
229
231 """
232 The ipAddress id, for indexing purposes.
233 """
234 return self.getPrimaryId()
235
237 """
238 The network id, for indexing purposes.
239 """
240 n = self.network()
241 if n: return n.getPrimaryId()
242 else: return None
243
244 InitializeClass(IpAddress)
245