From c8a2ffdb7225019be2d2878005f6812d4f58f2cc Mon Sep 17 00:00:00 2001 From: darshanime Date: Sun, 25 Jul 2021 22:04:32 +0530 Subject: [PATCH 1/2] Add computer name to azure sd Signed-off-by: darshanime --- discovery/azure/azure.go | 5 +++++ discovery/azure/azure_test.go | 20 ++++++++++++++++++++ docs/configuration/configuration.md | 1 + 3 files changed, 26 insertions(+) diff --git a/discovery/azure/azure.go b/discovery/azure/azure.go index a23e80d8c..7f6043c95 100644 --- a/discovery/azure/azure.go +++ b/discovery/azure/azure.go @@ -46,6 +46,7 @@ const ( azureLabelMachineID = azureLabel + "machine_id" azureLabelMachineResourceGroup = azureLabel + "machine_resource_group" azureLabelMachineName = azureLabel + "machine_name" + azureLabelMachineComputerName = azureLabel + "machine_computer_name" azureLabelMachineOSType = azureLabel + "machine_os_type" azureLabelMachineLocation = azureLabel + "machine_location" azureLabelMachinePrivateIP = azureLabel + "machine_private_ip" @@ -226,6 +227,7 @@ type azureResource struct { type virtualMachine struct { ID string Name string + ComputerName string Type string Location string OsType string @@ -306,6 +308,7 @@ func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) { azureLabelTenantID: model.LabelValue(d.cfg.TenantID), azureLabelMachineID: model.LabelValue(vm.ID), azureLabelMachineName: model.LabelValue(vm.Name), + azureLabelMachineComputerName: model.LabelValue(vm.ComputerName), azureLabelMachineOSType: model.LabelValue(vm.OsType), azureLabelMachineLocation: model.LabelValue(vm.Location), azureLabelMachineResourceGroup: model.LabelValue(r.ResourceGroup), @@ -463,6 +466,7 @@ func mapFromVM(vm compute.VirtualMachine) virtualMachine { return virtualMachine{ ID: *(vm.ID), Name: *(vm.Name), + ComputerName: *(vm.VirtualMachineProperties.OsProfile.ComputerName), Type: *(vm.Type), Location: *(vm.Location), OsType: osType, @@ -490,6 +494,7 @@ func mapFromVMScaleSetVM(vm compute.VirtualMachineScaleSetVM, scaleSetName strin return virtualMachine{ ID: *(vm.ID), Name: *(vm.Name), + ComputerName: *(vm.VirtualMachineScaleSetVMProperties.OsProfile.ComputerName), Type: *(vm.Type), Location: *(vm.Location), OsType: osType, diff --git a/discovery/azure/azure_test.go b/discovery/azure/azure_test.go index b6addbc8a..744d182de 100644 --- a/discovery/azure/azure_test.go +++ b/discovery/azure/azure_test.go @@ -30,10 +30,14 @@ func TestMapFromVMWithEmptyTags(t *testing.T) { name := "name" vmType := "type" location := "westeurope" + computerName := "computer_name" networkProfile := compute.NetworkProfile{ NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, } properties := &compute.VirtualMachineProperties{ + OsProfile: &compute.OSProfile{ + ComputerName: &computerName, + }, StorageProfile: &compute.StorageProfile{ OsDisk: &compute.OSDisk{ OsType: "Linux", @@ -54,6 +58,7 @@ func TestMapFromVMWithEmptyTags(t *testing.T) { expectedVM := virtualMachine{ ID: id, Name: name, + ComputerName: computerName, Type: vmType, Location: location, OsType: "Linux", @@ -71,6 +76,7 @@ func TestMapFromVMWithTags(t *testing.T) { name := "name" vmType := "type" location := "westeurope" + computerName := "computer_name" tags := map[string]*string{ "prometheus": new(string), } @@ -78,6 +84,9 @@ func TestMapFromVMWithTags(t *testing.T) { NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, } properties := &compute.VirtualMachineProperties{ + OsProfile: &compute.OSProfile{ + ComputerName: &computerName, + }, StorageProfile: &compute.StorageProfile{ OsDisk: &compute.OSDisk{ OsType: "Linux", @@ -98,6 +107,7 @@ func TestMapFromVMWithTags(t *testing.T) { expectedVM := virtualMachine{ ID: id, Name: name, + ComputerName: computerName, Type: vmType, Location: location, OsType: "Linux", @@ -115,10 +125,14 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) { name := "name" vmType := "type" location := "westeurope" + computerName := "computer_name" networkProfile := compute.NetworkProfile{ NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, } properties := &compute.VirtualMachineScaleSetVMProperties{ + OsProfile: &compute.OSProfile{ + ComputerName: &computerName, + }, StorageProfile: &compute.StorageProfile{ OsDisk: &compute.OSDisk{ OsType: "Linux", @@ -140,6 +154,7 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) { expectedVM := virtualMachine{ ID: id, Name: name, + ComputerName: computerName, Type: vmType, Location: location, OsType: "Linux", @@ -158,6 +173,7 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) { name := "name" vmType := "type" location := "westeurope" + computerName := "computer_name" tags := map[string]*string{ "prometheus": new(string), } @@ -165,6 +181,9 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) { NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, } properties := &compute.VirtualMachineScaleSetVMProperties{ + OsProfile: &compute.OSProfile{ + ComputerName: &computerName, + }, StorageProfile: &compute.StorageProfile{ OsDisk: &compute.OSDisk{ OsType: "Linux", @@ -186,6 +205,7 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) { expectedVM := virtualMachine{ ID: id, Name: name, + ComputerName: computerName, Type: vmType, Location: location, OsType: "Linux", diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index c0393c655..ca8f09925 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -386,6 +386,7 @@ The following meta labels are available on targets during [relabeling](#relabel_ * `__meta_azure_machine_id`: the machine ID * `__meta_azure_machine_location`: the location the machine runs in * `__meta_azure_machine_name`: the machine name +* `__meta_azure_machine_computer_name`: the machine computer name * `__meta_azure_machine_os_type`: the machine operating system * `__meta_azure_machine_private_ip`: the machine's private IP * `__meta_azure_machine_public_ip`: the machine's public IP if it exists From 898afa2fd9663e8d7f0de7a534a0800eab17b5d0 Mon Sep 17 00:00:00 2001 From: darshanime Date: Wed, 28 Jul 2021 19:23:17 +0530 Subject: [PATCH 2/2] Add nil checks Signed-off-by: darshanime --- discovery/azure/azure.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/discovery/azure/azure.go b/discovery/azure/azure.go index 7f6043c95..6de0995ef 100644 --- a/discovery/azure/azure.go +++ b/discovery/azure/azure.go @@ -452,6 +452,7 @@ func mapFromVM(vm compute.VirtualMachine) virtualMachine { osType := string(vm.StorageProfile.OsDisk.OsType) tags := map[string]*string{} networkInterfaces := []string{} + var computerName string if vm.Tags != nil { tags = vm.Tags @@ -463,10 +464,14 @@ func mapFromVM(vm compute.VirtualMachine) virtualMachine { } } + if vm.VirtualMachineProperties != nil && vm.VirtualMachineProperties.OsProfile != nil { + computerName = *(vm.VirtualMachineProperties.OsProfile.ComputerName) + } + return virtualMachine{ ID: *(vm.ID), Name: *(vm.Name), - ComputerName: *(vm.VirtualMachineProperties.OsProfile.ComputerName), + ComputerName: computerName, Type: *(vm.Type), Location: *(vm.Location), OsType: osType, @@ -480,6 +485,7 @@ func mapFromVMScaleSetVM(vm compute.VirtualMachineScaleSetVM, scaleSetName strin osType := string(vm.StorageProfile.OsDisk.OsType) tags := map[string]*string{} networkInterfaces := []string{} + var computerName string if vm.Tags != nil { tags = vm.Tags @@ -491,10 +497,14 @@ func mapFromVMScaleSetVM(vm compute.VirtualMachineScaleSetVM, scaleSetName strin } } + if vm.VirtualMachineScaleSetVMProperties != nil && vm.VirtualMachineScaleSetVMProperties.OsProfile != nil { + computerName = *(vm.VirtualMachineScaleSetVMProperties.OsProfile.ComputerName) + } + return virtualMachine{ ID: *(vm.ID), Name: *(vm.Name), - ComputerName: *(vm.VirtualMachineScaleSetVMProperties.OsProfile.ComputerName), + ComputerName: computerName, Type: *(vm.Type), Location: *(vm.Location), OsType: osType,