Skip to content

Commit

Permalink
[Monitor] Metric Alerts commands (Azure#7037)
Browse files Browse the repository at this point in the history
* Metric Alerts commands

* Code review feedback.
  • Loading branch information
tjprescott authored Aug 17, 2018
1 parent 6deace2 commit 0a5587d
Show file tree
Hide file tree
Showing 34 changed files with 2,865 additions and 440 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ exclude =
scripts
doc
build_scripts

*/grammar/
32 changes: 28 additions & 4 deletions azure-cli2017.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<Compile Include="azure-cli-command_modules-nspkg\azure\cli\command_modules\__init__.py" />
<Compile Include="azure-cli-command_modules-nspkg\setup.py" />
<Compile Include="azure-cli-core\azure\cli\core\adal_authentication.py" />
<Compile Include="azure-cli-core\azure\cli\core\api.py" />
<Compile Include="azure-cli-core\azure\cli\core\azlogging.py" />
<Compile Include="azure-cli-core\azure\cli\core\cloud.py" />
<Compile Include="azure-cli-core\azure\cli\core\commands\arm.py" />
Expand All @@ -46,6 +47,7 @@
<Compile Include="azure-cli-core\azure\cli\core\extensions\__init__.py" />
<Compile Include="azure-cli-core\azure\cli\core\file_util.py" />
<Compile Include="azure-cli-core\azure\cli\core\keys.py" />
<Compile Include="azure-cli-core\azure\cli\core\mock.py" />
<Compile Include="azure-cli-core\azure\cli\core\parser.py" />
<Compile Include="azure-cli-core\azure\cli\core\profiles\_shared.py" />
<Compile Include="azure-cli-core\azure\cli\core\profiles\__init__.py" />
Expand All @@ -69,6 +71,7 @@
<Compile Include="azure-cli-core\azure\cli\core\tests\test_util.py" />
<Compile Include="azure-cli-core\azure\cli\core\tests\test_core_validators.py" />
<Compile Include="azure-cli-core\azure\cli\core\util.py" />
<Compile Include="azure-cli-core\azure\cli\core\_completers.py" />
<Compile Include="azure-cli-core\azure\cli\core\_config.py" />
<Compile Include="azure-cli-core\azure\cli\core\_debug.py" />
<Compile Include="azure-cli-core\azure\cli\core\_environment.py" />
Expand Down Expand Up @@ -405,6 +408,14 @@
<Compile Include="command_modules\azure-cli-interactive\azure\cli\command_modules\interactive\__init__.py" />
<Compile Include="command_modules\azure-cli-interactive\azure_bdist_wheel.py" />
<Compile Include="command_modules\azure-cli-interactive\setup.py" />
<Compile Include="command_modules\azure-cli-iotcentral\azure\cli\command_modules\iotcentral\commands.py" />
<Compile Include="command_modules\azure-cli-iotcentral\azure\cli\command_modules\iotcentral\custom.py" />
<Compile Include="command_modules\azure-cli-iotcentral\azure\cli\command_modules\iotcentral\tests\latest\test_iotcentral_commands.py" />
<Compile Include="command_modules\azure-cli-iotcentral\azure\cli\command_modules\iotcentral\_client_factory.py" />
<Compile Include="command_modules\azure-cli-iotcentral\azure\cli\command_modules\iotcentral\_help.py" />
<Compile Include="command_modules\azure-cli-iotcentral\azure\cli\command_modules\iotcentral\_params.py" />
<Compile Include="command_modules\azure-cli-iotcentral\azure\cli\command_modules\iotcentral\__init__.py" />
<Compile Include="command_modules\azure-cli-iotcentral\setup.py" />
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\custom.py" />
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\commands.py" />
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\mgmt_iot_hub_device\lib\credentials.py" />
Expand Down Expand Up @@ -480,6 +491,11 @@
<Compile Include="command_modules\azure-cli-maps\__init__.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\commands.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\actions.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\grammar\MetricAlertConditionLexer.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\grammar\MetricAlertConditionListener.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\grammar\MetricAlertConditionParser.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\grammar\MetricAlertConditionValidator.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\grammar\__init__.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\operations\action_groups.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\operations\activity_log.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\operations\activity_log_alerts.py" />
Expand Down Expand Up @@ -507,7 +523,6 @@
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\_help.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\_params.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\__init__.py" />
<Compile Include="command_modules\azure-cli-monitor\azure_bdist_wheel.py" />
<Compile Include="command_modules\azure-cli-monitor\setup.py" />
<Compile Include="command_modules\azure-cli-network\azure\cli\command_modules\network\tests\latest\test_dns_commands.py" />
<Compile Include="command_modules\azure-cli-network\azure\cli\command_modules\network\tests\latest\test_network_commands.py" />
Expand Down Expand Up @@ -936,6 +951,13 @@
<Folder Include="command_modules\azure-cli-interactive\azure\cli\command_modules\interactive\tests\" />
<Folder Include="command_modules\azure-cli-interactive\azure\cli\command_modules\interactive\tests\latest\" />
<Folder Include="command_modules\azure-cli-interactive\docs\" />
<Folder Include="command_modules\azure-cli-iotcentral\" />
<Folder Include="command_modules\azure-cli-iotcentral\azure\" />
<Folder Include="command_modules\azure-cli-iotcentral\azure\cli\" />
<Folder Include="command_modules\azure-cli-iotcentral\azure\cli\command_modules\" />
<Folder Include="command_modules\azure-cli-iotcentral\azure\cli\command_modules\iotcentral\" />
<Folder Include="command_modules\azure-cli-iotcentral\azure\cli\command_modules\iotcentral\tests\" />
<Folder Include="command_modules\azure-cli-iotcentral\azure\cli\command_modules\iotcentral\tests\latest\" />
<Folder Include="command_modules\azure-cli-iot\" />
<Folder Include="command_modules\azure-cli-iot\azure\" />
<Folder Include="command_modules\azure-cli-iot\azure\cli\" />
Expand Down Expand Up @@ -984,6 +1006,7 @@
<Folder Include="command_modules\azure-cli-monitor\azure\cli\" />
<Folder Include="command_modules\azure-cli-monitor\azure\cli\command_modules\" />
<Folder Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\" />
<Folder Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\grammar\" />
<Folder Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\operations\" />
<Folder Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\tests\" />
<Folder Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\tests\latest\" />
Expand Down Expand Up @@ -1228,6 +1251,7 @@
<Content Include="command_modules\azure-cli-interactive\README.md" />
<Content Include="command_modules\azure-cli-interactive\README.rst" />
<Content Include="command_modules\azure-cli-interactive\setup.cfg" />
<Content Include="command_modules\azure-cli-iotcentral\HISTORY.rst" />
<Content Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\mgmt_iot_hub_device\swagger_iot_hub_device_identity.json" />
<Content Include="command_modules\azure-cli-iot\HISTORY.rst" />
<Content Include="command_modules\azure-cli-keyvault\azure\cli\command_modules\keyvault\tests\latest\policy.json" />
Expand All @@ -1244,15 +1268,15 @@
<Content Include="command_modules\azure-cli-lab\README.rst" />
<Content Include="command_modules\azure-cli-lab\setup.cfg" />
<Content Include="command_modules\azure-cli-maps\HISTORY.rst" />
<Content Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\grammar\MetricAlertCondition.g4" />
<Content Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\grammar\build_python.bat" />
<Content Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\grammar\run_test.bat" />
<Content Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\operations\autoscale-parameters-template.json" />
<Content Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\_autoscale_util.py">
<SubType>Code</SubType>
</Content>
<Content Include="command_modules\azure-cli-monitor\HISTORY.rst" />
<Content Include="command_modules\azure-cli-monitor\linter_exclusions.yml" />
<Content Include="command_modules\azure-cli-monitor\MANIFEST.in" />
<Content Include="command_modules\azure-cli-monitor\README.rst" />
<Content Include="command_modules\azure-cli-monitor\setup.cfg" />
<Content Include="command_modules\azure-cli-network\azure\cli\command_modules\network\tests\latest\zone_files\fail1.txt" />
<Content Include="command_modules\azure-cli-network\azure\cli\command_modules\network\tests\latest\zone_files\fail2.txt" />
<Content Include="command_modules\azure-cli-network\azure\cli\command_modules\network\tests\latest\zone_files\fail3.txt" />
Expand Down
5 changes: 5 additions & 0 deletions src/azure-cli-core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
DEPENDENCIES.append('ndg-httpsclient')
DEPENDENCIES.append('pyasn1')

if sys.version_info < (3, 0):
DEPENDENCIES.append('antlr4-python2-runtime')
else:
DEPENDENCIES.append('antlr4-python3-runtime')

with open('README.rst', 'r', encoding='utf-8') as f:
README = f.read()
with open('HISTORY.rst', 'r', encoding='utf-8') as f:
Expand Down
3 changes: 2 additions & 1 deletion src/command_modules/azure-cli-monitor/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Release History

0.2.3
+++++
* Minor fixes
* Added `monitor metrics alert` commands for near-realtime metric alerts.
* Deprecated `monitor alert` commands.

0.2.2
+++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ def cf_activity_log(cli_ctx, _):

def cf_event_categories(cli_ctx, _):
return cf_monitor(cli_ctx, _).event_categories


def cf_metric_alerts(cli_ctx, _):
return cf_monitor(cli_ctx, _).metric_alerts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def monitor_exception_handler(ex):
# work around for issue: https://github.com/Azure/azure-sdk-for-python/issues/1556
error_payload = ex.response.json()
error_payload = {k.lower(): v for k, v in error_payload.items()}
if 'error' in error_payload:
error_payload = error_payload['error']
if 'code' in error_payload and 'message' in error_payload:
message = '{}.'.format(error_payload['message']) if error_payload['message'] else 'Operation failed.'
code = '[Code: "{}"]'.format(error_payload['code']) if error_payload['code'] else ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,95 @@
short-summary: Lists the metric definitions for the resource.
"""

# endregion
helps['monitor metrics alert'] = """
type: group
short-summary: Manage near-realtime metric alert rules.
"""

helps['monitor metrics alert create'] = """
type: command
short-summary: Create an alert rule.
parameters:
- name: --action -a
short-summary: Add an action group and optional webhook properties to fire when the alert is triggered.
long-summary: |
Usage: --action ACTION_GROUP_NAME_OR_ID [KEY=VAL [KEY=VAL ...]]
Multiple action groups can be specified by using more than one `--action` argument.
- name: --disabled
short-summary: Create the rule in a disabled state.
- name: --condition
short-summary: The condition which triggers the rule.
long-summary: |
Usage: --conditon {avg,min,max,total} [NAMESPACE.]METRIC {=,!=,>,>=,<,<=} THRESHOLD
[where DIMENSION {includes,excludes} VALUE [or VALUE ...]
[and DIMENSION {includes,excludes} VALUE [or VALUE ...] ...]]
Dimensions can be queried by adding the 'where' keyword and multiple dimensions can be queried by combining them with the 'and' keyword.
Values for METRIC, DIMENSION and appropriate THRESHOLD values can be obtained from `az monitor metrics list-definition` command.
Multiple conditons can be specified by using more than one `--condition` argument.
examples:
- name: Create a high CPU usage alert on a VM with no actions.
text: >
az monitor metrics alert create -n alert1 -g {ResourceGroup} --scopes {VirtualMachineID} --condition "avg Percentage CPU > 90"
- name: Create a high CPU usage alert on a VM with email and webhook actions.
text: |
az monitor metrics alert create -n alert1 -g {ResourceGroup} --scopes {VirtualMachineID} \\
--condition "avg Percentage CPU > 90" --window-size 5m --evaluation-frequency 1m \\
--action {actionGroupId} apiKey={APIKey} type=HighCPU
- name: Create an alert when a storage account shows a high number of slow transactions, using multi-dimensional filters.
text: |
az monitor metrics alert create -g {ResourceGroup} -n alert1 --scopes {StorageAccountId} \\
--description "Storage Slow Transactions" \\
--condition "total transactions > 5 where ResponseType includes Success" \\
--condition "avg SuccessE2ELatency > 250 where ApiName includes GetBlob or PutBlob"
"""


helps['monitor metrics alert update'] = """
type: command
short-summary: Update an alert rule.
parameters:
- name: --add-condition
short-summary: Add a condition which triggers the rule.
long-summary: |
Usage: --add-conditon {avg,min,max,total} [NAMESPACE.]METRIC {=,!=,>,>=,<,<=} THRESHOLD
[where DIMENSION {includes,excludes} VALUE [or VALUE ...]
[and DIMENSION {includes,excludes} VALUE [or VALUE ...] ...]]
Dimensions can be queried by adding the 'where' keyword and multiple dimensions can be queried by combining them with the 'and' keyword.
Values for METRIC, DIMENSION and appropriate THRESHOLD values can be obtained from `az monitor metrics list-definition` command.
Multiple conditons can be specified by using more than one `--condition` argument.
- name: --remove-conditions
short-summary: Space-separated list of condition names to remove.
- name: --add-action
short-summary: Add an action group and optional webhook properties to fire when the alert is triggered.
long-summary: |
Usage: --add-action ACTION_GROUP_NAME_OR_ID [KEY=VAL [KEY=VAL ...]]
Multiple action groups can be specified by using more than one `--action` argument.
- name: --remove-actions
short-summary: Space-separated list of action group names to remove.
"""

helps['monitor metrics alert delete'] = """
type: command
short-summary: Delete an alert rule.
"""

helps['monitor metrics alert list'] = """
type: command
short-summary: List alert rules.
"""

helps['monitor metrics alert show'] = """
type: command
short-summary: Show an alert rule.
"""

helps['monitor log-profiles'] = """
type: group
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
from azure.cli.command_modules.monitor.actions import (
AlertAddAction, AlertRemoveAction, ConditionAction, AutoscaleAddAction, AutoscaleRemoveAction,
AutoscaleScaleAction, AutoscaleConditionAction, period_type,
timezone_offset_type, timezone_name_type)
timezone_offset_type, timezone_name_type, MetricAlertConditionAction, MetricAlertAddAction)
from azure.cli.command_modules.monitor.util import get_operator_map, get_aggregation_map
from azure.cli.command_modules.monitor.validators import (
process_webhook_prop, validate_autoscale_recurrence, validate_autoscale_timegrain)
process_webhook_prop, validate_autoscale_recurrence, validate_autoscale_timegrain, get_action_group_validator,
get_action_group_id_validator)


