… or how to find an interface object from d.os.interfaces() or d.hw.fans() without a loop.
The problem: in Zenoss transforms, I often need to do a check on an attribute of a component, e.g. re.search(‘stuff’, interface.description). The problem is, the event component does not contain the “interface” object. Instead, it contains the interface.getInterfaceName() string. The usual way to find the object until now was:
for iface in device.os.interfaces(): if iface.getInterfaceName() == evt.component: # found if re.search('stuff', iface.description): # do something break
For devices with huge number of interfaces (Cisco 4500 or Cisco Nexus), this took often 40-100 ms, which is a lot inside an event transform. Just imagine a small flood of events…
The solution: found in a transformation by Lionel Seydoux:
iface = d.os.interfaces._getOb(evt.component) if re.search('stuff', iface.description): # do something
Works as well for other elements, e.g. a fan:
d.hw.fans._getOb('Switch_1, Fan_1')
Now I’m not sure what _getOb() really does. Maybe it loops as well. But still, the code is much nicer without these unrolled loops.