This repository has been archived by the owner on Aug 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathjirabix.py
executable file
·230 lines (191 loc) · 8.05 KB
/
jirabix.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
#!/usr/bin/env python
# coding: utf-8
from jira import JIRA
import config
import sys
import requests
import re
import os
import sqlite3
# PARAMS RECEIVED FROM ZABBIX SERVER:
# sys.argv[1] = TO
# sys.argv[2] = SUBJECT
# sys.argv[3] = BODY
def jira_login():
jira_server = {'server': config.jira_server}
return JIRA(options=jira_server, basic_auth=(config.jira_user, config.jira_pass))
def create_issue(to, tittle, body, project, issuetype, priority):
jira = jira_login()
issue_params = {
'project': {'key': project},
'summary': tittle,
'description': body,
'issuetype': {'name': issuetype},
'assignee': {'name': to},
'priority': {'id': priority}
}
return jira.create_issue(fields=issue_params).key
def add_attachment(issue, attachment):
jira = jira_login()
jira.add_attachment(issue, attachment)
def close_issue(issue, status):
jira = jira_login()
jira.transition_issue(issue, status)
def add_comment(issue, comment):
jira = jira_login()
jira.add_comment(issue, comment)
def get_transition(issue_key):
jira_server = {'server': config.jira_server}
jira = JIRA(options=jira_server, basic_auth=(config.jira_user, config.jira_pass))
issue = jira.issue(issue_key)
transitions = jira.transitions(issue)
for t in transitions:
if t['name'] == config.jira_transition:
return t['id']
class ZabbixAPI:
def __init__(self, server, username, password):
self.debug = False
self.server = server
self.username = username
self.password = password
self.proxies = {}
self.verify = True
self.cookie = None
def login(self):
data_api = {"name": self.username, "password": self.password, "enter": "Sign in"}
req_cookie = requests.post(self.server + "/", data=data_api, proxies=self.proxies, verify=self.verify)
cookie = req_cookie.cookies
if len(req_cookie.history) > 1 and req_cookie.history[0].status_code == 302:
print_message("probably the server in your config file has not full URL (for example "
"'{0}' instead of '{1}')".format(self.server, self.server + "/zabbix"))
if not cookie:
print_message("authorization has failed, url: {0}".format(self.server + "/"))
cookie = None
self.cookie = cookie
def graph_get(self, itemid, period, title, width, height, tmp_dir):
file_img = tmp_dir + "/{0}.png".format(itemid)
title = requests.utils.quote(title)
zbx_img_url = self.server + "/chart3.php?period={1}&name={2}" \
"&width={3}&height={4}&graphtype=0&legend=1" \
"&items[0][itemid]={0}&items[0][sortorder]=0" \
"&items[0][drawtype]=5&items[0][color]=00CC00".format(itemid, period, title,
width, height)
if self.debug:
print_message(zbx_img_url)
res = requests.get(zbx_img_url, cookies=self.cookie, proxies=self.proxies, verify=self.verify, stream=True)
res_code = res.status_code
if res_code == 404:
print_message("can't get image from '{0}'".format(zbx_img_url))
return False
res_img = res.content
with open(file_img, 'wb') as fp:
fp.write(res_img)
return file_img
def print_message(string):
string = str(string) + "\n"
filename = sys.argv[0].split("/")[-1]
sys.stderr.write(filename + ": " + string)
def main():
if not os.path.exists(config.zbx_tmp_dir):
os.makedirs(config.zbx_tmp_dir)
tmp_dir = config.zbx_tmp_dir
# zbx_body = open('entry.txt', 'r').read()
zbx_body = sys.argv[3]
zbx = ZabbixAPI(server=config.zbx_server, username=config.zbx_api_user,
password=config.zbx_api_pass)
if config.proxy_to_zbx:
zbx.proxies = {
"http": "http://{0}/".format(config.proxy_to_zbx),
"https": "https://{0}/".format(config.proxy_to_zbx)
}
try:
zbx_api_verify = config.zbx_api_verify
zbx.verify = zbx_api_verify
except:
pass
zbx_body = zbx_body.splitlines()
zbx_body_text = []
settings = {
"zbx_itemid": "0", # itemid for graph
"zbx_triggerid": "0", # uniqe trigger id of event
"zbx_ok": "0", # flag of resolve problem, 0 - no, 1 - yes
"zbx_priority": None, # zabbix trigger priority
"zbx_title": None, # title for graph
"zbx_image_period": "3600",
"zbx_image_width": "900",
"zbx_image_height": "200",
}
settings_description = {
"itemid": {"name": "zbx_itemid", "type": "int"},
"triggerid": {"name": "zbx_triggerid", "type": "int"},
"ok": {"name": "zbx_ok", "type": "int"},
"priority": {"name": "zbx_priority", "type": "str"},
"title": {"name": "zbx_title", "type": "str"},
"graphs_period": {"name": "zbx_image_period", "type": "int"},
"graphs_width": {"name": "zbx_image_width", "type": "int"},
"graphs_height": {"name": "zbx_image_height", "type": "int"},
"graphs": {"name": "tg_method_image", "type": "bool"},
}
trigger_desc = {
"not_classified": {"name": "Not classified", "id": "5"},
"information": {"name": "Information", "id": "5"},
"warning": {"name": "Warning", "id": "4"},
"average": {"name": "Average", "id": "3"},
"high": {"name": "High", "id": "2"},
"disaster": {"name": "Disaster", "id": "1"},
}
for line in zbx_body:
if line.find(config.zbx_prefix) > -1:
setting = re.split("[\s\:\=]+", line, maxsplit=1)
key = setting[0].replace(config.zbx_prefix + ";", "")
if len(setting) > 1 and len(setting[1]) > 0:
value = setting[1]
else:
value = True
if key in settings_description:
settings[settings_description[key]["name"]] = value
else:
zbx_body_text.append(line)
trigger_ok = int(settings['zbx_ok'])
trigger_id = int(settings['zbx_triggerid'])
# print(os.path.join(os.path.dirname(__file__), 'test.db'))
conn = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'jirabix.db'))
c = conn.cursor()
c.execute('''CREATE TABLE if not exists events
(trigger_id integer, issue_key text)''')
conn.commit()
c.execute('SELECT issue_key FROM events WHERE trigger_id=?', (trigger_id,))
result = c.fetchall()
# print(result)
priority = "5"
if not result and trigger_ok == 0:
for i in trigger_desc.values():
if i['name'] == settings['zbx_priority']:
priority = i.get('id')
issue_key = create_issue(sys.argv[1], sys.argv[2], '\n'.join(zbx_body_text), config.jira_project,
config.jira_issue_type, priority)
zbx.login()
if not zbx.cookie:
print_message("Login to Zabbix web UI has failed, check manually...")
else:
zbx_file_img = zbx.graph_get(settings["zbx_itemid"], settings["zbx_image_period"],
settings["zbx_title"], settings["zbx_image_width"],
settings["zbx_image_height"], tmp_dir)
if not zbx_file_img:
print_message("Can't get image, check URL manually")
elif isinstance(zbx_file_img, str):
add_attachment(issue_key, zbx_file_img)
os.remove(zbx_file_img)
c.execute("INSERT INTO events VALUES (?, ?);", (trigger_id, issue_key))
conn.commit()
elif not result and trigger_ok == 1:
pass
else:
issue_key = result[0][0]
add_comment(issue_key, '\n'.join(zbx_body_text))
close_issue(issue_key, get_transition(issue_key))
c.execute('DELETE FROM events WHERE trigger_id=?', (trigger_id,))
conn.commit()
conn.close()
if __name__ == '__main__':
main()