Microsoft cuts hundreds of Azure jobs in China as the borderless cloud splinters
Back to Tutorials
techTutorialintermediate

Microsoft cuts hundreds of Azure jobs in China as the borderless cloud splinters

June 9, 202612 views4 min read

Learn to build a cloud monitoring dashboard using Python and Azure SDK to track resource utilization across distributed cloud deployments.

Introduction

In the wake of Microsoft's recent Azure cloud layoffs in China, it's more important than ever to understand how to effectively manage and monitor cloud infrastructure. This tutorial will guide you through setting up a cloud monitoring dashboard using Python and Azure SDK, helping you track resource utilization and performance metrics across your cloud deployments. This is especially valuable as cloud providers increasingly segment their services based on geography and compliance requirements.

Prerequisites

  • Azure subscription with appropriate permissions
  • Python 3.7 or higher installed
  • Basic understanding of cloud computing concepts
  • Installed Azure CLI tools
  • Access to Azure Monitor and Application Insights

Step-by-Step Instructions

1. Set up your Python environment

First, create a virtual environment to isolate your project dependencies. This ensures that your cloud monitoring tools don't interfere with other Python projects.

python -m venv azure_monitor_env
source azure_monitor_env/bin/activate  # On Windows: azure_monitor_env\Scripts\activate
pip install azure-mgmt-monitor azure-identity azure-mgmt-resource

Why: Using a virtual environment prevents dependency conflicts and ensures reproducible environments for your monitoring tools.

2. Configure Azure authentication

Before accessing Azure resources, you'll need to authenticate. The recommended approach is using Azure CLI authentication:

az login

Then, create a service principal for programmatic access:

az ad sp create-for-rbac --name "CloudMonitorSP" --role contributor --scopes /subscriptions/YOUR_SUBSCRIPTION_ID

Why: This creates a secure authentication mechanism for your monitoring application without hardcoding credentials.

3. Initialize Azure Monitor client

Create a Python script to initialize the monitoring client:

from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient

# Initialize credential
credential = DefaultAzureCredential()
subscription_id = "your-subscription-id"

# Create monitor client
monitor_client = MonitorManagementClient(credential, subscription_id)
print("Azure Monitor client initialized successfully")

Why: The DefaultAzureCredential automatically handles authentication using your CLI login, making it easy to switch between local development and production environments.

4. Query resource metrics

Now, let's query metrics for a specific Azure resource:

from datetime import datetime, timedelta

# Define time range
end_time = datetime.utcnow()
start_time = end_time - timedelta(hours=1)

# Query metrics for a specific resource
resource_uri = "/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Compute/virtualMachines/{vm_name}"

metrics = monitor_client.metrics.list(
    resource_uri=resource_uri,
    timespan=f"{start_time.isoformat()}Z/{end_time.isoformat()}Z",
    interval="PT1H",
    metricnames="Percentage CPU,Network In,Network Out"
)

for metric in metrics.value:
    print(f"Metric: {metric.name.value}")
    for timeseries in metric.timeseries:
        for data in timeseries.data:
            print(f"  Time: {data.time_stamp}, Value: {data.average}")

Why: This allows you to track resource utilization over time, which is crucial for understanding how your cloud infrastructure performs and where optimization might be needed.

5. Create a simple monitoring dashboard

Let's build a basic dashboard to visualize key metrics:

import matplotlib.pyplot as plt
import pandas as pd

# Sample data processing
metric_data = []
for metric in metrics.value:
    for timeseries in metric.timeseries:
        for data in timeseries.data:
            metric_data.append({
                'timestamp': data.time_stamp,
                'metric': metric.name.value,
                'value': data.average or data.total or data.count
            })

# Convert to DataFrame for easier handling
df = pd.DataFrame(metric_data)
df['timestamp'] = pd.to_datetime(df['timestamp'])
df = df.sort_values('timestamp')

# Plot the data
plt.figure(figsize=(12, 6))
for metric_name in df['metric'].unique():
    metric_df = df[df['metric'] == metric_name]
    plt.plot(metric_df['timestamp'], metric_df['value'], label=metric_name)

plt.xlabel('Time')
plt.ylabel('Metric Value')
plt.title('Azure Resource Metrics Dashboard')
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Why: Visual dashboards help quickly identify performance issues and trends, especially important when managing distributed cloud resources across different regions.

6. Set up automated alerting

Configure alerts to notify you when resources exceed thresholds:

from azure.mgmt.monitor.models import (
    AlertRule, WebhookNotification, Action, RuleCondition, ThresholdRuleCondition
)

# Create an alert rule
rule = AlertRule(
    name="CPU_Utilization_Alert",
    description="Alert when CPU utilization exceeds 80%",
    is_enabled=True,
    condition=ThresholdRuleCondition(
        metric_source_uri=resource_uri,
        metric_name="Percentage CPU",
        operator="GreaterThan",
        threshold=80.0,
        time_aggregation="Average",
        window_size="PT5M"
    ),
    actions=[
        Action(
            action_type="Webhook",
            webhook_url="https://your-webhook-url.com/alert"
        )
    ]
)

# Create the rule
monitor_client.activity_log_alerts.create_or_update(
    resource_group_name="your-resource-group",
    activity_log_alert_name="CPU_Utilization_Alert",
    parameters=rule
)

Why: Automated alerts help you proactively manage cloud resources, especially important when dealing with geographic segmentation and regional compliance requirements.

7. Test your monitoring solution

Run your monitoring script to verify it works correctly:

python azure_monitor.py

Check that metrics are being collected and displayed properly. You should see output showing your resource utilization data and potentially the alert configuration.

Why: Testing ensures your monitoring solution works as expected before deploying it in production environments where cloud infrastructure might be distributed across multiple regions.

Summary

This tutorial demonstrated how to set up a cloud monitoring solution using Python and Azure SDK. By following these steps, you've learned to authenticate with Azure, query resource metrics, create visual dashboards, and set up automated alerts. These skills are crucial for managing cloud infrastructure, especially in light of recent industry trends showing how cloud providers are increasingly segmenting services by geography and compliance requirements. Understanding how to monitor and manage distributed cloud resources will help you maintain optimal performance even as cloud architecture becomes more complex.

Source: TNW Neural

Related Articles