Bruce's Blog

The Internet needed another source of rants and uninvited uninformed opinions.

    • Edit
    • Delete
    • Tags
    • Autopost

    Zenoss and Python: One Line Of Evil

    The following lines of code when used within Zenoss's zendmd environment will take a list of IP addresses from a text file and spit out a list of those IP addresses that do not yet have a corresponding object in Zenoss.

    import string
    
    print [devip for devip in map(string.strip, open("iplist.txt", "r").readlines())
        if not [True for device in dmd.Devices.getSubDevices() if device.manageIp == devip]]

    This is useful for those cases where you are sure Zenoss discovery missed a few network switches due to a network administrator screwup and find that it's too time consuming to find the handful among hundreds the manual way.

    As for the code quality I beg forgiveness from the Pythonic gods for thinking a list comprehension within a list comprehension with a map thrown in was a good idea. But then again I'm sure there are some Ruby fanatics out there that think that looks like Hello World. ;)

    Update on May 13th: A kind poster pointed out the presence of find() in zendmd and thus dmd.Devices.findDevice(). That means one can just do this instead:

    import string
    
    print [devip for devip in map(string.strip, open("iplist.txt", "r").readlines())
        if not dmd.Devices.findDevice(devip)]

    Not only is it shorter its also a magnitude of order faster which shouldn't be too surprising considering the evil in the original version. Thanks Erik!

    Tags » General
    • 12 May 2008
  • Bruce Locke's Blog

    Network Analyst who plays around with many things open source when he is not feeding his MMORPG addiction.

  • About Bruce Locke

    Network Analyst who plays around with many things open source when he is not feeding his MMORPG addiction.

  • Subscribe

    Subscribe to this posterous
    Unsubscribe
    Follow this posterous RSS
    You're a contributor here (Edit)
    This is your Space (Edit)
    Follow by email »
    Get the latest updates in your email box automatically.
  • Me Elsewhere

    • Twitter
    • GitHub

Original theme created for Posterous by Obox.