how to send an alert email if an azure virtual machine is running for more than 12 hours

Sujana 0 Reputation points
2024-03-08T12:10:01.1766667+00:00

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

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,645 questions
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,015 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Marcin Policht 49,640 Reputation points MVP Volunteer Moderator
    2024-03-08T12:47:26.9466667+00:00
    0 comments No comments

  2. Dan Rios 2,020 Reputation points MVP
    2024-03-08T12:54:20.0366667+00:00

    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:

    https://learn.microsoft.com/en-us/azure/azure-monitor/agents/azure-monitor-agent-windows-client#install-the-agent

    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.

    • From here, you can now click the 'New alert rule' button, click next on the default and then move onto the 'Action' tab.
    • Under the action tab you will be able to create an 'Action group' giving it a name and display name.
    • On the notifications tab you can select the 'Email/SMS message/Push/Voice' type and tick the Email option, enter your alert address and OK.

    User's image

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.