12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #!/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.2"
- # }}}
- # {{{ 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('datafiles', nargs='+')
- options = parser.parse_args()
- data = {'data': []}
- for datafile in options.datafiles:
- if os.path.exists(datafile):
- try:
- with open(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
|