gpt4 book ai didi

terraform - 使用 terraform 为现有虚拟机启用 Azure Monitor

转载 作者:行者123 更新时间:2023-12-04 07:22:49 26 4
gpt4 key购买 nike

我正在尝试为现有虚拟机启用 azure 监视器功能,该功能使用 terraform 检查虚拟机的运行状况和性能,但我无法找到相同的正确文档。你能帮我做同样的事情吗,因为我想要对 Azure 进行详细监控?

最佳答案

要在现有虚拟机上启用 VMinsights,您需要拥有虚拟机的数据源,然后部署存储帐户、日志分析工作区、日志分析解决方案、虚拟机操作系统的日志分析代理和虚拟机操作系统的依赖代理虚拟机。

provider "azurerm" {
features {}
}
data "azurerm_virtual_machine" "example" {
name = "test1"
resource_group_name = "testgroup"# where your VM resides in your subscription
}

output "virtual_machine_id" {
value = data.azurerm_virtual_machine.example.id
}

resource "azurerm_storage_account" "main" {
name = "vminsightstest1234"
resource_group_name = data.azurerm_virtual_machine.example.resource_group_name # where your VM resides in your subscription
location = data.azurerm_virtual_machine.example.location # which region your VM resides
account_tier = "Standard"
account_replication_type = "GRS"
}

resource "azurerm_log_analytics_workspace" "LAW" {
name = "vminsights-logAnalytics"
location = data.azurerm_virtual_machine.example.location #which region your VM resides
resource_group_name = data.azurerm_virtual_machine.example.resource_group_name # where your VM resides in your subscription
sku = "PerGB2018"
retention_in_days = 30
}

resource "azurerm_log_analytics_solution" "example" {
solution_name = "ContainerInsights"
location = data.azurerm_virtual_machine.example.location # which region your VM resides
resource_group_name = data.azurerm_virtual_machine.example.resource_group_name # where your VM resides in your subscription
workspace_resource_id = azurerm_log_analytics_workspace.LAW.id
workspace_name = azurerm_log_analytics_workspace.LAW.name
plan {
publisher = "Microsoft"
product = "OMSGallery/ContainerInsights"
}
}
# Agent for Linux
resource "azurerm_virtual_machine_extension" "OMS" {
name = "test-OMSExtension"
virtual_machine_id = data.azurerm_virtual_machine.example.id
publisher = "Microsoft.EnterpriseCloud.Monitoring"
type = "OmsAgentForLinux"
type_handler_version = "1.13"
auto_upgrade_minor_version = true

settings = <<SETTINGS
{
"workspaceId" : "${azurerm_log_analytics_workspace.LAW.workspace_id}"
}
SETTINGS

protected_settings = <<PROTECTED_SETTINGS
{
"workspaceKey" : "${azurerm_log_analytics_workspace.LAW.primary_shared_key}"
}
PROTECTED_SETTINGS
}

# Dependency Agent for Linux
resource "azurerm_virtual_machine_extension" "da" {
name = "DAExtension"
virtual_machine_id = data.azurerm_virtual_machine.example.id
publisher = "Microsoft.Azure.Monitoring.DependencyAgent"
type = "DependencyAgentLinux"
type_handler_version = "9.5"
auto_upgrade_minor_version = true

}
#Agent for Windows
resource "azurerm_virtual_machine_extension" "MMA" {
name = "test-MMAextension"
virtual_machine_id = data.azurerm_virtual_machine.example.id
publisher = "Microsoft.EnterpriseCloud.Monitoring"
type = "MicrosoftMonitoringAgent"
type_handler_version = "1.0"
auto_upgrade_minor_version = true

settings = <<SETTINGS
{
"workspaceId" : "${azurerm_log_analytics_workspace.LAW.workspace_id}"
}
SETTINGS

protected_settings = <<PROTECTED_SETTINGS
{
"workspaceKey" : "${azurerm_log_analytics_workspace.LAW.primary_shared_key}"
}
PROTECTED_SETTINGS
}

# Dependency Agent for Windows
resource "azurerm_virtual_machine_extension" "da" {
name = "DAExtension"
virtual_machine_id = data.azurerm_virtual_machine.example.id
publisher = "Microsoft.Azure.Monitoring.DependencyAgent"
type = "DependencyAgentWindows"
type_handler_version = "9.5"
auto_upgrade_minor_version = true

}

注意:根据您的操作系统要求添加监控代理和依赖代理。

以上代码我已经在我的实验室中针对我创建的 Windows VM 进行了测试。

enter image description here

enter image description here

enter image description here

enter image description here

关于terraform - 使用 terraform 为现有虚拟机启用 Azure Monitor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68389589/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com