Skip to content

Instantly share code, notes, and snippets.

@alexeldeib
Created July 8, 2019 14:55
Show Gist options
  • Save alexeldeib/5096dea875500592c743cc517180bfd2 to your computer and use it in GitHub Desktop.
Save alexeldeib/5096dea875500592c743cc517180bfd2 to your computer and use it in GitHub Desktop.
Log Analytics Python SDK Demo
import requests
import json
from azure.common.credentials import ServicePrincipalCredentials
from azure import loganalytics
TENANT_ID = ''
CLIENT_ID = ''
KEY = '' # Client Secret
WORKSPACE_ID = '' # from the log analytics workspace
credentials = ServicePrincipalCredentials(
client_id = CLIENT_ID,
secret = KEY,
tenant = TENANT_ID,
resource = "https://api.loganalytics.io "
)
client = loganalytics.log_analytics_data_client.LogAnalyticsDataClient(credentials, base_url=None)
workspace_id = WORKSPACE_ID
body = loganalytics.models.QueryBody(query = "union * | take 1") # the query
query_results = client.query(workspace_id, body) # type: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/loganalytics/azure-loganalytics/azure/loganalytics/models/query_results.py
table = query_results.tables[0] # https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/loganalytics/azure-loganalytics/azure/loganalytics/models/table.py
columns = table.columns
rows = table.rows # [][] of arbitrary data
print(columns) # VERY noisy
print(rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment