-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect_hourly.py
More file actions
142 lines (134 loc) · 5.79 KB
/
Copy pathcollect_hourly.py
File metadata and controls
142 lines (134 loc) · 5.79 KB
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
"""Collect data that is relevant to be sampled once an hour."""
from jax_omerometrics import server_up
import timeit
import argparse
import datetime
from pandas import DataFrame
from pathlib import Path
import smtplib
from email.message import EmailMessage
from config import OMERO_USER, OMERO_PASS, SMTP_HOST, SMTP_PORT, ALERTEES
def collect_data(repeats, user, pwd, img_id, web_address, address):
"""Collect the actual data using different API calls."""
web_status = server_up.check_web_response(web_address)
if (not web_status):
web_timing = 8
else:
web_timing = timeit.timeit(
lambda: server_up.check_web_response(web_address),
number=repeats, globals=globals(),
) / repeats
web_timing = round(web_timing, 3)
print("starting check json status")
json_status = server_up.check_web_api(user, pwd, img_id, web_address)
print(f"initial json status: {json_status}")
if (not json_status):
json_timing = 8
else:
json_timing = timeit.timeit(lambda: server_up.check_web_api(
user, pwd, img_id, web_address),
number=repeats, globals=globals()
) / repeats
json_timing = round(json_timing, 3)
ldap_status = server_up.check_ldap_login(user, pwd, address)
if (not ldap_status):
ldap_timing = 8
else:
ldap_timing = timeit.timeit(lambda: server_up.check_ldap_login(
user, pwd, address),
number=repeats, globals=globals()
) / repeats
ldap_timing = round(ldap_timing, 3)
blitz_status = server_up.check_img_return(img_id, user, pwd, address)
if (not blitz_status):
blitz_timing = 8
else:
blitz_timing = timeit.timeit(
lambda: server_up.check_img_return(
img_id, user, pwd, address),
number=repeats, globals=globals()
) / repeats
blitz_timing = round(blitz_timing, 3)
now = datetime.datetime.now()
timestamp = now.strftime("%Y-%m-%d %H:%M")
status_list = [timestamp, web_status,
json_status, ldap_status, blitz_status]
timing_list = [timestamp, web_timing,
json_timing, ldap_timing, blitz_timing]
color_status = [web_status,
json_status, ldap_status, blitz_status]
if all(color_status):
status_list.append("green")
elif any(color_status):
status_list.append("orange")
else:
status_list.append("red")
status_headers = ['timestamp', 'webpage', 'json_api',
'ldap', 'blitz_api', 'color']
timing_headers = ['timestamp', 'webpage', 'json_api', 'ldap', 'blitz_api']
status = DataFrame([status_list], columns=status_headers)
timing = DataFrame([timing_list], columns=timing_headers)
return status, timing
def write_csvs(status, timing, folder):
"""Write the collected data into a CSV file for display."""
if (Path(folder) / 'status.csv').exists():
status.to_csv(Path(folder) / 'status.csv',
mode='a', index=False, header=False)
else:
status.to_csv(Path(folder) / 'status.csv', mode='a', index=False)
if (Path(folder) / 'timings.csv').exists():
timing.to_csv(Path(folder) / 'timings.csv',
mode='a', index=False, header=False)
else:
timing.to_csv(Path(folder) / 'timings.csv', mode='a', index=False)
def send_email(content):
s = smtplib.SMTP(host=SMTP_HOST, port=SMTP_PORT)
for alertee in ALERTEES:
msg = EmailMessage()
msg.set_content(content) # "OMERO Blitz connection time exceeded alert limit"
msg['Subject'] = "Alert from omerodashboard.jax.org"
msg['From'] = ALERTEES[0]
msg['To'] = alertee
s.send_message(msg)
s.quit()
def send_alerts(status, timing):
if not status["blitz_api"][0]:
print("Sending emails for Blitz API alert unresponsive")
send_email("Blitz api unresponsive")
elif timing["blitz_api"][0] > 4:
print("Sending emails for Blitz API alert slow")
blitz_timing = timing["blitz_api"][0]
send_email(f"Blitz API slow response time at {blitz_timing} seconds.")
if not status["json_api"][0]:
print("Sending emails for JSON API alert unresponsive")
send_email("JSON api unresponsive")
elif timing["json_api"][0] > 6:
print("Sending emails for JSON API alert slow")
json_timing = timing["json_api"][0]
send_email(f"JSON API slow response time at {json_timing} seconds.")
if __name__ == "__main__":
description = 'Run this to collect data hourly.'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('folder',
type=str,
help='Full path to folder where\
data will be saved')
parser.add_argument('--img_id',
type=int,
default=87066,
help='Image ID to use for testing')
parser.add_argument('--web_addr',
type=str,
default="https://omeroweb.jax.org",
help='Address for OMERO web instance')
parser.add_argument('--addr',
type=str,
default="ctomero01lp.jax.org",
help='Address for OMERO server instance')
args = parser.parse_args()
repeats = 5
print(args.img_id)
status, timing = collect_data(repeats, OMERO_USER, OMERO_PASS, args.img_id,
args.web_addr, args.addr)
write_csvs(status, timing, args.folder)
send_alerts(status, timing)