Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. """
  2. Quick, little script for periodically checking the relay count in the
  3. consensus. Queries are done every couple hours and this sends an email notice
  4. if it changes dramatically throughout the week.
  5. """
  6.  
  7. import time
  8. import getpass
  9. import smtplib
  10. from email.mime.text import MIMEText
  11.  
  12. import util.torTools
  13.  
  14. USERNAME = "atagar1@gmail.com"
  15. PASSWORD = ""
  16. SENDER = "project113@nowhere.com" # evidently ignored
  17. RECEIVER = "atagar1@gmail.com"
  18.  
  19. # size of change (+/-) at which an alert is sent
  20. BIHOURLY_THRESHOLD = 25
  21. DAILY_THRESHOLD = 100
  22. WEEKLY_THRESHOLD = 250
  23.  
  24. def sendAlert(msg):
  25. mimeMsg = MIMEText(msg)
  26. mimeMsg['Subject'] = "Tor Relay Threshold Alert"
  27. mimeMsg['From'] = SENDER
  28. mimeMsg['To'] = RECEIVER
  29.  
  30. # Send the message via our own SMTP server, but don't include the
  31. # envelope header.
  32. server = smtplib.SMTP('smtp.gmail.com:587')
  33. server.starttls()
  34. server.login(USERNAME, PASSWORD)
  35.  
  36. server.sendmail(SENDER, [RECEIVER], mimeMsg.as_string())
  37. server.quit()
  38.  
  39. def getCount(conn):
  40. nsEntries = conn.get_network_status()
  41. return len(nsEntries)
  42.  
  43. if __name__ == '__main__':
  44. if not PASSWORD:
  45. PASSWORD = getpass.getpass("GMail Password: ")
  46.  
  47. # gets the baseline count
  48. conn = util.torTools.connect()
  49. counts = [getCount(conn)] # has entries for the past week
  50. lastQuery = time.time()
  51.  
  52. while True:
  53. # sleep for a couple hours
  54. while time.time() < (lastQuery + 7200):
  55. sleepTime = max(1, 7200 - (time.time() - lastQuery))
  56. time.sleep(sleepTime)
  57.  
  58. # adds new count to the beginning
  59. newCount = getCount(conn)
  60. counts.insert(0, newCount)
  61. if len(counts) > 84: counts.pop()
  62.  
  63. # check if we broke any thresholds (alert at the highest increment)
  64. alertType = None
  65.  
  66. if abs(newCount - counts[1]) >= BIHOURLY_THRESHOLD:
  67. alertType = "hourly"
  68.  
  69. dayMin, dayMax = min(counts[:12]), max(counts[:12])
  70. if (dayMax - dayMin) > DAILY_THRESHOLD:
  71. alertType = "daily"
  72.  
  73. weekMin, weekMax = min(counts), max(counts)
  74. if (weekMax - weekMin) > WEEKLY_THRESHOLD:
  75. alertType = "weekly"
  76.  
  77. lastQuery = time.time()
  78.  
  79. # sends a notice with counts for the last week
  80. if alertType:
  81. msg = "%s threshold broken:\n" % alertType
  82.  
  83. entryTime = lastQuery
  84. for countEntry in counts:
  85. timeLabel = time.strftime("%H:%M %m/%d/%Y", time.localtime(entryTime))
  86. msg += "%s - %i\n" % (timeLabel, countEntry)
  87. entryTime -= 7200
  88.  
  89. sendAlert(msg)
  90.  
  91. # clears entries so we don't repeatidly send daily/weekly alarms
  92. if alertType == "weekly":
  93. del counts[12:]
  94. elif alertType == "daily":
  95. del counts[2:]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement