This month’s tip is actually several techniques for zendmd scripting with interfaces.
The first tip comes from Zenoss user ‘crashdummymch‘ in his wiki entry ZenDMD Find Device with MAC Address. This let’s you find a specific device, given the MAC address:
for d in dmd.Devices.getSubDevices():
for i in d.os.interfaces():
if i.macaddress == '00:50:56:AA:7C:8D':
print d.id +" "+ i.macaddress
This can be modified to leave off the if statement and simply print out all devices and their MAC addresses. For a complete list of Devices, Interfaces and their MAC addresses in a report check out the MAC Address Report.
The next tip also comes from Zenoss user ‘crashdummymch‘ in his wiki entry ZenDMD - Search for IP addresses. This shows an example of how to dump out devices from a given range, or the query could be modified to show all IPs in a Group or Location:
import re
ipsearch = re.compile('192\.168\.1\.[0-9]{1,3}')
#for d in dmd.Groups..getSubDevices():
for d in dmd.Devices.getSubDevices():
for i in d.os.interfaces():
ipaddress = i.getIpAddress()
if ipaddress == None:
continue
else:
if ipsearch.search(ipaddress):
print d.id+" "+ipaddress
And the final set of examples comes from Zenoss user ‘massimo‘ and his very thorough wiki article ZenDMD - playing with interfaces:
Simple Examples
Now lets disable monitoring for all interfaces of all devices:
>>> for d in dmd.Devices.getSubDevices():
... for interface in d.os.interfaces():
... interface.monitor = False
...
>>> commit()
>>>
You can double-check opening a device via the web interface and selecting an interface.
Some of you might only want to monitor interface which have an IP assigned:
>>> for d in dmd.Devices.getSubDevices():
... for interface in d.os.interfaces():
... if interface.getIpAddress() != None:
... interface.monitor = True
...
>>> commit()
>>>
Of course, sometimes you only want to modify a group (location, device group) of devices. This can be done like this:
>>> for d in dmd.Devices.getSubDevices():
... if d.getDeviceClassPath() == '/Discovered':
... for interface in d.os.interface():
... interface.monitor = False
...
>>> commit()
>>>
Some of the interfaces’ operational status might be down. Monitoring might be disabled for those:
>>> for d in dmd.Devices.getSubDevices():
... for interface in d.os.interface():
... if interface.operStatus > 1:
... interface.monitor = False
...
>>> commit()
>>>
Advanced Examples
Sometimes you want, e.g., to select devices based on their name. With the help of regular expression you can do this. Due to naming conventions (e.g. for locations, systems, etc.) you can select a group of devices, e.g., all devices starting with ap. If you not familiar with regular expression you find help on the web.
>>> import re
>>> p = re.compile('^ap', re.IGNORECASE)
>>> for d in dmd.Devices.getSubDevices():
... if p.match(d.getDeviceName()):
... for interface in d.os.interface():
... interface.monitor = False
...
>>> commit()
>>>
Of course, you can do this with interface names too. Selecting only interfaces which name starts with FastEthernet.
>>> import re
>>> p = re.compile('^FastEthernet')
>>> for d in dmd.Devices.getSubDevices():
... for interface in d.os.interface():
... if p.match(interface.getInterfaceName()):
... interface.monitor = True
...
>>> commit()
>>>
You probably do not want to monitor Loopback interfaces:
>>> import re
>>> p = re.compile('^Loopback', re.IGNORECASE)
>>> for d in dmd.Devices.getSubDevices():
... for interface in d.os.interface():
... if p.match(interface.getInterfaceName()):
... interface.monitor = False
...
>>> commit()
>>>
Thanks again to ‘crashdummymch‘ and ‘massimo‘ for this month’s tips!
UPDATE
Zenoss CTO Erik Dahl reports that these techniques would be really slow with lots of devices and interfaces. The best way is to use the component index pulling out just the interface objects for probably more than a 10x speed up.
[ i.getObject() for i in dmd.Devices.componentSearch({'meta_type':'IpInterface'})]
with the macaddress index added…
[ i.getObject() for i in dmd.Devices.componentSearch({'macaddress': '00:15:C5:F5:5B:24'})]