Ejemplo n.º 1
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.schema = MessageSchema()
     if 'TEMPLATE_PATH' in app.config:
         self.sender = AlarmSender(app.config['TEMPLATE_PATH'])
     else:
         self.sender = AlarmSender()
Ejemplo n.º 2
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.schema = MessageSchema()
     if 'TEMPLATE_PATH' in app.config:
         self.sender = AlarmSender(app.config['TEMPLATE_PATH'],
                                   app.config['GROUP_ALERTS'])
     else:
         self.sender = AlarmSender(group_alerts=app.config['GROUP_ALERTS'])
Ejemplo n.º 3
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.schema = MessageSchema(
         exclude_fields=app.config['LABELS_EXCLUDED'],
         exclude_annotations=app.config['ANNOTATIONS_EXCLUDED'])
     self.sender = AlarmSender(
         template_path=app.config.get('TEMPLATE_PATH'),
         group_alerts_by=app.config['GROUP_ALERTS_BY'],
         teams_client_config=app.config.get('TEAMS_CLIENT_CONFIG'))
Ejemplo n.º 4
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.schema = MessageSchema(
         exclude_fields=app.config['LABELS_EXCLUDED'])
     if app.config['TEMPLATE_PATH']:
         self.sender = AlarmSender(app.config['TEMPLATE_PATH'],
                                   app.config['GROUP_ALERTS_BY'])
     else:
         self.sender = AlarmSender(
             group_alerts_by=app.config['GROUP_ALERTS_BY'])
Ejemplo n.º 5
0
class AlertReceiver(Resource):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.schema = MessageSchema()
        if 'TEMPLATE_PATH' in app.config:
            self.sender = AlarmSender(app.config['TEMPLATE_PATH'],
                                      app.config['GROUP_ALERTS'])
        else:
            self.sender = AlarmSender(group_alerts=app.config['GROUP_ALERTS'])

    @api_v2.expect(message)
    def post(self, connector):
        alerts = self.schema.load(request.get_json()).data
        self.sender.send_alarms(alerts, app.config['MICROSOFT_TEAMS'])
        return 'OK', 201
Ejemplo n.º 6
0
    def test_with_too_long_payload(self):
        with open(
                os.path.join(self.TEST_CONFIG_FILES_PATH,
                             'all_ok_multiple.json')) as json_data:
            with open(
                    os.path.join(
                        self.TEST_CONFIG_FILES_PATH,
                        'teams_alarm_all_ok_splited.json')) as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema().load(json_received)
                rendered_data = '[' + ','.join([
                    a.replace("\n\n\n", " ")
                    for a in AlarmSender(group_alerts_by='name',
                                         teams_client_config={
                                             'MAX_PAYLOAD': 800
                                         })._create_alarms(alerts)
                ]) + ']'
                json_rendered = json.loads(rendered_data)

                diff = DeepDiff(json_rendered,
                                json_expected,
                                ignore_order=True)
                self.assertTrue(not diff)
Ejemplo n.º 7
0
class AlertReceiver(Resource):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.schema = MessageSchema()
        self.sender = AlarmSender(
            template_path=app.config.get('TEMPLATE_PATH'),
            teams_client_config=app.config.get('TEAMS_CLIENT_CONFIG'))

    @api_v1.expect(message)
    def post(self):
        _show_deprecated_warning(
            "Call to deprecated function. It will be removed in future versions. "
            "Please view the README file.")
        alerts = self.schema.load(request.get_json())
        self.sender.send_alarms(alerts,
                                app.config['MICROSOFT_TEAMS']['Connector'])
        return 'OK', 201
Ejemplo n.º 8
0
class AlertReceiver(Resource):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.schema = MessageSchema(
            exclude_fields=app.config['LABELS_EXCLUDED'],
            exclude_annotations=app.config['ANNOTATIONS_EXCLUDED'])
        self.sender = AlarmSender(
            template_path=app.config.get('TEMPLATE_PATH'),
            group_alerts_by=app.config['GROUP_ALERTS_BY'],
            teams_client_config=app.config.get('TEAMS_CLIENT_CONFIG'))

    @api_v2.expect(message)
    def post(self, connector):
        alerts = self.schema.load(request.get_json())
        self.sender.send_alarms(alerts,
                                app.config['MICROSOFT_TEAMS'][connector])
        return 'OK', 201
Ejemplo n.º 9
0
class AlertReceiver(Resource):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.schema = MessageSchema(
            exclude_fields=app.config['LABELS_EXCLUDED'])
        if app.config['TEMPLATE_PATH']:
            self.sender = AlarmSender(app.config['TEMPLATE_PATH'],
                                      app.config['GROUP_ALERTS_BY'])
        else:
            self.sender = AlarmSender(
                group_alerts_by=app.config['GROUP_ALERTS_BY'])

    @api_v2.expect(message)
    def post(self, connector):
        alerts = self.schema.load(request.get_json())
        self.sender.send_alarms(alerts,
                                app.config['MICROSOFT_TEAMS'][connector])
        return 'OK', 201
Ejemplo n.º 10
0
class AlertReceiver(Resource):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.schema = MessageSchema()
        if app.config['TEMPLATE_PATH']:
            self.sender = AlarmSender(app.config['TEMPLATE_PATH'],
                                      app.config['GROUP_ALERTS_BY'])
        else:
            self.sender = AlarmSender(
                group_alerts_by=app.config['GROUP_ALERTS_BY'])

    @api_v2.expect(message)
    def post(self, connector):
        log.debug('JSON received is:\n%s', str(request.get_json()))
        alerts = self.schema.load(request.get_json())
        self.sender.send_alarms(alerts,
                                app.config['MICROSOFT_TEAMS'][connector])
        return 'OK', 201
Ejemplo n.º 11
0
class AlertReceiver(Resource):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.schema = MessageSchema()
        if 'TEMPLATE_PATH' in app.config:
            self.sender = AlarmSender(app.config['TEMPLATE_PATH'])
        else:
            self.sender = AlarmSender()

    @api_v1.expect(message)
    def post(self):
        _show_deprecated_warning(
            "Call to deprecated function. It will be removed in future versions. "
            "Please view the README file.")
        alerts = self.schema.load(request.get_json()).data
        self.sender.send_alarms(alerts,
                                app.config['MICROSOFT_TEAMS']['Connector'])
        return 'OK', 201
Ejemplo n.º 12
0
class AlertLabelReceiver(Resource):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.schema = MessageSchema(
            exclude_fields=app.config['LABELS_EXCLUDED'],
            exclude_annotations=app.config['ANNOTATIONS_EXCLUDED'])
        if app.config['TEMPLATE_PATH']:
            self.sender = AlarmSender(app.config['TEMPLATE_PATH'],
                                      app.config['GROUP_ALERTS_BY'])
        else:
            self.sender = AlarmSender(
                group_alerts_by=app.config['GROUP_ALERTS_BY'])

    @api_v2.expect(message)
    def post(self):
        alerts = self.schema.load(request.get_json())
        self.sender.send_alerts_by_label(alerts)
        return 'OK', 201
Ejemplo n.º 13
0
    def test_compose_all(self):
        with open(self.TEST_CONFIG_FILES_PATH + 'all_ok.json') as json_data:
            with open(self.TEST_CONFIG_FILES_PATH + 'teams_alarm_all_ok.json') as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema().load(json_received)
                rendered_data = AlarmSender()._create_alarms(alerts)[0]
                json_rendered = json.loads(rendered_data)

                self.assertDictEqual(json_rendered, json_expected)
Ejemplo n.º 14
0
    def test_grouping_multiple_alerts(self):
        with open(self.TEST_CONFIG_FILES_PATH + 'all_ok_multiple.json') as json_data:
            with open(self.TEST_CONFIG_FILES_PATH + 'teams_alarm_all_ok_multiple.json') as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema().load(json_received)
                rendered_data = AlarmSender(group_alerts_by='name')._create_alarms(alerts)[0].replace("\n\n\n", " ")
                json_rendered = json.loads(rendered_data)

                self.assertDictEqual(json_rendered, json_expected)
