Skip to content

Commit

Permalink
#21
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose A. Noguera committed Sep 14, 2017
1 parent 59358b3 commit 37783a6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
29 changes: 19 additions & 10 deletions prom2teams/message/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@


def parse(json_str):
logger.debug('received: %s', json_str)

return_dict = {}
json_values = json.loads(json_str)

json_alerts_attr = json_values['alerts'][0]
json_alerts_labels_attr = json_alerts_attr['labels']
json_alerts_annotations_attr = json_alerts_attr['annotations']

return {
'alert_name': json_alerts_labels_attr['alertname'],
'alert_instance': json_alerts_labels_attr['instance'],
'alert_severity': json_alerts_labels_attr['severity'],
'alert_summary': json_alerts_annotations_attr['summary'],
'alert_description': json_alerts_annotations_attr['description'],
'alert_status': json_alerts_attr['status']
}
mandatory_fields = ['alertname', 'status', 'instance', 'summary']
optional_fields = ['severity', 'description']
fields = mandatory_fields + optional_fields

for field in fields:
if field in json_alerts_attr:
return_dict['alert_'+field] = json_alerts_attr[field]
elif field in json_alerts_labels_attr:
return_dict['alert_'+field] = json_alerts_labels_attr[field]
elif field in json_alerts_annotations_attr:
return_dict['alert_'+field] = json_alerts_annotations_attr[field]
elif field in mandatory_fields:
return_dict['alert_severity'] = 'severe'
return_dict['alert_summary'] = 'Incorrect JSON received. At least one mandatory field ('+field+') is absent.'
return_dict['alert_status'] = 'incorrect'
return return_dict

return return_dict
12 changes: 11 additions & 1 deletion prom2teams/teams/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,30 @@
"sections": [{
"activityTitle": "{{ msg_text.alert_summary }}",
"facts": [{
{% if msg_text.alert_alertname %}
"name": "Alarm",
"value": "{{ msg_text.alert_name }}"
"value": "{{ msg_text.alert_alertname }}"
}, {
{% endif %}
{% if msg_text.alert_instance %}
"name": "In host",
"value": "{{ msg_text.alert_instance }}"
}, {
{% endif %}
{% if msg_text.alert_severity %}
"name": "Severity",
"value": "{{ msg_text.alert_severity }}"
}, {
{% endif %}
{% if msg_text.alert_description %}
"name": "Description",
"value": "{{ msg_text.alert_description }}"
}, {
{% endif %}
{% if msg_text.alert_status %}
"name": "Status",
"value": "{{ msg_text.alert_status }}"
{% endif %}
}],
"markdown": true
}]
Expand Down

0 comments on commit 37783a6

Please sign in to comment.