How to: Schedule Azure VM Shutdown based on CPU, Disk, Network Usage

In my previous post, I talked about how you can schedule Azure VMs to shutdown based on times of the day or week with a 3rd party service called VMPower. However you may also want to shutdown your VMs based on cpu, disk or network utilization. This is useful for things such as machines that users regularly remote desktop into or VMs that run tasks which vary in the time they take (such as CI/Build servers).

My Desktop Linux Machine

I have an internal 'jumpbox' Linux development machine which I use to directly access things on an internal subnet within an Azure Virtual Network. I use VNC (similar to RDP for Windows) to connect to it. Here's the network utilization of the VM when someone is connected to this machine, vs when someone is not:

Just based on Network utilization alone, it is easy to tell when this VM is in use. It would be great if this VM can turn off after I stop using it - incase I forget to turn it off myself.

The Hard Way

You can do this using Azure automation by creating a Powershell script which reads from the Azure metrics API. At some point you would call the Virtual Machines API at the right time (and optionally send a notification) . This way requires a bit of Powershell scripting prowess and can be a bit time consuming setting up the policies which control how your VMs shutdown.

The Easy Way

You can also do the same thing very quickly with VMPower. It allows you to set Azure VMs to shutdown based on cpu, disk, and network utilization average or maximum values. It also handles sending your team notifications via email and Slack.

Step 1) Signup for an account at VMPower.io. It doesn’t require a credit card and they have a decent free tier as well as credit-card-free trials.

Step 2) Connect your Azure subscription to VMPower like I mention in my last blog post.