How to enable Virtual Machine Diagnostic using python SDK on Azure Cloud

from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.compute.models import RunCommandInput, VirtualMachineExtension
import json

subscription_id = "your subscription ID"
resource_group_name = "WorkSpace"
vm_name = "nginx-demo-vm"
location = "southindia"

credential = DefaultAzureCredential()
compute_client = ComputeManagementClient(credential, subscription_id)

# Run shell command to install Python on the VM
command = RunCommandInput(
    command_id='RunShellScript',
    script=[
        'apt install python2 -y',
        'python3 --version'
    ]
)

install_python = compute_client.virtual_machines.begin_run_command(
    resource_group_name, vm_name, command
)

result = install_python.result()

output_messages = []
for message in result.value:
    stdout_start = message.message.find("[stdout]") + len("[stdout]\n")
    stderr_start = message.message.find("[stderr]") + len("[stderr]\n")

    stdout = message.message[stdout_start:stderr_start - len("[stderr]\n")].strip()
    stderr = message.message[stderr_start:].strip()

    output_message = {
        "code": message.code,
        "stdout": stdout,
        "stderr": stderr
    }
    output_messages.append(output_message)

result_dict = {"output": output_messages}
print(json.dumps(result_dict, indent=4))


settings = {
  "StorageAccount": "logsofthevm",
  "ladCfg": {
    "diagnosticMonitorConfiguration": {
      "eventVolume": "Medium",
      "metrics": {
        "metricAggregation": [
          { "scheduledTransferPeriod": "PT1M" },
          { "scheduledTransferPeriod": "PT1H" }
        ],
        "resourceId": "{your vm resource_id}"
      },
      "syslogEvents": {
        "syslogEventConfiguration": {
          "LOG_AUTH": "LOG_DEBUG",
          "LOG_AUTHPRIV": "LOG_DEBUG",
          "LOG_CRON": "LOG_DEBUG",
          "LOG_DAEMON": "LOG_DEBUG",
          "LOG_FTP": "LOG_DEBUG",
          "LOG_KERN": "LOG_DEBUG",
          "LOG_LOCAL0": "LOG_DEBUG",
          "LOG_LOCAL1": "LOG_DEBUG",
          "LOG_LOCAL2": "LOG_DEBUG",
          "LOG_LOCAL3": "LOG_DEBUG",
          "LOG_LOCAL4": "LOG_DEBUG",
          "LOG_LOCAL5": "LOG_DEBUG",
          "LOG_LOCAL6": "LOG_DEBUG",
          "LOG_LOCAL7": "LOG_DEBUG",
          "LOG_LPR": "LOG_DEBUG",
          "LOG_MAIL": "LOG_DEBUG",
          "LOG_NEWS": "LOG_DEBUG",
          "LOG_SYSLOG": "LOG_DEBUG",
          "LOG_USER": "LOG_DEBUG",
          "LOG_UUCP": "LOG_DEBUG"
        }
      },
      "performanceCounters": {
        "performanceCounterConfiguration": [
          {
            "annotation": [{ "displayName": "CPU IO wait time", "locale": "en-us" }],
            "class": "processor",
            "condition": "IsAggregate=TRUE",
            "counter": "percentiowaittime",
            "counterSpecifier": "/builtin/processor/percentiowaittime",
            "type": "builtin",
            "unit": "Percent",
            "sampleRate": "PT15S"
          },
          {
            "annotation": [{ "displayName": "CPU user time", "locale": "en-us" }],
            "class": "processor",
            "condition": "IsAggregate=TRUE",
            "counter": "percentusertime",
            "counterSpecifier": "/builtin/processor/percentusertime",
            "type": "builtin",
            "unit": "Percent",
            "sampleRate": "PT15S"
          },
          {
            "annotation": [{ "displayName": "CPU nice time", "locale": "en-us" }],
            "class": "processor",
            "condition": "IsAggregate=TRUE",
            "counter": "percentnicetime",
            "counterSpecifier": "/builtin/processor/percentnicetime",
            "type": "builtin",
            "unit": "Percent",
            "sampleRate": "PT15S"
          },
          {
            "annotation": [{ "displayName": "CPU percentage guest OS", "locale": "en-us" }],
            "class": "processor",
            "condition": "IsAggregate=TRUE",
            "counter": "percentprocessortime",
            "counterSpecifier": "/builtin/processor/percentprocessortime",
            "type": "builtin",
            "unit": "Percent",
            "sampleRate": "PT15S"
          },
          {
            "annotation": [{ "displayName": "Memory available", "locale": "en-us" }],
            "class": "memory",
            "counter": "availablememory",
            "counterSpecifier": "/builtin/memory/availablememory",
            "type": "builtin",
            "unit": "Bytes",
            "sampleRate": "PT15S"
          },
          {
            "annotation": [{ "displayName": "Swap percent used", "locale": "en-us" }],
            "class": "memory",
            "counter": "percentusedswap",
            "counterSpecifier": "/builtin/memory/percentusedswap",
            "type": "builtin",
            "unit": "Percent",
            "sampleRate": "PT15S"
          },
          {
            "annotation": [{ "displayName": "Filesystem % used space", "locale": "en-us" }],
            "class": "filesystem",
            "condition": "IsAggregate=TRUE",
            "counter": "percentusedspace",
            "counterSpecifier": "/builtin/filesystem/percentusedspace",
            "type": "builtin",
            "unit": "Percent",
            "sampleRate": "PT15S"
          }
        ]
      }
    }
  }
}

