Share via


monitoring VM uptime

Question

Tuesday, January 29, 2019 1:05 AM

I have a large VM that occasionally started up and do some work then shutdown after the work is done. Sometime I just forget shut it down and so get unnecessary charge. Is there a way to monitor it, so that if the VM is up more than half hour, it will send alert email to me?

Thanks.

All replies (5)

Tuesday, January 29, 2019 9:41 AM âś…Answered | 1 vote

Hi,

The above mentioned doc explains about automating the start/stop of VMs using Azure Automation service and you can also schedule them accordingly.

To answer your question for getting alerted if a VM is up for more than 30 minutes then you may follow below listed steps,

  1. Create a Log Analytics workspace if you don't have one already.

  2. Create an action group by providing your email id under action type.

  3. Develop a uptime Kusto query something like shown below.

Perf
| where ( ObjectName == "System" )
| where ( CounterName == "Uptime" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)

  1. Create a custom new alert under Log Analytics alerts tile by including above mentioned query in the condition block. Then set the threshold greater than 1800 (as the countervalue of uptime counter is represented in seconds), period and frequency as required. Add the action group which you have created earlier in step #2.

Regards,

Subhash.V


Tuesday, January 29, 2019 2:34 AM

Hi,

Thanks for posting here.

You can start/stop VMs during oof-hours in Azure Automation. For quick reference, Please have a look at this doc.

VMs-Management

If you think your question has been answered click "Mark as Answer" if just helped click "Vote as helpful". This can be beneficial to other community members reading this forum thread.
________________________________________________________________________________
Best regards
Subhash


Tuesday, January 29, 2019 4:20 AM

Adding to what Subhash mentioned, you can run the below script to turn OFF all the VMs in your subscription:

https://gallery.technet.microsoft.com/Script-to-turn-OFF-all-VMs-dfd3740a?redir=0 

Regards, 

Msrini


Tuesday, January 29, 2019 4:40 AM

Hi Subhash and Msrini,

thanks for the response. I know how to shutdown VM. My original question is that I want to get notified when the VM has been up for more than half hour. It maybe legit and work is still going on, or could be just forget to shutdown.

Thanks!


Tuesday, January 29, 2019 6:48 AM | 1 vote

Hello, 

The below command will return 1 if the VM is Turned ON:

Get-AzureRmVM `
    -ResourceGroupName $vm.ResourceGroupName `
    -Name $vm.Name `
    -Status |Select {$_.Statuses[1].Code -Match "PowerState/running"}

So you can create a script which will trigger an alert when the status is ON for a specific time. 

If it is useful, hit a vote. If it answer's your question, please mark it as answered. 

Cheers

Msrini