Discussion:
using d.collectDevice() and d.DeviceLoader.loadDevice() pollutes STDERR
zenorr
2012-08-14 15:24:34 UTC
Permalink
zenorr [http://community.zenoss.org/people/zenorr] created the discussion

"using d.collectDevice() and d.DeviceLoader.loadDevice() pollutes STDERR"

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

--------------------------------------------------------------
Hi,

I'm developing a script to sync Zenoss (Core 3.2.1) from a tech-CMDB. It is working quite well, but I have some anger with the zendmd commands collectDevice() and DeviceLoader.loadDevice(). They pollute STDERR of my script with all sorts of info messages like :

INFO:zen.Device:device name 'xxx' for ip '10.19.30.9'
INFO:zen.Device:xxx's IP address has been set to 10.19.30.9.
INFO:zen.Device:setting tag to ''
INFO:zen.Device:setting serialNumber to ''

or

INFO:zen.Utils:Executing command: /opt/zenoss/zenoss/bin/zenmodeler run --now --monitor localhost -F -d xxx
2012-08-14 17:05:25,606 INFO zen.ZenModeler: Connecting to localhost:8789
2012-08-14 17:05:25,614 INFO zen.ZenModeler: Connected to ZenHub
2012-08-14 17:05:25,688 INFO zen.ZenModeler: Collecting for device xxx

What I see is that the zenoss scripts behind these API calls both use log.info() call, but I can't figure how to avoid this to propagate up to my STDERR console.

My own script has this :

logger.propagate = False

but I still get the "pollution" from the above API calls.

I get the dmd using:

dmd = ZenScriptBase(connect=True, noopts=True).dmd

Help appreciated...
Charles
--------------------------------------------------------------

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

Start a new discussion in zenoss-users by email
[discussions-community-forums-zenoss--***@community.zenoss.org] -or- at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
zenorr
2012-08-24 11:30:08 UTC
Permalink
zenorr [http://community.zenoss.org/people/zenorr] created the discussion

"Re: using d.collectDevice() and d.DeviceLoader.loadDevice() pollutes STDERR"

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

--------------------------------------------------------------
OK, I went to the hard route to debug all that stuff, and at least on my zenoss version (Core 3.2.1), the behavior is not coherent, so I have a mix of two methods, each detailed below.

*Method for collectDevice() :*

* Redirect STDOUT to a file
* run command
* restore STDOUT
* do whatever you want with the file (eg here I add it manually to my log)

    # redirect stdout to avoid polluting the script's STDOUT
    save_stdout = sys.stdout
    fd, path = tempfile.mkstemp(suffix = '.log', text = True)
    sys.stdout = open(path, "w")

    # then model device
    main_logger.debug("model device")
    d.collectDevice()

    # restore STDOUT
    os.close(fd)
    sys.stdout = save_stdout

    # read STDOUT stuff and add it to out normal log
    f = open(path)
    log_output = f.read()
    main_logger.debug("output of zenmodeler command:")
    main_logger.debug("\n" + log_output)
    f.close()
    os.unlink(path)

    # commit all the changes
    main_logger.debug("commit")
    commit()

*Method for loadDevice() :*

* re-configure logging
* call loadDevice()

    # avoid loadDevice() lib mess to STDERR/STDOUT
    lib_logger = logging.getLogger('zen')
    lib_hdlr = logging.FileHandler(main_log_file)
    lib_formatter = logging.Formatter('%(asctime)s - %(process)d - %(levelname)s zenlib - %(message)s')
    lib_hdlr.setFormatter(lib_formatter)
    lib_logger.addHandler(lib_hdlr)
    lib_logger.setLevel(logging.DEBUG)
    lib_logger.propagate = False

later :
    dmd.DeviceLoader.loadDevice(
        name, class_name,
        tag, serialNumber,
        snmp_ro_community, snmp_port, snmp_version,
        rackSlot, productionState, comments,
        hwManufacturer, hwProductName,
        osManufacturer, osProductName,
        locationPath, groupPathsList, systemPathsList,
        "localhost",
        "none")

Even if this method should work as well for collectDevice() above, it does not. If someone from Zenoss can explain this, you are welcome.
--------------------------------------------------------------

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

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