-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.py
executable file
·169 lines (131 loc) · 5.5 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
import os
import sys
import click
from flask import Flask, request, make_response
from jira import JIRA
from jinja2 import Template
import prometheus_client as prometheus
app = Flask(__name__)
jira = None
summary_tmpl = Template(r'{% if commonAnnotations.summary %}{{ commonAnnotations.summary }}{% else %}{% for k, v in groupLabels.items() %}{{ k }}="{{v}}" {% endfor %}{% endif %}')
description_tmpl = Template(r'''
h2. Common information
{% for k, v in commonAnnotations.items() -%}
* *{{ k }}*: {{ v }}
{% endfor %}
h2. Active alerts
{% for a in alerts if a.status == 'firing' -%}
_Annotations_:
{% for k, v in a.annotations.items() -%}
* {{ k }} = {{ v }}
{% endfor %}
_Labels_:
{% for k, v in a.labels.items() -%}
* {{ k }} = {{ v }}
{% endfor %}
[Source|{{ a.generatorURL }}]
----
{% endfor %}
alert_group_key={{ groupKey }}
''')
description_boundary = '_-- Alertmanager -- [only edit above]_'
# Order for the search query is important for the query performance. It relies
# on the 'alert_group_key' field in the description that must not be modified.
search_query = 'project = %s and labels = "alert" and description ~ "alert_group_key=%s"'
jira_request_time = prometheus.Histogram('jira_request_latency_seconds', 'Latency when querying the JIRA API', ['action'])
request_time = prometheus.Histogram('request_latency_seconds', 'Latency of incoming requests')
jira_request_time_transitions = jira_request_time.labels({'action': 'transitions'})
jira_request_time_close = jira_request_time.labels({'action': 'close'})
jira_request_time_reopen = jira_request_time.labels({'action': 'reopen'})
jira_request_time_update = jira_request_time.labels({'action': 'update'})
jira_request_time_create = jira_request_time.labels({'action': 'create'})
@jira_request_time_transitions.time()
def transitions(issue):
return jira.transitions(issue)
@jira_request_time_close.time()
def close(issue, tid):
return jira.transition_issue(issue, tid)
@jira_request_time_reopen.time()
def reopen(issue, tid):
return jira.transition_issue(issue, trans['reopen'])
@jira_request_time_update.time()
def update_issue(issue, summary, description):
custom_desc = issue.fields.description.rsplit(description_boundary, 1)[0]
return issue.update(
summary=summary,
description="%s\n\n%s\n%s" % (custom_desc.strip(), description_boundary, description))
@jira_request_time_create.time()
def create_issue(project, team, summary, description):
return jira.create_issue({
'project': {'key': project},
'summary': summary,
'description': "%s\n\n%s" % (description_boundary, description),
'issuetype': {'name': 'Task'},
'labels': ['alert', team],
})
@app.route('/-/health')
def health():
return "OK", 200
@request_time.time()
@app.route('/issues/<project>/<team>', methods=['POST'])
def file_issue(project, team):
"""
This endpoint accepts a JSON encoded notification according to the version 3
of the generic webhook of the Prometheus Alertmanager.
"""
data = request.get_json()
if data['version'] != "3":
return "unknown message version %s" % data['version'], 400
resolved = data['status'] == "resolved"
description = description_tmpl.render(data)
summary = summary_tmpl.render(data)
# If there's already a ticket for the incident, update it and reopen/close if necessary.
result = jira.search_issues(search_query % (project, data['groupKey']))
if result:
issue = result[0]
# We have to check the available transitions for the issue. These differ
# between boards depending on setup.
trans = {}
for t in transitions(issue):
trans[t['name'].lower()] = t['id']
# Try different possible transitions for resolved incidents
# in order of preference. Different ones may work for different boards.
if resolved:
for t in ["resolved", "closed", "done", "complete"]:
if t in trans:
close(issue, trans[t])
break
# For issues that are closed by one of the status definitions below, reopen them.
elif issue.fields.status.name.lower() in ["resolved", "closed", "done", "complete"]:
for t in trans:
if t in ['reopen', 'open']:
reopen(issue, trans[t])
break
# Update the base information regardless of the transition.
update_issue(issue, summary, description)
# Do not create an issue for resolved incidents that were never filed.
elif not resolved:
create_issue(project, team, summary, description)
return "", 200
@app.route('/metrics')
def metrics():
resp = make_response(prometheus.generate_latest(prometheus.core.REGISTRY))
resp.headers['Content-Type'] = prometheus.CONTENT_TYPE_LATEST
return resp, 200
@click.command()
@click.option('--host', help='Host listen address')
@click.option('--port', '-p', default=9050, help='Listen port for the webhook')
@click.option('--debug', '-d', default=False, is_flag=True, help='Enable debug mode')
@click.argument('server')
def main(host, port, server, debug):
global jira
username = os.environ.get('JIRA_USERNAME')
password = os.environ.get('JIRA_PASSWORD')
if not username or not password:
print("JIRA_USERNAME or JIRA_PASSWORD not set")
sys.exit(2)
jira = JIRA(basic_auth=(username, password), server=server, logging=debug)
app.run(host=host, port=port, debug=debug)
if __name__ == "__main__":
main()