1
2
3
4
5
6
7
8
9
10
11 __doc__="""IpInterface
12
13 IpInterface is a collection of devices and subsystems that make
14 up a business function
15 """
16
17 import re
18 import copy
19 import logging
20 log = logging.getLogger("zen.IpInterface")
21
22 from Globals import DTMLFile
23 from Globals import InitializeClass
24 from Acquisition import aq_base
25 from App.Dialogs import MessageDialog
26 from AccessControl import ClassSecurityInfo
27 from zope.event import notify
28 from zope.container.contained import ObjectMovedEvent
29
30 from Products.ZenRelations.RelSchema import *
31
32 from Products.ZenUtils.Utils import localIpCheck, localInterfaceCheck, convToUnits
33 from Products.ZenUtils.IpUtil import *
34
35 from ConfmonPropManager import ConfmonPropManager
36 from OSComponent import OSComponent
37 from Products.ZenModel.Exceptions import *
38 from Products.ZenModel.Linkable import Layer2Linkable
39
40 from Products.ZenModel.ZenossSecurity import *
41
54
55 addIpInterface = DTMLFile('dtml/addIpInterface',globals())
56
57
59 """
60 IpInterface object
61 """
62
63 portal_type = meta_type = 'IpInterface'
64
65 manage_editIpInterfaceForm = DTMLFile('dtml/manageEditIpInterface',
66 globals())
67
68
69
70
71
72 ifindex = '0'
73 interfaceName = ''
74 macaddress = ""
75 type = ""
76 description = ""
77 mtu = 0
78 speed = 0
79 adminStatus = 0
80 operStatus = 0
81 duplex = 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 {'id':'duplex', 'type':'int', 'mode':'w'},
97 )
98
99 _relations = OSComponent._relations + (
100 ("os", ToOne(ToManyCont,"Products.ZenModel.OperatingSystem","interfaces")),
101 ("ipaddresses", ToMany(ToOne,"Products.ZenModel.IpAddress","interface")),
102 ("iproutes", ToMany(ToOne,"Products.ZenModel.IpRouteEntry","interface")),
103 )
104
105 zNoPropertiesCopy = ('ips','macaddress')
106
107 localipcheck = re.compile(r'^127.|^0.|^::1$|^fe80:').search
108 localintcheck = re.compile(r'^lo0').search
109
110 defaultIgnoreTypes = ('Other', 'softwareLoopback', 'CATV MAC Layer')
111
112 factory_type_information = (
113 {
114 'id' : 'IpInterface',
115 'meta_type' : 'IpInterface',
116 'description' : """Arbitrary device grouping class""",
117 'icon' : 'IpInterface_icon.gif',
118 'product' : 'ZenModel',
119 'factory' : 'manage_addIpInterface',
120 'immediate_view' : 'viewIpInterface',
121 'actions' :
122 (
123 { 'id' : 'status'
124 , 'name' : 'Status'
125 , 'action' : 'viewIpInterface'
126 , 'permissions' : (ZEN_VIEW,)
127 },
128 { 'id' : 'events'
129 , 'name' : 'Events'
130 , 'action' : 'viewEvents'
131 , 'permissions' : (ZEN_VIEW, )
132 },
133 { 'id' : 'perfConf'
134 , 'name' : 'Template'
135 , 'action' : 'objTemplates'
136 , 'permissions' : ("Change Device", )
137 },
138 )
139 },
140 )
141
142 security = ClassSecurityInfo()
143
150
151
152 security.declareProtected('View', 'viewName')
154 """
155 Use the unmagled interface name for display
156 """
157 return self.interfaceName.rstrip('\x00')
158 name = primarySortKey = viewName
159
161 """
162 Override from PerpertyManager to handle checks and ip creation
163 """
164 self._wrapperCheck(value)
165 if id == 'ips':
166 self.setIpAddresses(value)
167 else:
168 setattr(self,id,value)
169 if id == 'macaddress':
170 self.index_object()
171
181
191
201
213
214
216 """
217 Allow access to ipAddresses via the ips attribute
218 """
219 if name == 'ips':
220 return self.getIpAddresses()
221 else:
222 raise AttributeError( name )
223
224
225 - def _prepIp(self, ip, netmask=24):
226 """
227 Split ips in the format 1.1.1.1/24 into ip and netmask.
228 Default netmask is 24.
229 """
230 iparray = ip.split("/")
231 if len(iparray) > 1:
232 ip = iparray[0]
233 checkip(ip)
234 netmask = maskToBits(iparray[1])
235 return ip, netmask
236
237
239 """
240 Add an ip to the ipaddresses relationship on this interface.
241 """
242 networks = self.device().getNetworkRoot()
243 ip, netmask = self._prepIp(ip, netmask)
244
245 ipobj = networks.findIp(ip)
246 if ipobj:
247 dev = ipobj.device()
248 if dev and dev != self.device():
249 log.warn("Adding IP Address %s to %s found it on device %s",
250 ip, self.getId(), dev.getId())
251 self.ipaddresses.addRelation(ipobj)
252
253 else:
254 ipobj = networks.createIp(ip, netmask)
255 self.ipaddresses.addRelation(ipobj)
256 ipobj.index_object()
257 os = self.os()
258 notify(ObjectMovedEvent(self, os, self.id, os, self.id))
259
260
261
271
272
274 """
275 If no IPs are sent remove all in the relation
276 """
277 if not ips:
278 self.removeRelation('ipaddresses')
279 return True
280
281
318
319
329
330
339
340
349
350
359
360
362 """
363 Return the first real IP address object or None if none are found.
364 """
365 if len(self.ipaddresses()):
366 return self.ipaddresses()[0]
367
368
379
380
382 """
383 Return list of ip addresses as strings in the form 1.1.1.1/24.
384 """
385 return map(str, self.getIpAddressObjs())
386
387
394
395
397 """
398 Return the network name for the first ip on this interface.
399 """
400 net = self.getNetwork()
401 if net: return net.getNetworkName()
402 return ""
403
404
419
420
422 """
423 Return a list of network links for each ip in this interface.
424 """
425 addrs = self.ipaddresses() + self._ipAddresses
426 if addrs:
427 links = []
428 for addr in addrs:
429 if hasattr(aq_base(addr), 'network'):
430 if self.checkRemotePerm('View', addr.network()):
431 links.append(addr.network.getPrimaryLink())
432 else:
433 links.append(addr.network.getRelatedId())
434 else:
435 links.append("")
436 return "<br/>".join(links)
437 else:
438 return ""
439
440
441 security.declareProtected('View', 'getInterfaceName')
449
450
451 security.declareProtected('View', 'getInterfaceMacaddress')
453 """
454 Return the mac address of this interface.
455 """
456 return self.macaddress
457
458
460 """
461 Return the interface type as the target type name.
462 """
463 return self.prepId(self.type or "Unknown")
464
465
467 """
468 Return a list containing the appropriate RRDTemplate for this
469 IpInterface. If none is found then the list will be empty.
470
471 Order of preference if the interface supports 64bit counters.
472 1. <type>_64
473 2. ethernetCsmacd_64
474 3. <type>
475 4. ethernetCsmacd
476
477 Order of preference if the interface doesn't support 64bit counters.
478 1. <type>
479 2. ethernetCsmacd
480 """
481 templateName = self.getRRDTemplateName()
482
483 order = ['ethernetCsmacd']
484 if templateName.endswith('_64'):
485 order.insert(0, 'ethernetCsmacd_64')
486 if templateName not in order:
487 order.insert(0, templateName)
488 order.insert(2, templateName[:-3])
489 else:
490 if templateName not in order:
491 order.insert(0, templateName)
492
493 for name in order:
494 template = self.getRRDTemplateByName(name)
495 if template:
496 return [template]
497
498 return []
499
500
502 """
503 Ignore interface that are administratively down.
504 """
505
506
507 return self.adminStatus > 1 or self.monitor == False
508
509
511 """
512 Get the current administrative state of the interface.
513 """
514 return self.adminStatus
515
516
518 """
519 Return the current administrative state of the interface converted to
520 its string version.
521 """
522 return {1: 'Up', 2: 'Down', 3: 'Testing'}.get(
523 self.getAdminStatus(), 'Unknown')
524
525
527 """
528 Get the current operational state of the interface.
529 """
530 return self.operStatus
531
533 """
534 Return the current operational state of the interface converted to
535 its string version.
536 """
537 return {
538 1: 'Up', 2: 'Down', 3: 'Testing', 5: 'Dormant', 6: 'Not Present',
539 7: 'Lower Layer Down'}.get(
540 self.getOperStatus(), 'Unknown')
541
542
544 """
545 Return the status number for this interface.
546 """
547
548 if self.snmpIgnore():
549 return -1
550
551 return super(IpInterface, self).getStatus()
552
553
555 """
556 Return a string that expresses self.speed in reasonable units.
557 """
558 if not self.speed:
559 return 'Unknown'
560 return convToUnits(self.speed, divby=1000, unitstr='bps')
561
563 """
564 The device id, for indexing purposes.
565 """
566 d = self.device()
567 if d: return d.getPrimaryId()
568 else: return None
569
571 """
572 The interface id, for indexing purposes.
573 """
574 return self.getPrimaryId()
575
577 """
578 pass
579 """
580 return 'None'
581
583 """
584 Return a string that expresses self.duplex into human readable format.
585 """
586
587 if self.duplex == 2:
588 return 'halfDuplex'
589 elif self.duplex == 3:
590 return 'fullDuplex'
591 return 'unknown'
592
593 InitializeClass(IpInterface)
594
599