-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten4gws.py
169 lines (130 loc) · 4.85 KB
/
listen4gws.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
import gcn
import numpy as np
#import healpy as hp
from helper_funcs import send_error_email, send_email
import traceback
import socket
import os
import logging
import sys
import subprocess
# sys.path.append('/var/lib/mysql_drbd/amon/monitor_scripts/check_events/')
# from functions import slack_message
# run as nohup python listen4gws.py > /storage/work/jjd330/local/bat_data/listen4gws_out.log 2>&1 &
workdir='/storage/work/j/jjd330/local/bat_data/realtime_workdir/'
script_path='/storage/work/j/jjd330/local/bat_data/BatML/run_stuff.sh'
# Function to call every time a GCN is received.
# Run only for notices of type
# LVC_PRELIMINARY, LVC_INITIAL, or LVC_UPDATE.
@gcn.handlers.include_notice_types(
gcn.notice_types.LVC_PRELIMINARY,
gcn.notice_types.LVC_INITIAL,
gcn.notice_types.LVC_UPDATE,
gcn.notice_types.LVC_RETRACTION)
def process_gcn(payload, root):
print root.attrib['role']
role = root.attrib['role']
try:
eventtime = root.find('.//ISOTime').text
print eventtime
except Exception as E:
body = str(E)
body += '\n' + traceback.format_exc()
subject = 'listen4gws.py error on ' + socket.gethostname()
send_error_email(subject, body)
# Read all of the VOEvent parameters from the "What" section.
params = {elem.attrib['name']:
elem.attrib['value']
for elem in root.iterfind('.//Param')}
rev = params['Pkt_Ser_Num']
new_alert = False
try:
direc = os.path.join(workdir, params['GraceID'])
if not os.path.exists(direc):
os.mkdir(direc)
new_alert = True
fname = os.path.join(direc, params['GraceID'] + '_' + rev + '.xml')
with open(fname, 'wb') as f:
f.write(payload)
except Exception as E:
body = str(E)
body += '\n' + traceback.format_exc()
subject = 'listen4gws.py error on ' + socket.gethostname()
send_error_email(subject, body)
try:
if role == 'test':
eventtime = "2019-06-16T0"
eventtime += str(np.random.randint(0,high=9))
eventtime += ":33:27.059"
subj = "Test GW event: Alert Type " + params['AlertType']
body = "Test GW event\n"
channel = 'test-alerts'
mail_fname = 'mailing_list.txt'
else:
subj = "GW event: Maybe Real Alert Type " + params['AlertType']
body = "GW event\n"
channel = 'alerts'
mail_fname = 'mailing_list.txt'
body += "Starting analysis\n"
body += "role: " + role + '\n'
body += "IVORN: " + str(root.attrib['ivorn']) + '\n'
body += "IsoTime: " + eventtime + '\n'
for key, value in params.items():
body += key
body += '='
body += value
body += '\n'
GraceID = str(params['GraceID'])
to = []
f = open(mail_fname, 'r')
for line in f:
to.append(line.split('\n')[0])
f.close()
print to
send_email(subj, body, to)
# slack_message(':gw: ' +body, channel ,attachment=None)
except Exception as E:
body = str(E)
body += '\n' + traceback.format_exc()
print body
subject = 'listen4gws.py error on ' + socket.gethostname()
send_error_email(subject, body)
try:
script_out_path= os.path.join(direc,'run_stuff_out.log')
if new_alert and not (role == "test"):
with open(script_out_path, 'w') as f:
process_args = [script_path, eventtime, params['GraceID']]
subprocess.Popen(process_args, stdout=f, stderr=f)
except Exception as E:
body = str(E)
body += '\n' + traceback.format_exc()
try:
graceID = params['GraceID']
body += '\nGraceID: ' + graceID
except:
logger.warn("error and doesn't know GraceID")
logger.error(body)
subject = 'listen4gws.py error on ' + socket.gethostname()
send_error_email(subject, body)
if __name__ == "__main__":
logger = logging.getLogger('gw_gcn_logger')
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
log_fname = os.path.join(workdir, 'gw_gcn.log')
pid_fname = os.path.join(workdir, 'gw_gcn.pid')
with open(pid_fname, 'wb') as f:
f.write(str(os.getpid()))
fh = logging.FileHandler(filename=log_fname)
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
logger.addHandler(fh)
sh = logging.StreamHandler(sys.stdout)
sh.setLevel(logging.INFO)
sh.setFormatter(formatter)
logger.addHandler(sh)
she = logging.StreamHandler(sys.stderr)
she.setLevel(logging.ERROR)
she.setFormatter(formatter)
logger.addHandler(she)
print 'PID: ', os.getpid()
gcn.listen(handler=process_gcn, log=logger)