Example #1
0
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,
                             alert_rule_resource_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)
Example #2
0
    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
        )
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
Example #4
0
 def get_action(self, values, option_string):  # pylint: disable=no-self-use
     _type = values[0].lower()
     if _type == 'email':
         from azure.mgmt.monitor.models import RuleEmailAction
         return RuleEmailAction(custom_emails=values[1:])
     if _type == 'webhook':
         from azure.mgmt.monitor.models import RuleWebhookAction
         uri = values[1]
         try:
             properties = dict(x.split('=', 1) for x in values[2:])
         except ValueError:
             raise CLIError('usage error: {} webhook URI [KEY=VALUE ...]'.format(option_string))
         return RuleWebhookAction(service_uri=uri, properties=properties)
     raise CLIError('usage error: {} TYPE KEY [ARGS]'.format(option_string))