Advertisement
Guest User

Untitled

a guest
Aug 14th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. import adal
  2. import datetime
  3. import json
  4. import os
  5. import sys
  6. import time
  7. import requests
  8. from splunklib.modularinput import *
  9.  
  10. def validate_input(helper, definition):
  11. inputs=helper.get_input_stanza()
  12. for input_name, input_item in inputs.iteritems():
  13. max_count = str(input_item["max_count"])
  14. start_date = str(input_item["start_date"])
  15. if int(max_count) <= 0:
  16. helper.log_error("Max count must be greater than zero (0); found max_count=" + str(max_count))
  17. try:
  18. valid_date = datetime.datetime.strptime(start_date, '%d/%m/%Y %H:%M:%S')
  19. except ValueError:
  20. helper.log_error("Start date must be in the format of dd/mm/yyyy hh:mm:ss")
  21. pass
  22.  
  23. def collect_events(helper, ew):
  24.  
  25. # Go through each input for this modular input
  26. inputs=helper.get_input_stanza()
  27. for input_name, input_item in inputs.iteritems():
  28. # Get the values, cast them as floats
  29. resource_group = str(input_item["resource_group"])
  30. workspace = str(input_item["workspace_id"])
  31. query = str(input_item["oms_query"])
  32. subscription_id = str(input_item["subscription_id"])
  33. tenant_id = str(input_item["tenant_id"])
  34. application_id = str(input_item["application_id"])
  35. application_key = str(input_item["application_key"])
  36.  
  37. # Date and delta
  38. if helper.get_check_point('last_date'):
  39. start_datetime = datetime.datetime.strptime(helper.get_check_point('last_date'),'%d/%m/%Y %H:%M:%S')
  40. else:
  41. start_datetime = datetime.datetime.strptime(str(input_item['start_date']),'%d/%m/%Y %H:%M:%S')
  42. now = datetime.datetime.utcnow() - datetime.timedelta(minutes=15)
  43. now_dt = now.replace(microsecond=0)
  44.  
  45. # URLs for authentication
  46. authentication_endpoint = 'https://login.microsoftonline.com/'
  47. resource = 'https://api.loganalytics.io/'
  48.  
  49. # Get access token
  50. context = adal.AuthenticationContext('https://login.microsoftonline.com/' + tenant_id)
  51. token_response = context.acquire_token_with_client_credentials('https://api.loganalytics.io/', application_id, application_key)
  52. access_token = token_response.get('accessToken')
  53.  
  54. # Add token to header
  55. headers = {
  56. "Authorization": 'Bearer ' + access_token,
  57. "Content-Type":'application/json'
  58. }
  59.  
  60. # URLs for retrieving data
  61. uri_base = 'https://api.loganalytics.io/'
  62. uri_api = 'v1/'
  63. uri_workspace = 'workspaces/' + workspace + '/'
  64. uri_area = "query"
  65. uri = uri_base + uri_api + uri_workspace + uri_area
  66.  
  67. # Build search parameters from query details
  68. search_params = {
  69. "query": query,
  70. "timespan": start_datetime.strftime('%Y-%m-%dT%H:%M:%S') + '/' + now_dt.strftime('%Y-%m-%dT%H:%M:%S')
  71. }
  72.  
  73. # Send post request
  74. response = requests.post(uri,json=search_params,headers=headers)
  75.  
  76. # Response of 200 if successful
  77. if response.status_code == 200:
  78. # If debug, log event
  79. helper.log_debug('OMSInputName="' + str(input_name) + '" status="' + str(response.status_code) + '" step="Post Query" search_params="' + str(search_params) + "'")
  80. # Parse the response to get the ID and status
  81. data = response.json()
  82. else:
  83. # Request failed
  84. helper.log_error('OMSInputName="' + str(input_name) + '" status="' + str(response.status_code) + '" step="Post Query" response="' + str(response.text) + '"')
  85.  
  86. #Building proper json format from original request
  87. #First loop checks how many events returned is in response
  88. for i in range(len(data["tables"][0]["rows"])):
  89. data1 = "{"
  90. #This nested loop goes through each field, in each event, and concatenates the field name to the field value
  91. for n in range(len(data["tables"][0]["rows"][i])):
  92. field = str(data["tables"][0]["columns"][n]["name"])
  93. value = str(data["tables"][0]["rows"][i][n]).replace('"',"'").replace("\\", "\\\\").replace("None", "")
  94. if value == "":
  95. continue
  96. else:
  97. data1 += '"%s":"%s",' % (field, value)
  98. data1 += "}"
  99. data1 = data1.replace(",}", "}")
  100. event = Event()
  101. event.stanza = input_name
  102. event.data = data1
  103. ew.write_event(event)
  104.  
  105. #Delta
  106. state = now_dt.strftime("%d/%m/%Y %H:%M:%S")
  107. helper.save_check_point('last_date', state)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement