[英]KQL :: return only tags with more than 4 records
I have created a Kusto query that allows me to return all our database park.我创建了一个 Kusto 查询,允许我返回我们所有的数据库公园。 The query only takes 10 lines of code:
查询只需要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
By contract we are supposed to give only 4 Azure SQL Database per customer but sometimes developers take a copy of them and they rename it _old
or _backup
and suddenly a customer can have 5 or 6 databases.根据合同,我们应该只为每个客户提供 4 个 Azure SQL 数据库,但有时开发人员会复制一份并将其重命名为
_old
或_backup
,突然一个客户可以拥有 5 或 6 个数据库。
This increase the overall costs of the Cloud and I would like to have a list of all customers that have more than 4 databases.这增加了云的总体成本,我想列出拥有 4 个以上数据库的所有客户。
In order to do so I can use the tag tags.customerCode
which has the 3 letters identifier for each customer.为此,我可以使用标签
tags.customerCode
,它具有每个客户的 3 个字母标识符。
The code should work like this: if a customer is called ABC and there are 4 Azure SQL Databases with tags.customerCode
ABC the query should return nothing.代码应该像这样工作:如果一个客户被称为 ABC 并且有 4 个 Azure SQL 带有
tags.customerCode
ABC 的数据库,查询应该不返回任何内容。 If there are 5 or 6 databases with tags.customerCode
ABC the query should return all of them.如果有 5 或 6 个数据库带有
tags.customerCode
ABC,则查询应返回所有这些数据库。
Not sure if Kusto can be that flexible.不确定 Kusto 是否可以那么灵活。
Here is a possible solution.这是一个可能的解决方案。
It should be noted that Azure resource graph supports only a limited subset of KQL.需要注意的是,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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.