Programming in zendmd, an interactive shell

We can use zendmd to test our python statements and to access the methods and attributes available to us from Zenoss Core. From zendmd, we can write and test Python statements that manipulate the attributes of the devices:

You can use zendmd as a way to:

  • Test event transformations
  • Mass update device properties from a command line
  • Test programming expressions when writing plugins or zenpacks

This section introduces the environment and provides some basic commands to get us started.

To start the zendmd shell, you'll need to run the following command as the zenoss user:

zendmd

The Zenoss dmd command shell opens and displays with a>>> prompt. Enter the following statements at the shell (exclude the commented text that begins with #):

	zhelp() # Display a list of objects 
	pprint(dir(dmd)) # Display methods available to dmd object 
	dir(devices) # Display methods available to devices object 
	find('Coyote') # Find the device by name 
	d = find('Coyote') # Assign the device to the variable d 
d.deviceClass() # Display the device class

The dmd object is the root of the Zenoss object database. When we execute the dir(devices) statement, one of the methods we return is deviceClass(), which we then use to print Coyote's device class. In this example, d.deviceClass() returns:

<DeviceClass at /zport/dmd/Devices/Server/Remote/devices/Coyote/deviceClass/Remote>

The following script prints all the devices in the Zenoss object database with the corresponding device class. The zendmd command prompts are preserved:

	>>> for x in dmd.Devices.getSubDevices(): 
	... print "%s, %s" % (x. x.getDeviceClassName()) 
	... 

When working in the zendmd shell, the spacing of our code is important. Python requires that all lines of a block be indented the same number of spaces. If you make a mistake, the zendmd interpreter will display an IndentationError: expected an indented block error.

When you finish typing the python statement, hit enter on a blank line that's preceded by three periods (...) and the shell will evaluate the statement. The following is a sample output:

	<Device at Crow>, /Network/Router
	<Device at Fox>, /Server/Linux
	<Device at Master>, /Server/Windows
	<Device at Coyote>, /Server/Remote
	<Device at Print Server>, /Printer
	<Device at Bobcat>, /Workstation
	<Device at badgerfiles.com>, /Web

If we want to print only the devices in the /Server device class, our Python statement becomes:

	>>> for x in dmd.Devices.Server.getSubDevices():
	... print "%s, %s" % (x, x.deviceClass())
	...

If we want to print only the devices in the /Server/Linux device class, our Python statement becomes:

	>>> for x in dmd.Devices.Server.Linux.getSubDevices():
	... print "%s, %s" % (x, x.deviceClass())
	...

As we run each statement, our results become very specific. We can also commit changes to the Zenoss object database from zendmd. Our next example finds the device named Bobcat and sets the production state to Production:

	>>> x = find('Bobcat')
	... x.productionState = 1000
	....
>>> commit()

The commit() method applies our changes to the Zenoss object database. When you commit changes from zendmd, the changes will be reflected in the Zenoss Core web interface, too.

For more information about using zendmd, check out the Zenoss Core community wiki at http://community.zenoss.org/community/documentation/wiki/zendmd. We'll dig deeper into zendmd when we review custom device reports in Chapter 11,

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.249.154