Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. #!/usr/bin/python
  2. import time, requests, json, csv, sys
  3. from datetime import datetime
  4. import argparse
  5. import splunk.Intersplunk
  6. from requests.auth import HTTPBasicAuth
  7.  
  8. #TODO
  9. # filtering results
  10. # end scroll
  11. # support for errors
  12. # support for auth (encrypted storage?)
  13. # support for generating commands - for partial results (ticket?)
  14.  
  15. timeFormat = '%Y-%m-%dT%H:%M:%S.%fZ'
  16. si = None
  17.  
  18. #test = open("/tmp/TEST" + str(time.time()), "w+")
  19. for row in sys.stdin:
  20. if row[:9] == "infoPath:":
  21. for i in csv.DictReader(open(row[9:].rstrip())):
  22. si = i
  23. break
  24. # else:
  25. # test.write(row)
  26.  
  27. # parse timestamps from time input, and fallback to 'All Time'
  28. earliest = float(si.get('_search_et', '0'))
  29. earliest2 = time.strftime(timeFormat, time.gmtime(earliest))
  30. earliest = earliest2.replace("%fZ", str(round(earliest % 1, 6))[2:])
  31. latest = float(si.get('_search_lt', '2000000000'))
  32. latest2 = time.strftime(timeFormat, time.gmtime(latest))
  33. latest = latest2.replace("%fZ", str(round(latest % 1, 6))[2:])
  34.  
  35. # Parse params from SPL command
  36. # sourcetype, source, index are obligatory
  37. parser = argparse.ArgumentParser(description='Query Elasticsearch.')
  38. parser.add_argument('--server', dest='server', default='http://127.0.0.1:9200', required=False,
  39. help='URI to the Elasticsearch server, defaults to http://127.0.0.1:9200')
  40. parser.add_argument('--index', dest='index', required=True,
  41. help='index to query the data from, defaults to all')
  42. parser.add_argument('--sourcetype', dest='stype', default='', required=False,
  43. help='sourcetype to query the data from, defaults to all')
  44. parser.add_argument('--onlyraw', dest='onlyraw', default=False, required=False,
  45. help='do not get field extractions, defaults to False')
  46. parser.add_argument('query', nargs=1,
  47. help='search terms (must be enclosed in quotation marks)')
  48.  
  49. args = parser.parse_args()
  50.  
  51. scroll_id = False
  52. csvWriter = False
  53. results = []
  54.  
  55. ts = time.time()
  56. offset = (datetime.fromtimestamp(ts) - datetime.utcfromtimestamp(ts)).total_seconds()
  57. q = args.query[0].replace("\'","\"")
  58. if len(q)>1:
  59. q=q+","
  60.  
  61. while True:
  62. # get data
  63. if not scroll_id:
  64. url = '{}/{}/{}/_search?size=10000&scroll=1h'.format(args.server, args.index, args.stype)
  65. resp = requests.post(url, '{ "sort": {"@timestamp": "desc"}, "query": { "bool": { %s "filter": { "range": { "@timestamp": { "gte": "%s", "lte": "%s" } } } } } }' % (q, earliest, latest), auth=('splunk-read', 'iefaiZ8ael'))
  66. open("/tmp/DEBUG1","w+").write(resp.text)
  67. else:
  68. url = '{}/_search/scroll'.format(args.server)
  69. resp = requests.post(url, '{"scroll": "15m", "scroll_id": "%s"}' % scroll_id, auth=('splunk-read', 'iefaiZ8ael') )
  70. open("/tmp/DEBUG2","w+").write(resp.text)
  71. try:
  72. resp = json.loads(resp.text)
  73. except:
  74. break
  75. # check if there are correct results
  76. try:
  77. scroll_id = resp['_scroll_id']
  78. if not len(resp['hits']['hits']):
  79. break
  80. except:
  81. break
  82. # write rows
  83. for row in resp['hits']['hits']:
  84. tmp = { '_raw': json.dumps(row['_source']),
  85. #'sourcetype': row.get('_type',''),
  86. 'sourcetype': "_json",
  87. 'index': row.get('_index','')
  88. }
  89. try:
  90. tmp['_time'] = time.mktime(time.strptime(row['_source']['@timestamp'], timeFormat)) + offset
  91. except:
  92. if args.onlyraw:
  93. tmp['_time'] = ''
  94. try:
  95. tmp['host'] = row['_source']['host']
  96. except:
  97. if args.onlyraw:
  98. tmp['_host'] = ''
  99. try:
  100. tmp['source'] = row['_source']['source']
  101. except:
  102. if args.onlyraw:
  103. tmp['source'] = 'ELASTIC'
  104. if args.onlyraw:
  105. if not csvWriter:
  106. csvWriter = csv.writer(sys.stdout)
  107. csvWriter.writerow(['_time','index','host','sourcetype','source','_raw'])
  108. csvWriter.writerow([tmp['_time'], tmp['index'], tmp['host'], tmp['sourcetype'], tmp['source'], tmp['_raw']])
  109. else:
  110. # add each field to the results
  111. for key in row['_source'].keys():
  112. tmp[key] = row['_source'][key]
  113. results.append(tmp)
  114. if args.onlyraw:
  115. sys.stdout.flush()
  116.  
  117. if not args.onlyraw:
  118. splunk.Intersplunk.outputResults(results)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement