Using Azure Monitor with Shell Scripts
Introduction to Azure Monitor
Azure Monitor is a comprehensive monitoring service provided by Microsoft Azure. It helps you maximize the availability and performance of your applications by delivering a comprehensive solution for collecting, analyzing, and acting on telemetry from your cloud and on-premises environments. Integrating Azure Monitor with shell scripts can streamline and automate your monitoring processes.
Setting Up Azure CLI
Before integrating Azure Monitor with shell scripts, you need to set up the Azure Command Line Interface (CLI). Follow these steps to install and configure the Azure CLI:
#!/bin/bash
# Update the package list and install Azure CLI
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
# Log in to Azure
az login
During the login process, you will be prompted to open a web page and enter a code to authenticate your Azure account.
Monitoring Virtual Machines
You can use shell scripts to monitor your Azure Virtual Machines (VMs) using Azure Monitor. Here is an example script to get CPU utilization of a VM:
#!/bin/bash
# Define variables
RESOURCE_GROUP="your_resource_group"
VM_NAME="your_vm_name"
METRIC_NAME="Percentage CPU"
START_TIME=$(date -u -d '1 hour ago' +"%Y-%m-%dT%H:%M:%SZ")
END_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
TIME_GRAIN="PT1M"
# Get CPU utilization from Azure Monitor
az monitor metrics list --resource $VM_NAME --resource-group $RESOURCE_GROUP --metric $METRIC_NAME --start-time $START_TIME --end-time $END_TIME --interval $TIME_GRAIN
Replace your_resource_group
and your_vm_name
with the appropriate values. This script fetches the CPU utilization of the specified VM in the past hour.
Creating Alerts
Azure Monitor Alerts allow you to monitor metrics and automatically perform actions when a threshold is breached. Here is a script to create a CPU utilization alert for a VM:
#!/bin/bash
# Define variables
ALERT_NAME="HighCPUUtilization"
RESOURCE_GROUP="your_resource_group"
VM_NAME="your_vm_name"
METRIC_NAME="Percentage CPU"
THRESHOLD=80
DESCRIPTION="Alert when CPU utilization is greater than 80%"
# Create Azure Monitor alert
az monitor metrics alert create --name $ALERT_NAME --resource-group $RESOURCE_GROUP --resource $VM_NAME --metric $METRIC_NAME --operator GreaterThan --threshold $THRESHOLD --description "$DESCRIPTION"
Replace the placeholders with your specific values. This script creates an alert that triggers when the CPU utilization of the specified VM exceeds 80%.
Logging to Azure Monitor
Azure Monitor Logs allows you to collect and analyze log data from various sources. Here is a script to configure Azure Monitor Logs for your application logs:
#!/bin/bash
# Define variables
WORKSPACE_NAME="your_log_analytics_workspace"
RESOURCE_GROUP="your_resource_group"
LOG_TYPE="CustomLog"
LOG_MESSAGE="This is a test log message"
TIMESTAMP=$(date +%s)
# Send a log message to Azure Monitor Logs
az monitor log-analytics workspace list
az monitor log-analytics workspace create --resource-group $RESOURCE_GROUP --workspace-name $WORKSPACE_NAME
az monitor log-analytics workspace data-export create --workspace-name $WORKSPACE_NAME --resource-group $RESOURCE_GROUP --destination $LOG_TYPE --query "$LOG_MESSAGE" --start-time $TIMESTAMP
Replace the placeholders with your specific values. This script creates a Log Analytics workspace and sends a test log message to Azure Monitor Logs.
Automating Azure Monitor Setup with Shell Scripts
Here is a complete shell script to automate the setup of Azure Monitor monitoring and logging:
#!/bin/bash
# Install Azure CLI
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
az login
# Define variables
RESOURCE_GROUP="your_resource_group"
VM_NAME="your_vm_name"
METRIC_NAME="Percentage CPU"
START_TIME=$(date -u -d '1 hour ago' +"%Y-%m-%dT%H:%M:%SZ")
END_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
TIME_GRAIN="PT1M"
ALERT_NAME="HighCPUUtilization"
THRESHOLD=80
DESCRIPTION="Alert when CPU utilization is greater than 80%"
WORKSPACE_NAME="your_log_analytics_workspace"
LOG_TYPE="CustomLog"
LOG_MESSAGE="This is a test log message"
TIMESTAMP=$(date +%s)
# Get CPU utilization from Azure Monitor
az monitor metrics list --resource $VM_NAME --resource-group $RESOURCE_GROUP --metric $METRIC_NAME --start-time $START_TIME --end-time $END_TIME --interval $TIME_GRAIN
# Create Azure Monitor alert
az monitor metrics alert create --name $ALERT_NAME --resource-group $RESOURCE_GROUP --resource $VM_NAME --metric $METRIC_NAME --operator GreaterThan --threshold $THRESHOLD --description "$DESCRIPTION"
# Create Log Analytics workspace and send a log message
az monitor log-analytics workspace create --resource-group $RESOURCE_GROUP --workspace-name $WORKSPACE_NAME
az monitor log-analytics workspace data-export create --workspace-name $WORKSPACE_NAME --resource-group $RESOURCE_GROUP --destination $LOG_TYPE --query "$LOG_MESSAGE" --start-time $TIMESTAMP
Save this script as setup_azure_monitor.sh
and make it executable:
chmod +x setup_azure_monitor.sh
./setup_azure_monitor.sh
Conclusion
Integrating Azure Monitor with shell scripts provides a powerful way to automate the monitoring and logging of your Azure resources. By following this tutorial, you can set up Azure CLI, monitor your VMs, create alerts, and log data to Azure Monitor Logs. This automation helps ensure your applications remain performant and reliable.