-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcreate_bulk_ticket_with_template-by-domain.py
executable file
·148 lines (125 loc) · 3.78 KB
/
create_bulk_ticket_with_template-by-domain.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
#!/usr/bin/python
import sys
from string import Template
import os
from pyurlabuse import PyURLAbuse
import rt
import logging
import sphinxapi
import time
import csv
if len(sys.argv) < 4:
print("Usage: %s Incident-ID Templatename csv-file" % sys.argv[0])
sys.exit(1)
incident = sys.argv[1]
template = sys.argv[2]
csvfile = sys.argv[3]
mypath = os.path.dirname(os.path.realpath(sys.argv[0]))
template = os.path.join(mypath, template)
# Config
import config as cfg
rt_url = cfg.rt_url
rt_user = cfg.rt_user
rt_pass = cfg.rt_pass
sphinx_server = cfg.sphinx_server
sphinx_port = cfg.sphinx_port
excludelist = cfg.known_good_excludelist
debug = False
# RT
logger = logging.getLogger('rt')
tracker = rt.Rt(rt_url, rt_user, rt_pass, verify_cert=False)
tracker.login()
# Sphinx
client = sphinxapi.SphinxClient()
client.SetServer(sphinx_server, sphinx_port)
client.SetMatchMode(2)
def is_ticket_open(id):
status = False
try:
rt_response = tracker.get_ticket(id)
ticket_status = rt_response['Status']
if ticket_status == "open" or ticket_status == "new":
status = id
except Exception:
return False
return status
def open_tickets_for_url(url):
q = "\"%s\"" % url
res = 0
result = client.Query(q)
for match in result['matches']:
res = is_ticket_open(match['id'])
return res
# inputfile = 'ava.txt'
inputfile = csvfile
f = open(inputfile, 'r')
headerline = f.readline().strip()
f.close()
if 'Format' in headerline:
asns = set()
with open(inputfile, 'rt') as f:
reader = csv.reader(f)
my_list = list(reader)
for item in my_list:
print(item)
if (item and 'Format' not in item[0]) and item[1] not in excludelist:
asns.add(item[0])
for asn in asns:
sendto = []
print(asn)
# asn_string = "AS" + asn.strip()
asn_string = asn.strip()
my_pyurlabuse = PyURLAbuse()
response = my_pyurlabuse.run_query(asn, with_digest=True)
time.sleep(5)
response = my_pyurlabuse.run_query(asn, with_digest=True)
for email in response['digest'][1]:
print("".join(email))
sendto.append("".join(email))
asns = response['digest'][2]
#
print("Sendto: " + ", ".join(sendto))
#
print(headerline)
detail_text = headerline
for item in my_list:
if item and 'Format' not in item[0]:
if item[0] == asn:
detail_text += "\n " + ','.join(item)
#
print(','.join(item))
text = detail_text
d = {'details': text}
try:
f = open(template)
subject = f.readline().rstrip()
templatecontent = Template(f.read())
body = templatecontent.substitute(d)
except Exception:
print("Couldn't open template file (%s)" % template)
sys.exit(1)
f.close()
if debug:
sendto = ["[email protected]"]
requestor = ", ".join(sendto)
subject = "%s (%s)" % (subject, asn)
if debug:
print(subject)
# sys.exit(42)
try:
ticketid = tracker.create_ticket(Queue=5, Subject=subject, Text=body, Requestors=requestor)
print("Ticket created: %s" % ticketid)
success = tracker.reply(ticketid, text=body)
except rt.RtError as e:
logger.error(e)
# update ticket link
try:
rt_response = tracker.edit_ticket_links(ticketid, MemberOf=incident)
logger.info(rt_response)
except rt.RtError as e:
logger.error(e)
if debug:
# One testmail is enough
sys.exit(42)
else:
print("Header doesn't contain 'Format:' string")