protected_settings = {
    "storageAccountName": "your storage account name",
    "storageAccountSasToken": "your sas token"
 }

# print current install extensions
list_result = compute_client.virtual_machine_extensions.list(resource_group_name, vm_name)

for extension in list_result.value:
    print(f"{extension.location}")
    print(f"{extension.tags}")
    print(f"{extension.force_update_tag}")
    print(f"{extension.publisher}")
    print(f"{extension.type_properties_type}")
    print(f"{extension.type_handler_version}")
    print(f"{extension.auto_upgrade_minor_version}")
    print(f"{extension.settings}")
    print(f"{extension.protected_settings}")
    print(f"{extension.instance_view}")
    print(f"{extension.name}")

print("Installation for VirtualMachineExtension Started")

vmExt = VirtualMachineExtension(location="southindia", publisher= "Microsoft.Azure.Diagnostics", type_handler_version="3.0", type_properties_type="LinuxDiagnostic", auto_upgrade_minor_version=True, settings=settings, protected_settings=protected_settings)

result = compute_client.virtual_machine_extensions.begin_create_or_update(resource_group_name="WorkSpace", vm_name="nginx-demo-vm", vm_extension_name="LinuxDiagnostic", extension_parameters=vmExt, content_type="application/json")

result = result.result()

print(result)
print("Installation for VirtualMachineExtension Completed")

# print current install extensions
list_result = compute_client.virtual_machine_extensions.list(resource_group_name, vm_name)

for extension in list_result.value:
    print(f"{extension.location}")
    print(f"{extension.tags}")
    print(f"{extension.force_update_tag}")
    print(f"{extension.publisher}")
    print(f"{extension.type_properties_type}")
    print(f"{extension.type_handler_version}")
    print(f"{extension.auto_upgrade_minor_version}")
    print(f"{extension.settings}")
    print(f"{extension.protected_settings}")
    print(f"{extension.instance_view}")
    print(f"{extension.name}")

Import Statements:

  • Imports necessary Azure SDK modules (azure.identity, azure.mgmt.compute).
  • Also imports json for handling JSON data.

Azure Credentials and Compute Client Setup:

  • Sets up Azure credentials using DefaultAzureCredential(), which attempts multiple methods of authentication (like environment variables, managed identity, etc.).
  • Initializes a ComputeManagementClient using the credentials and subscription ID.

Run Command on VM:

  • Defines a shell script command to install Python (apt install python2 -y, python2 --version).
  • Uses RunCommandInput to specify the command and runs it on the VM (begin_run_command).
  • Collects and parses the command output (stdout and stderr) for further processing or logging.

Diagnostic Extension Settings:

  • Sets up diagnostic settings (settings) for the VM to collect metrics and syslog events.
  • Includes performance counters and other metrics configuration.

Protected Settings:

  • Defines protected_settings which include sensitive information (like SAS tokens) needed for the diagnostic extension.

Manage VM Extensions:

  • Lists existing VM extensions for the VM.
  • Creates a new VM extension (VirtualMachineExtension) for diagnostic monitoring (LinuxDiagnostic) with specified settings and protected settings.
  • Begins the creation/update operation (begin_create_or_update), waits for completion (result()), and prints the result.

Final VM Extension Details:

  • Lists VM extensions again to verify the newly created extension.

Output:

  • Outputs results of the run command and extension operations in JSON format for easy readability.

Leave a Reply

Your email address will not be published. Required fields are marked *