Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 0 additions & 158 deletions src/azure-cli/azure/cli/command_modules/monitor/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,149 +377,6 @@
short-summary: List the event categories of activity logs.
"""

helps['monitor alert'] = """
type: group
short-summary: Manage classic metric-based alert rules.
"""

helps['monitor alert create'] = """
type: command
short-summary: Create a classic metric-based alert rule.
parameters:
- name: --action -a
short-summary: Add an action to fire when the alert is triggered.
long-summary: |
Usage: --action TYPE KEY [ARG ...]
Email: --action email bob@contoso.com ann@contoso.com
Webhook: --action webhook https://www.contoso.com/alert apiKey=value
Webhook: --action webhook https://www.contoso.com/alert?apiKey=value
Multiple actions can be specified by using more than one `--action` argument.
- name: --description
short-summary: Free-text description of the rule. Defaults to the condition expression.
- name: --disabled
short-summary: Create the rule in a disabled state.
- name: --condition
short-summary: The condition which triggers the rule.
long-summary: >
The form of a condition is "METRIC {>,>=,<,<=} THRESHOLD {avg,min,max,total,last} PERIOD".
Values for METRIC and appropriate THRESHOLD values can be obtained from `az monitor metric` commands,
and PERIOD is of the form "##h##m##s".
- name: --email-service-owners
short-summary: Email the service owners if an alert is triggered.
examples:
- name: Create a high CPU usage alert on a VM with no actions.
text: >
az monitor alert create -n rule1 -g {ResourceGroup} --target {VirtualMachineID} --condition "Percentage CPU > 90 avg 5m"
- name: Create a high CPU usage alert on a VM with email and webhook actions.
text: |
az monitor alert create -n rule1 -g {ResourceGroup} --target {VirtualMachineID} \\
--condition "Percentage CPU > 90 avg 5m" \\
--action email bob@contoso.com ann@contoso.com --email-service-owners \\
--action webhook https://www.contoso.com/alerts?type=HighCPU \\
--action webhook https://alerts.contoso.com apiKey={APIKey} type=HighCPU
"""

helps['monitor alert delete'] = """
type: command
short-summary: Delete an alert rule.
examples:
- name: Delete an alert rule. (autogenerated)
text: |
az monitor alert delete --name MyAlertRule --resource-group MyResourceGroup
crafted: true
"""

helps['monitor alert list'] = """
type: command
short-summary: List alert rules in a resource group.
examples:
- name: List alert rules in a resource group. (autogenerated)
text: |
az monitor alert list --resource-group MyResourceGroup
crafted: true
"""

helps['monitor alert list-incidents'] = """
type: command
short-summary: List all incidents for an alert rule.
examples:
- name: List all incidents for an alert rule. (autogenerated)
text: |
az monitor alert list-incidents --resource-group MyResourceGroup --rule-name MyRule
crafted: true
"""

helps['monitor alert show'] = """
type: command
short-summary: Show an alert rule.
examples:
- name: Show an alert rule. (autogenerated)
text: |
az monitor alert show --name MyAlertRule --resource-group MyResourceGroup
crafted: true
"""

helps['monitor alert show-incident'] = """
type: command
short-summary: Get the details of an alert rule incident.
"""

helps['monitor alert update'] = """
type: command
short-summary: Update a classic metric-based alert rule.
parameters:
- name: --description
short-summary: Description of the rule.
- name: --condition
short-summary: The condition which triggers the rule.
long-summary: >
The form of a condition is "METRIC {>,>=,<,<=} THRESHOLD {avg,min,max,total,last} PERIOD".
Values for METRIC and appropriate THRESHOLD values can be obtained from `az monitor metric` commands,
and PERIOD is of the form "##h##m##s".
- name: --add-action -a
short-summary: Add an action to fire when the alert is triggered.
long-summary: |
Usage: --add-action TYPE KEY [ARG ...]
Email: --add-action email bob@contoso.com ann@contoso.com
Webhook: --add-action webhook https://www.contoso.com/alert apiKey=value
Webhook: --add-action webhook https://www.contoso.com/alert?apiKey=value
Multiple actions can be specified by using more than one `--add-action` argument.
- name: --remove-action -r
short-summary: Remove one or more actions.
long-summary: |
Usage: --remove-action TYPE KEY [KEY ...]
Email: --remove-action email bob@contoso.com ann@contoso.com
Webhook: --remove-action webhook https://contoso.com/alert https://alerts.contoso.com
- name: --email-service-owners
short-summary: Email the service owners if an alert is triggered.
- name: --metric
short-summary: Name of the metric to base the rule on.
populator-commands:
- az monitor metrics list-definitions
- name: --operator
short-summary: How to compare the metric against the threshold.
- name: --threshold
short-summary: Numeric threshold at which to trigger the alert.
- name: --aggregation
short-summary: Type of aggregation to apply based on --period.
- name: --period
short-summary: >
Time span over which to apply --aggregation, in nDnHnMnS shorthand or full ISO8601 format.
examples:
- name: Update a classic metric-based alert rule. (autogenerated)
text: |
az monitor alert update --email-service-owners true --name MyAlertRule --resource-group MyResourceGroup
crafted: true
- name: Update a classic metric-based alert rule. (autogenerated)
text: |
az monitor alert update --name MyAlertRule --remove-action email bob@contoso.com --resource-group MyResourceGroup
crafted: true
- name: Update a classic metric-based alert rule. (autogenerated)
text: |
az monitor alert update --name MyAlertRule --resource-group MyResourceGroup --set retentionPolicy.days=365
crafted: true
"""

helps['monitor autoscale'] = """
type: group
short-summary: Manage autoscale settings.
Expand Down Expand Up @@ -783,21 +640,6 @@
crafted: true
"""

helps['monitor autoscale-settings'] = """
type: group
short-summary: Manage autoscale settings.
"""

helps['monitor autoscale-settings update'] = """
type: command
short-summary: Updates an autoscale setting.
examples:
- name: Updates an autoscale setting. (autogenerated)
text: |
az monitor autoscale-settings update --name MyAutoscaleSetting --resource-group MyResourceGroup --set retentionPolicy.days=365
crafted: true
"""

helps['monitor diagnostic-settings'] = """
type: group
short-summary: Manage service diagnostic settings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,10 @@ def load_arguments(self, _):
c.argument('offset', type=get_period_type(as_timedelta=True))

