Wednesday, June 25, 2014

Squid log parsing script in Ubuntu 12.04

Squid log parsing script in Python

Step1: copy the following script and paste into any editor (gedit, vim, vi or nano)

#!/usr/bin/env python

import sys, os
from datetime import datetime

try:
    if not os.path.isfile(sys.argv[1]):
        sys.stderr.write("[Error]: %s is not a file\n" % sys.argv[1])
        sys.exit(1)
except IndexError:
    sys.stderr.write("Usage: parse_squid_log.py access.log\n")
    sys.exit(1)

with open(sys.argv[1]) as log:
    for line in log:
        line = line.split(' ')
        line[0] = str(datetime.fromtimestamp(float(line[0])))
        print ' '.join(line).strip()

Step2: Change the permission as like this (sudo chmod +x scriptname.py)
Step3: ./scriptname.py /var/log/squid3/access.log

The output will be in a readable format.

0 Comments: