The DevOps Quest

🔎

Virtual Machines Insights

📅

⏱️ 2 min read (242 words)

I recently worked on implementing VM Insights into Virtual Machines. However the latest templates provided by Microsoft were still written in ARM and generally a little complex. I have converted them to Bicep and edited them a little bit.

param location string = resourceGroup().location

param virtualMachineName string
param logAnalyticsNamespaceName string

resource virtualMachine 'Microsoft.Compute/virtualMachines@2023-07-01' existing = {
  name: virtualMachineName
}

resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2022-10-01' existing = {
  name: logAnalyticsNamespaceName
}

resource MSVMI_DCR 'Microsoft.Insights/dataCollectionRules@2022-06-01' = {
  name: 'MSVMI-${virtualMachine.name}'
  location: location
  properties: {
    description: 'Standard data collection rule for VM Insights.'
    dataSources: {
      performanceCounters: [
        {
          name: 'VMInsightsPerfCounters'
          streams: [
            'Microsoft-InsightsMetrics'
          ]
          samplingFrequencyInSeconds: 60
          counterSpecifiers: [
            '\\VmInsights\\DetailedMetrics'
          ]
        }
      ]
      extensions: [
        {
          streams: [
            'Microsoft-ServiceMap'
          ]
          extensionName: 'DependencyAgent'
          name: 'DependencyAgentDataSource'
        }
      ]
    }
    destinations: {
      logAnalytics: [
        {
          workspaceResourceId: logAnalyticsWorkspace.id
          name: 'VMInsightsPerf-Logs-Dest'
        }
      ]
    }
    dataFlows: [
      {
        streams: [
          'Microsoft-InsightsMetrics'
        ]
        destinations: [
          'VMInsightsPerf-Logs-Dest'
        ]
      }
      {
        streams: [
          'Microsoft-ServiceMap'
        ]
        destinations: [
          'VMInsightsPerf-Logs-Dest'
        ]
      }
    ]
  }
}

resource Vm_DaExtension 'Microsoft.Compute/virtualMachines/extensions@2023-07-01' = {
  parent: virtualMachine
  name: 'DependencyAgentWindows'
  location: location
  properties: {
    publisher: 'Microsoft.Azure.Monitoring.DependencyAgent'
    type: 'DependencyAgentWindows'
    typeHandlerVersion: '9.5'
    autoUpgradeMinorVersion: true
    settings: {
      enableAMA: 'true'
    }
  }
}

resource Vm_AmaExtension 'Microsoft.Compute/virtualMachines/extensions@2023-07-01' = {
  parent: virtualMachine
  name: 'AzureMonitorWindowsAgent'
  location: location
  properties: {
    publisher: 'Microsoft.Azure.Monitor'
    type: 'AzureMonitorWindowsAgent'
    typeHandlerVersion: '1.0'
    autoUpgradeMinorVersion: true
  }
}

resource Vm_Microsoft_Insights_VMInsights_Dcr_Association 'Microsoft.Insights/dataCollectionRuleAssociations@2022-06-01' = {
  name: '${virtualMachine.name}-VMInsights-Dcr-Association'
  scope: virtualMachine
  properties: {
    description: 'Association of data collection rule for VM Insights.'
    dataCollectionRuleId: MSVMI_DCR.id
  }
}

Health ❤️ ❤️ ❤️ ❤️ ❤️

See you soon 🗡️