Govern and monitor VM applications on Azure Arc-enabled servers (preview)

Monitor and govern VM applications on Azure Arc-enabled servers to track deployments and assess compliance with your organization's application requirements. This article shows you how to use Azure Resource Graph to view deployment status across your environment and Azure Policy to audit and deploy required applications. You can also review application installation logs to troubleshoot deployment issues.

Monitor deployed VM applications with Azure Resource Graph

Use Azure Resource Graph to monitor VM application deployments across your Arc-enabled servers. The following query provides an environment-wide view of deployed applications, including their provisioning state and package reference.

resources
| where type =~ "microsoft.hybridcompute/machines/applications"
| project tenantId, subscriptionId, resourceGroup, location,
    machineName = tostring(split(id, "/")[8]),
    applicationName = name,
    provisioningState = tostring(properties.provisioningState),
    packageReferenceId = tostring(properties.packageReferenceId),
    id

Govern required VM applications with Azure Policy

Use three Azure Policy effects to govern VM applications on Arc-enabled servers:

  • AuditIfNotExists: Report servers that are missing a required application without changing resources.
  • DeployIfNotExists (DINE): Deploy a required application to servers that don't have it.
  • Modify: Enforce the version, install order, and failure-handling settings of an existing application. This effect doesn't deploy missing applications.

Prerequisites and remediation

For DINE and Modify:

DINE deploys missing applications after successful server creation or update requests. Modify changes matching application settings during application resource creation or update. Compliance evaluation alone doesn't change existing resources. For either effect, create a remediation task to correct existing noncompliant resources.

Define and assign a policy

Select an effect for its policy definition and assignment steps.

Create a custom policy definition

{
  "properties": {
    "displayName": "Audit Azure Arc-enabled servers that do not have a VM application installed",
    "policyType": "Custom",
    "mode": "Indexed",
    "description": "Audits Azure Arc-enabled servers that match the target operating system and do not have the specified VM application installed.",
    "metadata": {
      "category": "Azure Arc",
      "version": "1.0.0"
    },
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy."
        },
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "AuditIfNotExists"
      },
      "applicationName": {
        "type": "String",
        "metadata": {
          "displayName": "Application name",
          "description": "The name of the VM application to check for on Azure Arc-enabled servers."
        }
      },
      "osType": {
        "type": "String",
        "metadata": {
          "displayName": "Operating system type",
          "description": "The target operating system. Only Azure Arc-enabled servers with a matching operating system are audited."
        },
        "allowedValues": [
          "Windows",
          "Linux"
        ]
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.HybridCompute/machines"
          },
          {
            "field": "Microsoft.HybridCompute/machines/osName",
            "equals": "[parameters('osType')]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.HybridCompute/machines/applications",
          "existenceCondition": {
            "field": "name",
            "equals": "[parameters('applicationName')]"
          }
        }
      }
    }
  }
}

Assign the policy

Assign the custom policy to the scope you want to monitor, such as a subscription or resource group.

  1. Set applicationName to the name of the required VM application.
  2. Set osType to Windows or Linux, depending on the application's target operating system.
  3. Set effect to AuditIfNotExists to report missing applications.

Create a separate assignment for each required application and target operating system. By using AuditIfNotExists, matching servers are noncompliant if the named application resource is missing.

Access VM application logs

Use installation logs to troubleshoot application installation issues, and use the Azure Activity log to review control-plane operations on VM application resources.

Use Run command to review installation logs

Use Run command to read the installation logs on an Arc-enabled server. For instructions on creating and executing a Run command, see Run command on Azure Arc-enabled servers (preview).

Run the following script for your server's operating system. Replace vm-application-name with the VM application definition name and 1.0.0 with the application version.

Windows

Set $extensionVersion to the version of the VM Application Manager extension installed on the server. The following example uses 1.0.17.

$appName = "vm-application-name"
$appVersion = "1.0.0"
$extensionVersion = "1.0.17"
$logDirectory = "C:\Packages\Plugins\Microsoft.CPlat.Core.VMApplicationManagerWindows\$extensionVersion\Downloads\$appName\$appVersion"
$stdoutFilePath = Join-Path $logDirectory "stdout"
$stderrFilePath = Join-Path $logDirectory "stderr"

Write-Output "`n=== Contents of stdout ==="
Get-Content -Path $stdoutFilePath -ErrorAction Stop
Write-Output "`n=== Contents of stderr ==="
Get-Content -Path $stderrFilePath -ErrorAction Stop

Linux

Run the following Bash script to view standard output and standard error.

set -e

appName="vm-application-name"
appVersion="1.0.0"
logDirectory="/var/lib/waagent/Microsoft.CPlat.Core.VMApplicationManagerLinux/$appName/$appVersion"
stdoutFilePath="$logDirectory/stdout"
stderrFilePath="$logDirectory/stderr"

printf '\n=== Contents of stdout ===\n'
cat "$stdoutFilePath"
printf '\n=== Contents of stderr ===\n'
cat "$stderrFilePath"

Use the Activity log to review resource operations

The Azure Activity log records control-plane operations on VM application resources, such as creating, updating, or deleting a deployment. These records are separate from the installation logs on the server.

  1. In the Azure portal, go to the Arc-enabled server where the VM application is deployed.
  2. Select Activity log.
  3. Adjust the time range and filters to find the operation you want to review.
  4. Select an event to view its details, and then select JSON for the full event record.

Use the following operation names to identify VM application events:

  • Deployment creation or updates: Microsoft.HybridCompute/machines/applications/write.
  • Deployment deletions: Microsoft.HybridCompute/machines/applications/delete.

Collect full Azure Arc agent logs

To collect all available Azure Connected Machine agent and extension logs, rather than only the most recent logs, run the following command on the Arc-enabled server:

azcmagent logs -f

The command saves the logs in a ZIP archive in the current directory. For more information, see azcmagent logs.