Created
January 10, 2020 16:37
-
-
Save JamesDLD/19b3714627bb78e8decc7e6774735f63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
import datetime | |
import hashlib | |
import hmac | |
import base64 | |
#Retrieve your Log Analytics Workspace ID from your Key Vault Databricks Secret Scope | |
wks_id = dbutils.secrets.get(scope = "keyvault_scope", key = "wks-id-logaw1") | |
#Retrieve your Log Analytics Primary Key from your Key Vault Databricks Secret Scope | |
wks_shared_key = dbutils.secrets.get(scope = "keyvault_scope", key = "wks-shared-key-logaw1") | |
#The log type is the name of the event that is being submitted | |
log_type = 'WebMonitorTest' | |
#An example JSON web monitor object | |
json_data = [{ | |
"slot_ID": 12345, | |
"ID": "5cdad72f-c848-4df0-8aaa-ffe033e75d57", | |
"availability_Value": 100, | |
"performance_Value": 6.954, | |
"measurement_Name": "last_one_hour", | |
"duration": 3600, | |
"warning_Threshold": 0, | |
"critical_Threshold": 0, | |
"IsActive": "true" | |
}, | |
{ | |
"slot_ID": 67890, | |
"ID": "b6bee458-fb65-492e-996d-61c4d7fbb942", | |
"availability_Value": 100, | |
"performance_Value": 3.379, | |
"measurement_Name": "last_one_hour", | |
"duration": 3600, | |
"warning_Threshold": 0, | |
"critical_Threshold": 0, | |
"IsActive": "false" | |
}] | |
body = json.dumps(json_data) | |
##################### | |
######Functions###### | |
##################### | |
#Build the API signature | |
def build_signature(customer_id, shared_key, date, content_length, method, content_type, resource): | |
x_headers = 'x-ms-date:' + date | |
string_to_hash = method + "\n" + str(content_length) + "\n" + content_type + "\n" + x_headers + "\n" + resource | |
bytes_to_hash = str.encode(string_to_hash,'utf-8') | |
decoded_key = base64.b64decode(shared_key) | |
encoded_hash = (base64.b64encode(hmac.new(decoded_key, bytes_to_hash, digestmod=hashlib.sha256).digest())).decode() | |
authorization = "SharedKey {}:{}".format(customer_id,encoded_hash) | |
return authorization | |
#Build and send a request to the POST API | |
def post_data(customer_id, shared_key, body, log_type): | |
method = 'POST' | |
content_type = 'application/json' | |
resource = '/api/logs' | |
rfc1123date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT') | |
content_length = len(body) | |
signature = build_signature(customer_id, shared_key, rfc1123date, content_length, method, content_type, resource) | |
uri = 'https://' + customer_id + '.ods.opinsights.azure.com' + resource + '?api-version=2016-04-01' | |
headers = { | |
'content-type': content_type, | |
'Authorization': signature, | |
'Log-Type': log_type, | |
'x-ms-date': rfc1123date | |
} | |
response = requests.post(uri,data=body, headers=headers) | |
if (response.status_code >= 200 and response.status_code <= 299): | |
print ('Accepted') | |
else: | |
print ("Response code: {}".format(response.status_code)) | |
#Post the log | |
post_data(wks_id, wks_shared_key, body, log_type) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried this but got an error when sending requests.post(..).
Do you have any hints for this? thank you. Canh
ConnectionError: HTTPSConnectionPool(host='5040fd09-2b1a-460a-9d43-969ee00e162d.ods.opinsights.azure.com', port=443): Max retries exceeded with url: /api/logs?api-version=2016-04-01 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f1641156880>: Failed to establish a new connection: [Errno 110] Connection timed out'))
TimeoutError Traceback (most recent call last)
/databricks/python/lib/python3.8/site-packages/urllib3/connection.py in _new_conn(self)
158 try:
--> 159 conn = connection.create_connection(
160 (self._dns_host, self.port), self.timeout, **extra_kw
/databricks/python/lib/python3.8/site-packages/urllib3/util/connection.py in create_connection(address, timeout, source_address, socket_options)
83 if err is not None:
---> 84 raise err
85
/databricks/python/lib/python3.8/site-packages/urllib3/util/connection.py in create_connection(address, timeout, source_address, socket_options)
73 sock.bind(source_address)
---> 74 sock.connect(sa)
75 return sock
TimeoutError: [Errno 110] Connection timed out
During handling of the above exception, another exception occurred:
NewConnectionError Traceback (most recent call last)
/databricks/python/lib/python3.8/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
669 # Make the request on the httplib connection object.
--> 670 httplib_response = self._make_request(
671 conn,
/databricks/python/lib/python3.8/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
380 try:
--> 381 self._validate_conn(conn)
382 except (SocketTimeout, BaseSSLError) as e:
/databricks/python/lib/python3.8/site-packages/urllib3/connectionpool.py in _validate_conn(self, conn)
977 if not getattr(conn, "sock", None): # AppEngine might not have
.sock
--> 978 conn.connect()
979
/databricks/python/lib/python3.8/site-packages/urllib3/connection.py in connect(self)
308 # Add certificate verification
--> 309 conn = self._new_conn()
310 hostname = self.host
/databricks/python/lib/python3.8/site-packages/urllib3/connection.py in _new_conn(self)
170 except SocketError as e:
--> 171 raise NewConnectionError(
172 self, "Failed to establish a new connection: %s" % e
NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x7f1641156880>: Failed to establish a new connection: [Errno 110] Connection timed out
During handling of the above exception, another exception occurred:
MaxRetryError Traceback (most recent call last)
/databricks/python/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
438 if not chunked:
--> 439 resp = conn.urlopen(
440 method=request.method,
/databricks/python/lib/python3.8/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
725
--> 726 retries = retries.increment(
727 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
/databricks/python/lib/python3.8/site-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
445 if new_retry.is_exhausted():
--> 446 raise MaxRetryError(_pool, url, error or ResponseError(cause))
447
MaxRetryError: HTTPSConnectionPool(host='5040fd09-2b1a-460a-9d43-969ee00e162d.ods.opinsights.azure.com', port=443): Max retries exceeded with url: /api/logs?api-version=2016-04-01 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f1641156880>: Failed to establish a new connection: [Errno 110] Connection timed out'))
During handling of the above exception, another exception occurred:
ConnectionError Traceback (most recent call last)
in
----> 1 response = requests.post(uri,data=body, headers=headers)
/databricks/python/lib/python3.8/site-packages/requests/api.py in post(url, data, json, **kwargs)
117 """
118
--> 119 return request('post', url, data=data, json=json, **kwargs)
120
121
/databricks/python/lib/python3.8/site-packages/requests/api.py in request(method, url, **kwargs)
59 # cases, and look like a memory leak in others.
60 with sessions.Session() as session:
---> 61 return session.request(method=method, url=url, **kwargs)
62
63
/databricks/python/lib/python3.8/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
528 }
529 send_kwargs.update(settings)
--> 530 resp = self.send(prep, **send_kwargs)
531
532 return resp
/databricks/python/lib/python3.8/site-packages/requests/sessions.py in send(self, request, **kwargs)
641
642 # Send the request
--> 643 r = adapter.send(request, **kwargs)
644
645 # Total elapsed time of the request (approximately)
/databricks/python/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
514 raise SSLError(e, request=request)
515
--> 516 raise ConnectionError(e, request=request)
517
518 except ClosedPoolError as e:
ConnectionError: HTTPSConnectionPool(host='5040fd09-2b1a-460a-9d43-969ee00e162d.ods.opinsights.azure.com', port=443): Max retries exceeded with url: /api/logs?api-version=2016-04-01 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f1641156880>: Failed to establish a new connection: [Errno 110] Connection timed out')