ZenDMD Tips

From Zenoss Wiki
This is the approved revision of this page, as well as being the most recent.
Jump to: navigation, search

zendmd is a command line tool that can either be used as a REPL for Zenoss, or as an interpreter to run scripts. It is installed by Zenoss to $ZENHOME/bin/zendmd.

You can do a lot of powerful things from zendmd. The best thing to start would be to review some of the examples linked from this page to see how it works. There is also a bit of reference on the usage of the language here and a very nice quick start here and here

You can use zendmd in two different modes

Using as a REPL

To use in REPL mode, execute zendmd as the zenoss user.

$ zendmd
Welcome to the Zenoss dmd command shell!
'dmd' is bound to the DataRoot. 'zhelp()' to get a list of commands.
Use TAB-TAB to see a list of zendmd related commands.
Tab completion also works for objects -- hit tab after an object name and '.'
 (eg dmd. + tab-key).

Using as an Interpreter

zendmd can be used as a script interpreter as of Zenoss 4.2. For example, create a file called example.zendmd. The extension doesn't matter, but is a good convention. Add the following contents.

#!/usr/bin/env zendmd
for device in dmd.Devices.getSubDevicesGen():
    print "%s (%s)" % (device.titleOrId(), device.manageIp)

You can then make example.zendmd executable, and execute it by running the following commands. As the zenoss user.

$ chmod 0755 example.zendmd
$  ./example.zendmd

You should see the name and management IP address of each device in your system printed.

ZenDMD Tips - Don't work hard, work smart!

See the examples in in the ZenDMD category that have been contributed to demonstrate the kinds of things that can be done using zendmd. They are applicable either to the REPL or interpreter modes.

ZenDMD configuration file

If you often have to access some objects in zendmd, or write shortcut functions or test data, you could create script that does this for you, and then runs interactive bpython or ipython session like this:

    # try to do some imports
    from ZenPacks.zenoss.Layer2.connections_catalog import CatalogAPI
    # create some objects
    cat = CatalogAPI(zport)
    # create some shortcuts
    def gc(x):
        ''' get entities connected to entity x '''
        return list(cat.get_connected(x))
except ImportError, e:
    print e
# initialize some variables
devices = dmd.Devices.getSubDevices()
import bpython; bpython.embed(locals())

Save this file for example in ~/bzendmd.py and add following alias in .bashrc:

alias dmd="zendmd --script=/home/zenoss/bzendmd.py"

Now you could start your custom interactive environment using command dmd.