Extract information from Zenoss without using the WebUI
by
zenoss
—
last modified
2007-10-17 11:10
Return a device list from Zenoss
Using the Zope Management Interface (ZMI)
http://localhost:8080/zport/dmd/Devices/manage
Make a script object called for instance getMyDeviceList. Then put the following into the body of the script...
return [ d.id for d in context.getSubDevices() ]
Then call it like this
http://localhost:8080/zport/dmd/Devices/getMyDeviceList
You can do all kinds of stuff this way. This will return all device ips...
return [ d.manageIp for d in context.getSubDevices() ]
You get the idea. You can call this method form different parts of the tree to limit the list of devices...
http://localhost:8080/zport/dmd/Devices/Server/Linux/getMyDeviceList
Using zendmd
Instead of using ZMI one can also use zendmd to extract information from Zenoss. To invoke zendmd one should become the zenoss user and invoke zendmd. This will bring up a shell very similar to a regular python shell. Zendmd offers a terrific playground full of possibilities.
1. ---------------------------------------------------
>>> find("build.zenoss.loc").manageIp
'192.168.1.17'
OR
2. ---------------------------------------------------
>>> for dev in dmd.Devices.getSubDevices():
... print dev.id+ " - " +dev.manageIp
...
ConfPrinter - 1.2.3.4
BlahHost - 1.2.3.5
VirtualHost - 1.2.3.6
OR
3. ---------------------------------------------------
>>> for dev in dmd.Devices.getSubDevices():
... print dev.id
... print "--------"
... for int in dev.os.interfaces():
... print int.id
... print "============================"
...
ConfPrinter
--------
Wifi0
============================
BlahHost
--------
HP ETHERNET MULTI-ENVIRONMENT,ROM G.07.02,JETDIRECT,JD30,EEPROM G.08.04
============================
VirtualHost
--------
eth0
lo
============================
Script number one will simply find a device and print the IP, script two prints a list of devices, and script three prints devices and each interfaces associated with it.
from Products.ZenModel.DeviceGroup import manage_addDeviceGroup
baseGroup = dmd.Groups.Customers
f = open('mycustomerlist.txt', 'r')
for line in f:
customer = line.strip()
print "Adding /Customers/" + customer
manage_addDeviceGroup(baseGroup, customer)
commit()
The script above reads in a list of customers from a file and creates a new group under "Groups" in Zenoss. The dmd has many more objects you can interact with. If you want to find out more download the API Doc or simply do a dir(dmd), dir(dmd.Devices), dir(dmd.Monitors) from within zendmd.
All the scripts here can also be feed to zendmd by saving them to a file and output redirecting them to zendmd (zendmd < myscript.py).