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 from zope.event import notify
31 from zope.app.container.contained import ObjectMovedEvent
32
33 from Products.ZenRelations.RelSchema import *
34
35 from Products.ZenUtils.Utils import localIpCheck, localInterfaceCheck
36 from Products.ZenUtils.IpUtil import *
37
38 from ConfmonPropManager import ConfmonPropManager
39 from OSComponent import OSComponent
40 from Products.ZenModel.Exceptions import *
41 from Products.ZenModel.Linkable import Layer2Linkable
42
43 from Products.ZenModel.ZenossSecurity import *
44
57
58 addIpInterface = DTMLFile('dtml/addIpInterface',globals())
59
60
62 """
63 IpInterface object
64 """
65
66 portal_type = meta_type = 'IpInterface'
67
68 manage_editIpInterfaceForm = DTMLFile('dtml/manageEditIpInterface',
69 globals())
70
71
72
73
74
75 ifindex = '0'
76 interfaceName = ''
77 macaddress = ""
78 type = ""
79 description = ""
80 mtu = 0
81 speed = 0
82 adminStatus = 0
83 operStatus = 0
84 duplex = 0
85 _ipAddresses = []
86
87
88 _properties = OSComponent._properties + (
89 {'id':'ips', 'type':'lines', 'mode':'w', 'setter':'setIpAddresses'},
90 {'id':'interfaceName', 'type':'string', 'mode':'w'},
91 {'id':'ifindex', 'type':'string', 'mode':'w'},
92 {'id':'macaddress', 'type':'string', 'mode':'w'},
93 {'id':'type', 'type':'string', 'mode':'w'},
94 {'id':'description', 'type':'string', 'mode':'w'},
95 {'id':'mtu', 'type':'int', 'mode':'w'},
96 {'id':'speed', 'type':'long', 'mode':'w'},
97 {'id':'adminStatus', 'type':'int', 'mode':'w'},
98 {'id':'operStatus', 'type':'int', 'mode':'w'},
99 {'id':'duplex', 'type':'int', 'mode':'w'},
100 )
101
102 _relations = OSComponent._relations + (
103 ("os", ToOne(ToManyCont,"Products.ZenModel.OperatingSystem","interfaces")),
104 ("ipaddresses", ToMany(ToOne,"Products.ZenModel.IpAddress","interface")),
105 ("iproutes", ToMany(ToOne,"Products.ZenModel.IpRouteEntry","interface")),
106 )
107
108 zNoPropertiesCopy = ('ips','macaddress')
109
110 localipcheck = re.compile(r'^127.|^0.').search
111 localintcheck = re.compile(r'^lo0').search
112
113 defaultIgnoreTypes = ('Other', 'softwareLoopback', 'CATV MAC Layer')
114
115 factory_type_information = (
116 {
117 'id' : 'IpInterface',
118 'meta_type' : 'IpInterface',
119 'description' : """Arbitrary device grouping class""",
120 'icon' : 'IpInterface_icon.gif',
121 'product' : 'ZenModel',
122 'factory' : 'manage_addIpInterface',
123 'immediate_view' : 'viewIpInterface',
124 'actions' :
125 (
126 { 'id' : 'status'
127 , 'name' : 'Status'
128 , 'action' : 'viewIpInterface'
129 , 'permissions' : (ZEN_VIEW,)
130 },
131 { 'id' : 'events'
132 , 'name' : 'Events'
133 , 'action' : 'viewEvents'
134 , 'permissions' : (ZEN_VIEW, )
135 },
136 { 'id' : 'perfConf'
137 , 'name' : 'Template'
138 , 'action' : 'objTemplates'
139 , 'permissions' : ("Change Device", )
140 },
141 { 'id' : 'viewHistory'
142 , 'name' : 'Modifications'
143 , 'action' : 'viewHistory'
144 , 'permissions' : (ZEN_VIEW_MODIFICATIONS,)
145 },
146 )
147 },
148 )
149
150 security = ClassSecurityInfo()
151
158
159
160 security.declareProtected('View', 'viewName')
162 """
163 Use the unmagled interface name for display
164 """
165 return self.interfaceName.rstrip('\x00')
166 name = primarySortKey = viewName
167
168
170 """
171 Override from PerpertyManager to handle checks and ip creation
172 """
173 self._wrapperCheck(value)
174 if id == 'ips':
175 self.setIpAddresses(value)
176 else:
177 setattr(self,id,value)
178 if id == 'macaddress':
179 self.index_object()
180
181
188
189
196
197
209
210
212 """
213 Allow access to ipAddresses via the ips attribute
214 """
215 if name == 'ips':
216 return self.getIpAddresses()
217 else:
218 raise AttributeError( name )
219
220
221 - def _prepIp(self, ip, netmask=24):
222 """
223 Split ips in the format 1.1.1.1/24 into ip and netmask.
224 Default netmask is 24.
225 """
226 iparray = ip.split("/")
227 if len(iparray) > 1:
228 ip = iparray[0]
229 checkip(ip)
230 netmask = maskToBits(iparray[1])
231 return ip, netmask
232
233
235 """
236 Add an ip to the ipaddresses relationship on this interface.
237 """
238 networks = self.device().getNetworkRoot()
239 ip, netmask = self._prepIp(ip, netmask)
240
241 ipobj = networks.findIp(ip)
242 if ipobj:
243 dev = ipobj.device()
244 if dev and dev != self.device():
245 log.warn("Adding IP Address %s to %s found it on device %s",
246 ip, self.getId(), dev.getId())
247 self.ipaddresses.addRelation(ipobj)
248
249 else:
250 ipobj = networks.createIp(ip, netmask)
251 self.ipaddresses.addRelation(ipobj)
252 ipobj.index_links()
253 os = self.os()
254 notify(ObjectMovedEvent(self, os, self.id, os, self.id))
255
256
257
267
268
270 """
271 If no IPs are sent remove all in the relation
272 """
273 if not ips:
274 self.removeRelation('ipaddresses')
275 return True
276
277
314
315
325
326
335
336
345
346
355
356
358 """
359 Return the first real ipaddress object or None if none are found.
360 """
361 if len(self.ipaddresses()):
362 return self.ipaddresses()[0]
363
364
366 """
367 Return a list of the ip objects on this interface.
368 """
369 retval=[]
370 for ip in self.ipaddresses.objectValuesAll():
371 retval.append(ip)
372 for ip in self._ipAddresses:
373 retval.append(ip)
374 return retval
375
376
378 """
379 Return list of ip addresses as strings in the form 1.1.1.1/24.
380 """
381 return map(str, self.getIpAddressObjs())
382
383
390
391
393 """
394 Return the network name for the first ip on this interface.
395 """
396 net = self.getNetwork()
397 if net: return net.getNetworkName()
398 return ""
399
400
415
416
418 """
419 Return a list of network links for each ip in this interface.
420 """
421 addrs = self.ipaddresses() + self._ipAddresses
422 if addrs:
423 links = []
424 for addr in addrs:
425 if hasattr(aq_base(addr), 'network'):
426 if self.checkRemotePerm('View', addr.network()):
427 links.append(addr.network.getPrimaryLink())
428 else:
429 links.append(addr.network.getRelatedId())
430 else:
431 links.append("")
432 return "<br/>".join(links)
433 else:
434 return ""
435
436
437 security.declareProtected('View', 'getInterfaceName')
445
446
447 security.declareProtected('View', 'getInterfaceMacaddress')
449 """
450 Return the mac address of this interface.
451 """
452 return self.macaddress
453
454
456 """
457 Return the interface type as the target type name.
458 """
459 return self.prepId(self.type or "Unknown")
460
461
463 """
464 Return a list containing the appropriate RRDTemplate for this
465 IpInterface. If none is found then the list will be empty.
466
467 Order of preference if the interface supports 64bit counters.
468 1. <type>_64
469 2. ethernetCsmacd_64
470 3. <type>
471 4. ethernetCsmacd
472
473 Order of preference if the interface doesn't support 64bit counters.
474 1. <type>
475 2. ethernetCsmacd
476 """
477 templateName = self.getRRDTemplateName()
478
479 order = ['ethernetCsmacd']
480 if templateName.endswith('_64'):
481 order.insert(0, 'ethernetCsmacd_64')
482 if templateName not in order:
483 order.insert(0, templateName)
484 order.insert(2, templateName[:-3])
485 else:
486 if templateName not in order:
487 order.insert(0, templateName)
488
489 for name in order:
490 template = self.getRRDTemplateByName(name)
491 if template:
492 return [template]
493
494 return []
495
496
498 """
499 Ignore interface that are administratively down.
500 """
501
502
503 return self.adminStatus > 1 or self.monitor == False
504
505
507 """
508 Get the current administrative state of the interface. Prefer real-time
509 value over modeled value.
510 """
511 s = self.cacheRRDValue('ifAdminStatus', None)
512 if s is None: s = self.adminStatus
513 return s
514
515
517 """
518 Get the current operational state of the interface. Prefer real-time
519 value over modeled value.
520 """
521 s = self.cacheRRDValue('ifOperStatus', None)
522 if s is None: s = self.operStatus
523 return s
524
525
527 """
528 Return the status number for this interface.
529 """
530
531 if self.snmpIgnore():
532 return -1
533
534 return super(IpInterface, self).getStatus()
535
536
538 """
539 Return a string that expresses self.speed in reasonable units.
540 """
541 if not self.speed:
542 return 'Unknown'
543 speed = self.speed
544 for unit in ('bps', 'Kbps', 'Mbps', 'Gbps'):
545 if speed < 1000: break
546 speed /= 1000.0
547 return "%.3f%s" % (speed, unit)
548
550 """
551 The device id, for indexing purposes.
552 """
553 d = self.device()
554 if d: return d.getPrimaryId()
555 else: return None
556
558 """
559 The interface id, for indexing purposes.
560 """
561 return self.getPrimaryId()
562
564 """
565 pass
566 """
567 return 'None'
568
570 """
571 Return a string that expresses self.duplex into human readable format.
572 """
573
574 if self.duplex == 2:
575 return 'halfDuplex'
576 elif self.duplex == 3:
577 return 'fullDuplex'
578 return 'unknown'
579
580 InitializeClass(IpInterface)
581
586