Azure Log Analytics Management Client

13.1.1 · active · verified Thu Apr 09

The `azure-mgmt-loganalytics` library is the Microsoft Azure Log Analytics Management Client Library for Python. It allows developers to programmatically manage Log Analytics workspaces, solutions, linked services, and other related resources within Azure. As part of the wider Azure SDK for Python, it adheres to the unified API guidelines and current authentication patterns. The current version is 13.1.1, and Azure SDKs generally follow a frequent release cadence, often monthly or bi-monthly, to keep pace with Azure service updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate using `DefaultAzureCredential` and create a `LogAnalyticsManagementClient`. It then lists Log Analytics workspaces within a specified resource group. Ensure `AZURE_SUBSCRIPTION_ID` and `AZURE_RESOURCE_GROUP` (or substitute for an existing RG) are set as environment variables, or other Azure credentials are configured for `DefaultAzureCredential` to pick up.

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.loganalytics import LogAnalyticsManagementClient

# Ensure you have AZURE_SUBSCRIPTION_ID and other Azure credentials set in your environment
# E.g., AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID for service principal
# Or login via `az login` for DefaultAzureCredential to pick up user credentials

subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")
resource_group_name = os.environ.get("AZURE_RESOURCE_GROUP", "my-loganalytics-rg")

if not subscription_id or subscription_id == "YOUR_SUBSCRIPTION_ID":
    raise ValueError("Please set the AZURE_SUBSCRIPTION_ID environment variable.")

credential = DefaultAzureCredential()
client = LogAnalyticsManagementClient(credential, subscription_id)

print(f"Listing Log Analytics workspaces in subscription {subscription_id}...")

# Example: List all workspaces in a specific resource group
# For simplicity, this example just lists the first 5 if available
workspaces = client.workspaces.list_by_resource_group(resource_group_name)

found_any = False
for i, workspace in enumerate(workspaces):
    if i >= 5: # Limit output for quickstart
        break
    print(f"- Workspace: {workspace.name} (Location: {workspace.location})")
    found_any = True

if not found_any:
    print(f"No Log Analytics workspaces found in resource group '{resource_group_name}'.")

view raw JSON →