Follow https://stackoverflow.com/questions/59442611/alert-for-azure-virtual-machine-running-for-x-hours
hth
Marcin
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
We are running azure virtual desktop personal pools with start vm on connect. Would like to monitor uptime of virtual machines and if VM is running for more than 11 hours would like to send an email to our admin group
Hi Sujana,
Firstly, make sure the VM has the Azure Monitoring Agent installed so it can report into Azure Monitor, you can follow the Microsoft documentation to help you if this needs to be done:
Once installed, you should be able to receive heartbeat information about the Virtual Machine and we can then create an alert based on the query. Here's an example one I mocked up and checked the results for:
Heartbeat
| where TimeGenerated > ago(12h)
| where Computer == "YOUR VM NAME"
| summarize Uptime = max(TimeGenerated) - min(TimeGenerated) by Computer
| where Uptime > 11h
Go to your Azure Virtual Machine > Under Monitoring > Logs > Paste the query in to the editor, amend the name to suit > Run
Verify this is returning what you expect.
Review & create to complete.
Hope this helps guide you to the solution, obviously you can tweak the values based on the requirements.
Alternatively, I would look to explore the auto-shutdown option natively built-in to the Azure Portal if a concern if runtime of a VM, see more about this here: https://learn.microsoft.com/en-us/azure/virtual-machines/auto-shutdown-vm?tabs=portal
If this was useful, please feel free to mark as the accepted answer.