1

If an azure VM is idle for 30 minutes I need to shut it down. By idle I mean CPU percent is less than 30%. How can I achieve this?

I have tried with run books default functions but it has shutdown and start but not with idle time.

theduck
  • 2,589
  • 13
  • 17
  • 23

1 Answers1

0

Try this by Powershell , you can run this command as a scheduled job based on your requirement :

$vm = Get-AzureRmVM -Name <your vm name> -ResourceGroupName <your resource group name>
$current = Get-Date

#get cpuMetrics for each minute in past 30 mins 
$cpuMetrics = Get-AzureRmMetric -ResourceId $vm.Id -TimeGrain 00:01:00 -StartTime $current.AddMinutes(-30) -EndTime $current -DetailedOutput -MetricNames "Percentage CPU"
$CPUUsangeRange = ($cpuMetrics.Data | select Average).Average | measure -Maximum

#get Maximum value of cpu usage percentage in past 30 mins, if the Maximum value less than 30% ,its idle and stop it .
if($CPUUsangeRange.Maximum -lt 30){
   Stop-AzureRMvm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force
}

Sometimes you cannot get last 2 or 3 mins cpu Metrics data as there will be some latency.

Stanley Gong
  • 11,522
  • 1
  • 8
  • 16
  • Get-AzureRmMetric : The term 'Get-AzureRmMetric' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:28 char:15 + $cpuMetrics = Get-AzureRmMetric -ResourceId $vm.Id -TimeGrain 00:01:0 ... + ~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-AzureRmMetric:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException – user12395612 Nov 21 '19 at 06:30
  • Hi @user12395612 , could you pls have a check that have you installed AzureRM.Insights module ? – Stanley Gong Nov 21 '19 at 06:33
  • Hi @user12395612 , there is no such command : Get-AzureRmMetricDefinition in my script . I am not sure why you will get this error . Btw , the AzureRM.Insights module version is 5.1.5 . I have tested this script on my side and it did work for me. – Stanley Gong Nov 21 '19 at 09:55
  • what is for Timegrain used – user12395612 Nov 21 '19 at 10:21
  • i have checked and installed azurerm.insights. now its working – user12395612 Nov 21 '19 at 10:22
  • Hi @user12395612 Timegrain is the unit of metrics , in my case is 00:01:00 which means to get each minute average CPU usage. Glad to know that it works for you . If it is helpful , pls mark this as an answer and I am glad to answer your further questions , thanks ! – Stanley Gong Nov 21 '19 at 11:54
  • Is there anything like Get-AzureRmMetric command will work only if insights is enabled in vm.. – user12395612 Nov 22 '19 at 07:17
  • Hi @user12395612 , it is based on what metrics that you want to monitor. Different kind of resources have different metrics . By default , for Azure VMs, the following metrics is enabled without additional configuration : CPU usage, network usage, total of disk bytes, and disk operations per second. You can see these metrics on Azure portal too. – Stanley Gong Nov 22 '19 at 07:33