Advertisement
Guest User

Untitled

a guest
Nov 20th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from collections import Counter
  4. from sys import version_info
  5. import datetime
  6. import argparse
  7. import getpass
  8. import smtplib
  9. import time
  10. import sys
  11. import os
  12. import re
  13.  
  14. py3 = version_info[0] > 2 # creates boolean value for test that Python major version > 2
  15. username = False
  16. password = False
  17.  
  18.  
  19. def main():
  20. """
  21. Main method that gets called when script is executed
  22.  
  23. :return: void
  24. """
  25.  
  26. parser = argparse.ArgumentParser(description=("Parses provided log file and sends an email if a pattern is noticed "
  27. "to repeat R times in M amount of minutes."))
  28. parser.add_argument("logfile", type=file, help="Log file to parse")
  29. parser.add_argument("-r", "--repeat", type=int, help="Number of times pattern can repeat")
  30. parser.add_argument("-m", "--minutes", type=int, help="Time in minutes that pattern will be monitored")
  31.  
  32. args = parser.parse_args()
  33. logfile = args.logfile
  34. repeat = abs(args.repeat)
  35. minutes = abs(args.minutes)
  36.  
  37. if not repeat:
  38. print "Please provide number of pattern accurrences to monitor for: -r INT"
  39. sys.exit(0)
  40. if not minutes:
  41. print "Please provide number of minutes to monitor a pattern for: -m INT"
  42. sys.exit(0)
  43.  
  44. patterns = []
  45. first_time = 0
  46. for qline in readlines_reverse(logfile):
  47. # lets get time from the qline
  48. match = re.findall(r"\[(.{26}).+(\{.+\})", qline)
  49.  
  50. time_string = match[0][0]
  51. log_time = datetime.datetime.strptime(time_string, "%Y-%m-%dT%H:%M:%S.%f").timetuple()
  52.  
  53. if first_time == 0:
  54. first_time = int(time.mktime(log_time))
  55.  
  56. pattern_string = match[0][1]
  57. patterns.append(pattern_string)
  58.  
  59. # lets check stop condition
  60. if (first_time - int(time.mktime(log_time))) >= minutes*60:
  61. # reset first time
  62. first_time = 0
  63. # count number of times every pattern appears in list
  64. counts = Counter(patterns)
  65. for key, value in counts.iteritems():
  66. if value >= repeat:
  67. # we need to send email now
  68. global username, password
  69.  
  70. while not username:
  71. if py3:
  72. username = input("Please enter your Gmail username: ")
  73. else:
  74. username = raw_input("Please enter your Gmail username: ")
  75.  
  76. while not password:
  77. password = getpass.getpass("Please enter your Gmail password: ")
  78. print "Pattern: " + key + "\n"\
  79. "appeared more than " + str(repeat) + " times in " + str(minutes) + " minutes. " \
  80. "Sending email now."
  81.  
  82. send_mail("realmathtutor@gmail.com", "okryshchenko@gmail.com", "log parser report",
  83. "The pattern has been noticed to repeat " + str(repeat) + " times in " + str(minutes),
  84. "smtp.gmail.com")
  85.  
  86. # reset patterns list
  87. patterns = []
  88.  
  89.  
  90. def readlines_reverse(filename):
  91. """
  92. Reads file from the end to the top
  93.  
  94. :param filename: file to read
  95. :return: list
  96. """
  97.  
  98. with filename as qfile:
  99. qfile.seek(0, os.SEEK_END)
  100. position = qfile.tell()
  101. line = ''
  102. while position >= 0:
  103. qfile.seek(position)
  104. next_char = qfile.read(1)
  105. if next_char == "\n":
  106. yield line[::-1]
  107. line = ''
  108. else:
  109. line += next_char
  110. position -= 1
  111. yield line[::-1]
  112.  
  113.  
  114. def send_mail(from_address, to_address, subject, text, server):
  115. """
  116. This function send emails
  117.  
  118. :param from_address: who send the email
  119. :param to_address: recipient of the email
  120. :param subject: email subject
  121. :param text: email body message
  122. :param server: smtp server
  123. :return: void
  124. """
  125.  
  126. message = """\
  127. From: %s
  128. To: %s
  129. Subject: %s
  130. %s
  131. """ % (from_address, ", ".join(to_address), subject, text)
  132. # Send the mail
  133. server = smtplib.SMTP(server, 587)
  134. server.ehlo()
  135. server.starttls()
  136. server.login(username, password)
  137. server.sendmail(from_address, to_address, message)
  138. server.quit()
  139.  
  140.  
  141. if __name__ == '__main__':
  142. sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement