From 13581eff02fc5421b2445932912634b7957f6e2a Mon Sep 17 00:00:00 2001 From: Daniel Ortega Date: Sun, 19 Nov 2017 20:42:24 +0100 Subject: [PATCH 1/5] Deleting extraspace --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 214dd77..d570735 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ prom2teams provides a [default template](app/teams/template.j2) built with [Jinj To run the test suite you should type the following: ```bash -# After cloning prom2 teams :) +# After cloning prom2teams :) $ python3 -m unittest discover tests ``` From 085fb48d18a001a70913d86294c66f8328f06388 Mon Sep 17 00:00:00 2001 From: Roger Lindholm Date: Mon, 20 Nov 2017 19:21:28 +0100 Subject: [PATCH 2/5] All mandatory fields now have default values --- CHANGELOG.md | 1 + README.md | 5 +++++ prom2teams/message/parser.py | 18 +++++++----------- prom2teams/teams/template.j2 | 5 +++-- tests/data/jsons/without_mandatory_field.json | 8 ++------ tests/test_json_fields.py | 8 ++++---- 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87dc002..2c64477 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a changelog](https://github.com/olivierlacan/keep-a-changelog). ## [Unreleased](https://github.com/idealista/prom2teams/tree/develop) +- *[#32](https://github.com/idealista/prom2teams/issues/32) Support alerts with missing mandatory attributes @lindhor ## [1.2.0](https://github.com/idealista/prom2teams/tree/1.2.0) [Full Changelog](https://github.com/idealista/prom2teams/compare/1.1.3...1.2.0) diff --git a/README.md b/README.md index d570735..4bdb8af 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,11 @@ url: 0.0.0.0:8089 prom2teams provides a [default template](app/teams/template.j2) built with [Jinja2](http://jinja.pocoo.org/docs/2.9/) to render messages in Microsoft Teams. This template could be overrided using the 'templatepath' argument ('--templatepath ') during the application start. +Some fields are considered mandatory when received from Alert Manager. +If such a field is not included a default value of 'unknown' is assigned as described below: + +Other optional fields are skipped and not included in the Teams message. + ## Testing To run the test suite you should type the following: diff --git a/prom2teams/message/parser.py b/prom2teams/message/parser.py index 2d3f216..35c7e02 100644 --- a/prom2teams/message/parser.py +++ b/prom2teams/message/parser.py @@ -5,15 +5,12 @@ logger = logging.getLogger() def check_fields(json_alerts_attr, json_alerts_labels_attr, json_alerts_annotations_attr): - mandatory_fields = ['alertname', 'status', 'summary'] - optional_fields = ['severity', 'description', 'instance'] + mandatory_fields = ['alertname', 'status', 'instance', 'summary'] + optional_fields = ['severity', 'description'] fields = mandatory_fields + optional_fields alert_fields = {} - # Set the instance to 'none' by default. - alert_fields['alert_instance'] = 'none' - for field in fields: alert_field_key = 'alert_' + field if field in json_alerts_attr: @@ -22,13 +19,12 @@ def check_fields(json_alerts_attr, json_alerts_labels_attr, json_alerts_annotati alert_fields[alert_field_key] = json_alerts_labels_attr[field] elif field in json_alerts_annotations_attr: alert_fields[alert_field_key] = json_alerts_annotations_attr[field] - # If the field isn't in the JSON but it's a mandatory field, then we send an error message + # If the field isn't in the JSON but it's a mandatory field, then we use default values elif field in mandatory_fields: - alert_fields['alert_severity'] = 'severe' - alert_fields['alert_status'] = 'incorrect' - alert_fields['alert_summary'] = 'Incorrect JSON received. At least one mandatory field ('+field+') is absent.' - return alert_fields - + if field in json_alerts_attr: + alert_fields[alert_field_key] = json_alerts_attr[field] + else: + alert_fields[alert_field_key] = 'unknown' return alert_fields def parse(json_str): diff --git a/prom2teams/teams/template.j2 b/prom2teams/teams/template.j2 index 39fef2e..72836e7 100644 --- a/prom2teams/teams/template.j2 +++ b/prom2teams/teams/template.j2 @@ -3,7 +3,8 @@ 'resolved' : '2DC72D', 'critical' : '8C1A1A', 'severe' : '8C1A1A', - 'warning' : 'FF9A0B' + 'warning' : 'FF9A0B', + 'unknown' : 'CCCCCC' } -%} @@ -12,7 +13,7 @@ "@context": "http://schema.org/extensions", "themeColor": "{% if alert_status=='resolved' %} {{ theme_colors.resolved }} {% else %} {{ theme_colors[msg_text.alert_severity] }} {% endif %}", "summary": "{{ msg_text.alert_summary }}", - "title": "Prometheus alarm {% if alert_status=='resolved' %} (Resolved) {% elif alert_status=='incorrect' %} (Incorrect) {% endif %}", + "title": "Prometheus alarm {% if alert_status=='resolved' %} (Resolved) {% elif alert_status=='unknown' %} (status unknown) {% endif %}", "sections": [{ "activityTitle": "{{ msg_text.alert_summary }}", "facts": [{% if msg_text.alert_alertname %}{ diff --git a/tests/data/jsons/without_mandatory_field.json b/tests/data/jsons/without_mandatory_field.json index 87b4f30..713b4b5 100644 --- a/tests/data/jsons/without_mandatory_field.json +++ b/tests/data/jsons/without_mandatory_field.json @@ -3,17 +3,13 @@ "alerts": [ { "labels": { - "alertname": "DiskSpace", "device": "rootfs", "fstype": "rootfs", - "instance": "cs30.evilcorp", "job": "fsociety", - "mountpoint": "/", - "severity": "severe" + "mountpoint": "/" }, "annotations": { - "description": "disk usage 73% on rootfs device", - "summary": "Disk usage alert on CS30.evilcorp" + "description": "disk usage 73% on rootfs device" }, "startsAt": "2017-05-09T07:01:37.803Z", "endsAt": "2017-05-09T07:08:37.818278068Z", diff --git a/tests/test_json_fields.py b/tests/test_json_fields.py index 6519929..ce346f9 100644 --- a/tests/test_json_fields.py +++ b/tests/test_json_fields.py @@ -12,25 +12,25 @@ def test_json_with_all_fields(self): with open(self.TEST_CONFIG_FILES_PATH + 'all_ok.json') as json_data: json_received = json.load(json_data) alert_fields = parse(json.dumps(json_received)) - self.assertNotIn('Incorrect',str(alert_fields)) + self.assertNotIn('unknown',str(alert_fields)) def test_json_without_mandatory_field(self): with open(self.TEST_CONFIG_FILES_PATH + 'without_mandatory_field.json') as json_data: json_received = json.load(json_data) alert_fields = parse(json.dumps(json_received)) - self.assertIn('Incorrect',str(alert_fields)) + self.assertIn('unknown',str(alert_fields)) def test_json_without_optional_field(self): with open(self.TEST_CONFIG_FILES_PATH + 'without_optional_field.json') as json_data: json_received = json.load(json_data) alert_fields = parse(json.dumps(json_received)) - self.assertNotIn('Incorrect',str(alert_fields)) + self.assertNotIn('unknown',str(alert_fields)) def test_json_without_instance_field(self): with open(self.TEST_CONFIG_FILES_PATH + 'without_instance_field.json') as json_data: json_received = json.load(json_data) alert_fields = parse(json.dumps(json_received)) - self.assertEqual('none',str(alert_fields['alarm_0']['alert_instance'])) + self.assertEqual('unknown',str(alert_fields['alarm_0']['alert_instance'])) if __name__ == '__main__': unittest.main() From ea4cb2769e71f14063518aa0825c03e8be750198 Mon Sep 17 00:00:00 2001 From: Jose Date: Thu, 4 Jan 2018 11:53:51 +0100 Subject: [PATCH 3/5] #38 Add Dockerfile --- Dockerfile | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..988102e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.5-slim-jessie + +LABEL maintainer="labs@idealista.com" + +EXPOSE 8089 + +RUN pip install prom2teams +COPY config.ini /opt/prom2teams/config.ini + +ENTRYPOINT ["prom2teams", "--configpath", "/opt/prom2teams/config.ini"] From 71dd271223a13c31746d9143bb44b48682644495 Mon Sep 17 00:00:00 2001 From: Jose Date: Thu, 4 Jan 2018 11:54:24 +0100 Subject: [PATCH 4/5] Fix error in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 214dd77..cd1de80 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ $ pip3 install prom2teams ```bash # To start the server (a config file path must be provided, log file path, log level and Jinja2 template path are optional arguments): -$ prom2teams start --configpath [--logfilepath ] [--loglevel (DEBUG|INFO|WARNING|ERROR|CRITICAL)] [--templatepath ] +$ prom2teams --configpath [--logfilepath ] [--loglevel (DEBUG|INFO|WARNING|ERROR|CRITICAL)] [--templatepath ] # To show the help message: $ prom2teams --help From 3ccad27ca289fc4586177193619b8f397c9e24d6 Mon Sep 17 00:00:00 2001 From: Jose Date: Mon, 8 Jan 2018 09:28:04 +0100 Subject: [PATCH 5/5] Update with v1.3.0 --- CHANGELOG.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c64477..4e4fdf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,17 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a changelog](https://github.com/olivierlacan/keep-a-changelog). ## [Unreleased](https://github.com/idealista/prom2teams/tree/develop) -- *[#32](https://github.com/idealista/prom2teams/issues/32) Support alerts with missing mandatory attributes @lindhor + +## [1.3.0](https://github.com/idealista/prom2teams/tree/1.3.0) +[Full Changelog](https://github.com/idealista/prom2teams/compare/1.2.0...1.3.0) +### Added +- *[#32](https://github.com/idealista/prom2teams/issues/32) Support alerts with missing mandatory attributes* @lindhor +- *[#38](https://github.com/idealista/prom2teams/issues/38) Dockerfile* @jnogol ## [1.2.0](https://github.com/idealista/prom2teams/tree/1.2.0) [Full Changelog](https://github.com/idealista/prom2teams/compare/1.1.3...1.2.0) ### Added -- *[#32](https://github.com/idealista/prom2teams/issues/32) Sets default value for "instance" in alerts* @maglo +- *[#32](https://github.com/idealista/prom2teams/issues/32) Set default value for "instance" in alerts* @maglo ### Fixed - *[#26](https://github.com/idealista/prom2teams/issues/26) Able to handle multiple received alarms* @jnogol