Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Monitor] support schedule query rule #2328

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test finish
  • Loading branch information
MyronFanQiu committed Sep 14, 2020
commit c58f47cba113c26f3256850898a66b313e00e13a
7 changes: 4 additions & 3 deletions src/scheduled-query/azext_scheduled_query/_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ def __call__(self, parser, namespace, values, option_string=None):
from azext_scheduled_query.grammar.scheduled_query import (
ScheduleQueryConditionLexer, ScheduleQueryConditionParser, ScheduleQueryConditionValidator)

usage = 'usage error: --condition {avg,min,max,total,count} [NAMESPACE.]METRIC {=,!=,>,>=,<,<=} THRESHOLD\n' \
usage = 'usage error: --condition {avg,min,max,total,count} ["METRIC COLUMN" from] "QUERY" {=,!=,>,>=,<,<=} THRESHOLD\n' \
' [resource id RESOURCEID]\n' \
' [where DIMENSION {includes,excludes} VALUE [or VALUE ...]\n' \
' [and DIMENSION {includes,excludes} VALUE [or VALUE ...] ...]]'

' [and DIMENSION {includes,excludes} VALUE [or VALUE ...] ...]]' \
' [at least MinTimeToFail out of EvaluationPeriod]'
string_val = ' '.join(values)

lexer = ScheduleQueryConditionLexer(antlr4.InputStream(string_val))
Expand Down
41 changes: 37 additions & 4 deletions src/scheduled-query/azext_scheduled_query/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,50 @@
- name: --condition
short-summary: The condition which triggers the rule.
long-summary: |
Usage: --condition {avg,min,max,total,count} METRIC {=,!=,>,>=,<,<=} THRESHOLD
[where DIMENSION {includes,excludes} VALUE [or VALUE ...]
[and DIMENSION {includes,excludes} VALUE [or VALUE ...] ...]]
Usage: --condition {avg,min,max,total,count} ["METRIC COLUMN" from]
"QUERY" {=,!=,>,>=,<,<=} THRESHOLD
[resource id RESOURCEID]
[where DIMENSION {includes,excludes} VALUE [or VALUE ...]
[and DIMENSION {includes,excludes} VALUE [or VALUE ...] ...]]
[at least MinTimeToFail out of EvaluationPeriod]'

Dimensions can be queried by adding the 'where' keyword and multiple dimensions can be queried by combining them with the 'and' keyword.
mmyyrroonn marked this conversation as resolved.
Show resolved Hide resolved
"""

Values for METRIC, DIMENSION and appropriate THRESHOLD values can be obtained from `az monitor metrics list-definitions` command.
helps['monitor scheduled-query update'] = """
type: command
short-summary: Update a scheduled query.
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: --condition
short-summary: The condition which triggers the rule.
long-summary: |
Usage: --condition {avg,min,max,total,count} ["METRIC COLUMN" from]
"QUERY" {=,!=,>,>=,<,<=} THRESHOLD
[resource id RESOURCEID]
[where DIMENSION {includes,excludes} VALUE [or VALUE ...]
[and DIMENSION {includes,excludes} VALUE [or VALUE ...] ...]]
[at least MinTimeToFail out of EvaluationPeriod]'

Dimensions can be queried by adding the 'where' keyword and multiple dimensions can be queried by combining them with the 'and' keyword.
"""

helps['monitor scheduled-query list'] = """
type: command
short-summary: List all scheduled queries.
"""

helps['monitor scheduled-query show'] = """
type: command
short-summary: Show detail of a scheduled query.
"""

helps['monitor scheduled-query delete'] = """
type: command
short-summary: Delete a scheduled query.
"""
6 changes: 0 additions & 6 deletions src/scheduled-query/azext_scheduled_query/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,8 @@ def load_arguments(self, _):
c.argument('scopes', nargs='+', help='Space-separated list of scopes the rule applies to. '
'The resources specified in this parameter must be of the same type and exist in the same location.')
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.')
c.argument('target_resource_type', options_list=['--target-resource-type', '--type'],
help='The resource type of the target resource(s) in scopes. '
'This must be provided when scopes is resource group or subscription.')
c.argument('scheduled_query', help='Log query alert')
c.argument('mute_actions_duration', type=get_period_type(as_timedelta=True), help='Mute actions for the chosen period of time (in ISO 8601 duration format) after the alert is fired.')
c.argument('number_of_evaluation_periods', type=float)
c.argument('min_failing_periods_to_alert', type=float)

with self.argument_context('monitor scheduled-query create', arg_group=None) as c:
c.argument('actions', options_list=['--action', '-a'], action=ScheduleQueryAddAction, nargs='+', validator=get_action_group_validator('actions'))
4 changes: 2 additions & 2 deletions src/scheduled-query/azext_scheduled_query/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def load_command_table(self, _):

with self.command_group('monitor scheduled-query', scheduled_query_sdk) as g:
g.custom_command('create', 'create_scheduled_query')
g.command('delete', 'delete')
g.command('delete', 'delete', confirmation=True)
g.custom_command('list', 'list_scheduled_query')
g.show_command('show', 'get')
# g.generic_update_command('update', setter_name='update', custom_func_name='update_scheduled_query')
g.generic_update_command('update', custom_func_name='update_scheduled_query')
22 changes: 19 additions & 3 deletions src/scheduled-query/azext_scheduled_query/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ def create_scheduled_query(client, resource_group_name, rule_name, scopes, condi
tags=None, location=None, actions=None, severity=2, window_size='5m', evaluation_frequency='5m',
target_resource_type=None, mute_actions_duration='PT30M'):
from .vendored_sdks.azure_mgmt_scheduled_query.models import (ScheduledQueryRuleResource,
ScheduledQueryRuleCriteria)

ScheduledQueryRuleCriteria,
ConditionFailingPeriods)

for cond in condition:
if cond.failing_periods is None:
cond.failing_periods = ConditionFailingPeriods(
min_failing_periods_to_alert=1,
number_of_evaluation_periods=1
)
criteria = ScheduledQueryRuleCriteria(all_of=condition)

kwargs = {
Expand All @@ -36,7 +43,16 @@ def list_scheduled_query(client, resource_group_name=None):
return client.list_by_subscription()


def update_scheduled_query(cmd, instance, tags=None):
def update_scheduled_query(cmd, instance, tags=None, disabled=False,
description=None, actions=None, severity=None, window_size=None,
evaluation_frequency=None, mute_actions_duration=None):
with cmd.update_context(instance) as c:
c.set_param('tags', tags)
c.set_param('enabled', not disabled)
c.set_param('description', description)
c.set_param('actions', actions)
c.set_param('severity', severity)
c.set_param('window_size', window_size)
c.set_param('evaluation_frequency', evaluation_frequency)
c.set_param('mute_actions_duration', mute_actions_duration)
return instance
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
# Generated from ScheduleQueryCondition.g4 by ANTLR 4.7.2
# encoding: utf-8
from __future__ import print_function
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
# Generated from ScheduleQueryCondition.g4 by ANTLR 4.7.2
from antlr4 import *

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
# Generated from ScheduleQueryCondition.g4 by ANTLR 4.7.2
# encoding: utf-8
from __future__ import print_function
Expand Down
Loading