Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added json exporter util for the spl parser + utils directory for future additions #132

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions scripts/splparserToJson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
###############################################
# initial code sample produced by: Sara Alspaugh
# date: 11/6/2017
# modified by: BAH, Greg Schmidt
# - added cmd args and usage
# - added writeJson
# date: 11/6/2017
###############################################

import sys
sys.path.append('../')
import json
import getopt
import json
import io


###############################################
## usage for cmd args
###############################################
def usage():
print 'python splparserToJson.py -h -s <Splunk_search_query>'
print ' -h : help menu'
print ' -o : output filename'
print ' -s : Splunk search query'
print '-----------------------------------------------------'
print ' e.g. python splparserToJson.py -o testout.json -s "sourcetype=access method=GET learn"'


###############################################
## write data to json
###############################################
def writeJson(data, filename):
try:
to_unicode = unicode
except NameError:
to_unicode = str

# Write JSON file
with io.open(filename, 'w', encoding='utf8') as outfile:
str_ = json.dumps(data, indent=2, sort_keys=True, separators=(',', ': '), ensure_ascii=False)
outfile.write(to_unicode(str_))


###############################################
# global parameters
###############################################
query = "*"
outputfilename = "queryOutput.json"

import splparser


###############################################
# process command arguments
###############################################
try:
opts, args = getopt.getopt(sys.argv[1:], "hs:o:")
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit(0)
elif opt == '-o':
outfilename = arg
elif opt == '-s':
query = arg
else:
usage()
sys.exit(2)

print "Search query = " + query


###############################################
# main processing
###############################################

parsetree = splparser.parse(query)
parsetree_as_json = parsetree.jsonify()


###############################################
# dump the json to file
###############################################

writeJson(parsetree_as_json, outfilename)