Created
April 30, 2019 16:59
-
-
Save 100daysofdevops/d575b4fab9ad55fa1f2541e796722491 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| """ | |
| USAGE: | |
| logparsing_apache.py apache_log_file | |
| This script takes apache log file as an argument and then generates a report, with hostname, | |
| bytes transferred and status | |
| """ | |
| import sys | |
| def apache_output(line): | |
| split_line = line.split() | |
| return {'remote_host': split_line[0], | |
| 'apache_status': split_line[8], | |
| 'data_transfer': split_line[9], | |
| } | |
| def final_report(logfile): | |
| for line in logfile: | |
| line_dict = apache_output(line) | |
| print(line_dict) | |
| if __name__ == "__main__": | |
| if not len(sys.argv) > 1: | |
| print (__doc__) | |
| sys.exit(1) | |
| infile_name = sys.argv[1] | |
| try: | |
| infile = open(infile_name, 'r') | |
| except IOError: | |
| print ("You must specify a valid file to parse") | |
| print (__doc__) | |
| sys.exit(1) | |
| log_report = final_report(infile) | |
| print (log_report) | |
| infile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment