question

SwapnilPanchal-4606 avatar image
0 Votes"
SwapnilPanchal-4606 asked hitesh81 edited

Unable to Deploy Guest Metrics using Terraform in Azure

I am trying to deploy below guest metric alert for all VMs under my subscription using terraform,But its throwing below error.

Error: Error creating or updating metric alert "Memory Usage Alert" (resource group "MyTemp"): insights.MetricAlertsClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="BadRequest" Message="Multi-resource alert rules are not supported for metrics in the Azure.VM.Windows.GuestMetrics namespace.

Looking at above error when I dig more in it , for guest metric multi resources are not supported.

I want to know is there any way we can pass mutliple Virtual machines and then run in for loop or something like that so that it will deploy the alert one by one on all VMs mentioned in list.?

Below is the Terraform template which is working absolutely fine for Platform Metrics and not for Guest Metrics.

 data "azurerm_subscription" "current" {
   subscription_id  = var.subscription_id //it will get the current subscription id
 }
    
 resource "azurerm_monitor_metric_alert" "myalert" {
   name                      = "Memory Usage Alert"
   resource_group_name       = "MyTemp" //resource name to which you want to deploy this alert
   scopes                    = [data.azurerm_subscription.current.id]
   description               = "Action will be triggered when Memory Usage is greater than 85."
   target_resource_type      = "Microsoft.Compute/virtualMachines"
   target_resource_location  = "centralindia"
   frequency                 = "PT30M"
   window_size               = "P1D"
   severity                  = "2"
   enabled                   = "true"
    
   criteria {
     metric_namespace = "Azure.VM.Windows.GuestMetrics"
     metric_name      = "Memory\\% Committed bytes in use"
     aggregation      = "Average"
     operator         = "GreaterThanOrEqual"
     threshold        = 85
   }
    
   action {
     action_group_id = "/subscriptions/xxxxxxxx/resourceGroups/xxxxx/providers/Microsoft.Insights/actionGroups/xxxxxxx"
   }
 }


azure-virtual-machinesazure-monitor
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Weily avatar image
0 Votes"
Weily answered JAROSLAWCZEPCZOR-3239 commented

You can try this sample at https://diverse.services/building-azure-monitor-for-application-monitoring-with-terraform/

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry to say, but this document is not helpful as answer of my question that I asked.

0 Votes 0 ·
CyrAz avatar image
1 Vote"
CyrAz answered hitesh81 edited

I'm currently working on a similar problem (deploying standardized alerts for every resources of a same type), and so far the solution we've come up with for metrics that were not supported in multi-resources mode is including the alerts resources in the resource's template itself.
So the alerts are created for each deployed resource at the same time as the resource itself.
That indeed creates a lot of alert rules, but we didn't come up with a better idea so far...

If anyone has a better idea, I'll be glad to hear it :)

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi CyrAz,

have you come across a new solution for this problem ? I need to create alerts for disk space monitor on all of our VMs and it looks like it is still not possible to scope the alert to all VMs.

Thanks
Thorsten


0 Votes 0 ·
hitesh81 avatar image hitesh81 TSchneider-3686 ·

You can use Telegraf plugin with influx db , thats what I did

install telegraf

 #!/bin/bash
 # Get the repo
 tee /etc/yum.repos.d/influxdb.repo <<-'EOF'
 [influxdb]
 name = InfluxDB Repository - RHEL 
 baseurl = https://repos.influxdata.com/rhel/7/x86_64/stable/
 enabled = 1
 gpgcheck = 1
 gpgkey = https://repos.influxdata.com/influxdb.key
 EOF
 #
 ## Install telegraf 
 #
    
 sudo yum install -y telegraf
    
 echo "Telegraf Package Installed"
 # generate the new Telegraf config file in the current directory 

configure telegraf


 #!/bin/bash
    
 

    telegraf --input-filter cpu:mem:disk:docker --output-filter azure_monitor config > /etc/telegraf/azm-telegraf.conf 
        
     # VERY IMPORTANT - DO NOT FORGET TO CHANGE THE FILE WITH CORRECT CONFIG TO telegraf.conf
     # replace the example config with the new generated config 
     sudo cp /etc/telegraf/azm-telegraf.conf /etc/telegraf/telegraf.conf
     # Add telegraf user to access docker daemon 
     sudo usermod -a -G docker telegraf
     # stop the telegraf agent on the VM 
     sudo systemctl stop telegraf 
     # start the telegraf agent on the VM to ensure it picks up the latest configuration 
     sudo systemctl start telegraf
     # Status of telegraf
     echo $(sudo systemctl status telegraf |grep -i 'Loaded' && sudo systemctl status telegraf |grep -i 'agent')










0 Votes 0 ·