# pylint: disable=line-too-long, too-many-statements
Expand Down Expand Up @@ -105,6 +106,31 @@ def load_arguments(self, _):
c.ignore('timespan', 'result_type')
# endregion

# region MetricAlerts
with self.argument_context('monitor metrics alert') as c:
c.argument('rule_name', name_arg_type, id_part='name', help='Name of the alert rule.')
c.argument('severity', type=int, help='Severity of the alert from 0 (low) to 4 (high).')
c.argument('window_size', type=period_type, help='Time over which to aggregate metrics in "##h##m##s" format.')
c.argument('evaluation_frequency', type=period_type, help='Frequency with which to evaluate the rule in "##h##m##s" format.')
c.argument('auto_mitigate', arg_type=get_three_state_flag(), help='Automatically resolve the alert.')
c.argument('condition', options_list=['--condition'], action=MetricAlertConditionAction, nargs='+')
c.argument('description', help='Free-text description of the rule.')
c.argument('scopes', nargs='+', help='Space-separated list of scopes the rule applies to.')
c.argument('disabled', arg_type=get_three_state_flag())
c.argument('enabled', arg_type=get_three_state_flag(), help='Whether the metric alert rule is enabled.')

with self.argument_context('monitor metrics alert create', arg_group=None) as c:
c.argument('actions', options_list=['--action', '-a'], action=MetricAlertAddAction, nargs='+', validator=get_action_group_validator('actions'))

with self.argument_context('monitor metrics alert update', arg_group='Action') as c:
c.argument('add_actions', options_list='--add-action', action=MetricAlertAddAction, nargs='+', validator=get_action_group_validator('add_actions'))
c.argument('remove_actions', nargs='+', validator=get_action_group_id_validator('remove_actions'))

with self.argument_context('monitor metrics alert update', arg_group='Condition') as c:
c.argument('add_conditions', options_list='--add-condition', action=MetricAlertConditionAction, nargs='+')
c.argument('remove_conditions', nargs='+')
# endregion

# region Autoscale
with self.argument_context('monitor autoscale') as c:
c.argument('autoscale_name', arg_type=autoscale_name_type, options_list=['--name', '-n'])
Expand Down
Loading

0 comments on commit 0a5587d

Please sign in to comment.