with self.argument_context('monitor activity-log list', arg_group='Filter') as c:
c.argument('filters', deprecate_info=c.deprecate(target='--filters', hide=True, expiration='3.0.0'), help='OData filters. Will ignore other filter arguments.')
c.argument('correlation_id')
c.argument('resource_group', resource_group_name_type)
c.argument('resource_id')
c.argument('resource_provider', options_list=['--namespace', c.deprecate(target='--resource-provider', redirect='--namespace', hide=True, expiration='3.0.0')])
c.argument('resource_provider', options_list=['--namespace'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this argument thoroughly?

c.argument('caller')
c.argument('status')
# endregion
Expand Down
32 changes: 1 addition & 31 deletions src/azure-cli/azure/cli/command_modules/monitor/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def load_command_table(self, _):

from ._client_factory import (
cf_alert_rules, cf_metric_def, cf_alert_rule_incidents, cf_log_profiles, cf_autoscale,
cf_alert_rules, cf_metric_def, cf_log_profiles, cf_autoscale,
cf_diagnostics, cf_activity_log, cf_action_groups, cf_activity_log_alerts, cf_event_categories,
cf_metric_alerts, cf_metric_ns, cf_log_analytics_deleted_workspaces, cf_log_analytics_workspace,
cf_log_analytics_workspace_tables, cf_log_analytics_workspace_management_groups,
Expand Down Expand Up @@ -60,18 +60,6 @@ def load_command_table(self, _):
operation_group='activity_log_alerts',
exception_handler=exception_handler)

alert_sdk = CliCommandType(
operations_tmpl='azure.mgmt.monitor.operations#AlertRulesOperations.{}',
client_factory=cf_alert_rules,
operation_group='alert_rules',
exception_handler=exception_handler)

alert_rule_incidents_sdk = CliCommandType(
operations_tmpl='azure.mgmt.monitor.operations#AlertRuleIncidentsOperations.{}',
client_factory=cf_alert_rule_incidents,
operation_group='alert_rule_incidents',
exception_handler=exception_handler)

autoscale_sdk = CliCommandType(
operations_tmpl='azure.mgmt.monitor.operations#AutoscaleSettingsOperations.{}',
client_factory=cf_autoscale,
Expand Down Expand Up @@ -294,15 +282,6 @@ def load_command_table(self, _):
g.custom_command('scope add', 'add_scope')
g.custom_command('scope remove', 'remove_scope')

with self.command_group('monitor alert', alert_sdk, custom_command_type=alert_custom, deprecate_info=self.deprecate(redirect='monitor metrics alert', hide='2.0.44')) as g:
g.custom_command('create', 'create_metric_rule')
g.command('delete', 'delete')
g.show_command('show', 'get')
g.command('list', 'list_by_resource_group')
g.command('show-incident', 'get', command_type=alert_rule_incidents_sdk)
g.command('list-incidents', 'list_by_alert_rule', command_type=alert_rule_incidents_sdk)
g.generic_update_command('update', custom_func_name='update_metric_rule')

with self.command_group('monitor autoscale', autoscale_sdk, custom_command_type=autoscale_custom) as g:
g.custom_command('create', 'autoscale_create', validator=process_autoscale_create_namespace)
g.generic_update_command('update', custom_func_name='autoscale_update', custom_func_type=autoscale_custom)
Expand All @@ -323,15 +302,6 @@ def load_command_table(self, _):
g.custom_command('delete', 'autoscale_rule_delete')
g.custom_command('copy', 'autoscale_rule_copy')

with self.command_group('monitor autoscale-settings', autoscale_sdk, custom_command_type=autoscale_custom,
deprecate_info=self.deprecate(redirect='monitor autoscale', hide='2.0.34')) as g:
g.command('create', 'create_or_update', deprecate_info='az monitor autoscale create')
g.command('delete', 'delete', deprecate_info='az monitor autoscale delete')
g.show_command('show', 'get', deprecate_info='az monitor autoscale show')
g.command('list', 'list_by_resource_group', deprecate_info='az monitor autoscale list')
g.custom_command('get-parameters-template', 'scaffold_autoscale_settings_parameters', deprecate_info='az monitor autoscale show')
g.generic_update_command('update', deprecate_info='az monitor autoscale update')

with self.command_group('monitor diagnostic-settings', diagnostics_sdk, custom_command_type=diagnostics_custom) as g:
from .validators import validate_diagnostic_settings
g.custom_command('create', 'create_diagnostics_settings', validator=validate_diagnostic_settings)
Expand Down
9 changes: 3 additions & 6 deletions src/azure-cli/azure/cli/command_modules/monitor/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@


# region ActivityLog
def list_activity_log(client, filters=None, correlation_id=None, resource_group=None, resource_id=None,
def list_activity_log(client, correlation_id=None, resource_group=None, resource_id=None,
resource_provider=None, start_time=None, end_time=None, caller=None, status=None, max_events=50,
select=None, offset='6h'):
if filters:
odata_filters = filters
else:
odata_filters = _build_activity_log_odata_filter(correlation_id, resource_group, resource_id, resource_provider,
start_time, end_time, caller, status, offset)
odata_filters = _build_activity_log_odata_filter(correlation_id, resource_group, resource_id, resource_provider,
start_time, end_time, caller, status, offset)

select_filters = _activity_log_select_filter_builder(select)
logger.info('OData Filter: %s', odata_filters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,6 @@
DEFAULT_PROFILE_NAME = 'default'


def scaffold_autoscale_settings_parameters(client): # pylint: disable=unused-argument
"""Scaffold fully formed autoscale-settings' parameters as json template """

import os.path
from azure.cli.core.util import get_file_json

# Autoscale settings parameter scaffold file path
curr_dir = os.path.dirname(os.path.realpath(__file__))
autoscale_settings_parameter_file_path = os.path.join(
curr_dir, 'autoscale-parameters-template.json')

if not os.path.exists(autoscale_settings_parameter_file_path):
raise CLIError('File {} not found.'.format(autoscale_settings_parameter_file_path))

return get_file_json(autoscale_settings_parameter_file_path)


# pylint: disable=too-many-locals
def autoscale_create(client, resource, count, autoscale_name=None, resource_group_name=None,
min_count=None, max_count=None, location=None, tags=None, disabled=None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# --------------------------------------------------------------------------------------------
# pylint: disable=too-many-locals

from azure.cli.command_modules.monitor.util import get_operator_map, get_aggregation_map
from knack.log import get_logger

logger = get_logger(__name__)
Expand Down Expand Up @@ -157,77 +156,6 @@ def list_metric_alerts(client, resource_group_name=None):
return client.list_by_subscription()


def create_metric_rule(client, resource_group_name, rule_name, target, condition, description=None, disabled=False,
location=None, tags=None, email_service_owners=False, actions=None):
from azure.mgmt.monitor.models import AlertRuleResource, RuleEmailAction
condition.data_source.resource_uri = target
custom_emails, webhooks, _ = _parse_actions(actions)
actions = [
RuleEmailAction(send_to_service_owners=email_service_owners, custom_emails=custom_emails)
] + (webhooks or [])
rule = AlertRuleResource(
location=location, name_properties_name=rule_name, is_enabled=not disabled,
condition=condition, tags=tags, description=description, actions=actions)
return client.create_or_update(resource_group_name, rule_name, rule)


def update_metric_rule(instance, target=None, condition=None, description=None, enabled=None, metric=None,
operator=None, threshold=None, aggregation=None, period=None, tags=None,
email_service_owners=None, add_actions=None, remove_actions=None):
# Update general properties
if description is not None:
instance.description = description
if enabled is not None:
instance.is_enabled = enabled
if tags is not None:
instance.tags = tags

# Update conditions
if condition is not None:
target = target or instance.condition.data_source.resource_uri
instance.condition = condition

if metric is not None:
instance.condition.data_source.metric_name = metric
if operator is not None:
instance.condition.operator = get_operator_map()[operator]
if threshold is not None:
instance.condition.threshold = threshold
if aggregation is not None:
instance.condition.time_aggregation = get_aggregation_map()[aggregation]
if period is not None:
instance.condition.window_size = period

if target is not None:
instance.condition.data_source.resource_uri = target

# Update actions
emails, webhooks, curr_email_service_owners = _parse_actions(instance.actions)

# process removals
if remove_actions is not None:
removed_emails, removed_webhooks = _parse_action_removals(remove_actions)
emails = [x for x in emails if x not in removed_emails]
webhooks = [x for x in webhooks if x.service_uri not in removed_webhooks]

# process additions
if add_actions is not None:
added_emails, added_webhooks, _ = _parse_actions(add_actions)
emails = list(set(emails) | set(added_emails))
webhooks = webhooks + added_webhooks

# Replace the existing actions array. This potentially restructures rules that were created
# via other methods (Portal, ARM template). However, the functionality of these rules should
# be the same.
from azure.mgmt.monitor.models import RuleEmailAction
if email_service_owners is None:
email_service_owners = curr_email_service_owners
actions = [RuleEmailAction(send_to_service_owners=email_service_owners, custom_emails=emails)] + webhooks
instance.actions = actions

return instance


def _parse_actions(actions):
""" Actions come in as a combined list. This method separates the webhook actions into a
separate collection and combines any number of email actions into a single email collection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@

from knack.util import CLIError

from azure.cli.command_modules.monitor.operations.autoscale_settings import scaffold_autoscale_settings_parameters


class FilterBuilderTests(unittest.TestCase):
def test_scaffold_autoscale_settings_parameters(self):
template = scaffold_autoscale_settings_parameters(None)
self.assertTrue(template)
self.assertTrue(isinstance(template, dict))


def _mock_get_subscription_id(_):
return '00000000-0000-0000-0000-000000000000'
Expand Down