Skip to content

Commit

Permalink
Merge pull request #67 from hmcts/DTSPO-18633-add-data-collection-rule
Browse files Browse the repository at this point in the history
Dtspo 18633 add data collection rule
  • Loading branch information
ieuanb74 authored Sep 5, 2024
2 parents 71e96da + 358fc68 commit 5c075e7
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 8 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ A virtual machine or virtual machine scale set.
|------|---------|
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | n/a |
| <a name="provider_azurerm.cnp"></a> [azurerm.cnp](#provider\_azurerm.cnp) | n/a |
| <a name="provider_azurerm.dcr"></a> [azurerm.dcr](#provider\_azurerm.dcr) | n/a |
| <a name="provider_azurerm.soc"></a> [azurerm.soc](#provider\_azurerm.soc) | n/a |

## Resources

| Name | Type |
|------|------|
| [azurerm_monitor_data_collection_rule_association.linux_vm_dcra](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_data_collection_rule_association) | resource |
| [azurerm_monitor_data_collection_rule_association.linux_vmss_dcra](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_data_collection_rule_association) | resource |
| [azurerm_monitor_data_collection_rule_association.windows_vm_dcra](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_data_collection_rule_association) | resource |
| [azurerm_monitor_data_collection_rule_association.windows_vmss_dcra](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_data_collection_rule_association) | resource |
| [azurerm_virtual_machine_extension.azure_monitor](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine_extension) | resource |
| [azurerm_virtual_machine_extension.azure_vm_run_command](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine_extension) | resource |
| [azurerm_virtual_machine_extension.custom_script](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine_extension) | resource |
Expand All @@ -51,6 +56,9 @@ A virtual machine or virtual machine scale set.
| [azurerm_key_vault_secret.splunk_password](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/key_vault_secret) | data source |
| [azurerm_key_vault_secret.splunk_username](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/key_vault_secret) | data source |
| [azurerm_key_vault_secret.token](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/key_vault_secret) | data source |
| [azurerm_monitor_data_collection_rule.linux_data_collection_rule](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/monitor_data_collection_rule) | data source |
| [azurerm_monitor_data_collection_rule.windows_data_collection_rule](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/monitor_data_collection_rule) | data source |
| [azurerm_resource_group.la_rg](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/resource_group) | data source |

## Inputs

Expand Down Expand Up @@ -85,6 +93,9 @@ A virtual machine or virtual machine scale set.
| <a name="input_install_endpoint_protection"></a> [install\_endpoint\_protection](#input\_install\_endpoint\_protection) | Install Endpoint Protection. | `bool` | `true` | no |
| <a name="input_install_nessus_agent"></a> [install\_nessus\_agent](#input\_install\_nessus\_agent) | Install Nessus Agent. | `bool` | `true` | no |
| <a name="input_install_splunk_uf"></a> [install\_splunk\_uf](#input\_install\_splunk\_uf) | Install Splunk UF. | `bool` | `true` | no |
| <a name="input_location"></a> [location](#input\_location) | The region in Azure that the Data collection rule will be deployed to. | `string` | `"UK South"` | no |
| <a name="input_log_analytics_sub_id"></a> [log\_analytics\_sub\_id](#input\_log\_analytics\_sub\_id) | A map of log analytics workspace names and their subscription IDs. | `map(string)` | <pre>{<br> "hmcts-nonprod": "1c4f0704-a29e-403d-b719-b90c34ef14c9",<br> "hmcts-prod": "8999dec3-0104-4a27-94ee-6588559729d1",<br> "hmcts-sandbox": "bf308a5c-0624-4334-8ff8-8dca9fd43783"<br>}</pre> | no |
| <a name="input_log_analytics_workspace_names"></a> [log\_analytics\_workspace\_names](#input\_log\_analytics\_workspace\_names) | A map of environments and their corresponding log analytics workspace names. | `map(string)` | <pre>{<br> "aat": "hmcts-nonprod",<br> "demo": "hmcts-nonprod",<br> "ithc": "hmcts-nonprod",<br> "nonprod": "hmcts-nonprod",<br> "perftest": "hmcts-nonprod",<br> "prod": "hmcts-prod",<br> "production": "hmcts-prod",<br> "ptl": "hmcts-prod",<br> "ptlsbox": "hmcts-sandbox",<br> "sandbox": "hmcts-sandbox",<br> "sbox": "hmcts-sandbox",<br> "staging": "hmcts-nonprod",<br> "stg": "hmcts-nonprod",<br> "test": "hmcts-nonprod"<br>}</pre> | no |
| <a name="input_nessus_groups"></a> [nessus\_groups](#input\_nessus\_groups) | Nessus group name. | `string` | `"Platform-Operation-Bastions"` | no |
| <a name="input_nessus_key"></a> [nessus\_key](#input\_nessus\_key) | Nessus linking key - read input from keyvault. | `string` | `null` | no |
| <a name="input_nessus_server"></a> [nessus\_server](#input\_nessus\_server) | Nessus server endpoint - read input from keyvault. | `string` | `""` | no |
Expand Down
54 changes: 54 additions & 0 deletions azure_monitor.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,57 @@ resource "azurerm_virtual_machine_extension" "azure_monitor" {

tags = var.common_tags
}

data "azurerm_resource_group" "la_rg" {
provider = azurerm.dcr
name = "oms-automation"
}

data "azurerm_monitor_data_collection_rule" "linux_data_collection_rule" {
provider = azurerm.dcr
name = "ama-linux-vm-logs"
resource_group_name = data.azurerm_resource_group.la_rg.name
}

data "azurerm_monitor_data_collection_rule" "windows_data_collection_rule" {
provider = azurerm.dcr
name = "ama-windows-vm-logs"
resource_group_name = data.azurerm_resource_group.la_rg.name
}


resource "azurerm_monitor_data_collection_rule_association" "linux_vm_dcra" {
count = var.install_azure_monitor == true && lower(var.os_type) == "linux" && var.virtual_machine_type == "vm" ? 1 : 0

name = "vm-${local.vm_name}-dcra"
target_resource_id = var.virtual_machine_id
data_collection_rule_id = data.azurerm_monitor_data_collection_rule.linux_data_collection_rule.id
description = "Association between a linux VM and the appropriate data collection rule."
}

resource "azurerm_monitor_data_collection_rule_association" "linux_vmss_dcra" {
count = var.install_azure_monitor == true && lower(var.os_type) == "linux" && var.virtual_machine_type == "vmss" ? 1 : 0

name = "vmss-${local.vmss_name}-dcra"
target_resource_id = var.virtual_machine_scale_set_id
data_collection_rule_id = data.azurerm_monitor_data_collection_rule.linux_data_collection_rule.id
description = "Association between a linux VMSS and the appropriate data collection rule."
}

resource "azurerm_monitor_data_collection_rule_association" "windows_vm_dcra" {
count = var.install_azure_monitor == true && lower(var.os_type) == "windows" && var.virtual_machine_type == "vm" ? 1 : 0

name = "vm-${local.vm_name}-dcra"
target_resource_id = var.virtual_machine_id
data_collection_rule_id = data.azurerm_monitor_data_collection_rule.windows_data_collection_rule.id
description = "Association between a windows VM and the appropriate data collection rule."
}

resource "azurerm_monitor_data_collection_rule_association" "windows_vmss_dcra" {
count = var.install_azure_monitor == true && lower(var.os_type) == "windows" && var.virtual_machine_type == "vmss" ? 1 : 0

name = "vmss-${local.vmss_name}-dcra"
target_resource_id = var.virtual_machine_scale_set_id
data_collection_rule_id = data.azurerm_monitor_data_collection_rule.windows_data_collection_rule.id
description = "Association between a windows VMSS and the appropriate data collection rule."
}
6 changes: 6 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ locals {

xdr_tags_list = var.xdr_tags != null ? join(",", [var.xdr_tags, "hmcts,server"]) : "hmcts,server"

log_analytics_workspace = var.log_analytics_workspace_names[var.env]
log_analytics_subscription = var.log_analytics_sub_id[local.log_analytics_workspace]

vm_name = var.virtual_machine_id != null ? regex("virtualMachines/([^/]+)", var.virtual_machine_id)[0] : null
vmss_name = var.virtual_machine_scale_set_id != null ? regex("virtualMachineScaleSets/([^/]+)", var.virtual_machine_scale_set_id)[0] : null

}
6 changes: 6 additions & 0 deletions providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ terraform {
}
}
}

provider "azurerm" {
alias = "dcr"
features {}
subscription_id = local.log_analytics_subscription
}
5 changes: 3 additions & 2 deletions tests/linux_vm_extensions.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ provider "azurerm" {

# Default variables for this test
variables {
env = "nonprod"
os_type = "Linux"
env = "nonprod"
os_type = "Linux"
location = "uksouth"
}

run "setup_vm" {
Expand Down
5 changes: 3 additions & 2 deletions tests/linux_vmss_extensions.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ provider "azurerm" {

# Default variables for this test
variables {
env = "nonprod"
os_type = "Linux"
env = "nonprod"
os_type = "Linux"
location = "uksouth"
}

run "setup_vm" {
Expand Down
5 changes: 3 additions & 2 deletions tests/windows_vm_extensions.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ provider "azurerm" {

# Default variables for this test
variables {
env = "nonprod"
os_type = "Windows"
env = "nonprod"
os_type = "Windows"
location = "uksouth"
}

run "setup_vm" {
Expand Down
5 changes: 3 additions & 2 deletions tests/windows_vmss_extensions.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ provider "azurerm" {

# Default variables for this test
variables {
env = "nonprod"
os_type = "Windows"
env = "nonprod"
os_type = "Windows"
location = "uksouth"
}

run "setup_vm" {
Expand Down
38 changes: 38 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,46 @@ variable "run_xdr_agent" {
default = false
description = "Install XDR agents using run command script?"
}

variable "xdr_env" {
description = "Set environment for XDR Agent to make sure which environment it should go to, defaults to prod"
type = string
default = "prod"
}

variable "log_analytics_workspace_names" {
type = map(string)
default = {
"production" = "hmcts-prod"
"prod" = "hmcts-prod"
"ptl" = "hmcts-prod"
"nonprod" = "hmcts-nonprod"
"aat" = "hmcts-nonprod"
"staging" = "hmcts-nonprod"
"stg" = "hmcts-nonprod"
"demo" = "hmcts-nonprod"
"perftest" = "hmcts-nonprod"
"test" = "hmcts-nonprod"
"ithc" = "hmcts-nonprod"
"sandbox" = "hmcts-sandbox"
"sbox" = "hmcts-sandbox"
"ptlsbox" = "hmcts-sandbox"
}
description = "A map of environments and their corresponding log analytics workspace names."
}

variable "log_analytics_sub_id" {
description = "A map of log analytics workspace names and their subscription IDs."
type = map(string)
default = {
"hmcts-prod" = "8999dec3-0104-4a27-94ee-6588559729d1"
"hmcts-nonprod" = "1c4f0704-a29e-403d-b719-b90c34ef14c9"
"hmcts-sandbox" = "bf308a5c-0624-4334-8ff8-8dca9fd43783"
}
}

variable "location" {
description = "The region in Azure that the Data collection rule will be deployed to."
type = string
default = "UK South"
}

0 comments on commit 5c075e7

Please sign in to comment.