Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This script takes in an apache log file, parses the information and then posts it to a mysql database
- #
- # there was nothing in the instructions that specified that we aren't allowed to use libraries
- # so I chose to use the apache-log-parser library to make things easier on the parsing side,
- # and I also chose to use PeeWee as a simple ORM to update the database.
- # Harjot Kainth
- # 11.19.2017
- import apache_log_parser
- from peewee import *
- db = MySQLDatabase(
- 'apache_logs',
- user='root',
- password='',
- host='localhost')
- class BaseModel(Model):
- class Meta:
- database = db
- class Log(BaseModel):
- remote_host = CharField()
- request_method = CharField()
- request_url = CharField()
- time_received = DateTimeField()
- # def create_tables():
- # db.connect()
- # db.create_table(Log)
- #
- # create_tables()
- # path to file
- log_file_path = "access.log"
- # open the file
- log_file = open(log_file_path, 'r')
- # read in all lines
- logs = log_file.readlines()
- # create log parser and specify format
- log_line_parser = apache_log_parser.make_parser("%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"")
- # loop through each line in our logs
- for log in logs:
- # parse the line data
- log_line_data = log_line_parser(log)
- # get required information for database from the line data and enter it into the database Model
- db.connect()
- log_data_object = Log(remote_host=log_line_data["remote_host"],
- request_method=log_line_data["request_method"],
- request_url=log_line_data["request_url"],
- time_received=log_line_data["time_received_datetimeobj"])
- # post information to database
- log_data_object.save()
- db.close()
Add Comment
Please, Sign In to add comment