def amtool_suppress(self, mess, author="errbot", comment="", weeks=0, days=0, hours=0, minutes=0, criteria=[]): """Puts exact suppress match on alert""" helper = AmtoolHelper( alertmanager_address=self.config['server_address']) start_period = datetime.now(pytz.timezone(self.config['time_zone'])) if weeks+days+hours+minutes == 0: hours = 1 end_period = start_period + timedelta(minutes=minutes, hours=hours, days=days, weeks=weeks) self.log.info("Suppressing {0}->{1}".format(start_period, end_period)) fingerprint = criteria.pop(0) self.log.info("Getting alert by fingerprint {0}".format(fingerprint)) alert = helper.get_alert(fingerprint) matchers = helper.get_matchers_by_alert(alert, include_terms=criteria) self.log.info("Matchers {0}".format(matchers)) result = helper.post_silence( matchers=matchers, starts_at=start_period.isoformat(), ends_at=end_period.isoformat(), created_by=author, comment=comment ) self.send_card(title="Alert suppressed until {0}".format(end_period), body="Alert created by {0} with description '{1}'. To cancel !amtool silence expire {2}".format(author, comment, result.silence_id), # thumbnail='https://raw.githubusercontent.com/errbotio/errbot/master/docs/_static/errbot.png', # image='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png', # link=result["generatorURL"], fields=helper.convert_matchers_to_tuples(matchers), color='blue', in_reply_to=mess)
def test_post_silence_suppress(self): amtoolhelper = AmtoolHelper(alertmanager_address=ALERTMANAGER_HOST) start_period = datetime.now(pytz.timezone('Europe/Kiev')) end_period = start_period + timedelta(minutes=1) alert = amtoolhelper.get_alert('ee5d73c3f0a498f1') matchers = amtoolhelper.get_matchers_by_alert(alert, ["name", "role"]) silence = amtoolhelper.post_silence(matchers=matchers, starts_at=start_period.isoformat(), ends_at=end_period.isoformat(), created_by="Someone", comment="test silence") self.assertIsNotNone(silence)