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}
def amtool_silences(self, mess, args): """Returns current silences list""" helper = AmtoolHelper( alertmanager_address=self.config['server_address']) result = helper.get_silences() return {"silences": result}
def test_expired_within_silences(self): amtoolhelper = AmtoolHelper(alertmanager_address=ALERTMANAGER_HOST) silences = amtoolhelper.get_silences(filter=[], expired=True, within="1h") self.assertIsNotNone(silences)
def test_get_silences(self): amtoolhelper = AmtoolHelper(alertmanager_address=ALERTMANAGER_HOST) silences = amtoolhelper.get_silences(filter=[]) self.assertIsNotNone(silences)