Discussion:
a basic tree traversal of all devices
Matthew Purdy
2013-04-02 21:54:02 UTC
Permalink
Matthew Purdy [http://community.zenoss.org/people/mpurdy] created the discussion

"a basic tree traversal of all devices"

To view the discussion, visit: http://community.zenoss.org/message/72687#72687

--------------------------------------------------------------
i have a basic script which i started; which traverses the dmd tree and then call a method device_info(d) to return all the attributes from the device (using *http://fqdn:8080/zport/dmd/manage http://fqdn:8080/zport/dmd/manage*, clicking on *Devices* and then clicking on the *"property" tab*)

problem is when i iterate over the dmd.Devices the objects are strings; which then i cannot get any of the attributes.  i was assuming that dmd.Devices would return an object reference to some sub-class of DeviceClass.

+question: once you get the list of devices as a string; how can you then use that string to get all its attributes?  also, is there a way to just simple get the DeviceClass objects directly?+

def *indent_spaces*(level = 1):
        ret = ''
        indent = '   '
        for i in xrange(level):
                ret += indent
        return ret

def *device_info*(device, level = 1):
        ret = ""
       

        #add device description

        ret += indent_spaces(level)
        ret += 'description => '
        ret += device.description
        ret += "\n"

        #add device zPythonClass

        ret += indent_spaces(level)
        ret += 'pythonClass => '
        ret += device.zPythonClass
        ret += "\n"

        # add more attributes here

        return ret

dmd = ZenScriptBase(connect=True).dmd

print '######################################'
print 'dmd.Devices'
print '######################################'
for d in dmd.Devices:
          print "device =>  %s " % d
          print *device_info(d)*
print ''
print ''

print '######################################'
print 'dmd.Devices.getSubDevices'
print '######################################'
for d in dmd.Devices.getSubDevices():
         print "device.subDevice => %s" % d
         print *device_info(d)*

print ''
print ''
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/72687#72687]

Start a new discussion in zenoss-users at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
jcurry
2013-04-05 15:51:47 UTC
Permalink
jcurry [http://community.zenoss.org/people/jcurry] created the discussion

"Re: a basic tree traversal of all devices"

To view the discussion, visit: http://community.zenoss.org/message/72712#72712

--------------------------------------------------------------
Not sure you want to iterate over Devices - just the subDevices.  Then you need to print d.id to get the "name" of the device - or, better, use the titleOrId function so if you have set up a human-friendly name, it will use that rather than the id field:

#!/usr/bin/env python
import Globals
import csv

from Products.ZenUtils.ZenScriptBase import ZenScriptBase
from transaction import commit

def indent_spaces(level = 1):
    ret = ''
    indent = '   '
    for i in xrange(level):
        ret += indent
    return ret

def device_info(device, level = 1):
    ret = ""

    # device description
    ret += indent_spaces(level)
    ret += 'description => '
    #ret += 'testing'
    ret += device.description
    ret += "\n"

    # device zPythonClass
    ret += indent_spaces(level)
    ret += 'zPythonClass => '
    #ret += 'testing'
    ret += device.zPythonClass
    ret += "\n"

    # device property - add here   

    return ret


dmd = ZenScriptBase(connect=True).dmd

#print '######################################'
#print 'dmd.Devices'
#print '######################################'
#for d in dmd.Devices:
#    print "device =>  %s " % d
#    print device_info(d)
#print ''
#print ''

print '######################################'
print 'dmd.Devices.getSubDevices'
print '######################################'
for d in dmd.Devices.getSubDevices():
    print "device.subDevice => %s" % d.titleOrId()
    print device_info(d)
print ''
print ''

Does that do what you want?

Cheers,
Jane
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/72712#72712]

Start a new discussion in zenoss-users at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
Loading...