[英]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.