-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathZabbixAlertYTWorkflow.py
301 lines (236 loc) · 10.7 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# (c) DevOpsHQ, 2016
# Integration YouTrack and Zabbix alerts.
import yaml
from pyzabbix import ZabbixAPI
import sys
import urllib
import logging
import time
from youtrack.connection import Connection
import re
from requests.packages.urllib3 import disable_warnings
disable_warnings() # disable ssl certificate errors
# ------------ START Constants ------------
YT_PROJECT_NAME = 'CM' # ID project in Youtrack
YT_ASSIGNEE = 'Zabbix' # Assignee to after create issue
YT_TYPE = 'Error' # Youtrack Issue type
YT_SERVICE = 'Zabbix' # Youtrack Issue service
YT_SUBSYSTEM = 'DevOps' # Youtrack Issue subsystem
YT_USER = 'Zabbix' # Youtrack Issue create user
YT_PASSWORD = sys.argv[4] # Youtrack user password
YT_TIME = 'About 1 hour' # Estimated time
# YT_TIME = 'Undefined' # Estimated time
YT_COMMENT = "Now is {status}. \n{text}\n\n" # Add this comment in issue
LOG_FILE_NAME = '/var/log/zabbix/PtZabbixAlertYTWorkflow.log' # Path to Log-file for debug
# LOG_FILE_NAME = 'PtZabbixAlertYTWorkflow.log' # Uncomment for debug in Windows-OS
ZABBIX_SERVER = "https://zabbix.example.com/zabbix"
ZBX_USER = "zabbix_api"
ZBX_PASSWORD = sys.argv[5]
# ------------ END Constants ------------
# ------------ START Setup logging ------------
# Use logger to log information
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# Log to file
fh = logging.FileHandler(LOG_FILE_NAME)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
# Log to stdout
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
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(logging.DEBUG)
# ------------ END Setup logging ------------
# ------------ START ZabbixAPI block ------------
Zbx = ZabbixAPI(ZABBIX_SERVER)
Zbx.session.verify = False
Zbx.login(ZBX_USER, 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 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=YT_PROJECT_NAME,
query=urllib.parse.quote(searchString, safe='')
)
issueDescription = """
{ytName}
-----
{yamlMessage}
-----
- [https://zabbix.example.com/zabbix.php?action=dashboard.view Zabbix Dashboard]
- Show [{linkToHostIssue} all issue for *this host*]
""".format(
ytName=ytName,
yamlMessage=yamlMessage,
linkToHostIssue=linkToHostIssue, )
# ----- END Youtrack Issue description -----
# ----- START Youtrack current week -----
# Create connect to Youtrack API
connection = Connection(sendTo, YT_USER, YT_PASSWORD)
# Get current week in YT format (Sprint planned)
version = connection.getAllBundles('version')
for fixVersion in version[0].values:
if fixVersion['archived'] == False and fixVersion['released'] == False:
fixVersionWeek = fixVersion['name']
break
# ----- END Youtrack current week -----
# ----- 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(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(YT_PROJECT_NAME,
'Unassigned',
ytName,
issueDescription,
priority=ytPriority,
subsystem=YT_SUBSYSTEM,
type=YT_TYPE,
)
time.sleep(3)
# Parse ID for new issue
result = re.search(r'(CM-\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, "Service {}".format(YT_SERVICE))
# Update priority
ExecAndLog(connection, issueId, "Priority {}".format(ytPriority))
# ----- END Youtrack get or create issue -----
# ----- START PROBLEM block ------
if messages['Status'] == "PROBLEM":
# Issue exist and NOT Hold on, Unnassigned and Estimated time set
if issue['State'] != 'Hold on':
# Estimated time
ExecAndLog(connection, issueId, "Estimated time {}".format(YT_TIME))
# Update fix version
ExecAndLog(connection=connection, issueId=issueId, command="Sprint planned {}".format(fixVersionWeek))
# Reopen if Fixed or Verified or Canceled
if issue['State'] == 'Fixed' or issue['State'] == 'Verified' or issue['State'] == 'Canceled':
# Reopen Issue
ExecAndLog(connection, issueId, "State reopen")
# Assignee issue
ExecAndLog(connection, issueId, command="Assignee 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"""
))
connection.updateIssue(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=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'], message="Create Youtrack task")
Zbx.event.acknowledge(eventids=messages['EventID'],
message="https://youtrack.example.com/issue/{}".format(issueId))
# ----- End PROBLEM block ------
# ----- Start OK block -----
if messages['Status'] == "OK":
if issue['State'] == 'Hold on' or issue['State'] == 'Registered':
# Cancel if not in work
ExecAndLog(connection, issueId, command="State Cancel")
# Assignee issue
ExecAndLog(connection, issueId, command="Assignee {}".format(YT_ASSIGNEE))
if issue['State'] == 'Fixed':
# Verify if Fixed
ExecAndLog(connection, issueId, command="State verify")
logger.debug("Run command in {issueId}: {command}".format(issueId=issueId,
command="""Now is OK {}""".format(messages['Text'])
))
connection.executeCommand(issueId=issueId,
command="",
comment=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
sys.argv[1], # to
sys.argv[2], # subject
sys.argv[3], # 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)