繁体   English   中英

如何使用 python 为任何资源以及“标签”生成 Azure 日志活动数据的报告?

[英]How to generate reports for Azure Log Activity Data using python for any resource alongwith 'Tags'?

我的同事使用下面的 powershell 查询来检索过去 4 天(不包括今天)的日志数据,这些数据匹配资源的操作并收集诸如 EventTimeStamp、Caller、SubscriptionId 等特征。

Get-AzureRmLog -StartTime (Get-Date).AddDays(-4) -EndTime (Get-Date).AddDays(-1) | Where-Object {$_.OperationName.LocalizedValue -match "Start|Stop|Restart|Create|Update|Delete"} |
Select-Object EventTimeStamp, Caller, SubscriptionId, @{name="Operation"; Expression = {$_.operationname.LocalizedValue}},

我是 azure 的新手,我想生成一个报告,我还可以在此报告中针对过去 90 天的资源获取“标签”名称和值。 对此的 powershell 查询将是什么? 我也可以使用 python 来查询这些数据吗? 我尝试搜索文档但无法深入研究,因此如果有人可以将我重定向到正确的位置,那将会很有帮助。

首先,您应该知道并非所有 azure 资源都可以指定标签,因此您应该在代码中考虑这一点。 请参阅Azure 资源的标签支持以检查哪个 azure 资源支持标签。

对于 powershell 查询,我建议使用新的 azure powershell az 模块而不是旧的azureRM 模块

这是一个带有az module的简单 powershell 代码。 为了测试目的,我只介绍如何获取和添加标签到 output。 请随时根据您的要求进行更改。

#for testing purpose, I just get the azure activity logs from a specified resource group
$mylogs = Get-AzLog -ResourceGroupName "a resource group name"

foreach($log in $mylogs)
{   

    if(($log.Properties.Content.Values -ne $null))
    {
        #the tags is contains in the Properties of the log entry.
        $s = $log.Properties.Content.Values -as [string]
        if($s.startswith("{"))
        {
            $log | Select-Object EventTimeStamp, Caller, SubscriptionId,@{name="Operation"; Expression = {$_.operationname.LocalizedValue}}, @{name="tags"; Expression = {($s | ConvertFrom-Json).tags}}      
        }
        #if it does not contains tags.
        else
        {
            $log | Select-Object EventTimeStamp, Caller, SubscriptionId,@{name="Operation"; Expression = {$_.operationname.LocalizedValue}}, @{name="tags"; Expression = {""}}
        }
    }
    #if it does not contains tags.
    else
    {
        $log | Select-Object EventTimeStamp, Caller, SubscriptionId,@{name="Operation"; Expression = {$_.operationname.LocalizedValue}}, @{name="tags"; Expression = {""}}
    }

    Write-Output "************************"
}

测试结果:

在此处输入图像描述

For python, you can take a look at this github issue which introduces how to fetch logs from azure activity logs, but you need do some research on how to add tags to the output.

希望能帮助到你。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM