你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
以编程方式创建 Azure 仪表板
本文介绍如何以编程方式创建和发布 Azure 仪表板。 在整个文档中,引用的是下面所示的示例仪表板,但你在此过程中可以使用任何仪表板。
概述
Azure 门户中的共享仪表板与虚拟机和存储帐户一样,都是一种资源。 可以使用 REST API、Azure CLI 和 Azure PowerShell 命令以编程方式管理这些资源。
许多功能都建立这些 API 的基础之上,可以简化资源的管理。 所有这些 API 和工具都提供了创建、罗列、检索、修改和删除资源的方法。 由于仪表板是资源,因此可以选择使用你偏好的 API 或工具。
无论使用哪种工具,若要以编程方式创建仪表板,都请构造仪表板对象的 JSON 表示形式。 此对象包含有关仪表板上的磁贴的信息。 这包括大小、位置、绑定到的资源和任何用户自定义项。
生成此 JSON 文档最实用的方法是使用 Azure 门户创建具有所需磁贴的初始仪表板。 然后导出 JSON,并根据结果创建模板,你可以进一步修改该模板,并可将其用于脚本、程序和部署工具。
提取仪表板的 JSON 表示形式
首先,我们将下载现有仪表板的 JSON 表示形式。 打开要开始使用的仪表板。 选择“导出”,然后选择“下载”。
还可使用 REST API 或其他方法,以编程方式检索仪表板资源的相关信息。
从 JSON 创建模板
下一步是从下载的 JSON 创建模板。 可以编程方式将模板用于适当的资源管理 API、命令行工具,也可在此门户中使用模板。
大多数情况下,只需保留每个磁贴的结构和配置。 然后参数化磁贴指向的 Azure 资源集。 不必全面了解仪表板 JSON 结构也可创建模板。
在导出的 JSON 仪表板中,找到 Azure 资源 ID 的所有匹配项。 示例仪表板具有多个磁贴,它们都指向单个 Azure 虚拟机。 这是因为该仪表板仅在此单个资源中查找。 如果在文档末尾包含的示例 JSON 中搜索“/subscriptions”,将找到此 ID 的多个匹配项。
/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1
将来若要针对任何虚拟机发布此仪表板,请参数化 JSON 中此字符串的每个匹配项。
创建仪表板模板
Azure 提供协调多资源部署的功能。 创建用于表达要部署的资源集的部署模板及资源之间的关系。 有关详细信息,请参阅使用资源管理器模板和 Azure PowerShell 部署资源。
每个已部署的模板资源的 JSON 格式与通过上传导出的仪表板单独创建它们时的格式相同,只不过模板语言添加了一些概念,例如变量、参数、基本函数等。 此扩展语法仅在模板部署上下文中受支持。 有关详细信息,请参阅了解 ARM 模板的结构和语法。
应使用模板的参数语法来实现参数化。 替换之前找到的所有资源 ID 的实例,如下所示。
具有硬编码的资源 ID 的示例 JSON 属性
id: "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1"
基于模板参数转换为参数化版本的示例 JSON 属性
id: "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
在 JSON 模板顶部声明所需的模板元数据和参数,如下所示:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"virtualMachineName": {
"type": "string"
},
"virtualMachineResourceGroup": {
"type": "string"
},
"dashboardName": {
"type": "string"
}
},
"variables": {},
"resources": [
... rest of template omitted ...
]
}
配置模板后,使用以下任意方法部署模板:
接下来会看到示例仪表板 JSON 的两个版本。 第一个版本是从门户导出的、已绑定到资源的模板。 第二个是可以编程方式绑定到任何虚拟机并使用 Azure 资源管理器进行部署的模板版本。
从仪表板导出的示例 JSON 表示形式
在导出本文开头的仪表板时,会看到类似于此示例的内容。 硬编码资源标识符显示此仪表板指向特定的 Azure 虚拟机。
{
"properties": {
"lenses": [
{
"order": 0,
"parts": [
{
"position": {
"x": 0,
"y": 0,
"colSpan": 3,
"rowSpan": 2
},
"metadata": {
"inputs": [],
"type": "Extension/HubsExtension/PartType/MarkdownPart",
"settings": {
"content": {
"settings": {
"content": "## Azure Virtual Machines Overview\r\nNew team members should watch this video to get familiar with Azure Virtual Machines.",
"markdownUri": null
}
}
}
}
},
{
"position": {
"x": 3,
"y": 0,
"colSpan": 8,
"rowSpan": 4
},
"metadata": {
"inputs": [],
"type": "Extension/HubsExtension/PartType/MarkdownPart",
"settings": {
"content": {
"settings": {
"content": "This is the team dashboard for the test VM we use on our team. Here are some useful links:\r\n\r\n1. [Create a Linux virtual machine](https://docs.microsoft.com/azure/virtual-machines/linux/quick-create-portal)\r\n1. [Create a Windows virtual machine](https://docs.microsoft.com/azure/virtual-machines/windows/quick-create-portal)\r\n1. [Create a virtual machine scale set](https://docs.microsoft.com/azure/virtual-machine-scale-sets/quick-create-portal)",
"title": "Test VM Dashboard",
"subtitle": "Contoso",
"markdownUri": null
}
}
}
}
},
{
"position": {
"x": 0,
"y": 2,
"colSpan": 3,
"rowSpan": 2
},
"metadata": {
"inputs": [],
"type": "Extension/HubsExtension/PartType/VideoPart",
"settings": {
"content": {
"settings": {
"src": "https://www.youtube.com/watch?v=rOiSRkxtTeU",
"autoplay": false
}
}
}
}
},
{
"position": {
"x": 0,
"y": 4,
"colSpan": 11,
"rowSpan": 3
},
"metadata": {
"inputs": [
{
"name": "queryInputs",
"value": {
"timespan": {
"duration": "PT1H"
},
"id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/SimpleWinVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVirtualMachine",
"chartType": 0,
"metrics": [
{
"name": "Percentage CPU",
"resourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/SimpleWinVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVirtualMachine"
}
]
}
}
],
"type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart"
}
},
{
"position": {
"x": 0,
"y": 7,
"colSpan": 3,
"rowSpan": 2
},
"metadata": {
"inputs": [
{
"name": "queryInputs",
"value": {
"timespan": {
"duration": "PT1H"
},
"id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/SimpleWinVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVirtualMachine",
"chartType": 0,
"metrics": [
{
"name": "Disk Read Operations/Sec",
"resourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/SimpleWinVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVirtualMachine"
},
{
"name": "Disk Write Operations/Sec",
"resourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/SimpleWinVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVirtualMachine"
}
]
}
}
],
"type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart"
}
},
{
"position": {
"x": 3,
"y": 7,
"colSpan": 3,
"rowSpan": 2
},
"metadata": {
"inputs": [
{
"name": "queryInputs",
"value": {
"timespan": {
"duration": "PT1H"
},
"id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/SimpleWinVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVirtualMachine",
"chartType": 0,
"metrics": [
{
"name": "Disk Read Bytes",
"resourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/SimpleWinVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVirtualMachine"
},
{
"name": "Disk Write Bytes",
"resourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/SimpleWinVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVirtualMachine"
}
]
}
}
],
"type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart"
}
},
{
"position": {
"x": 6,
"y": 7,
"colSpan": 3,
"rowSpan": 2
},
"metadata": {
"inputs": [
{
"name": "queryInputs",
"value": {
"timespan": {
"duration": "PT1H"
},
"id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/SimpleWinVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVirtualMachine",
"chartType": 0,
"metrics": [
{
"name": "Network In Total",
"resourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/SimpleWinVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVirtualMachine"
},
{
"name": "Network Out Total",
"resourceId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/SimpleWinVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVirtualMachine"
}
]
}
}
],
"type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart"
}
},
{
"position": {
"x": 9,
"y": 7,
"colSpan": 2,
"rowSpan": 2
},
"metadata": {
"inputs": [
{
"name": "id",
"value": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/SimpleWinVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVirtualMachine"
}
],
"type": "Extension/Microsoft_Azure_Compute/PartType/VirtualMachinePart",
"asset": {
"idInputName": "id",
"type": "VirtualMachine"
}
}
}
]
}
],
"metadata": {
"model": {}
}
},
"name": "Simple VM Dashboard",
"type": "Microsoft.Portal/dashboards",
"location": "INSERT LOCATION",
"tags": {
"hidden-title": "Simple VM Dashboard"
},
"apiVersion": "2022-12-01-preview"
}
示例仪表板的模板表示形式
示例仪表板的模板版本定义了名为 virtualMachineName
、virtualMachineResourceGroup
和 dashboardName
的三个参数。 使用这些参数可在每次部署时将此仪表板指向不同的 Azure 虚拟机。 此仪表板可以编程方式进行配置并部署为指向任何 Azure 虚拟机。 若要测试此功能,请复制以下模板并将其粘贴到 Azure 门户的模板部署页。
此示例自行部署了一个仪表板,但使用模板语言可部署多个资源并将其与一个或多个仪表板绑定。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"virtualMachineName": {
"type": "string",
"metadata": {
"description": "Name of the existing virtual machine to show in the dashboard"
}
},
"virtualMachineResourceGroup": {
"type": "string",
"metadata": {
"description": "Name of the resource group that contains the virtual machine"
}
},
"dashboardName": {
"type": "string",
"defaultValue": "[guid(parameters('virtualMachineName'), parameters('virtualMachineResourceGroup'))]",
"metadata": {
"Description": "Resource name that Azure portal uses for the dashboard"
}
},
"dashboardDisplayName": {
"type": "string",
"defaultValue": "Simple VM Dashboard",
"metadata": {
"description": "Name of the dashboard to display in Azure portal"
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
}
},
"resources": [
{
"type": "Microsoft.Portal/dashboards",
"apiVersion": "2020-09-01-preview",
"name": "[parameters('dashboardName')]",
"location": "[parameters('location')]",
"tags": {
"hidden-title": "[parameters('dashboardDisplayName')]"
},
"properties": {
"lenses": [
{
"order": 0,
"parts": [
{
"position": {
"x": 0,
"y": 0,
"rowSpan": 2,
"colSpan": 3
},
"metadata": {
"inputs": [],
"type": "Extension/HubsExtension/PartType/MarkdownPart",
"settings": {
"content": {
"settings": {
"content": "## Azure Virtual Machines Overview\r\nNew team members should watch this video to get familiar with Azure Virtual Machines."
}
}
}
}
},
{
"position": {
"x": 3,
"y": 0,
"rowSpan": 4,
"colSpan": 8
},
"metadata": {
"inputs": [],
"type": "Extension/HubsExtension/PartType/MarkdownPart",
"settings": {
"content": {
"settings": {
"content": "This is the team dashboard for the test VM we use on our team. Here are some useful links:\r\n\r\n1. [Create a Linux virtual machine](https://docs.microsoft.com/azure/virtual-machines/linux/quick-create-portal)\r\n1. [Create a Windows virtual machine](https://docs.microsoft.com/azure/virtual-machines/windows/quick-create-portal)\r\n1. [Create a virtual machine scale set](https://docs.microsoft.com/azure/virtual-machine-scale-sets/quick-create-portal)",
"title": "Test VM Dashboard",
"subtitle": "Contoso"
}
}
}
}
},
{
"position": {
"x": 0,
"y": 2,
"rowSpan": 2,
"colSpan": 3
},
"metadata": {
"inputs": [],
"type": "Extension/HubsExtension/PartType/VideoPart",
"settings": {
"content": {
"settings": {
"src": "https://www.youtube.com/watch?v=rOiSRkxtTeU",
"autoplay": false
}
}
}
}
},
{
"position": {
"x": 0,
"y": 4,
"rowSpan": 3,
"colSpan": 11
},
"metadata": {
"inputs": [
{
"name": "queryInputs",
"value": {
"timespan": {
"duration": "PT1H"
},
"id": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",
"chartType": 0,
"metrics": [
{
"name": "Percentage CPU",
"resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
}
]
}
}
],
"type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart"
}
},
{
"position": {
"x": 0,
"y": 7,
"rowSpan": 2,
"colSpan": 3
},
"metadata": {
"inputs": [
{
"name": "queryInputs",
"value": {
"timespan": {
"duration": "PT1H"
},
"id": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",
"chartType": 0,
"metrics": [
{
"name": "Disk Read Operations/Sec",
"resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
},
{
"name": "Disk Write Operations/Sec",
"resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
}
]
}
}
],
"type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart"
}
},
{
"position": {
"x": 3,
"y": 7,
"rowSpan": 2,
"colSpan": 3
},
"metadata": {
"inputs": [
{
"name": "queryInputs",
"value": {
"timespan": {
"duration": "PT1H"
},
"id": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",
"chartType": 0,
"metrics": [
{
"name": "Disk Read Bytes",
"resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
},
{
"name": "Disk Write Bytes",
"resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
}
]
}
}
],
"type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart"
}
},
{
"position": {
"x": 6,
"y": 7,
"rowSpan": 2,
"colSpan": 3
},
"metadata": {
"inputs": [
{
"name": "queryInputs",
"value": {
"timespan": {
"duration": "PT1H"
},
"id": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",
"chartType": 0,
"metrics": [
{
"name": "Network In Total",
"resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
},
{
"name": "Network Out Total",
"resourceId": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
}
]
}
}
],
"type": "Extension/Microsoft_Azure_Monitoring/PartType/MetricsChartPart"
}
},
{
"position": {
"x": 9,
"y": 7,
"rowSpan": 2,
"colSpan": 2
},
"metadata": {
"inputs": [
{
"name": "id",
"value": "[resourceId(parameters('virtualMachineResourceGroup'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]"
}
],
"type": "Extension/Microsoft_Azure_Compute/PartType/VirtualMachinePart",
"asset": {
"idInputName": "id",
"type": "VirtualMachine"
}
}
}
]
}
]
}
}
]
}
通过示例了解如何使用参数化模板来部署仪表板后,就可以尝试使用 Azure 资源管理器 REST API、Azure CLI 或 Azure PowerShell 命令来部署模板。
后续步骤
- 详细了解 Azure 仪表板的结构。
- 了解如何在 Azure 仪表板上使用 Markdown 磁贴显示自定义内容。
- 了解如何管理共享仪表板的访问权限。