I am new in python and have written first code to convert Logstash YAML style configuration file into ElasticSearch Logstash JSON style config. Please have a look at my piece of code and let me know how can i make it better.
Type of input file:
input: file: type: "logRecords" start_position: "beginning" path: "location" codec: multiline: pattern: "regex" negate: "true" what: "previous" filter: if: type: 'logrecord' plugins: - grok: match: message: 'regex1' patterns_dir: 'dir1' - grok: match: message: 'regex2' patterns_dir: 'dir2' - aggregate: task_id: "%{recordId}" code: '' output: file: path: 'oplocation'
YAML to Logstash conf file convertor:
import yaml, json, pprint
f = open('../config/test.yml')
dataMap = yaml.safe_load(f)
f.close()
def configInput(content):
input = []
dictiterator(content,input)
return input
def configFilter(content):
result=[]
ifplugins = []
elseplugins = []
for key, value in content.items():
# print key,value
if key=='if':
ifplugins.append(key+" " +'[type] == \"')
ifplugins.append(value.get('type') +" \" { ")
plugins = value.get('plugins')
createFilterPlugin(plugins,ifplugins)
ifplugins.append('} \n ')
elif key =='elif':
elseplugins.append('else if' + " " + '[type] == \"')
elseplugins.append(value.get('type') + " \" { ")
plugins = value.get('plugins')
createFilterPlugin(plugins,elseplugins)
elseplugins.append(' } \n ')
else:
result.append(key+" { \n ")
dictiterator(value,result)
result.append(" } \n")
result+=ifplugins
result+=elseplugins
# result.append("\n } ")
return result
def createFilterPlugin(plugins,appenders):
for plugin in plugins:
for name, pluginValue in plugin.items():
if name == 'filterCondition':
for name1, condition in pluginValue.items():
if name1 == 'if':
appenders+=condtionalCase(condition, name1)
elif name1 == 'elif':
appenders +=condtionalCase(condition, 'else if')
else:
appenders.append(name + "{ ")
dictiterator(pluginValue,appenders)
appenders.append( '} ')
def condtionalCase(condition, name1):
conditionalCase = []
conditionalCase.append(name1 + ' ( ')
conditionalCase.append(condition['condition'] +' )')
# ifplugins1.append(value1.get('condition') + " \" { ")
conditionalPlugins = condition.get('plugins')
for items in conditionalPlugins:
dictiterator(items,conditionalCase)
# conditionalCase+=plugin
# print conditionalCase
return conditionalCase
def configoutput(content):
output = []
for k,v in content.iteritems():
output.append(k +" { \n ")
for k,item in v.iteritems():
if isinstance(item,list):
output.append(k+" => "+ ', '.join(item))
# print k +"=>" +' , '.join(item)
else:
output.append(k+" => "+ item)
output.append(" } ")
return output
def dictiterator(input,plugin):
for k, v in input.iteritems():
if isinstance(v, dict):
plugin.append(k + '{ \n ')
dictiterator(v,plugin)
plugin.append( '} \n ')
elif isinstance(v,list):
# print v
for item in v:
if isinstance(item,dict):
dictiterator(item,plugin)
# elif isinstance(item,str):
# print ite =m
else:
plugin.append( "{0} => {1}".format(k, v))
plugin.append(" \n ")
# return plugin
for k, v in dataMap.items():
inputs = []
filterplugin = []
output = []
finalOp = []
if k == 'input':
inputs.append('input { \n ')
input = configInput(v)
inputs += input
inputs.append(' } \n ')
# inputs = input + " ".join(input)+" } \n "
elif k =='filter':
filterplugin.append('filter { \n ')
filters = configFilter(v)
filterplugin+= filters
filterplugin.append(' } \n')
# op = filter+" ".join(filters)+" } \n "
# print op
elif k=='output':
output.append(" output { \n ")
temp = configoutput(v)
# print temp
output+= temp
output.append(' } \n ')
# print ' '.join(inputs)+op+''.join(output)
finalOp+=inputs
finalOp+=filterplugin
finalOp+=output
print ''.join(finalOp)