If you are familiar with Zenoss, you should already know that many objects and organizers can be added to ZenPacks that can be used to copy them to other servers. Surprisingly, there is no facility for adding reports(multi-graph or graph reports) to a ZenPack! Thus, I decided to develop a simple script that can export them as XML file which eventually can be fed into Zenoss zenload command. In following you can add top level organizers to topOrg that will be used to export everything inside including all sub-organizers and reports.
import Globals
import os, sys, commands
from Products.ZenUtils.ZenScriptBase import ZenScriptBase
from xml.dom.minidom import parse, parseString
import StringIO
filename = '/home/zenoss/exported-reports.xml'
topOrg = [ 'Reports/Multi-Graph Reports/MyReports', 'Reports/Graph Reports/Devices']
if __name__ == '__main__':
dmd = None
try:
scriptBase = ZenScriptBase(connect=True)
dmd = scriptBase.dmd
except Exception, e:
print "Exception found while trying to connect zenoss and exception is %s" %(str(e))
if dmd is None:
print "Can't connect to Zenoss - are you sure it's running?"
sys.exit(2)
try:
f = open(filename, 'a')
except Exception, e:
print "Exception found while opening the file %s to write and exception is %s" %(filename, str(e))
sys.exit(3)
orgList = []
for x in topOrg:
org = dmd.Reports.getObjByPath(x)
orgList.append(org)
for y in org.getSubOrganizers():
orgList.append(y)
f.write('\n')
f.write('\n')
for org in orgList:
print 'Processing %s' % org
stringIO = StringIO.StringIO()
org.exportXml(stringIO)
xml = parseString(stringIO.getvalue())
xml.getElementsByTagName('object')[0].setAttribute('id', "/zport/dmd%s" % org.getPrimaryDmdId())
f.write(xml.firstChild.toxml())
for report in org.reports():
stringIO = StringIO.StringIO()
report.exportXml(stringIO)
xml = parseString(stringIO.getvalue())
xml.getElementsByTagName('object')[0].setAttribute('id', "/zport/dmd%s" % report.getPrimaryDmdId())
f.write(xml.firstChild.toxml())
f.write('\n')
To load them back:
zenload -i /home/zenoss/exported-reports.xml
This script can be modified to export any object and organizer in Zenoss. Note that zenload can be used to import any objects.xml file included in any ZenPacks(in objects folder). This is handy when you don't want to end up with having multiple importing/exporting ZenPacks in your ZenPacks list.
Have fun;)