Last active
July 10, 2018 14:07
-
-
Save jstanley23/4e2b308de831aa56b0537065e5c0ade2 to your computer and use it in GitHub Desktop.
Export Zenoss template to ZPL yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import argparse | |
| import yaml | |
| import sys | |
| import Globals | |
| from Products.ZenUtils.ZenScriptBase import ZenScriptBase | |
| # Because of circular dependencies, we must import Dumper first | |
| from ZenPacks.zenoss.ZenPackLib.lib.helpers.Dumper import Dumper | |
| from ZenPacks.zenoss.ZenPackLib.lib.params.ZenPackSpecParams import ZenPackSpecParams | |
| from ZenPacks.zenoss.ZenPackLib.lib.params.RRDTemplateSpecParams import RRDTemplateSpecParams | |
| helpMsg = """ | |
| Convert Zenoss templates to ZenPackLib yaml | |
| Use -t to specify a template name. | |
| This will dump all tempaltes with the matching name. | |
| Use -u to specify a template Uid. | |
| This will dump just the one template. | |
| You can get a template Uid by calling getPrimaryUrlPath on the object. | |
| Example Uid: /zport/dmd/Devices/Azure/rrdTemplates/AzureContainer | |
| """ | |
| parser = argparse.ArgumentParser(description=helpMsg) | |
| parser.add_argument( | |
| '-t', | |
| '--template', | |
| action="store", | |
| dest="template", | |
| help='Template Name', | |
| default=None, | |
| ) | |
| parser.add_argument( | |
| '-u', | |
| '--uid', | |
| action="store", | |
| dest="templateUid", | |
| help='Template Uid (getPrimaryUrlPath)', | |
| default=None, | |
| ) | |
| args = parser.parse_args() | |
| templateUid = args.templateUid | |
| template = args.template | |
| dmd = ZenScriptBase(noopts=True, connect=True).dmd | |
| templates = [] | |
| if templateUid: | |
| try: | |
| templates.append(dmd.unrestrictedTraverse(templateUid)) | |
| except: | |
| print "Template not found: %s" % templateUid | |
| sys.exit() | |
| elif template: | |
| allTemplates = dmd.Devices.getAllRRDTemplates() | |
| templates.extend([t for t in allTemplates if t.id == template]) | |
| deviceClasses = { t.getOrganizerName(): {} for t in templates } | |
| zpsp = ZenPackSpecParams('ZenPacks.example.MyTemplates', device_classes=deviceClasses) | |
| for temp in templates: | |
| spec = RRDTemplateSpecParams.fromObject(temp) | |
| zpsp.device_classes[temp.getOrganizerName()].templates = { temp.id: spec } | |
| print yaml.dump(zpsp, Dumper=Dumper) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment