1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""IpInterface
15
16 IpInterface is a collection of devices and subsystems that make
17 up a business function
18 """
19
20 import re
21 import copy
22 import logging
23 log = logging.getLogger("zen.IpInterface")
24
25 from Globals import DTMLFile
26 from Globals import InitializeClass
27 from Acquisition import aq_base
28 from App.Dialogs import MessageDialog
29 from AccessControl import ClassSecurityInfo
30
31 from Products.ZenRelations.RelSchema import *
32
33 from Products.ZenUtils.Utils import localIpCheck, localInterfaceCheck
34 from Products.ZenUtils.IpUtil import *
35
36 from ConfmonPropManager import ConfmonPropManager
37 from OSComponent import OSComponent
38 from Products.ZenModel.Exceptions import *
39 from Products.ZenModel.Linkable import Layer2Linkable
40
41 from Products.ZenModel.ZenossSecurity import *
42
55
56 addIpInterface = DTMLFile('dtml/addIpInterface',globals())
57
58
60 """
61 IpInterface object
62 """
63
64 portal_type = meta_type = 'IpInterface'
65
66 manage_editIpInterfaceForm = DTMLFile('dtml/manageEditIpInterface',
67 globals())
68
69
70
71
72
73 ifindex = '0'
74 interfaceName = ''
75 macaddress = ""
76 type = ""
77 description = ""
78 mtu = 0
79 speed = 0
80 adminStatus = 0
81 operStatus = 0
82 _ipAddresses = []
83
84
85 _properties = OSComponent._properties + (
86 {'id':'ips', 'type':'lines', 'mode':'w', 'setter':'setIpAddresses'},
87 {'id':'interfaceName', 'type':'string', 'mode':'w'},
88 {'id':'ifindex', 'type':'string', 'mode':'w'},
89 {'id':'macaddress', 'type':'string', 'mode':'w'},
90 {'id':'type', 'type':'string', 'mode':'w'},
91 {'id':'description', 'type':'string', 'mode':'w'},
92 {'id':'mtu', 'type':'int', 'mode':'w'},
93 {'id':'speed', 'type':'long', 'mode':'w'},
94 {'id':'adminStatus', 'type':'int', 'mode':'w'},
95 {'id':'operStatus', 'type':'int', 'mode':'w'},
96 )
97
98 _relations = OSComponent._relations + (
99 ("os", ToOne(ToManyCont,"Products.ZenModel.OperatingSystem","interfaces")),
100 ("ipaddresses", ToMany(ToOne,"Products.ZenModel.IpAddress","interface")),
101 ("iproutes", ToMany(ToOne,"Products.ZenModel.IpRouteEntry","interface")),
102 )
103
104 zNoPropertiesCopy = ('ips','macaddress')
105
106 localipcheck = re.compile(r'^127.|^0.').search
107 localintcheck = re.compile(r'^lo0').search
108
109 defaultIgnoreTypes = ('Other', 'softwareLoopback', 'CATV MAC Layer')
110
111 factory_type_information = (
112 {
113 'id' : 'IpInterface',
114 'meta_type' : 'IpInterface',
115 'description' : """Arbitrary device grouping class""",
116 'icon' : 'IpInterface_icon.gif',
117 'product' : 'ZenModel',
118 'factory' : 'manage_addIpInterface',
119 'immediate_view' : 'viewIpInterface',
120 'actions' :
121 (
122 { 'id' : 'status'
123 , 'name' : 'Status'
124 , 'action' : 'viewIpInterface'
125 , 'permissions' : (ZEN_VIEW,)
126 },
127 { 'id' : 'events'
128 , 'name' : 'Events'
129 , 'action' : 'viewEvents'
130 , 'permissions' : (ZEN_VIEW, )
131 },
132 { 'id' : 'perfConf'
133 , 'name' : 'Template'
134 , 'action' : 'objTemplates'
135 , 'permissions' : ("Change Device", )
136 },
137 { 'id' : 'viewHistory'
138 , 'name' : 'Modifications'
139 , 'action' : 'viewHistory'
140 , 'permissions' : (ZEN_VIEW_MODIFICATIONS,)
141 },
142 )
143 },
144 )
145
146 security = ClassSecurityInfo()
147
154
155
156 security.declareProtected('View', 'viewName')
158 """
159 Use the unmagled interface name for display
160 """
161 return self.interfaceName.rstrip('\x00')
162 name = primarySortKey = viewName
163
164
166 """
167 Override from PerpertyManager to handle checks and ip creation
168 """
169 self._wrapperCheck(value)
170 if id == 'ips':
171 self.setIpAddresses(value)
172 else:
173 setattr(self,id,value)
174 if id == 'macaddress':
175 self.index_object()
176
177
184
185
192
193
205
206
208 """
209 Allow access to ipAddresses via the ips attribute
210 """
211 if name == 'ips':
212 return self.getIpAddresses()
213 else:
214 raise AttributeError( name )
215
216
217 - def _prepIp(self, ip, netmask=24):
218 """
219 Split ips in the format 1.1.1.1/24 into ip and netmask.
220 Default netmask is 24.
221 """
222 iparray = ip.split("/")
223 if len(iparray) > 1:
224 ip = iparray[0]
225 checkip(ip)
226 netmask = maskToBits(iparray[1])
227 return ip, netmask
228
229
249
250
260
261
263 """
264 If no IPs are sent remove all in the relation
265 """
266 if not ips:
267 self.removeRelation('ipaddresses')
268 return True
269
270
307
308
318
319
328
329
338
339
348
349
351 """
352 Return the first real ipaddress object or None if none are found.
353 """
354 if len(self.ipaddresses()):
355 return self.ipaddresses()[0]
356
357
359 """
360 Return a list of the ip objects on this interface.
361 """
362 retval=[]
363 for ip in self.ipaddresses.objectValuesAll():
364 retval.append(ip)
365 for ip in self._ipAddresses:
366 retval.append(ip)
367 return retval
368
369
371 """
372 Return list of ip addresses as strings in the form 1.1.1.1/24.
373 """
374 return map(str, self.getIpAddressObjs())
375
376
383
384
386 """
387 Return the network name for the first ip on this interface.
388 """
389 net = self.getNetwork()
390 if net: return net.getNetworkName()
391 return ""
392
393
408
409
411 """
412 Return a list of network links for each ip in this interface.
413 """
414 addrs = self.ipaddresses() + self._ipAddresses
415 if addrs:
416 links = []
417 for addr in addrs:
418 if hasattr(aq_base(addr), 'network'):
419 if self.checkRemotePerm('View', addr.network()):
420 links.append(addr.network.getPrimaryLink())
421 else:
422 links.append(addr.network.getRelatedId())
423 else:
424 links.append("")
425 return "<br/>".join(links)
426 else:
427 return ""
428
429
430 security.declareProtected('View', 'getInterfaceName')
438
439
440 security.declareProtected('View', 'getInterfaceMacaddress')
442 """
443 Return the mac address of this interface.
444 """
445 return self.macaddress
446
447
449 """
450 Return the interface type as the target type name.
451 """
452 return self.prepId(self.type or "Unknown")
453
454
456 """
457 Return a list containing the appropriate RRDTemplate for this
458 IpInterface. If none is found then the list will contain None.
459 """
460 templateName = self.getRRDTemplateName()
461 default = self.getRRDTemplateByName(templateName)
462
463
464
465 if not default and templateName.endswith("_64"):
466 default = self.getRRDTemplateByName(templateName[:-3])
467
468
469
470 if not default:
471 default = self.getRRDTemplateByName("ethernetCsmacd")
472
473 if default:
474 return [default]
475 return []
476
477
479 """
480 Ignore interface that are administratively down.
481 """
482
483
484 return self.adminStatus > 1 or self.monitor == False
485
486
488 """
489 Get the current administrative state of the interface. Prefer real-time
490 value over modeled value.
491 """
492 s = self.cacheRRDValue('ifAdminStatus', None)
493 if s is None: s = self.adminStatus
494 return s
495
496
498 """
499 Get the current operational state of the interface. Prefer real-time
500 value over modeled value.
501 """
502 s = self.cacheRRDValue('ifOperStatus', None)
503 if s is None: s = self.operStatus
504 return s
505
506
508 """
509 Return the status number for this interface.
510 """
511
512 if self.snmpIgnore():
513 return -1
514
515 return super(IpInterface, self).getStatus()
516
517
519 """
520 Return a string that expresses self.speed in reasonable units.
521 """
522 if not self.speed:
523 return 'Unknown'
524 speed = self.speed
525 for unit in ('bps', 'Kbps', 'Mbps', 'Gbps'):
526 if speed < 1000: break
527 speed /= 1000.0
528 return "%.3f%s" % (speed, unit)
529
531 """
532 The device id, for indexing purposes.
533 """
534 d = self.device()
535 if d: return d.getPrimaryId()
536 else: return None
537
539 """
540 The interface id, for indexing purposes.
541 """
542 return self.getPrimaryId()
543
545 """
546 pass
547 """
548 return 'None'
549
550
551 InitializeClass(IpInterface)
552
557