-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitoring.tf
More file actions
135 lines (107 loc) · 4.2 KB
/
Copy pathmonitoring.tf
File metadata and controls
135 lines (107 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# =============================================================================
# Monitoring & Diagnostics — Terraform
# =============================================================================
# Configures diagnostic settings on AVD resources and optional Defender plan.
# =============================================================================
variable "monitoring_enabled" {
description = "Enable AVD monitoring and diagnostics."
type = bool
default = true
}
variable "diagnostics_log_categories" {
description = "Log categories to enable on the host pool."
type = list(string)
default = [
"Checkpoint",
"Error",
"Management",
"Connection",
"HostRegistration",
"AgentHealthStatus"
]
}
variable "defender_enabled" {
description = "Enable Microsoft Defender for Cloud on session hosts."
type = bool
default = false
}
variable "alert_rules_enabled" {
description = "Enable AVD alert rules."
type = bool
default = false
}
variable "alert_action_group_id" {
description = "Resource ID of the action group for alert notifications."
type = string
default = ""
}
# ── Diagnostic settings for Host Pool ─────────────────────────────────────────
resource "azurerm_monitor_diagnostic_setting" "host_pool" {
count = var.monitoring_enabled ? 1 : 0
name = "${var.host_pool_name}-diag"
target_resource_id = azurerm_virtual_desktop_host_pool.avd.id
log_analytics_workspace_id = azurerm_log_analytics_workspace.avd.id
dynamic "enabled_log" {
for_each = var.diagnostics_log_categories
content {
category = enabled_log.value
}
}
}
# ── Diagnostic settings for Workspace ─────────────────────────────────────────
resource "azurerm_monitor_diagnostic_setting" "workspace" {
count = var.monitoring_enabled ? 1 : 0
name = "${var.workspace_name}-diag"
target_resource_id = azurerm_virtual_desktop_workspace.avd.id
log_analytics_workspace_id = azurerm_log_analytics_workspace.avd.id
enabled_log {
category = "Feed"
}
}
# ── Diagnostic settings for Application Group ─────────────────────────────────
resource "azurerm_monitor_diagnostic_setting" "app_group" {
count = var.monitoring_enabled ? 1 : 0
name = "${var.app_group_name}-diag"
target_resource_id = azurerm_virtual_desktop_application_group.avd.id
log_analytics_workspace_id = azurerm_log_analytics_workspace.avd.id
enabled_log {
category = "Checkpoint"
}
enabled_log {
category = "Error"
}
enabled_log {
category = "Management"
}
}
# ── Alert: No Available Session Hosts ─────────────────────────────────────────
resource "azurerm_monitor_metric_alert" "no_available_hosts" {
count = var.alert_rules_enabled ? 1 : 0
name = "${var.host_pool_name}-no-available-hosts"
resource_group_name = azurerm_resource_group.avd.name
scopes = [azurerm_virtual_desktop_host_pool.avd.id]
description = "Alert when no session hosts are available."
severity = 1
frequency = "PT5M"
window_size = "PT15M"
criteria {
metric_namespace = "Microsoft.DesktopVirtualization/hostpools"
metric_name = "SessionHostHealthCheckSucceededCount"
aggregation = "Average"
operator = "LessThan"
threshold = 1
}
dynamic "action" {
for_each = var.alert_action_group_id != "" ? [1] : []
content {
action_group_id = var.alert_action_group_id
}
}
tags = local.tags
}
# ── Defender for Servers (session hosts) ──────────────────────────────────────
resource "azurerm_security_center_subscription_pricing" "defender_servers" {
count = var.defender_enabled ? 1 : 0
tier = "Standard"
resource_type = "VirtualMachines"
}