Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Quick, little script for periodically checking the relay count in the
- consensus. Queries are done every couple hours and this sends an email notice
- if it changes dramatically throughout the week.
- """
- import time
- import getpass
- import smtplib
- from email.mime.text import MIMEText
- import util.torTools
- USERNAME = "atagar1@gmail.com"
- PASSWORD = ""
- SENDER = "project113@nowhere.com" # evidently ignored
- RECEIVER = "atagar1@gmail.com"
- # size of change (+/-) at which an alert is sent
- BIHOURLY_THRESHOLD = 25
- DAILY_THRESHOLD = 100
- WEEKLY_THRESHOLD = 250
- def sendAlert(msg):
- mimeMsg = MIMEText(msg)
- mimeMsg['Subject'] = "Tor Relay Threshold Alert"
- mimeMsg['From'] = SENDER
- mimeMsg['To'] = RECEIVER
- # Send the message via our own SMTP server, but don't include the
- # envelope header.
- server = smtplib.SMTP('smtp.gmail.com:587')
- server.starttls()
- server.login(USERNAME, PASSWORD)
- server.sendmail(SENDER, [RECEIVER], mimeMsg.as_string())
- server.quit()
- def getCount(conn):
- nsEntries = conn.get_network_status()
- return len(nsEntries)
- if __name__ == '__main__':
- if not PASSWORD:
- PASSWORD = getpass.getpass("GMail Password: ")
- # gets the baseline count
- conn = util.torTools.connect()
- counts = [getCount(conn)] # has entries for the past week
- lastQuery = time.time()
- while True:
- # sleep for a couple hours
- while time.time() < (lastQuery + 7200):
- sleepTime = max(1, 7200 - (time.time() - lastQuery))
- time.sleep(sleepTime)
- # adds new count to the beginning
- newCount = getCount(conn)
- counts.insert(0, newCount)
- if len(counts) > 84: counts.pop()
- # check if we broke any thresholds (alert at the highest increment)
- alertType = None
- if abs(newCount - counts[1]) >= BIHOURLY_THRESHOLD:
- alertType = "hourly"
- dayMin, dayMax = min(counts[:12]), max(counts[:12])
- if (dayMax - dayMin) > DAILY_THRESHOLD:
- alertType = "daily"
- weekMin, weekMax = min(counts), max(counts)
- if (weekMax - weekMin) > WEEKLY_THRESHOLD:
- alertType = "weekly"
- lastQuery = time.time()
- # sends a notice with counts for the last week
- if alertType:
- msg = "%s threshold broken:\n" % alertType
- entryTime = lastQuery
- for countEntry in counts:
- timeLabel = time.strftime("%H:%M %m/%d/%Y", time.localtime(entryTime))
- msg += "%s - %i\n" % (timeLabel, countEntry)
- entryTime -= 7200
- sendAlert(msg)
- # clears entries so we don't repeatidly send daily/weekly alarms
- if alertType == "weekly":
- del counts[12:]
- elif alertType == "daily":
- del counts[2:]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement