I want to monitor metrics of all virtual machines inside my subscription with Azure CLI. According to Microsoft's "az monitor metrics alert" documentation, each alert created for a specific resource id, for example:
az monitor metrics alert create -n alert1 -g {ResourceGroup} --scopes {VirtualMachineID} --condition "avg Percentage CPU > 90" --description "High CPU"
Can I create an alert for monitoring high CPU for all VMs (instead of creating many alerts associated with a specific VM)?
Moreover, how can I configure my email as an alert action?
Thank you.
From the syntax, it seems like if we just pass a comma-separated list of Resource IDs to --scope parameter we should be able to create a metrics alert for multiple resources. But it doesn't seem to work as expected.
As an alternative, you can configure an ARM Template to create Azure Monitor Metrics alert that can target/monitor multiple resources in one or more Resource Groups, or in a Subscription.
You can refer this documentation on how to build ARM Templates for a Metric alert that monitors multiple resources.
In this template, you'll need to provide the Action Group ID for the Action Group which specifies the actions to be taken when the alert triggers. To obtain the Action Group ID, use Set-AzActionGroupto create an Action Group wherein you can specify the action to receive email in the -Receiver parameter.
Once you've configured your template & template parameters JSON file, you can use the following Az CLI cmdlet block to deploy it in Azure:
Connect-AzAccount
Select-AzSubscription -SubscriptionName <yourSubscriptionName>
New-AzResourceGroupDeployment -Name myDeployment -ResourceGroupName <ResourceGroupWhereRuleShouldbeSaved> -TemplateFile template.json -TemplateParameterFile templateparams.parameters.json
Hope this helps!
Related
I'm figuring on how do I automate my daily reports on azure resources which will send through outlook on a daily basis. I am beginner to this MS Azure.
An excel sheet that contains a list of subscriptions and within it are the resource group and its resources. An example of status would be Storage Account: Available 256 GB capacity / Virtual machine: Running 256GB available. With this list I would send an email to the relevant recipients. (Any suggestions on sending an email with an excel sheet at a certain time e.g 9am and 6pm)
I can list the all azure subscriptions, resource groups and resources using Azure cli
To get the all subscriptions which are there use the below command
Get-AzureRmSubscription
To get the list of resource groups and its subscription and the status
Get-AzureRmResourceGroup
I can also list the resource for particular region, resource group and name
az resource list --query "[].{resourceGRP:resourceGroup,name:name,location:location}"
To get the all resources list under the resource group
az resource list --resource-group Alldemorg --output table
For more information Refer this Document
I want to get app insights of all the subscriptions available in the portal but whenever I run the script
$resources = az monitor app-insights component show | ConvertFrom-Json
I get app insights only for the same subscription every time , even during the time when I change the subscription through the script
Set-AzContext -SubscriptionName "some-name"
the whole script goes like this
Set-AzContext -SubscriptionName "some-name"
$resources = az monitor app-insights component show | ConvertFrom-Json
So even if I change the subscription name to something else suppose "some-name1"
still I am getting the app-insights for subscription "some-name"
This is by design.
While you could switch the context in a script, searches across multiple subscriptions are easier and much, much faster using the Resource Graph.
PowerShell Query:
Search-AzGraph -Query "resources | where type =~ 'Microsoft.Insights/components'"
Azure CLI Query:
az graph query -q "resources | where type =~ 'Microsoft.Insights/components'"
Both options should get you all Application Insights resources across your tenant.
For more details, please see the Starter Resource Graph query samples.
On a side note I would also recommend to stick to either Azure CLI or Az PowerShell. While the choice of language is personal preference, sticking to one of the two decreases the dependencies. If you stick to Azure CLI, the only prerequisite is having the Azure CLI binaries installed. If you stick to Az Modules in PowerShell, you don't need Azure CLI but only the Az Modules. Mixing both makes the code more difficult to port to other machines.
So, if using the Az Modules was preferred, instead of...
$resources = az monitor app-insights component show | ConvertFrom-Json
I would recommend:
$resources = Search-AzGraph -Query "resources | where type =~ 'Microsoft.Insights/components'"
The issue you're experiencing with the Set-AzContext command is that it only sets the subscription context for the current PowerShell session.
The az monitor app-insights component show command is running in a separate process or thread, so it is not able to see the updated subscription context set by the Set-AzContext command.
To work around this, you can pass the -Subscription parameter to the az monitor app-insights component show command, like so:
$resources = az monitor app-insights component show --subscription "some-name1" | ConvertFrom-Json
This will ensure that the az command is running with the correct subscription context, and you will get the app insights for the correct subscription.
If there any way can deploy the resources to different subscription from one centralized deployment console?
I'm planning create the resource monitoring dashboards in different subscription, as of now manually I'm importing the JSON configuration file into different subscription and changing the resource values.
Looking for the solution kind of centralized deployment.
You can do this using Azure Powershell or the Azure CLI. In order to change subscriptions, an Azure PowerShell Context object first needs to be retrieved with Get-AzSubscription and then the current context changed with Set-AzContext.
$context = Get-AzSubscription -SubscriptionId ...
Set-AzContext $context
For Azure CLI you can do:
az account set --subscription "My Demos"
CLI also lets you scope deployments to Subscriptions or Management Group. An example would be:
az deployment sub create --location <location> --template-file <path-to-template>
https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/deploy-cli
I'm having some issues assigning one of the built-in policies with a logAnalytics parameter where there are multiple subscriptions involved. I need to do it with code. Here's how I try to accomplish it.
Get a reference to the built-in policy definition to assign
$definition = Get-AzPolicyDefinition | Where-Object { $_.Properties.DisplayName -eq 'Deploy Log Analytics agent for Windows VMs' }
$parameter = #{
logAnalytics = '<resourceId to my logAnalytics workspace>'
}
Create the policy assignment with the built-in definition against your resource group
New-AzPolicyAssignment -Name 'Deploy LA Agent Windows VMs' -DisplayName 'Deploy LA Agent Windows VMs' -Scope "/subscriptions/<my subscriptionId" -PolicyDefinition $definition -AssignIdentity -Location 'norwayeast' -PolicyParameterObject $parameter
This code works fine if I assign the policy to the same subscription where the logAnalytics workspace is located, but if I scope the policy assignment to another subscription and afterward check the assignment in the portal, the Log Analytics Workspace parameter will be empty.
The service principal that runs these commands is owner of both subscriptions.
The most straight-forward way of applying policies across multiple subscriptions is to make them part of a management group. You can apply a policy to the management group and every subscription which is a member will inherit it.
Further information on management groups can be found here:
https://learn.microsoft.com/en-us/azure/governance/management-groups/overview
Amended code using management groups:
New-AzPolicyAssignment -Name 'Deploy LA Agent Windows VMs' -DisplayName 'Deploy LA Agent Windows VMs' -Scope "/providers/Microsoft.Management/managementGroups/managementGroup001" -PolicyDefinition $definition -AssignIdentity -Location 'norwayeast' -PolicyParameterObject $parameter
Notes
You will need to create a management group and add subscription(s) to it before running any commands.
Assigning subscription(s) to an existing management group can have adverse effects, check there are no conflicts.
An error is likely to be generated regarding the length of the names used as they are restricted to 24 characters. You should shorten or abbreviate them.
I have not tested this code so please double-check by reviewing the documentation on Microsoft's site before running in your own environment.
Is there a way to configure Azure Activity logs to be forwarded to a Log Analytics instance using Azure CLI?
Hopefully equivalent to the PowerShell command:
New-AzureRmOperationalInsightsAzureActivityLogDataSource -ResourceGroupName <LogAnalyticsOMSWorkspaceResourceGroupName> -WorkspaceName <LogAnalyticsOMSWorkspaceName> -Name <NameOfThisOperationalInsightsAzureActivityLogDataSource> -SubscriptionId <SubscriptionId>
Significant changes have been made to Azure Monitor recently, with different services being consolidated in order to simplify monitoring for Azure customers.
Dedicated Azure CLI commands under the set az monitor activity-log alert are available for managing activity log alert rules.
To create a new activity log alert rule, use in this order:
az monitor activity-log alert create: Create new activity log alert rule resource
az monitor activity-log alert scope: Add scope for the created activity log alert rule
az monitor activity-log alert action-group: Add action group to the activity log alert rule
To retrieve one activity log alert rule resource, the Azure CLI command az monitor activity-log alert show can be used. And for viewing all activity log alert rule resource in a resource group, use az monitor activity-log alert list. Activity log alert rule resources can be removed using Azure CLI command az monitor activity-log alert delete.
https://learn.microsoft.com/en-us/azure/azure-monitor/platform/alerts-activity-log#cli