以程式設計方式建立 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 資源識別碼。 我們的範例儀表板有多個圖格全部指向單一 Azure 虛擬機器。 這是因為該儀表板僅查看此單一資源。 如果您搜尋 "/subscriptions" 的樣本 JSON (附於文件結尾),您會發現此識別碼多次出現。
/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso/providers/Microsoft.Compute/virtualMachines/myVM1
未來若要對於任何虛擬機器發佈此儀表板,需要將 JSON 中每次出現的此字串參數化。
建立儀表板範本
Azure 可讓您協調多個資源的部署。 您可建立部署範本表示要部署的資源集合,以及這些資源之間的關聯性。 如需詳細資訊,請參閱使用 Resource Manager 範本與 Azure PowerShell 來部署資源。
每個已部署範本資源的 JSON 格式皆如同上傳匯出的儀表板來個別建立一樣,不同之處在於範本語言會新增一些概念,例如變數、參數、基本函式等等。 僅在範本部署的內容中支援這種延伸的語法。 如需詳細資訊,請參閱了解 ARM 範本的結構和語法。
應該使用範本的參數語法來完成參數化。 您可取代我們先前找到全部出現的資源識別碼,如下所示。
使用硬式編碼資源識別碼的 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 Resource Manager 部署的範本版本。
從儀表板匯出的範例 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 Resource Manager REST API、Azure CLI 或 Azure PowerShell 來部署範本。
下一步
- 深入了解 Azure 儀表板的結構。
- 瞭解如何在 Azure 儀表板上使用 Markdown 圖格來顯示自訂內容。
- 了解如何管理共用儀表板的存取權。