Skip to content

Commit 8ebc544

Browse files
committed
monitor ext
1 parent 57a02bc commit 8ebc544

File tree

4 files changed

+13014
-1
lines changed

4 files changed

+13014
-1
lines changed

src/azure-cli/azure/cli/command_modules/vm/_params.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ def load_arguments(self, _):
473473
c.argument('enable_vtpm', enable_vtpm_type)
474474
c.argument('user_data', help='UserData for the VM. It can be passed in as file or string.', completer=FilesCompleter(), type=file_type, min_api='2021-03-01')
475475
c.argument('enable_hibernation', arg_type=get_three_state_flag(), min_api='2021-03-01', help='The flag that enable or disable hibernation capability on the VM.')
476+
c.argument('enable_azure_monitor_metrics', options_list=['--enable-azure-monitor-metrics', '--enable-monitor-agent'], action='store_true', min_api='2021-03-01', help='The flag that enable azure monitor metrics on the VM.')
476477

477478
for scope in ['vm create', 'vm update']:
478479
with self.argument_context(scope) as c:

src/azure-cli/azure/cli/command_modules/vm/custom.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ def create_vm(cmd, vm_name, resource_group_name, image=None, size='Standard_DS1_
824824
proxy_agent_mode=None, source_snapshots_or_disks=None, source_snapshots_or_disks_size_gb=None,
825825
source_disk_restore_point=None, source_disk_restore_point_size_gb=None, ssh_key_type=None,
826826
additional_scheduled_events=None, enable_user_reboot_scheduled_events=None,
827-
enable_user_redeploy_scheduled_events=None):
827+
enable_user_redeploy_scheduled_events=None, enable_azure_monitor_metrics=None):
828828

829829
from azure.cli.core.commands.client_factory import get_subscription_id
830830
from azure.cli.core.util import random_string, hash_string
@@ -1167,6 +1167,32 @@ def create_vm(cmd, vm_name, resource_group_name, image=None, size='Standard_DS1_
11671167
except Exception as e:
11681168
error_type = "Trusted Launch" if is_trusted_launch else "Confidential VM"
11691169
logger.error('Failed to install Guest Attestation Extension for %s. %s', error_type, e)
1170+
1171+
if enable_azure_monitor_metrics:
1172+
vm = get_vm(cmd, resource_group_name, vm_name, 'instanceView')
1173+
client = _compute_client_factory(cmd.cli_ctx)
1174+
if vm.storage_profile.os_disk.os_type == 'Linux':
1175+
ext_name = 'AzureMonitorLinuxAgent'
1176+
if vm.storage_profile.os_disk.os_type == 'Windows':
1177+
ext_name = 'AzureMonitorWindowsAgent'
1178+
version = _normalize_extension_version(cmd.cli_ctx, 'Microsoft.Azure.Monitor',
1179+
ext_name, None, vm.location)
1180+
VirtualMachineExtension = cmd.get_models('VirtualMachineExtension')
1181+
ext = VirtualMachineExtension(location=vm.location,
1182+
publisher='Microsoft.Azure.Monitor',
1183+
type_properties_type=ext_name,
1184+
protected_settings=None,
1185+
type_handler_version=version,
1186+
settings=None,
1187+
auto_upgrade_minor_version=True,
1188+
enable_automatic_upgrade=True)
1189+
try:
1190+
LongRunningOperation(cmd.cli_ctx)(client.virtual_machine_extensions.begin_create_or_update(
1191+
resource_group_name, vm_name, ext_name, ext))
1192+
logger.info('Azure Monitor Agent Extension has been successfully installed')
1193+
except Exception as e:
1194+
logger.error('Failed to install Azure Monitor Agent Extension: %s', e)
1195+
11701196
if count:
11711197
vm_names = [vm_name + str(i) for i in range(count)]
11721198
else:

0 commit comments

Comments
 (0)