Exemplo n.º 1
0
 def get_rules_dict(self):
     # FIXME In the future we should propagate no-data rules, as well,
     # since we may allow limited actions to be performed on them by users.
     from mist.api.rules.models import MachineMetricRule, NoDataRule
     return {rule.id: rule.as_dict()
             for rule in MachineMetricRule.objects(owner_id=self.id)
             if not isinstance(rule, NoDataRule)}
Exemplo n.º 2
0
 def get_rules_dict(self):
     from mist.api.rules.models import MachineMetricRule
     m = self._instance
     return {
         rule.id: rule.as_dict()
         for rule in MachineMetricRule.objects(owner_id=m.owner.id)
         if rule.ctl.includes_only(m)
     }
Exemplo n.º 3
0
def migrate_old_rule(owner, rule_id, old_rule):
    """Migrate a single rule."""
    kwargs = prepare_kwargs(owner, old_rule)
    rule = MachineMetricRule.add(owner_id=owner.id, title=rule_id, **kwargs)
    verify(old_rule, rule_id, rule)
Exemplo n.º 4
0
def add_rule(request):
    """
    Tags: rules
    ---
    Add a new rule

    READ permission required on Cloud
    EDIT_RULES permission required on Machine

    ---

    queries:
      in: query
      required: true
      description: |
        a list of queries to be evaluated. Queries are chained together with a
        logical AND, meaning that all queries will have to evaluate to True in
        order for the rule to get triggered
      schema:
        type: array
        items:
          type: object
          properties:
            target:
              type: string
              required: true
              description: the metric's name, e.g. "load.shortterm"
            operator:
              type: string
              required: true
              description: |
                the operator used to compare the computed value with the given
                threshold
            threshold:
              type: string
              required: true
              description: the value over/under which an alert will be raised
            aggregation:
              type: string
              required: true
              description: |
                the function to be applied on the computed series. Must be one
                of: all, any, avg

    window:
      in: query
      required: true
      description: the time window of each query
      schema:
        type: object
        properties:
          start:
            type: integer
            required: true
            description: |
              a positive integer denoting the start of the search window in
              terms of "now() - start"
          stop:
            type: integer
            default: 0
            required: false
            description: |
              a positive integer, where stop < start, denoting the end of the
              search window. Defaults to now
          period:
            type: string
            required: true
            description: units of time, e.g. "seconds"

    frequency:
      in: query
      required: true
      description: the frequency of each evaluation
      schema:
        type: object
        properties:
          every:
            type: integer
            required: true
            description: >
              a positive integer denoting how often the rule must be evaluated
          period:
            type: string
            required: true
            description: units of time, e.g. "seconds"

    trigger_after:
      in: query
      required: false
      description: |
        an offset, which prevents an alert from actually being raised, unless
        the threshold is exceeded for "trigger_after" consecutive evaluations
      schema:
        type: object
        properties:
          offset:
            type: integer
            required: true
            description: a positive integer denoting the tolerance period
          period:
            type: string
            required: true
            description: units of time, e.g. "seconds"

    actions:
      in: query
      default: notification
      required: false
      description: |
        a list of actions to be executed once a rule is triggered. Defaults to
        sending a notification
      schema:
        type: array
        items:
          type: object
          properties:
            type:
              type: string
              required: true
              description: >
                the action's type: notification, machine_action, command
            users:
              type: array
              required: false
              description: |
                a list of user to be notified, denoted by their UUIDs. Can be
                used by a notification action (optional)
            teams:
              type: array
              required: false
              description: |
                a list of teams, denoted by their UUIDs, whose users will be
                notified. Can be used by a notification action (optional)
            emails:
              type: array
              required: false
              description: |
                a list of e-mails to send a notification to. Can be used by a
                notification action (optional)
            action:
              type: string
              required: false
              description: >
                the action to be performed. Required by machine_action type
            command:
              type: string
              required: false
              description: >
                the command to be executed. Required by the command type

    selectors:
      in: query
      required: false
      description: |
        a list of selectors to help match resources based on their UUIDs or
        assigned tags. In case of an empty selectors list, the rule will match
        all resources of the corresponding resource type, i.e. all machines
      schema:
        type: array
        items:
          type: object
          properties:
            type:
              type: string
              required: true
              description: one of "machines" or "tags"
            ids:
              type: array
              required: false
              description: a list of UUIDs in case type is "machines"
            tags:
              type: array
              required: false
              description: a list of tags in case type is "tags"

    """
    auth_context = auth_context_from_request(request)
    params = params_from_request(request)

    # Pyramid's `NestedMultiDict` is immutable.
    kwargs = dict(params.copy())

    # FIXME Remove. Now there is a discrete API endpoint for updates.
    if params.get('id'):
        raise BadRequestError('POST to /api/v1/rules/<rule-id> for updates')

    # Add new rule.
    rule = MachineMetricRule.add(auth_context, **kwargs)

    # FIXME Keep this?
    auth_context.owner.rule_counter += 1
    auth_context.owner.save()

    return rule.as_dict()