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, 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 --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 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;
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 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!