简体   繁体   English

获取 Azure 中连接到不同订阅中的工作区的虚拟机的 Log Analytics 工作区 ID

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

Here is my Azure setup:这是我的 Azure 设置:

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.两个订阅都在同一个租户下,我的用户 ID 可以访问这两个订阅。 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.为了将租户下所有虚拟机的指标记录在一个日志分析工作区中以进行报告/审计,我使用 arm 模板将所有虚拟机 virtualmachine01 和 virtualmachine02 链接到单个日志分析工作区 log_analytics_workspace02。

My automation requires me to get the workspace id attached to all virtual machines in a subscription.我的自动化要求我获取附加到订阅中所有虚拟机的工作区 ID。 For eg.例如。 subscription1 here.订阅1在这里。

I could get the workspace id linked to a vm using python sdk.我可以使用 python sdk 将工作区 id 链接到 vm。 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.该脚本循环遍历我有权访问的多个订阅,并获取该订阅中每个虚拟机的日志分析工作区 ID。 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.上面代码的问题是它只获取附加到与 vm (subscription1) 相同订阅中的工作空间的 vm 的工作空间 id,但是当它尝试获取链接到不同工作空间的 vm 的工作空间 id 时失败订阅比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.如何获取与运行 VM 的订阅不同的订阅中链接到日志分析工作区的虚拟机的 Log Analytics 工作区 ID。

Thanks.谢谢。

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.嗨,我无法在 Python 中测试您的代码,因为我正在使用 PowerShell 生成连接到多个订阅中的日志分析的 vm。 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!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 azure 机器学习工作区笔记本的版本控制 - Version control of azure machine learning workspace notebooks 如何从 Python Runbook 中的 Log Analytics 工作区获取共享密钥? - How can I get the shared key from Log Analytics workspace in a Python runbook? 无法使用 Watson Assistant 获取 workspace_id - Not able to get the workspace_id with Watson Assistant 从 Azure 机器学习工作区获取所有现有管道 - Getting all existing piplines from an Azure Machine Learning Workspace 收到错误代码:SubscriptionNotFound 消息:尝试获取有关 Azure 虚拟机的数据时未找到订阅? - Receiving the error Code: SubscriptionNotFound Message: The subscription not found when trying to get data about Azure Virtual Machine? 生成工作区ID - Generating workspace id 仅在 VSC 工作区(子)文件夹中使用不同的 python 版本(3.8)和虚拟环境 - use different python version (3.8) and virtual environment in VSC workspace (sub)folder only 如何使用 python 在 azure 中获取虚拟机的资源 ID - How to fetch the resource ID of an virtual machine in azure using python Azure虚拟机操作 - azure virtual machine operations 如何在 databricks 工作区中使用 python 获取 azure 数据湖存储中每个文件的最后修改时间? - How to get the last modification time of each files present in azure datalake storage using python in databricks workspace?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM