Azure资源图KQL – 过滤具有给定前缀标签的虚拟机

huangapple go评论125阅读模式
英文:

Azure Resource Graph KQL - filter virtual machines having tags with given prefixes

问题

以下是翻译好的部分:

我们有很多在Azure中创建的虚拟机。每个虚拟机都有标签。其中一些标签的键是动态构建的。这些键具有固定的前缀和动态的后缀。我们希望找到一种方法,可以筛选出所有具有给定前缀的2个或更多键的虚拟机。

考虑以下虚拟机:

  • VM1:{name: "vm-1", tags: {"bingo-1":1, "tringo-2":1, "key1": "value1"}}
  • VM2:{name: "vm-2", tags: {"tringo-2":1, "key2": "value2"}}
  • VM3:{name: "vm-3", tags: {"bingo-1":1, "key3": "value3"}}

预期输出:vm-1,因为它是唯一包含两个标签键前缀的虚拟机。

尝试:
我已经让这个工作了,但这似乎不是最优的,因为它两次调用了bag_keys函数。

resources
 | where type =~ 'Microsoft.Compute/virtualMachines'
 | where resourceGroup =~ 'test-resource-group'
 | where bag_keys(tags) hasprefix "bingo-"
 | where bag_keys(tags) hasprefix "tringo-"
 | project name

这可以按预期工作,但代价是两次调用bag_keys。是否有更符合惯用方法且更优化的方式来实现相同的目标,而不必两次调用bag_keys?

英文:

We have a lot of virtual machines that we create in azure. Each virtual machine has tags. Keys for some of these tags are dynamically constructed. Such keys have a fixed prefix and a dynamic suffix. We wanted a way to filter all virtual machines which have 2 or more keys with given prefixes.

Consider the following Virtualmachines:

  • VM1: {name: "vm-1", tags: {"bingo-1":1, "tringo-2":1, "key1": "value1"}}
  • VM1: {name: "vm-1", tags: {"tringo-2":1, "key2": "value2"}}
  • VM1: {name: "vm-1", tags: {"bingo-1":1, "key3": "value3"}}

Expected Output: vm-1 as that is the only VM which contains both the tag-key-prefixes.

Attempt:
I got this working but this does not seem to be optimal as it calls bag_keys function twice.

resources
 | where type =~ 'Microsoft.Compute/virtualMachines'
 | where resourceGroup =~ 'test-resource-group'
 | where bag_keys(tags) hasprefix "bingo-"
 | where bag_keys(tags) hasprefix "tringo-"
 | project name

This works as expected but at the cost of invoking bag_keys twice. Is there a more idiomatic and optimal way to achieve the same without calling bag_keys twice?

答案1

得分: 0

与使用两个 `where` 操作符不同,首先我使用了 [mv-expand](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/mvexpandoperator) 操作符将标签合并为一条记录,然后使用 `and` 来检查和过滤两个前缀的标签,以优化您的代码。

```ps1
resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| where resourceGroup =~ '<resourcegroup>'
| mvexpand tags
| extend tagKey = tostring(bag_keys(tags)[0])
| extend tagValue = tostring(tags[tagKey])
| where tagKey hasprefix "creat"  and tagKey hasprefix "cr"
| project name, tags, tagKey, tagValue

更新:

根据评论提供的信息,您可以使用 extend 操作符。它会创建一个包含标签对象中所有键的新列,如下所示。

extend tagKeys = bag_keys(tags) 
| where tagKeys hasprefix "bingo-" and tagKeys hasprefix "tringo-"

Azure资源图KQL – 过滤具有给定前缀标签的虚拟机

请参考 @John Kilmister 撰写的资源图探索器查询 文章 获取更多相关查询示例。


<details>
<summary>英文:</summary>

Instead of using two `where` operators, firstly I included
 [mv-expand](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/mvexpandoperator) operator to join tags into one record and then used an `and` to check and filter the tags of both prefixes to optimize your code.


```ps1
resources
| where type =~ &#39;Microsoft.Compute/virtualMachines&#39;
| where resourceGroup =~ &#39;&lt;resourcegroup&gt;&#39;
| mvexpand tags
| extend tagKey = tostring(bag_keys(tags)[0])
| extend tagValue = tostring(tags[tagKey])
| where tagKey hasprefix &quot;creat&quot;  and tagKey hasprefix &quot;cr&quot;
| project name, tags, tagKey, tagValue

Update:

As given in comments, You can use extend operator. It creates a new column that contains all the keys in the tags object in this way.

extend tagKeys = bag_keys(tags) 
| where tagKeys hasprefix &quot;bingo-&quot; and tagKeys hasprefix &quot;tringo-&quot;

Azure资源图KQL – 过滤具有给定前缀标签的虚拟机

Refer resource graph explorer query article by @John Kilmister for more related query samples.

huangapple
  • 本文由 发表于 2023年6月13日 14:44:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定