forked from devopshq/zabbix-youtrack-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZabbixAlertYTWorkflow.py
263 lines (212 loc) · 9.65 KB
/
ZabbixAlertYTWorkflow.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# (c) DevOpsHQ, 2016
# Integration YouTrack and Zabbix alerts.
import yaml
from pyzabbix import ZabbixAPI
import sys
from six.moves.urllib.parse import quote
import logging
import time
import settings
from youtrack.connection import Connection
import re
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# ------------ START Setup logging ------------
# Use logger to log information
logger = logging.getLogger()
logger.setLevel(settings.LOG_LEVEL)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# Log to file
fh = logging.FileHandler(settings.LOG_FILE_NAME)
fh.setLevel(settings.LOG_LEVEL)
fh.setFormatter(formatter)
logger.addHandler(fh)
# Log to stdout
ch = logging.StreamHandler()
ch.setLevel(settings.LOG_LEVEL)
ch.setFormatter(formatter)
logger.addHandler(ch) # Use logger to log information
# Log from pyzabbix
log = logging.getLogger('pyzabbix')
log.addHandler(ch)
log.addHandler(fh)
log.setLevel(settings.LOG_LEVEL)
# ------------ END Setup logging ------------
# ------------ START ZabbixAPI block ------------
Zbx = ZabbixAPI(settings.ZBX_SERVER)
Zbx.session.verify = False
Zbx.login(settings.ZBX_USER, settings.ZBX_PASSWORD)
# ------------ END ZabbixAPI block ------------
# ------------ START Function declaration ------------
def ExecAndLog(connection, issueId, command="", comment=""):
logger.debug("Run command in {issueId}: {command}. {comment}".format(issueId=issueId,
command=command,
comment=comment
))
connection.executeCommand(issueId=issueId,
command=command,
comment=comment,
)
# ------------ END Function declaration ------------
def updateIssue(connection, issueId, summary, description):
connection._req('POST', "/issue/{issueId}?summary={summary}&description={description}".format(
issueId=issueId,
summary=quote(summary),
description=quote(description)
))
def Main(sendTo, subject, yamlMessage):
"""
Workflow Zabbix-YouTrack
:param sendTo: URL to Youtrack (ex. https://youtrack.example.com)
:param subject: subject from Zabbix Action
:param yamlMessage: message from Zabbix Action
:return:
"""
# ----- Use below example yamlMessage to debug -----
# yamlMessage = """Name: 'Test Zabbix-YT workflow, ignore it'
# Text: 'Agent ping (server:agent.ping()): DOWN (1) '
# Hostname: 'server.exmpale.ru'
# Status: "OK"
# Severity: "High"
# EventID: "96976"
# TriggerID: "123456789012" """
messages = yaml.load(yamlMessage)
# ----- START Issue parameters -----
# Correspondence between the YouTrackPriority and ZabbixSeverity
# Critical >= High
# Normal < High
ytPriority = 'Normal'
if messages['Severity'] == 'Disaster' or messages['Severity'] == 'High':
ytPriority = 'Critical'
ytName = "{} ZabbixTriggerID::{}".format(messages['Name'], messages['TriggerID'])
# ----- END Issue parameters -----
# ----- START Youtrack Issue description -----
# Search link to other issue
searchString = "Hostname: '{}'".format(messages['Hostname'])
linkToHostIssue = "{youtrack}/issues/{projectname}?q={query}".format(
youtrack=sendTo,
projectname=settings.YT_PROJECT_NAME,
query=quote(searchString, safe='')
)
issueDescription = """
{ytName}
-----
{yamlMessage}
-----
- [Zabbix Dashboard]({zabbix}?action=dashboard.view)
- Show all issue for [*this host*]({linkToHostIssue})
""".format(
ytName=ytName,
yamlMessage=yamlMessage,
zabbix=settings.ZBX_SERVER,
linkToHostIssue=linkToHostIssue, )
# ----- END Youtrack Issue description -----
# Create connect to Youtrack API
connection = Connection(sendTo, token=settings.YT_TOKEN)
# ----- START Youtrack get or create issue -----
# Get issue if exist
# Search for TriggerID
createNewIssue = False
logger.debug("Get issue with text '{}'".format(messages['TriggerID']))
issue = connection.getIssues(settings.YT_PROJECT_NAME,
"ZabbixTriggerID::{}".format(messages['TriggerID']),
0,
1)
if len(issue) == 0:
createNewIssue = True
else:
# if issue contains TriggerID in summary, then it's good issue
# else create new issue, this is bad issue, not from Zabbix
if "ZabbixTriggerID::{}".format(messages['TriggerID']) in issue[0]['summary']:
issueId = issue[0]['id']
issue = connection.getIssue(issueId)
else:
createNewIssue = True
# Create new issue
if createNewIssue:
logger.debug("Create new issue because it is not exist")
issue = connection.createIssue(settings.YT_PROJECT_NAME,
'Unassigned',
ytName,
issueDescription,
priority=ytPriority,
subsystem=settings.YT_SUBSYSTEM,
state="Open",
type=settings.YT_TYPE,
)
time.sleep(3)
# Parse ID for new issue
result = re.search(r'(PI-\d*)', issue[0]['location'])
issueId = result.group(0)
issue = connection.getIssue(issueId)
logger.debug("Issue have id={}".format(issueId))
# Set issue service
ExecAndLog(connection, issueId, "Исполнитель {}".format(settings.YT_SERVICE))
# Update priority
ExecAndLog(connection, issueId, "Priority {}".format(ytPriority))
# ----- END Youtrack get or create issue -----
# ----- START PROBLEM block ------
if messages['Status'] == "PROBLEM":
# Reopen if Fixed or Verified or Canceled
if issue['State'] == u"На тестировании" or issue['State'] == u"Завершена" or issue['State'] == u"Исполнение не планируется":
# Reopen Issue
ExecAndLog(connection, issueId, "State Open")
# Исполнитель issue
ExecAndLog(connection, issueId, command="Исполнитель Unassigned")
# Update summary and description for issue
logger.debug("Run command in {issueId}: {command}".format(issueId=issueId,
command="""Update summary and description with connection.updateIssue method"""
))
updateIssue(connection, issueId=issueId, summary=ytName, description=issueDescription)
# Add comment
logger.debug("Run command in {issueId}: {command}".format(issueId=issueId,
command="""Now is PROBLEM {}""".format(
messages['Text'])
))
connection.executeCommand(issueId=issueId,
command="comment",
comment=settings.YT_COMMENT.format(
status=messages['Status'],
text=messages['Text'])
)
# Send ID to Zabbix:
logger.debug("ZABBIX-API: Send Youtrack ID to {}".format(messages['EventID']))
Zbx.event.acknowledge(eventids=messages['EventID'], action=4, message="Create Youtrack task")
Zbx.event.acknowledge(eventids=messages['EventID'], action=4,
message=(settings.YT_SERVER + "/issue/{}").format(issueId))
# ----- End PROBLEM block ------
# ----- Start OK block -----
if messages['Status'] == "OK":
if issue['State'] == u"На тестировании":
# Verify if Fixed
ExecAndLog(connection, issueId, command="State Завершена")
else:
if issue['State'] == u"Открыта":
ExecAndLog(connection, issueId, command="State Требует анализа проблемы")
logger.debug("Run command in {issueId}: {command}".format(issueId=issueId,
command="""Now is OK {}""".format(messages['Text'])
))
connection.executeCommand(issueId=issueId,
command="comment",
comment=settings.YT_COMMENT.format(
status=messages['Status'],
text=messages['Text'])
)
# ----- End OK block -----
if __name__ == "__main__":
logger.debug("Start script with arguments: {}".format(sys.argv[1:]))
try:
Main(
# Arguments WIKI: https://www.zabbix.com/documentation/3.0/ru/manual/config/notifications/media/script
settings.YT_SERVER, # to
sys.argv[2].decode('utf-8'), # subject
sys.argv[3].decode('utf-8'), # body
# FYI: Next argument used in code:
# sys.argv[4], # YT_PASSWORD
# sys.argv[5], # ZBX_PASSWORD
)
except Exception:
logger.exception("Exit with error") # Output exception
exit(1)