CodeLab
24Jun/100

Accessing zenoss dmd from standalone scripts

Quite often is is very useful scripting repetitive tasks in zenoss, whether it might be listing devices and its properties, dumping lists of users, re-arranging organizers or other type of routine maintenance. To do so invariably you need to get hold of the zenoss dmd. This is a short snipped showing a way of doing it:

#!/usr/bin/env python
import Globals, sys
from Products.ZenUtils.ZenScriptBase import ZenScriptBase
dmd = None
try:
    dmd = ZenScriptBase(connect=True).dmd
except Exception, e:
    print "Connection to zenoss dmd failed: %s\n" % e
    sys.exit(1)

for device in dmd.Devices.getSubDevices_recursive():
   print "device %s ip is %s" % (device.getDeviceName(), device.getDeviceIp()) 

 

Or better yet, you can subclass ZenScriptBase:

#!/usr/bin/env python
import Globals, sys
from Products.ZenUtils.ZenScriptBase import ZenScriptBase

class myDmdUtils(ZenScriptBase):
   '''
     Some utility methods for zenoss dmd
   '''
   def getAllDevs(self):
      return self.dmd.Devices.getSubDevices_recursive()

   def dumpDevsAndIps(self):
      for device in self.getAllDevs():
          print "device %s ip is %s" % (device.getDeviceName(), device.getDeviceIp())


if __name__ == "__main__":
    zenutils = None
    try:
        zenutils = myDmdUtils(connect=True)
    except Exception, e:
        print "Could not load myDmdUtils instance: %s" % e
        sys.exit(1)

    zenutils.dumpDevsAndIps()

Do not forget that to run these scripts under the zenoss's user environment...

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

Trackbacks are disabled.