From 6e504a4cc559b834851d096286960e0b37519bfa Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Wed, 8 May 2019 16:38:26 -0700 Subject: [PATCH 1/3] Fix scaleset vm resource id. --- .../outputs/azure_monitor/azure_monitor.go | 56 +++++++++++-------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/plugins/outputs/azure_monitor/azure_monitor.go b/plugins/outputs/azure_monitor/azure_monitor.go index 408976c536338..f7987430c5c86 100644 --- a/plugins/outputs/azure_monitor/azure_monitor.go +++ b/plugins/outputs/azure_monitor/azure_monitor.go @@ -43,6 +43,32 @@ type AzureMonitor struct { MetricOutsideWindow selfstat.Stat } +// VirtualMachineMetadata contains information about a VM from the metadata service +type virtualMachineMetadata struct { + Compute struct { + Location string `json:"location"` + Name string `json:"name"` + ResourceGroupName string `json:"resourceGroupName"` + SubscriptionID string `json:"subscriptionId"` + VMScaleSetName string `json:"vmScaleSetName"` + } `json:"compute"` +} + +func (m *virtualMachineMetadata) ResourceID() string { + var template string + if m.Compute.VMScaleSetName == "" { + template = resourceIDTemplate + } else { + template = resourceIDScaleSetTemplate + } + return fmt.Sprintf( + template, + m.Compute.SubscriptionID, + m.Compute.ResourceGroupName, + m.Compute.Name, + ) +} + type dimension struct { name string value string @@ -63,11 +89,12 @@ const ( defaultNamespacePrefix = "Telegraf/" defaultAuthResource = "https://monitoring.azure.com/" - vmInstanceMetadataURL = "http://169.254.169.254/metadata/instance?api-version=2017-12-01" - resourceIDTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s" - urlTemplate = "https://%s.monitoring.azure.com%s/metrics" - urlOverrideTemplate = "%s%s/metrics" - maxRequestBodySize = 4000000 + vmInstanceMetadataURL = "http://169.254.169.254/metadata/instance?api-version=2017-12-01" + resourceIDTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s" + resourceIDScaleSetTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachineScaleSets/%s" + urlTemplate = "https://%s.monitoring.azure.com%s/metrics" + urlOverrideTemplate = "%s%s/metrics" + maxRequestBodySize = 4000000 ) var sampleConfig = ` @@ -203,28 +230,13 @@ func vmInstanceMetadata(c *http.Client) (string, string, error) { return "", "", fmt.Errorf("unable to fetch instance metadata: [%v] %s", resp.StatusCode, body) } - // VirtualMachineMetadata contains information about a VM from the metadata service - type VirtualMachineMetadata struct { - Compute struct { - Location string `json:"location"` - Name string `json:"name"` - ResourceGroupName string `json:"resourceGroupName"` - SubscriptionID string `json:"subscriptionId"` - } `json:"compute"` - } - - var metadata VirtualMachineMetadata + var metadata virtualMachineMetadata if err := json.Unmarshal(body, &metadata); err != nil { return "", "", err } region := metadata.Compute.Location - resourceID := fmt.Sprintf( - resourceIDTemplate, - metadata.Compute.SubscriptionID, - metadata.Compute.ResourceGroupName, - metadata.Compute.Name, - ) + resourceID := metadata.ResourceID() return region, resourceID, nil } From a903e1111c18ae13ac6e999572d4bc52384b21a5 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Wed, 15 May 2019 15:15:35 -0700 Subject: [PATCH 2/3] Use scale set name instead of computer name in resource id url --- .../outputs/azure_monitor/azure_monitor.go | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/plugins/outputs/azure_monitor/azure_monitor.go b/plugins/outputs/azure_monitor/azure_monitor.go index f7987430c5c86..8105584286b44 100644 --- a/plugins/outputs/azure_monitor/azure_monitor.go +++ b/plugins/outputs/azure_monitor/azure_monitor.go @@ -55,18 +55,21 @@ type virtualMachineMetadata struct { } func (m *virtualMachineMetadata) ResourceID() string { - var template string if m.Compute.VMScaleSetName == "" { - template = resourceIDTemplate + return fmt.Sprintf( + resourceIDScaleSetTemplate, + m.Compute.SubscriptionID, + m.Compute.ResourceGroupName, + m.Compute.VMScaleSetName, + ) } else { - template = resourceIDScaleSetTemplate - } - return fmt.Sprintf( - template, - m.Compute.SubscriptionID, - m.Compute.ResourceGroupName, - m.Compute.Name, - ) + return fmt.Sprintf( + resourceIDTemplate, + m.Compute.SubscriptionID, + m.Compute.ResourceGroupName, + m.Compute.Name, + ) + } } type dimension struct { @@ -227,7 +230,8 @@ func vmInstanceMetadata(c *http.Client) (string, string, error) { return "", "", err } if resp.StatusCode >= 300 || resp.StatusCode < 200 { - return "", "", fmt.Errorf("unable to fetch instance metadata: [%v] %s", resp.StatusCode, body) + return "", "", fmt.Errorf("unable to fetch instance metadata: [%s] %d", + vmInstanceMetadataURL, resp.StatusCode) } var metadata virtualMachineMetadata From 8d5fb36cc12ed5995dc6f4f69c68586de08e9b1d Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Thu, 16 May 2019 10:28:33 -0700 Subject: [PATCH 3/3] Fix reversed scale set logic --- plugins/outputs/azure_monitor/azure_monitor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/outputs/azure_monitor/azure_monitor.go b/plugins/outputs/azure_monitor/azure_monitor.go index 8105584286b44..9039d4aa4c06e 100644 --- a/plugins/outputs/azure_monitor/azure_monitor.go +++ b/plugins/outputs/azure_monitor/azure_monitor.go @@ -55,7 +55,7 @@ type virtualMachineMetadata struct { } func (m *virtualMachineMetadata) ResourceID() string { - if m.Compute.VMScaleSetName == "" { + if m.Compute.VMScaleSetName != "" { return fmt.Sprintf( resourceIDScaleSetTemplate, m.Compute.SubscriptionID,