다음을 통해 공유


Azure CLI에서 메트릭 경고 만들기

이 샘플은 Azure CLI 명령을 사용하여 Azure Monitor에서 메트릭 경고 모니터를 만듭니다. 첫 번째 샘플은 가상 머신에 대한 경고를 만듭니다. 두 번째 명령은 App Service 플랜에 대한 차원을 포함하는 경고를 만듭니다.

사전 요구 사항

경고 만들기

이 경고는 ContosoVMRG라는 리소스 그룹에 있는 가상 머신(이름: VM07)을 모니터링합니다.

az group create 명령을 사용하여 리소스 그룹을 만들 수 있습니다. 가상 머신을 만드는 방법에 대한 정보는 Azure CLI를 사용하여 Windows 가상 머신 만들기, Azure CLI를 사용하여 Linux 가상 머신 만들기az vm Create 명령을 참조하세요.

# resource group name: ContosoVMRG
# virtual machine name: VM07

# Create scope
scope=$(az vm show --resource-group ContosoVMRG --name VM07 --output tsv --query id)

# Create action
action=$(az monitor action-group create --name ContosoWebhookAction \
  --resource-group ContosoVMRG --output tsv --query id \
  --action webhook https://alerts.contoso.com usecommonalertschema)

# Create condition
condition=$(az monitor metrics alert condition create --aggregation Average \
  --metric "Percentage CPU" --op GreaterThan --type static --threshold 90 --output tsv)

# Create metrics alert
az monitor metrics alert create --name alert-01 --resource-group ContosoVMRG \
  --scopes $scope --action $action --condition $condition --description "Test High CPU"

이 샘플에서는 인용 부호와 같은 원치 않는 기호를 포함하지 않는 tsv 출력 형식을 사용합니다. 자세한 내용은 효과적인 Azure CLI 사용을 참조하세요.

차원을 사용하여 경고 만들기

이 샘플에서는 App Service 플랜을 만든 다음, 해당 플랜에 대한 메트릭 경고를 만듭니다. 이 예제에서는 차원을 사용하여 App Service 플랜의 모든 인스턴스가 이 메트릭에 해당함을 지정합니다. 샘플에서는 리소스 그룹 및 애플리케이션 서비스 플랜을 만듭니다.

# Create resource group
az group create --name ContosoRG --location eastus2
 
# Create application service plan
az appservice plan create --resource-group ContosoRG --name ContosoAppServicePlan \
   --is-linux --number-of-workers 4 --sku S1 
 
# Create scope
scope=$(az appservice plan show --resource-group ContosoRG --name ContosoAppServicePlan \
   --output tsv --query id) 
 
# Create dimension
dim01=$(az monitor metrics alert dimension create --name Instance --value * --op Include --output tsv)
 
# Create condition
condition=$(az monitor metrics alert condition create --aggregation Average \
   --metric CpuPercentage --op GreaterThan --type static --threshold 90 \
   --dimension $dim01 --output tsv)

가능한 메트릭의 목록을 보려면 az monitor metrics list-definitions 명령을 실행합니다. --output 매개 변수는 읽을 수 있는 형식으로 값을 표시합니다.

az monitor metrics list-definitions --resource $scope --output table 
 
# Create metrics alert
az monitor metrics alert create --name alert-02 --resource-group ContosoRG \
   --scopes $scope --condition $condition --description "Service Plan High CPU"

배포 정리

이 명령을 테스트하기 위해 리소스 그룹을 만들었다면 az group delete 명령을 사용하여 해당 리소스 그룹과 관련 콘텐츠를 모두 제거할 수 있습니다.

az group delete --name ContosoVMRG

az group delete --name ContosoRG

그대로 유지하려는 기존 리소스 그룹을 사용했다면 az monitor metrics alert delete 명령을 사용하여 연습 경고를 삭제합니다.

az monitor metrics alert delete --name alert-01

az monitor metrics alert delete --name alert-02

이 문서에서 사용되는 Azure CLI 명령

이 문서에서는 다음 Azure CLI 명령을 사용합니다.

다음 단계