pythonazureazure-log-analytics

Get Log analytics workspace ID of a virtual machine in Azure connected to a workspace in a different subscription


Here is my Azure setup:

subscription1:
   virtualmachine01        --> linked to log_analytics_workspace01 in subscription01
   virtualmachine02        --> linke to log_analytics_workspace02 in subscription02
   log_analytics_workspace01   
subscription2:
   virtualmachine03        --> linked to log_analytics_workspace02 in subscription02
   log_analytics_workspace02

Both the subscriptions are under same tenant and my userid has access to both subscriptions. In order to log the metrics of all virtual machines under the tenant in one log analytics workspace for reporting/auditing purposes I have linked all virtual machines virtualmachine01 & virtualmachine02 to single log analytics workspace log_analytics_workspace02 using arm template.

My automation requires me to get the workspace id attached to all virtual machines in a subscription. For eg. subscription1 here.

I could get the workspace id linked to a vm using python sdk. Here is the snippet of the code:

compute_client = ComputeManagementClient(credentials, subscription_id)
vm_list = compute_client.virtual_machines.list_all()
for vm in vm_list:
  vm_name = vm.id.split('/')[-1]
  vm_rg = vm.id.split('/')[4]
  for extn in vm.resources:
    if extn.id.split('/')[-1] in ['MicrosoftMonitoringAgent', 'MMAExtension', 'OMSExtension', 'OmsAgentForLinux']:
       customer_id = compute_client.virtual_machine_extensions.get(
                            vm_rg,
                            vm_name,
                            extn.id.split('/')[-1]).settings['workspaceId']
law_client = LogAnalyticsManagementClient(credentials, subscription_id)
workspaces = law_client.workspaces.list()
for w in workspaces:
   if w.customer_id == customer_id:
      workspace_id = w.id
      print('Workspace_id '+workspace_id)

The script loops through multiple subscription I have access to and fetches the log analytics workspace id of each vm in that subscription. The problem with the above code is that it only fetches the workspace id of vm attached to a workspace in the same subscription as vm (subscription1) but fails when it tries to get workspace id of a vm which is linked to a workspace in a different subscription than vm.

How can I fetch Log Analytics workspace id of a virtual machine which is linked to a log analytics workspace in a different subscription than the one where VM is running.

Thanks.


Solution

  • Hi I am not able to test your the code in Python as I am using PowerShell to generate the vm connected to log analytics in multiple subscriptions. But please see the modified code snippet below. Hope it the logic helps.

    subscription_id_list = ["xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx", "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"]
    
    for subscription_id in subscription_id_list:
        law_client += LogAnalyticsManagementClient(credentials, subscription_id)
    
    workspaces = law_client.workspaces.list()
    
    for subscription_id in subscription_id_list:
        compute_client = ComputeManagementClient(credentials, subscription_id)
        vm_list = compute_client.virtual_machines.list_all()
        for vm in vm_list:
        vm_name = vm.id.split('/')[-1]
        vm_rg = vm.id.split('/')[4]
        for extn in vm.resources:
            if extn.id.split('/')[-1] in ['MicrosoftMonitoringAgent', 'MMAExtension', 'OMSExtension', 'OmsAgentForLinux']:
            customer_id = compute_client.virtual_machine_extensions.get(
                                    vm_rg,
                                    vm_name,
                                    extn.id.split('/')[-1]).settings['workspaceId']
        for w in workspaces:
            if w.customer_id == customer_id:
                workspace_id = w.id
                print('Workspace_id '+ workspace_id)
    

    Do let me know if it works. Thanks!