Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- from collections import Counter
- from sys import version_info
- import datetime
- import argparse
- import getpass
- import smtplib
- import time
- import sys
- import os
- import re
- py3 = version_info[0] > 2 # creates boolean value for test that Python major version > 2
- username = False
- password = False
- def main():
- """
- Main method that gets called when script is executed
- :return: void
- """
- parser = argparse.ArgumentParser(description=("Parses provided log file and sends an email if a pattern is noticed "
- "to repeat R times in M amount of minutes."))
- parser.add_argument("logfile", type=file, help="Log file to parse")
- parser.add_argument("-r", "--repeat", type=int, help="Number of times pattern can repeat")
- parser.add_argument("-m", "--minutes", type=int, help="Time in minutes that pattern will be monitored")
- args = parser.parse_args()
- logfile = args.logfile
- repeat = abs(args.repeat)
- minutes = abs(args.minutes)
- if not repeat:
- print "Please provide number of pattern accurrences to monitor for: -r INT"
- sys.exit(0)
- if not minutes:
- print "Please provide number of minutes to monitor a pattern for: -m INT"
- sys.exit(0)
- patterns = []
- first_time = 0
- for qline in readlines_reverse(logfile):
- # lets get time from the qline
- match = re.findall(r"\[(.{26}).+(\{.+\})", qline)
- time_string = match[0][0]
- log_time = datetime.datetime.strptime(time_string, "%Y-%m-%dT%H:%M:%S.%f").timetuple()
- if first_time == 0:
- first_time = int(time.mktime(log_time))
- pattern_string = match[0][1]
- patterns.append(pattern_string)
- # lets check stop condition
- if (first_time - int(time.mktime(log_time))) >= minutes*60:
- # reset first time
- first_time = 0
- # count number of times every pattern appears in list
- counts = Counter(patterns)
- for key, value in counts.iteritems():
- if value >= repeat:
- # we need to send email now
- global username, password
- while not username:
- if py3:
- username = input("Please enter your Gmail username: ")
- else:
- username = raw_input("Please enter your Gmail username: ")
- while not password:
- password = getpass.getpass("Please enter your Gmail password: ")
- print "Pattern: " + key + "\n"\
- "appeared more than " + str(repeat) + " times in " + str(minutes) + " minutes. " \
- "Sending email now."
- send_mail("realmathtutor@gmail.com", "okryshchenko@gmail.com", "log parser report",
- "The pattern has been noticed to repeat " + str(repeat) + " times in " + str(minutes),
- "smtp.gmail.com")
- # reset patterns list
- patterns = []
- def readlines_reverse(filename):
- """
- Reads file from the end to the top
- :param filename: file to read
- :return: list
- """
- with filename as qfile:
- qfile.seek(0, os.SEEK_END)
- position = qfile.tell()
- line = ''
- while position >= 0:
- qfile.seek(position)
- next_char = qfile.read(1)
- if next_char == "\n":
- yield line[::-1]
- line = ''
- else:
- line += next_char
- position -= 1
- yield line[::-1]
- def send_mail(from_address, to_address, subject, text, server):
- """
- This function send emails
- :param from_address: who send the email
- :param to_address: recipient of the email
- :param subject: email subject
- :param text: email body message
- :param server: smtp server
- :return: void
- """
- message = """\
- From: %s
- To: %s
- Subject: %s
- %s
- """ % (from_address, ", ".join(to_address), subject, text)
- # Send the mail
- server = smtplib.SMTP(server, 587)
- server.ehlo()
- server.starttls()
- server.login(username, password)
- server.sendmail(from_address, to_address, message)
- server.quit()
- if __name__ == '__main__':
- sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement