-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail-test.py
More file actions
executable file
·219 lines (188 loc) · 7.27 KB
/
Copy pathmail-test.py
File metadata and controls
executable file
·219 lines (188 loc) · 7.27 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
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
#!/usr/bin/env python3
from argparse import Namespace
from multiprocessing import Process, Manager
import time, datetime
import configparser
import argparse
import os
import signal
from mail.send import send_from_smtp
from mail.recv import recv_from_imap
from prometheus_client import start_http_server, Gauge, Info
# Config that store account & password
CONFIG_FILE = '/root/mail-test/mail-test.cfg'
DESCRIPTION = 'External mailing test'
EPILOG = 'Calculate delay of four different direction'
DAEMON_PID_FILE = '/root/mail-test/.pid'
# Object for sending and receiving test
class Task:
# arg: [ send_addr, send_pass, recv_addr, recv_pass, smtp_server, imap_server ]
def __init__(self, name, info, timestamp=''):
self.name = name
self.sendargs = {
'send_addr': info[0],
'send_pass': info[1],
'recv_addr': info[2],
'smtp_server': info[4],
'subject': f'MAILTEST. {name} {timestamp}',
}
self.recvargs = {
'recv_addr': info[2],
'recv_pass': info[3],
'send_addr': info[0],
'imap_server': info[5],
'subject': f'MAILTEST. {name} {timestamp}',
}
'''#
Outer: Gmail
Inner: Csie mail server & G-suite
'''
config = Namespace(
outer_addr = '',
outer_pass = '',
inner_addr = '',
inner_pass = '',
gmail_smtp = ('smtp.gmail.com', 587),
csie_smtp = ('smtp.csie.ntu.edu.tw', 587),
gmail_imap = 'imap.gmail.com',
csie_imap = 'imap.csie.ntu.edu.tw',
)
# Grab information from config file
def setup_config():
fconfig = configparser.ConfigParser()
fconfig.read(CONFIG_FILE)
config.outer_addr = fconfig['MAILTEST']['outer_addr']
config.outer_pass = fconfig['MAILTEST']['outer_pass']
config.inner_addr = fconfig['MAILTEST']['inner_addr']
config.inner_pass = fconfig['MAILTEST']['inner_pass']
def gettaskscfg():
# Tasks config
taskscfg = {
'Gmail -> G-Suite': [
config.outer_addr, # sender address
config.outer_pass, # sender password
config.inner_addr, # receiver address
config.inner_pass, # receiver password
config.gmail_smtp, # smtp of sender
config.gmail_imap, # imap of receiver
],
'Gmail -> SMTP': [
config.outer_addr,
config.outer_pass,
config.inner_addr,
config.inner_pass,
config.gmail_smtp,
config.csie_imap,
],
'G-Suite -> Gmail': [
config.inner_addr,
config.inner_pass,
config.outer_addr,
config.outer_pass,
config.gmail_smtp,
config.gmail_imap,
],
'SMTP -> Gmail': [
config.inner_addr,
config.inner_pass,
config.outer_addr,
config.outer_pass,
config.csie_smtp,
config.gmail_imap,
]
}
return taskscfg
# timeout: second(s)
def sequential_test(taskscfg, timeout=200, noreply=300):
timestamp = str(int(time.time()))
latency = dict.fromkeys(taskscfg.keys(), noreply)
tasks = [ Task(task_name, arg, timestamp=timestamp) for task_name, arg in taskscfg.items() ]
for task in tasks:
send_from_smtp(**task.sendargs, content=timestamp)
start_time = time.time()
while time.time() - start_time <= timeout and latency[task.name] == noreply:
latency[task.name] = time.time() - start_time if timestamp == recv_from_imap(**task.recvargs)[0] else noreply
time.sleep((time.time() - start_time) / 10) # Sleep longer when waiting longer
return latency
# Fake parallel
# timeout: second(s)
def parallel_test(taskscfg, timeout=200, noreply=300):
timestamp = str(int(time.time()))
latency = dict.fromkeys(taskscfg.keys(), noreply)
tasks = [ Task(task_name, arg, timestamp=timestamp) for task_name, arg in taskscfg.items() ]
for task in tasks:
send_from_smtp(**task.sendargs, content=timestamp)
start_time = time.time()
for task in tasks:
while time.time() - start_time <= timeout and latency[task.name] == noreply:
latency[task.name] = time.time() - start_time if timestamp == recv_from_imap(**task.recvargs)[0] else noreply
time.sleep((time.time() - start_time) / 10) # Sleep longer when waiting longer
return latency
# Signal handler
def sighandler(signum, frame):
return
# Mailing test (hourly)
def hourly_test(taskscfg, latency):
# Set signal handler and write pid
signal.signal(signal.SIGUSR1, sighandler)
with open(DAEMON_PID_FILE, 'w') as f:
f.write(str(os.getpid()))
# First Test
latency.update(sequential_test(taskscfg, noreply=-20))
while True:
# Sleep until the time on the hour (Remove for debugging)
# now = datetime.datetime.now()
# next = now.replace(minute=0, second=0, microsecond=0) + datetime.timedelta(hours=1)
# time.sleep((next - now).total_seconds())
signal.pause()
# Mailing Test
latency.update(sequential_test(taskscfg, noreply=-20))
def setup_metrics(sample_information, key_to_prom_metrics):
metrics_dict = {}
for key in sample_information:
metrics = key_to_prom_metrics[key]
metrics_dict[metrics] = Gauge(metrics, metrics)
return metrics_dict
def generate_metrics(latency, key_to_prom_metrics, metrics_dict):
for key, value in latency.items():
if value:
metrics_dict[key_to_prom_metrics[key]].set(value)
# Prometheus client(?)
def prometheus(latency):
start_http_server(9091)
sample_information = {'Gmail -> G-Suite': None, 'Gmail -> SMTP': None, 'G-Suite -> Gmail': None, 'SMTP -> Gmail': None}
key_to_prom_metrics = {'Gmail -> G-Suite': "Gmail_to_GSuite", 'Gmail -> SMTP': "Gmail_to_SMTP", 'G-Suite -> Gmail': "GSuite_to_Gmail", 'SMTP -> Gmail': "SMTP_to_Gmail"}
metrics_dict = setup_metrics(sample_information, key_to_prom_metrics)
while True:
generate_metrics(latency, key_to_prom_metrics, metrics_dict)
# Main function
if __name__ == '__main__':
# Parse arguments
argparser = argparse.ArgumentParser(description=DESCRIPTION, epilog=EPILOG)
argparser.add_argument('--daemon', action='store_true', help=f'run as daemon')
argparser.add_argument('-f', metavar='FILE', action='store', default=CONFIG_FILE, type=str, help=f'specify path of config file (default: {CONFIG_FILE})')
argparser.add_argument('-s', action='store_true', help=f'signal the daemon by pid of daemon')
args = argparser.parse_args()
CONFIG_FILE = args.f
# Get test account config data
setup_config()
if args.daemon:
# Get tasks configuration
taskscfg = gettaskscfg()
manager = Manager()
# Latency for all tasks (None: not received)
latency = manager.dict(dict.fromkeys(taskscfg.keys()))
# Run mailing test and promethus
p1 = Process(target=hourly_test, args=(taskscfg, latency))
p2 = Process(target=prometheus, args=(latency,))
p1.start()
p2.start()
p1.join()
p2.join()
elif args.s:
with open(DAEMON_PID_FILE, 'r') as f:
daemon_pid = int(f.read())
print(f'Send signal to {daemon_pid}')
os.kill(daemon_pid, signal.SIGUSR1)
else:
argparser.print_help()