Ejemplo n.º 15
0
    def test_with_common_items(self):
        self.maxDiff = None
        with open(self.TEST_CONFIG_FILES_PATH + 'with_common_items.json') as json_data:
            with open(self.TEST_CONFIG_FILES_PATH + 'teams_alarm_with_common_items.json') as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema().load(json_received)
                rendered_data = AlarmSender()._create_alarms(alerts)[0]
                json_rendered = json.loads(rendered_data)

                self.assertDictEqual(json_rendered, json_expected)
Ejemplo n.º 16
0
class AlertReceiver(Resource):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.schema = MessageSchema()
        if 'TEMPLATE_PATH' in app.config:
            self.sender = AlarmSender(app.config['TEMPLATE_PATH'])
        else:
            self.sender = AlarmSender()

    @api_v2.expect(message)
    def post(self, connector):
        alerts = self.schema.load(request.get_json()).data
        if 'Critical' in app.config['MICROSOFT_TEAMS']:
            critical_alerts = [
                alert for alert in alerts if alert.severity is 'critical'
            ]
            self.sender.send_alarms(critical_alerts,
                                    app.config['MICROSOFT_TEAMS']['Critical'])
        self.sender.send_alarms(alerts,
                                app.config['MICROSOFT_TEAMS'][connector])
        return 'OK', 201
Ejemplo n.º 17
0
    def test_with_extra_labels(self):
        excluded_labels = ('pod_name', )
        with open(self.TEST_CONFIG_FILES_PATH +
                  'all_ok_extra_labels.json') as json_data:
            with open(self.TEST_CONFIG_FILES_PATH +
                      'teams_alarm_all_ok_extra_labels.json') as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema(
                    exclude_fields=excluded_labels).load(json_received)
                rendered_data = AlarmSender()._create_alarms(alerts)[0]
                json_rendered = json.loads(rendered_data)

                self.assertDictEqual(json_rendered, json_expected)
Ejemplo n.º 18
0
    def test_compose_all(self):
        with open(os.path.join(self.TEST_CONFIG_FILES_PATH,
                               'all_ok.json')) as json_data:
            with open(
                    os.path.join(self.TEST_CONFIG_FILES_PATH,
                                 'teams_alarm_all_ok.json')) as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema().load(json_received)
                rendered_data = AlarmSender()._create_alarms(alerts)[0]
                json_rendered = json.loads(rendered_data)

                diff = DeepDiff(json_rendered,
                                json_expected,
                                ignore_order=True)
                self.assertTrue(not diff)
Ejemplo n.º 19
0
    def test_grouping_multiple_alerts(self):
        with open(
                os.path.join(self.TEST_CONFIG_FILES_PATH,
                             'all_ok_multiple.json')) as json_data:
            with open(
                    os.path.join(
                        self.TEST_CONFIG_FILES_PATH,
                        'teams_alarm_all_ok_multiple.json')) as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema().load(json_received)
                rendered_data = AlarmSender(
                    group_alerts_by='name')._create_alarms(alerts)[0].replace(
                        "\n\n\n", " ")
                json_rendered = json.loads(rendered_data)

                diff = DeepDiff(json_rendered,
                                json_expected,
                                ignore_order=True)
                self.assertTrue(not diff)
Ejemplo n.º 20
0
    def test_with_extra_annotations(self):
        excluded_annotations = ('message', )
        with open(
                os.path.join(self.TEST_CONFIG_FILES_PATH,
                             'all_ok_extra_annotations.json')) as json_data:
            with open(
                    os.path.join(self.TEST_CONFIG_FILES_PATH,
                                 'teams_alarm_all_ok_extra_annotations.json')
            ) as expected_data:
                json_received = json.load(json_data)
                json_expected = json.load(expected_data)

                alerts = MessageSchema(exclude_annotations=excluded_annotations
                                       ).load(json_received)
                rendered_data = AlarmSender()._create_alarms(alerts)[0]
                json_rendered = json.loads(rendered_data)

                diff = DeepDiff(json_rendered,
                                json_expected,
                                ignore_order=True)
                self.assertTrue(not diff)
Ejemplo n.º 21
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.schema = MessageSchema()
     self.sender = AlarmSender(
         template_path=app.config.get('TEMPLATE_PATH'),
         teams_client_config=app.config.get('TEAMS_CLIENT_CONFIG'))