【问题标题】:KQL :: return only tags with more than 4 recordsKQL :: 只返回超过 4 条记录的标签
【发布时间】:2023-01-17 11:54:03
【问题描述】:

我创建了一个 Kusto 查询,允许我返回我们所有的数据库公园。查询只需要10行代码:

Resources
| join kind=inner (
    resourcecontainers
    | where type == 'microsoft.resources/subscriptions'
    | project subscriptionId, subscriptionName = name)
    on subscriptionId
| where subscriptionName in~ ('Subscription1','Subscription2')
| where type =~ 'microsoft.sql/servers/databases'
| where name != 'master'
| project  subscriptionName, resourceGroup, name, type, location,sku.tier, properties.requestedServiceObjectiveName, tags.customerCode

根据合同,我们应该只为每个客户提供 4 个 Azure SQL 数据库,但有时开发人员会复制一份并将其重命名为_old 或_backup,突然间客户可以拥有 5 或 6 个数据库。

这增加了云的总体成本,我想列出拥有 4 个以上数据库的所有客户。

为此,我可以使用标签tags.customerCode,每个客户都有 3 个字母的标识符。

代码应该像这样工作:如果客户称为 ABC 并且有 4 个带有 tags.customerCodeABC 的 Azure SQL 数据库,则查询应该不返回任何内容。如果有 5 个或 6 个带有 tags.customerCodeABC 的数据库,查询应该返回所有这些数据库。

不确定 Kusto 是否可以那么灵活。

【问题讨论】:

    标签: kql azure-resource-graph


    【解决方案1】:

    这是一个可能的解决方案。
    需要注意的是,Azure 资源图仅支持 KQL 的有限子集。

    resourcecontainers 
    | where     type == 'microsoft.resources/subscriptions'
            //and name in~ ('Subscription1','Subscription2')
    | project subscriptionId, subscriptionName = name
    | join kind=inner  
      (
        resources
        | where     type =~ 'microsoft.sql/servers/databases'
                and name != 'master'
      )
      on  subscriptionId            
    | project   subscriptionId, subscriptionName, resourceGroup, name, type, location
               ,tier                            = sku.tier
               ,requestedServiceObjectiveName   = properties.requestedServiceObjectiveName
               ,customerCode                    = tostring(tags.customerCode)    
    | summarize dbs = count(), details = make_list(pack_all()) by customerCode
    | where dbs > 4
    | mv-expand with_itemindex=db_seq ['details']
    | project   customerCode
               ,dbs
               ,db_seq = db_seq + 1
               ,subscriptionId                  = details.subscriptionId
               ,subscriptionName                = details.subscriptionName
               ,resourceGroup                   = details.resourceGroup
               ,name                            = details.name
               ,type                            = details.type
               ,location                        = details.location
               ,tier                            = details.tier
               ,requestedServiceObjectiveName   = details.requestedServiceObjectiveName
    

    【讨论】:

      猜你喜欢
      • 2014-02-11
      • 2017-12-23
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2016-05-22
      • 2018-09-13
      相关资源
      最近更新 更多