#!/usr/bin/python3 # coding: utf-8 # {{{ Imports import os import sys import argparse import yaml import json import traceback # }}} # {{{ Constants _NAME = "yaml2json" _DESC = "Convert yaml data to zabbix json discovery format" _VERSION = "0.1" # }}} # {{{ main() def main(): parser = argparse.ArgumentParser(prog=_NAME, description=_DESC) parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + _VERSION) parser.add_argument('-d', '--debug', dest='debug', default=False, action='store_true', help='Print additional debug informations: traceback, etc. (default: no)') parser.add_argument('datafile') options = parser.parse_args() data = {'data': []} if os.path.exists(options.datafile): try: with open(options.datafile, encoding='utf-8') as f: datayml = yaml.safe_load(f) if datayml is not None: for section in datayml: localdata = { '{#KEY}': section, } for k in datayml[section]: localdata['{#%s}' % (k.upper())] = datayml[section][k] data['data'].append(localdata) except Exception as e: if not options.debug: print('Exception raised (use --debug option to get more info): %s' % (e), file=sys.stderr) else: print('Exception raised: %s' % (e), file=sys.stderr) traceback.print_exc(file=sys.stderr) print('ZBX_NOTSUPPORTED') sys.exit(-1) print(json.dumps(data, indent=4)) # }}} # {{{ __main__ if __name__ == "__main__": main() # }}} # vim: foldmethod=marker foldlevel=0 foldenable