diff --git a/discovery/azure/azure.go b/discovery/azure/azure.go index a23e80d8c3..6de0995ef0 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), @@ -449,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 @@ -460,9 +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: computerName, Type: *(vm.Type), Location: *(vm.Location), OsType: osType, @@ -476,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 @@ -487,9 +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: computerName, Type: *(vm.Type), Location: *(vm.Location), OsType: osType, diff --git a/discovery/azure/azure_test.go b/discovery/azure/azure_test.go index b6addbc8a5..744d182de7 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 ffd790ff07..e48ba6a0b4 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