-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrelay_check.py
executable file
·65 lines (47 loc) · 1.72 KB
/
relay_check.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
#!/usr/bin/env python
# Copyright 2018-2020, Damian Johnson and The Tor Project
# See LICENSE for licensing information
"""
Health checks for your relay. This provides a simple email notification when
your relay becomes unavailable.
"""
import smtplib
import traceback
import stem
import stem.descriptor.remote
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
RELAY_ADDRESS = '208.113.135.162'
RELAY_OR_PORT = 1443
RELAY_NAME = 'caersidi'
RELAY_FINGERPRINT = '3BB34C63072D9D10E836EE42968713F7B9325F66'
EMAIL_ADDRESS = '[email protected]'
RELAY_LINK = 'https://metrics.torproject.org/rs.html#details/%s' % RELAY_FINGERPRINT
def main():
try:
desc = stem.descriptor.remote.their_server_descriptor(
endpoints = [stem.ORPort(RELAY_ADDRESS, RELAY_OR_PORT)],
).run()[0]
if desc.nickname != RELAY_NAME:
raise ValueError('Unexpected descriptor:\n\n%s' % desc)
except stem.SocketError:
email('Unable to reach %s' % RELAY_NAME, "Unable to reach %s (%s):\n\n%s" % (RELAY_NAME, RELAY_LINK, traceback.format_exc()))
def email(subject, body):
"""
Sends an email notification via the local mail application.
:param str subject: email subject
:param str body: email content
:raises: **Exception** if the email fails to be sent
"""
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['To'] = EMAIL_ADDRESS
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('localhost')
server.sendmail('[email protected]', [EMAIL_ADDRESS], msg.as_string())
server.quit()
if __name__ == '__main__':
try:
main()
except:
email('Health check error', "Unable to check the health of %s (%s):\n\n%s" % (RELAY_NAME, RELAY_LINK, traceback.format_exc()))