def __call__(self, parser, namespace, values, option_string=None): from azure.mgmt.monitor.models import ThresholdRuleCondition, RuleMetricDataSource # get default description if not specified if namespace.description is None: namespace.description = ' '.join(values) if len(values) == 1: # workaround because CMD.exe eats > character... Allows condition to be # specified as a quoted expression values = values[0].split(' ') if len(values) < 5: from knack.util import CLIError raise CLIError( 'usage error: --condition METRIC {>,>=,<,<=} THRESHOLD {avg,min,max,total,last} DURATION' ) metric_name = ' '.join(values[:-4]) operator = get_operator_map()[values[-4]] threshold = int(values[-3]) aggregation = get_aggregation_map()[values[-2].lower()] window = period_type(values[-1]) metric = RuleMetricDataSource( resource_uri=None, metric_name=metric_name) # target URI will be filled in later condition = ThresholdRuleCondition(operator=operator, threshold=threshold, data_source=metric, window_size=window, time_aggregation=aggregation) namespace.condition = condition
def test_alert_rules(self, resource_group, location): # Get the VM or your resource and use "id" attribute, or build the id yourself from RG and name resource_id = ( "subscriptions/{}/" "resourceGroups/MonitorTestsDoNotDelete/" "providers/Microsoft.Compute/virtualMachines/MonitorTest" ).format(self.settings.SUBSCRIPTION_ID) # I need a subclass of "RuleDataSource" data_source = RuleMetricDataSource( resource_uri=resource_id, metric_name='Percentage CPU' ) # I need a subclasses of "RuleCondition" rule_condition = ThresholdRuleCondition( data_source=data_source, operator='GreaterThanOrEqual', threshold=90, window_size='PT5M', time_aggregation='Average' ) # I need a subclass of "RuleAction" rule_action = RuleEmailAction( send_to_service_owners=True, custom_emails=[ '*****@*****.**' ] ) rule_name = 'MyPyTestAlertRule' my_alert = self.mgmt_client.alert_rules.create_or_update( resource_group.name, rule_name, { 'location': location, 'alert_rule_resource_name': rule_name, 'description': 'Testing Alert rule creation', 'is_enabled': True, 'condition': rule_condition, 'actions': [ rule_action ] } ) my_alert = self.mgmt_client.alert_rules.get( resource_group.name, rule_name ) my_alerts = list(self.mgmt_client.alert_rules.list_by_resource_group( resource_group.name )) self.mgmt_client.alert_rules.delete( resource_group.name, rule_name )