Skip to content

Instantly share code, notes, and snippets.

@cluther
Last active December 10, 2015 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cluther/4382323 to your computer and use it in GitHub Desktop.
Save cluther/4382323 to your computer and use it in GitHub Desktop.
zendmd IPython Extension
##############################################################################
#
# Copyright (C) Zenoss, Inc. 2012, all rights reserved.
#
# This content is made available according to terms specified in
# License.zenoss under the directory where your Zenoss product is installed.
#
##############################################################################
'''
IPython extension that allows IPython to be used as a shell like Zenoss'
zendmd both in normal console mode, and in notebook mode.
This assumes that ipython is installed for the zenoss user.
easy_install ipython
To use IPython's notebook mode you must also have tornado and pyzmq
installed.
easy_install tornado pyzmq
Use by running the following at an IPython prompt.
%load_ext izendmd
You can also load the extension at startup with the --ext parameter.
ipython --ext=izendmd
ipython notebook --ext=izendmd
This code was copied directly from Zenoss' zendmd.py.
'''
def load_ipython_extension(ipython):
import os
import socket
import sys
from pprint import pprint
import Globals
from transaction import commit
import Zope2
CONF_FILE = os.path.join(os.environ['ZENHOME'], 'etc', 'zope.conf')
# hide any positional arguments during Zope2 configure
_argv = sys.argv
sys.argv = [sys.argv[0], ] + [x for x in sys.argv[1:] if x.startswith("-")]
Zope2.configure(CONF_FILE)
sys.argv = _argv
# Now we have the right paths, so we can do the rest of the imports
from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManagement import noSecurityManager
from Products.CMFCore.utils import getToolByName
from Products.ZenModel.IpNetwork import IpNetworkPrinterFactory
from Products.ZenUtils.Utils import set_context, unused
from Products.Zuul import getFacade, listFacades
# Connect to the database, set everything up
app = Zope2.app()
app = set_context(app)
def login(username='admin'):
utool = getToolByName(app, 'acl_users')
user = utool.getUserById(username)
if user is None:
user = app.zport.acl_users.getUserById(username)
user = user.__of__(utool)
newSecurityManager(None, user)
login('admin')
# Useful references
zport = app.zport
dmd = zport.dmd
sync = zport._p_jar.sync
find = dmd.Devices.findDevice
devices = dmd.Devices
me = find(socket.getfqdn())
# Make pyflakes happy.
unused(Globals)
unused(getFacade)
unused(listFacades)
unused(devices)
unused(me)
def reindex():
sync()
dmd.Devices.reIndex()
dmd.Events.reIndex()
dmd.Manufacturers.reIndex()
dmd.Networks.reIndex()
commit()
def logout():
noSecurityManager()
def grepdir(obj, regex=""):
if regex:
import re
pattern = re.compile(regex)
for key in dir(obj):
if pattern.search(key):
print key
def lookupGuid(guid):
"""
Given a guid this returns the object that it identifies
"""
from Products.ZenUtils.guid.interfaces import IGUIDManager
manager = IGUIDManager(dmd)
return manager.getObject(guid)
def version():
for info in zport.About.getAllVersions():
print "%10s: %s" % (info['header'], info['data'])
print "%10s: %s" % ("DMD", dmd.version)
def printNets(net=dmd.Networks, format="text", out=sys.stdout):
"""
Print out the IpNetwork and IpAddress hierarchy under net. To print
out everything call printNets(dmd.Networks). format can be text,
python, or xml.
"""
factory = IpNetworkPrinterFactory()
printer = factory.createIpNetworkPrinter(format, out)
printer.printIpNetwork(net)
def cleandir(obj):
portaldir = set(dir(dmd))
objdir = set(dir(obj))
appdir = set(dir(app))
result = sorted(objdir - portaldir - appdir)
pprint(result)
ipython.push(locals())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment