Skip to content

Instantly share code, notes, and snippets.

@100daysofdevops
Created April 30, 2019 16:59
Show Gist options
  • Select an option

  • Save 100daysofdevops/d575b4fab9ad55fa1f2541e796722491 to your computer and use it in GitHub Desktop.

Select an option

Save 100daysofdevops/d575b4fab9ad55fa1f2541e796722491 to your computer and use it in GitHub Desktop.
#!/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