Alerts are great for letting you know when Logic Apps or Function Apps fail to run for any reason.. the problem is, while you can list out which resources have Alerts, you can’t get a list of resources which don’t have Alerts!
So how do you find out which resources you need to add Alerts to? Well, the CLI has the tools you need, but it’s a bit more complicated than it should be!
First you need a list of resources which have alerts.. you can do that with a command like this;
az resource list --output tsv --resource-type "Microsoft.Insights/metricAlerts" --query [].id
You can run that as part of this command, which isĀ supposed to take the Alert ID list, pipe it into az resource show, and output the value in properties.scopes[0], which is the ID of the resource the alert is set up to monitor.
az resource show --ids $(az resource list --output tsv --resource-type "Microsoft.Insights/metricAlerts" --query [].id) --query properties.scopes[0] --output tsv
However, this fails miserably when the Alert name has a space in it; there ends up being a space in the Alert ID, which makes our command throw an error. UPDATE – Microsoft are figuring out if they can fig it in this bug report I raised.
What I ended up having to do is break up the commands inside a Bash script & use a loop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alerts=$(az resource list –output tsv –resource-type "Microsoft.Insights/metricAlerts" –query [].id) | |
alerts=${alerts// /SPACEFIX} | |
for alert in $alerts | |
do | |
az resource show –ids "${alert//SPACEFIX/ }" –query properties.scopes[0] –output tsv | |
done |
This will assign all the Alert IDs to a variable, then replaces spaces with ‘SPACEFIX’. We then loop over the Alert IDs, querying the Alert to find what resource they’re monitoring. ‘SPACEFIX’ is replaced within the loop. It’s somewhat of a hack to get round this silly issue!
There might be a better way to handle this, but I didn’t find it in a hurry.
Now we’ve got a list of resources that have alerting enabled, we need to list out all the resources we want to check.. for this example I’m just interested in Logic Apps, and this script will list them out, one resource per line;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
logicapps=$(az resource list –output tsv –resource-type "Microsoft.Logic/workflows" –query [].id) | |
for logicapp in $logicapps | |
do | |
echo "$logicapp" | |
done |
Now we can redirect the output of the 2 scripts into temporary files, and use the ‘comm’ command to show us the lines that exist in one file that don’t in the other.. here’s how it looks;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo Finding what Alerts exist already … | |
./listAlertIds.sh > listAlertIds.tmp | |
echo Finding what Logic Apps exist … | |
./listLogicApps.sh > listLogicApps.tmp | |
echo Logic Apps without an Alert are … | |
comm -23 <(sort -u ./listLogicApps.tmp) <(sort -u ./listAlertIds.tmp) |
This gets us what we want.. a list of Logic Apps that don’t have Alerts set up for them. You can pipe the results through grep to check certain Resource Groups, or whatever you need.
Hope this helps save you some time, and gives you a few ideas for useful scripts.
I’ve published some videos on YouTube showing various Azure tips, which you might want to check out!