def on_message(self, body, message): try: alert = AlertDocument.parse_alert(body) alertid = alert.get_id() except Exception as e: LOG.warn(e) return if alert.repeat: message.ack() return if alert.status not in ["open", "closed"]: message.ack() return if alert.severity not in ["critical", "major"] and alert.previous_severity not in ["critical", "major"]: message.ack() return if alertid in on_hold: if alert.severity in ["normal", "ok", "cleared"]: try: del on_hold[alertid] except KeyError: pass message.ack() else: on_hold[alertid] = (alert, time.time() + HOLD_TIME) message.ack() else: on_hold[alertid] = (alert, time.time() + HOLD_TIME) message.ack()
def test_rules_evaluation(alert_spec, input_rules, expected_contacts): ''' Test that rules are properly evaluated ''' with patch.dict(mailer.OPTIONS, mailer.DEFAULT_OPTIONS): mailer.OPTIONS['mail_to'] = [] mailer.OPTIONS['group_rules'] = input_rules mail_sender = mailer.MailSender() with patch.object(mail_sender, '_send_email_message') as _sem: alert = AlertDocument.parse_alert(alert_spec) _, emailed_contacts = mail_sender.send_email(alert) assert _sem.call_count == 1 assert emailed_contacts == expected_contacts
def query(self, args, from_date=None): response = self._alerts(args.filters, from_date) alerts = response['alerts'] if args.output == "json": print(json.dumps(alerts, indent=4)) sys.exit(0) for alert in reversed(alerts): a = AlertDocument.parse_alert(alert) line_color = '' end_color = _ENDC if args.color: line_color = _COLOR_MAP.get(a.severity, _COLOR_MAP['unknown']) print( line_color + '{0}|{1}|{2}|{3:5d}|{4}|{5:<5s}|{6:<10s}|{7:<18s}|{8:12s}|{9:16s}|{10:12s}' .format( a.id[0:8], a.get_date('last_receive_time', 'local', args.timezone), a.severity, a.duplicate_count, a.customer or "-", a.environment, ','.join(a.service), a.resource, a.group, a.event, a.value) + end_color) print(line_color + ' |{}'.format(a.text) + end_color) if args.details: print(line_color + ' severity | {} -> {}'.format( a.previous_severity, a.severity) + end_color) print(line_color + ' trend | {}'.format(a.trend_indication) + end_color) print(line_color + ' status | {}'.format(a.status) + end_color) print(line_color + ' resource | {}'.format(a.resource) + end_color) print(line_color + ' group | {}'.format(a.group) + end_color) print(line_color + ' event | {}'.format(a.event) + end_color) print(line_color + ' value | {}'.format(a.value) + end_color) print(line_color + ' tags | {}'.format(' '.join(a.tags)) + end_color) for key, value in a.attributes.items(): print(line_color + ' {} | {}'.format(key.ljust(10), value) + end_color) latency = a.receive_time - a.create_time print(line_color + ' time created | {}'.format( a.get_date('create_time', 'iso', args.timezone)) + end_color) print(line_color + ' time received | {}'.format( a.get_date('receive_time', 'iso', args.timezone)) + end_color) print(line_color + ' last received | {}'.format( a.get_date('last_receive_time', 'iso', args.timezone)) + end_color) print(line_color + ' latency | {}ms'.format( (latency.microseconds / 1000)) + end_color) print(line_color + ' timeout | {}s'.format(a.timeout) + end_color) print(line_color + ' alert id | {}'.format(a.id) + end_color) print( line_color + ' last recv id | {}'.format(a.last_receive_id) + end_color) print(line_color + ' customer | {}'.format(a.customer) + end_color) print(line_color + ' environment | {}'.format(a.environment) + end_color) print(line_color + ' service | {}'.format( ','.join(a.service)) + end_color) print(line_color + ' resource | {}'.format(a.resource) + end_color) print(line_color + ' type | {}'.format(a.event_type) + end_color) print(line_color + ' repeat | {}'.format(a.repeat) + end_color) print(line_color + ' origin | {}'.format(a.origin) + end_color) print(line_color + ' correlate | {}'.format( ','.join(a.correlate)) + end_color) return response.get('lastTime', '')
def query(self, args, from_date=None): response = self._alerts(args.filters, from_date) alerts = response['alerts'] if args.output == "json": print(json.dumps(alerts, indent=4)) sys.exit(0) for alert in reversed(alerts): a = AlertDocument.parse_alert(alert) line_color = '' end_color = _ENDC if args.color: line_color = _COLOR_MAP.get(a.severity, _COLOR_MAP['unknown']) print(line_color + '{0}|{1}|{2}|{3:5d}|{4}|{5:<5s}|{6:<10s}|{7:<18s}|{8:12s}|{9:16s}|{10:12s}'.format( a.id[0:8], a.get_date('last_receive_time', 'local', args.timezone), a.severity, a.duplicate_count, a.customer or "-", a.environment, ','.join(a.service), a.resource, a.group, a.event, a.value) + end_color) print(line_color + ' |{}'.format(a.text) + end_color) if args.details: print(line_color + ' severity | {} -> {}'.format(a.previous_severity, a.severity) + end_color) print(line_color + ' trend | {}'.format(a.trend_indication) + end_color) print(line_color + ' status | {}'.format(a.status) + end_color) print(line_color + ' resource | {}'.format(a.resource) + end_color) print(line_color + ' group | {}'.format(a.group) + end_color) print(line_color + ' event | {}'.format(a.event) + end_color) print(line_color + ' value | {}'.format(a.value) + end_color) print(line_color + ' tags | {}'.format(' '.join(a.tags)) + end_color) for key, value in a.attributes.items(): print(line_color + ' {} | {}'.format(key.ljust(10), value) + end_color) latency = a.receive_time - a.create_time print(line_color + ' time created | {}'.format(a.get_date('create_time', 'iso', args.timezone)) + end_color) print(line_color + ' time received | {}'.format(a.get_date('receive_time', 'iso', args.timezone)) + end_color) print(line_color + ' last received | {}'.format(a.get_date('last_receive_time', 'iso', args.timezone)) + end_color) print(line_color + ' latency | {}ms'.format((latency.microseconds / 1000)) + end_color) print(line_color + ' timeout | {}s'.format(a.timeout) + end_color) print(line_color + ' alert id | {}'.format(a.id) + end_color) print(line_color + ' last recv id | {}'.format(a.last_receive_id) + end_color) print(line_color + ' customer | {}'.format(a.customer) + end_color) print(line_color + ' environment | {}'.format(a.environment) + end_color) print(line_color + ' service | {}'.format(','.join(a.service)) + end_color) print(line_color + ' resource | {}'.format(a.resource) + end_color) print(line_color + ' type | {}'.format(a.event_type) + end_color) print(line_color + ' repeat | {}'.format(a.repeat) + end_color) print(line_color + ' origin | {}'.format(a.origin) + end_color) print(line_color + ' correlate | {}'.format(','.join(a.correlate)) + end_color) return response.get('lastTime', '')