Difference between revisions of "ZenDMD Tips"

From Zenoss Wiki
Jump to: navigation, search
(Add information on using zendmd as an interpreter.)
 
(16 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
zendmd is a command line tool that can either be used as a [http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop REPL] for Zenoss, or as an interpreter to run scripts. It is installed by Zenoss to $ZENHOME/bin/zendmd.
 
zendmd is a command line tool that can either be used as a [http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop 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 <tt>zendmd</tt>. This page documents some of them.
+
You can do a lot of powerful things from <tt>zendmd</tt>. 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 [https://support.zenoss.com/hc/en-us/articles/202946755-An-Introduction-to-zendmd here] and a very nice quick start [http://www.ryanisroot.net/zenoss:zendmd here] and [https://zindilis.com/docs/zenoss/zendmd-get-devices.html here]
 +
 
 +
 
 +
You can use zendmd in two different modes
  
 
== Using as a REPL ==
 
== Using as a REPL ==
Line 19: Line 22:
 
== Using as an Interpreter ==
 
== Using as an Interpreter ==
  
Create a file called ''example.zendmd''. The extension doesn't matter, but is a good convention. Add the following contents.
+
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.
  
<code>
+
<syntaxhighlight lang=python>
 
#!/usr/bin/env zendmd
 
#!/usr/bin/env zendmd
  
 
for device in dmd.Devices.getSubDevicesGen():
 
for device in dmd.Devices.getSubDevicesGen():
 
     print "%s (%s)" % (device.titleOrId(), device.manageIp)
 
     print "%s (%s)" % (device.titleOrId(), device.manageIp)
</code>
+
</syntaxhighlight>
  
 
You can then make ''example.zendmd'' executable, and execute it by running the following commands. As the zenoss user.
 
You can then make ''example.zendmd'' executable, and execute it by running the following commands. As the zenoss user.
Line 39: Line 42:
 
== ZenDMD Tips - Don't work hard, work smart! ==
 
== ZenDMD Tips - Don't work hard, work smart! ==
  
The following examples 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.
+
See the examples in in the [[:Category:ZenDMD|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.
 +
 
 +
{{#ask: [[Category:ZenDMD]]
 +
  |format=category
 +
  |limit=500
 +
}}
 +
__NOCACHE__
 +
 
 +
== 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:
 +
 
 +
<syntaxhighlight lang=python>
 +
try:
 +
    # 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())
 +
</syntaxhighlight>
 +
 
 +
Save this file for example in <code>~/bzendmd.py</code> and add following alias in <code>.bashrc</code>:
 +
 
 +
<syntaxhighlight lang=bash>
 +
alias dmd="zendmd --script=/home/zenoss/bzendmd.py"
 +
</syntaxhighlight>
 +
 
 +
Now you could start your custom interactive environment using command <code>dmd</code>.
  
* [[ZenDMD Tip - Test & Delete Overrides]]
+
[[Category:ZenDMD]]
* [[ZenDMD Tip - Quickly Audit Enterprise Collectors]]
+
[[Category:Tips]]
* [[ZenDMD Tip - Replace modeler plugin programmatically]]
+
* [[ZenDMD Tip - Create Users in a Jiffy]]
+
* [[ZenDMD Tip - Recreate Device Rels]]
+
* [[ZenDMD Tip - Fix Broken Muiltigraph Collections]]
+
* [[ZenDMD Tip - Fix Broken Graph Report Elements]]
+
* [[ZenDMD Tip - Set Titles via Reverse Lookup (PTR record)]]
+
* [[ZenDMD Tip - Zet manageIp via Host Lookup (A record)]]
+
* [[ZenDMD Tip - Remove Invalid Devices from Collectors]]
+
* [[ZenDMD Tip - Audit Transforms]]
+
* [[ZenDMD Tip - Check and Repair Device Objects with Missing DeviceClass]]
+
* [[ZenDMD Tip - Manipulate Events]]
+

Latest revision as of 16:52, 5 February 2016

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:
    # 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.