zoukankan      html  css  js  c++  java
  • 【Azure Developer】在Azure Resource Graph Explorer中查看当前订阅下的所有资源信息列表并导出(如VM的名称,IP地址内网/公网,OS,区域等)

    问题描述

    通过Azure的Resource Graph Explorerhttps://portal.azure.cn/#blade/HubsExtension/ArgQueryBlade),可以查看到当前列表中的各种资源并导出CSV格式,以便日常的管理或生成Power BI等报表的源数据。

    如查看虚拟机,MySQL,Redis,Application Gateway(应用程序网关),VNET(虚拟网络),公共IP等资源信息。只要找到这些资源的类型(type)后就可以写类SQL语句(Kusto)。如下:

    查询语句

    上图中的SQL语句非常的简单,只是根据类型列出部分信息。 查询语句内容:

    resources 
    | where type in ('microsoft.compute/virtualmachines',
                    'microsoft.network/virtualnetworks',
                    'microsoft.network/publicipaddresses',
                    'microsoft.cache/redis',
                    'microsoft.network/applicationGateways',
                    'microsoft.dbformysql/servers')
    |project  id, name, type, sku
    • where : 指定查询的过滤条件
    • project :只输出后面列出的属性值

    而如果需要非常与其他资源的信息关联查询,则需要使用到 leftouter 等。如在问题描述中提到的要查看VM的内网IP地址和公网IP地址,由于内网IP地址属于虚拟网络资源的信息,而公网IP地址也是属于publicipaddresses的资源信息,所以需要使用两次leftouter 来关联VM信息查询。

    完整的类SQL语句为:

    Resources
    | where type =~ 'microsoft.compute/virtualmachines'
    |project name, OS=tostring(properties.storageProfile.osDisk.osType), location, 
    ipid = tolower(tostring(properties.networkProfile.networkInterfaces[0].id))
    | join kind=leftouter (
        Resources
        | where type =~ 'microsoft.network/networkinterfaces'
        | project ipid = tolower(id), elasticPoolName = name, 
        privateIP = properties.ipConfigurations[0].properties.privateIPAddress, 
        publicIPid = properties.ipConfigurations[0].properties.publicIPAddress.id)
    on ipid
    | project-away ipid
    | project name, OS, location, privateIP, pubipid=tolower(tostring(publicIPid))
    | join kind=leftouter (
        Resources
        | where type =~ 'microsoft.network/publicipaddresses'
        | project pubipid = tolower(id), publicIP = properties.ipAddress)
    on pubipid
    | project-away pubipid
    | project name, privateIP,publicIP, OS, location

    第一次leftouter关联查询时候用的是私网IP地址资源的ID作为关联条件:

    • ipid = tolower(tostring(properties.networkProfile.networkInterfaces[0].id)) 为从VM资源中获取的私网IP资源ID
    • ipid = tolower(id) 为类型'microsoft.network/networkinterfaces'的资源ID

    第二次leftouter关联查询时候用的是公网IP地址资源的ID作为关联条件:

    • publicIPid = properties.ipConfigurations[0].properties.publicIPAddress.id为从第一个leftouter表中获取到公网IP资源ID, 为了保持与第二个leftouter中的字段名一致,所以在project中转换列名pubipid=tolower(tostring(publicIPid))
    • pubipid = tolower(id) 为类型'microsoft.network/networkinterfaces'的资源ID

    注:主表数据和leftouter关联表数据使用的on关键字表示关联条件。

      | project-away pubipid :表示在结果中移除pubipid字段显示。详见:project-away 运算符: https://docs.microsoft.com/zh-cn/azure/data-explorer/kusto/query/projectawayoperator

    以上查询语句的结果如图:

    查看MySQL数据库信息,显示数据库服务名,配置和资源所在区域信息

    resources 
    | where type =~ 'microsoft.dbformysql/servers'
    | project  name, properties, location

    查看Redis数据库信息,显示服务名,配置和资源所在区域信息

    resources 
    | where type =~ 'microsoft.cache/redis'
    | project  name, properties, location

    查看虚拟网络(VNET)信息,显示网络名,配置的内网网段,资源所在区域信息

    resources 
    | where type =~ 'microsoft.network/virtualnetworks'
    | project  name, addressPrefixes=tostring(properties.addressSpace.addressPrefixes[0]), location

    查看Application Gateway(应用程序网关)信息,显示服务名称,配置和资源所在区域信息

    resources 
    | where type =~ 'microsoft.network/applicationGateways'
    | project  name, properties, location

    注:配置信息从properties中查找,为JSON数据。可通过对象方式获取,如properties.addressSpace.addressPrefixes[0]。

    参考资料

    Kusto查询语句示例https://docs.microsoft.com/zh-cn/azure/data-explorer/kusto/query/samples?pivots=azuredataexplorer

    当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

  • 相关阅读:
    快速搭建http server
    cwmp part2 测试基础RPC
    Leetcode-5223 Queens That Can Attack the King(可以攻击国王的皇后)
    Leetcode-5222 Split a String in Balanced Strings(分割平衡字符串)
    Leetcode-5224 Dice Roll Simulation(掷骰子模拟)
    P2604-[ZJOI2010]网络扩容
    P2053-[SCOI2007]修车
    P2153-[SDOI2009]晨跑
    P2774 方格取数问题
    P2763-试题库问题
  • 原文地址:https://www.cnblogs.com/lulight/p/14342556.html
Copyright © 2011-2022 走看看