Esempio n. 1
0
    def amtool_alert_query(self, mess, inhibited=False, silenced=False,
        active=False, unprocessed=False, receiver="", matchers=[]):
        """
          Queries alert in amtool command style
          --inhibited          Show inhibited alerts
          --silenced           Show silenced alerts
          --active             Show active alerts
          --unprocessed        Show unprocessed alerts
          --receiver=RECEIVER  Show alerts matching receiver (Supports regex syntax)

        Args:
          [<matcher-groups>]  Query filter
        """
        if not (inhibited or silenced or active or unprocessed):
            inhibited = True
            silenced = True
            active = True
            unprocessed = True
        self.log.info("matchers {0}".format(matchers))
        helper = AmtoolHelper(
            alertmanager_address=self.config['server_address'])
        filter = helper.get_filters_by_terms(matchers)
        result = helper.get_alerts(
            active=active,
            silenced=silenced,
            inhibited=inhibited,
            unprocessed=unprocessed,
            filter=filter,
            receiver=receiver
        )
        return result
Esempio n. 2
0
    def amtool_silence_query(self, mess, expired=None, within=None, matchers=[]):
        """
          Amtool has a simplified prometheus query syntax The non-option section of arguments constructs a list of "Matcher Groups"
          that will be used to filter your query. The following examples will attempt to show this behaviour in action:

          amtool silence query alertname=foo node=bar

          This query will match all silences with the alertname=foo and node=bar label
          value pairs set.

          amtool silence query foo node=bar

          If alertname is omitted and the first argument does not contain a '=' or a
          '=~' then it will be assumed to be the value of the alertname pair.

          amtool silence query 'alertname=~foo.*'

          As well as direct equality, regex matching is also supported. The '=~' syntax
          (similar to prometheus) is used to represent a regex match. Regex matching
          can be used in combination with a direct match.

          In addition to filtering by silence labels, one can also query for silences that are due to expire soon
          with the "--within" parameter. In the event that you want to preemptively act upon expiring silences by
          either fixing them or extending them. For example:

          amtool silence query --within 8h

          returns all the silences due to expire within the next 8 hours. This syntax can also be combined with the label based
          filtering above for more flexibility.

          The "--expired" parameter returns only expired silences. Used in combination with "--within=TIME", amtool returns
          the silences that expired within the preceding duration.

          amtool silence query --within 2h --expired

returns all silences that expired within the preceeding 2 hours.

      --expired        Show expired silences instead of active
      --within=WITHIN  Show silences that will expire or have expired within a duration
        """
        helper = AmtoolHelper(
            alertmanager_address=self.config['server_address'])
        filters = helper.get_filters_by_terms(matchers)
        self.log.info("Expired {0} within {1} filtered {2}".format(expired, within, filters))
        result = helper.get_silences(filter=filters, expired=expired, within=within)
        return {"silences": result}
Esempio n. 3
0
 def test_get_alerts_with_filter(self):
     amtoolhelper = AmtoolHelper(alertmanager_address=ALERTMANAGER_HOST)
     filter = amtoolhelper.get_filters_by_terms(["env=live"])
     alerts = amtoolhelper.get_alerts(filter=filter)
     self.assertIsNotNone(alerts)