Loading

A walkthrough with pics | TLDR: Get the VM, setup Alerts for metric data (cpu usage), setup an alert action group to call a runbook. Runbook turns off VM.

Need to know how to turn off a VM if the machine is idle and not being used? Well, you should! Since Azure is pay-as-you-go, you really only want to keep them machines on IF you are using them, otherwise turn them off. Because if you don’t you are just giving your money away.

Let’s start then…go find that VM you want to shut down!

1.) Select the VM. In our example I am using LennyTestVM.

In the left side blade, menu, section whatever you want to call it, go to Alerts. From there, click on ‘+Create’ then Alert rule. We want to be alerted when the machine’s cpu is on idle for a certain time. This will be our trigger.

Once Alert Rule has been selected, now we configure this rule. In this example we are using metric data based on the platform monitoring service.

So, what is our Signal? In the search box, type and search for Percentage CPU.

Look at the right bottom quadrant to see what I’m talking about.

Select it and click on next. Let’s take a look at the Alert Logic next. For the threshold select Static. What’s the difference between Static and Dynamic?

For Aggregation Type select Average. This says, whatever time you specify for me to look at, if the average of the data points is below/above x amount of time, then do y. For the operator select less than. The threshold value is how you define the CPU percentage value, so if you set it at 10%, that means that if over a certain amount of time, if the average of CPU percentage is less than 10%, set the trigger off.

Let’s go to the next section. When to evaluate. Here we have 2 options: Check every and Lookback period. Check every means how often this rule will check to see if the condition is meant. It’s kind of like the supervisor saying to you, ‘hey how’s it going? How’s that project?’ The lookback period means how far back you will check at each check every. In this case I used 1 minute for the check every and 5 minutes for the lookback period.

So, let’s put it together to understand it completely. I want my alert rule to trigger based on a condition (the average CPU percentage is less than 10%), right? So, this rule will check to see if this is true, every 1 minute. At that minute, it will lookback at the previous 5 minutes and take the average of those datapoints. IF the average is less than my threshold value of 10%, boom, something happens. Wait, what happens?

2.) Selecting or creating your Alert Action Group.

This next part is the action if the trigger goes off. Like a gun, the trigger is…well the trigger. The action it does afterwards – whether it’s some sort of notification or calling another azure service – is like the bullet in the gun. In this case, we want to shut down the VM. So, let’s go ahead and create an action group.

So, click on ‘+Create Action group’. Afterwards give it a name and display name if you’d like.

You click on next and if you’d like to setup a notification you can. For the sake of this example, we will give it a simple notification. Under notification type, I’ll select Email/SMS message/Push/Voice. Then give that notification a name. On the right menu that popped up, define whether you want an email or SMS message or whatever you want then click Ok and next.

Next is what we actually want to happen! Turn off the damn VM. Under Action type, you’ll see several actions you can call from here. It’s pretty neat. Select Automation Runbook as if this alert gets triggered, we want to call an automation runbook to shut down our VM.

Like babies, give it a name. Then tell it what runbook you want it to run when it gets triggered. In this case, I am using a simple runbook called TurnoffVM. After you select it, click on ok. You can then add a tag if you’d like or just go to the review and create section and finish there.

The runbook is doing this:

Disable-AzContextAutosave -Scope Process
Connect-AzAccount -Identity
$AzureContext = Set-AzContext -SubscriptionId "xxxxxxx-xxxxx-xxxxx-xxxxxxxxxxxxx"
 
$StopVM = Stop-AzVm -ResourceGroupName 'Resource Group where VM lives in' -Name 'VM Name here' -Force

$StopVM

In all, what we just did was create an alert rule + an action to take when it gets triggered. Every minute the alert will check the last 5 minutes of the VM’s CPU usage. If the average CPU usage is less than my threshold value of 10% then it will trigger or call my automation runbook called TurnoffVM which will turn off my VM I specified.

There ya go! Good job, you just automatically shut down a VM based on average CPU usage!