再来分享一个terraform 部署performance counter的技巧,performance counter主要指的是像CPU, Memory, Disk这样的一些性能指标,在Azure里,一般可以把这些数据发送到log analytics workspace里,然后通过kusto query来进行查询,包括设置一些alert
如果是在Portal上操作来添加performance counter的话,其实非常简单,只需要在log analytics workspace中的legacy agent management这里选择需要的指标,然后应用即可,包括一些windows event log以及对应的性能指标,都是用这种方法来添加。不过这种方法实际上2024年就要retire了,新的方案是用data collection来做,之后有时间可以大概介绍下
总而言之,添加performance counter如果是在portal来操作的话会比较简单,但是如果是大批量重复性操作就显得比较麻烦了,这种场景下正适合用terraform这种IAC工具,但是如果要用terraform部署performance counter的话,目前来讲这方面的支持不是很完整,azure的provider目前只能够支持azurerm_log_analytics_datasource_windows_performance_counter这一种资源类型的部署,所以就需要采用一些其他手段,我们这里用的还是Terraform+ARM Template的方式
ARM Template的代码完全可以在官网上找到
https://learn.microsoft.com/en-us/azure/azure-monitor/logs/resource-manager-workspace?tabs=json
这里要注意都是官网给的是部署log analytics workspace+performance counter的方案,我们这里略微有点不同的是,workspace是由terraform直接部署的,arm template只负责部署performance counter,所以代码要做些调整,移除掉workspace以及对应的depends on
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"log_analytics_workspace_name": {
"type": "String"
}
},
"resources": [
{
"type": "Microsoft.OperationalInsights/workspaces/dataSources",
"apiVersion": "2020-08-01",
"name": "[format('{0}/{1}', parameters('log_analytics_workspace_name'), 'WindowsPerfLogicalDiskFreeSpacePercentage')]",
"kind": "WindowsPerformanceCounter",
"properties": {
"objectName": "LogicalDisk",
"instanceName": "*",
"intervalSeconds": 60,
"counterName": "% Free Space"
}
},
{
"type": "Microsoft.OperationalInsights/workspaces/dataSources",
"apiVersion": "2020-08-01",
"name": "[format('{0}/{1}', parameters('log_analytics_workspace_name'), 'WindowsPerfLogicalDiskFreeSpaceMegabytes')]",
"kind": "WindowsPerformanceCounter",
"properties": {
"objectName": "LogicalDisk",
"instanceName": "*",
"intervalSeconds": 60,
"counterName": "Free Megabytes"
}
},
{
"type": "Microsoft.OperationalInsights/workspaces/dataSources",
"apiVersion": "2020-08-01",
"name": "[format('{0}/{1}', parameters('log_analytics_workspace_name'), 'WindowsPerfMemoryPercentageBytes')]",
"kind": "WindowsPerformanceCounter",
"properties": {
"objectName": "Memory",
"instanceName": "*",
"intervalSeconds": 60,
"counterName": "% Committed Bytes in Use"
}
},
{
"type": "Microsoft.OperationalInsights/workspaces/dataSources",
"apiVersion": "2020-08-01",
"name": "[format('{0}/{1}', parameters('log_analytics_workspace_name'), 'WindowsPerfMemoryAvailableBytes')]",
"kind": "WindowsPerformanceCounter",
"properties": {
"objectName": "Memory",
"instanceName": "*",
"intervalSeconds": 60,
"counterName": "Available MBytes "
}
},
{
"type": "Microsoft.OperationalInsights/workspaces/dataSources",
"apiVersion": "2020-08-01",
"name": "[format('{0}/{1}', parameters('log_analytics_workspace_name'), 'WindowsPerfNetworkAdapterBytesReceived')]",
"kind": "WindowsPerformanceCounter",
"properties": {
"objectName": "Network Adapter",
"instanceName": "*",
"intervalSeconds": 60,
"counterName": "Bytes Received/sec"
}
},
{
"type": "Microsoft.OperationalInsights/workspaces/dataSources",
"apiVersion": "2020-08-01",
"name": "[format('{0}/{1}', parameters('log_analytics_workspace_name'), 'WindowsPerfNetworkAdapterBytesSent')]",
"kind": "WindowsPerformanceCounter",
"properties": {
"objectName": "Network Adapter",
"instanceName": "*",
"intervalSeconds": 60,
"counterName": "Bytes Sent/sec"
}
},
{
"type": "Microsoft.OperationalInsights/workspaces/dataSources",
"apiVersion": "2020-08-01",
"name": "[format('{0}/{1}', parameters('log_analytics_workspace_name'), 'WindowsPerfProcessorPercentage')]",
"kind": "WindowsPerformanceCounter",
"properties": {
"objectName": "Processor",
"instanceName": "_Total",
"intervalSeconds": 10,
"counterName": "% Processor Time"
}
}
]
}
ARM Template准备完成后就是terraform的代码了,这部分代码也很简单,就是一个标准的resource group level的deployment
data "template_file" "performance_counter_win" {
template = file(local.performance_counter_win_logic_apps_file_path)
}
resource "azurerm_resource_group_template_deployment" "performance_counter_win" {
name = local.performance_counter_deployment_name_win
resource_group_name = azurerm_log_analytics_workspace.log_analytics_windows.resource_group_name
deployment_mode = "Incremental"
template_content = data.template_file.performance_counter_win.template
parameters_content = jsonencode({
log_analytics_workspace_name = { value = local.log_analytics_workspace_name_windows }
})
}
接下来就是常规的terraform plan & apply的操作了
一套下来,一次性就可以把要加的很多performance counter部署好了