Skip to content

Instantly share code, notes, and snippets.

@pacoqueen
Created January 27, 2016 11:56
Show Gist options
  • Save pacoqueen/1d823dad70a3b4bd2e34 to your computer and use it in GitHub Desktop.
Save pacoqueen/1d823dad70a3b4bd2e34 to your computer and use it in GitHub Desktop.
Script for Squid log "live" parsing.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Hecho para funcionar con tail -f /var/log/squid/access.log | ./la_fisgona.py
"""
import sys, os, datetime
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def disable(self):
self.HEADER = ''
self.OKBLUE = ''
self.OKGREEN = ''
self.WARNING = ''
self.FAIL = ''
self.ENDC = ''
ips = open("/home/bogado/ips.txt")
dips = {}
forevah = True
for l in ips.readlines():
try:
ip = l.split()[0].split("\t")[0].strip()
except IndexError:
continue
usuario = " ".join([i.strip() for i in l.split()][1:])
try:
dips[ip].append(usuario)
except:
dips[ip] = [usuario]
coloritos = bcolors()
while forevah:
l = sys.stdin.readline()
if not l:
break
for ip in dips:
if ip+" " in l:
fechahora = l.split()[0]
try:
l = (coloritos.OKGREEN
+ datetime.datetime.fromtimestamp(float(fechahora)).strftime("%d/%m/%Y %H:%M:%S")
+ coloritos.ENDC
+ " " + " ".join(l.split()[1:]))
except (TypeError, ValueError):
pass
try:
ip_colorful = coloritos.HEADER + ip + coloritos.ENDC
l = l.replace(ip, ip_colorful)
li, url, le = l.split()[:6], l.split()[6], l.split()[7:]
url_colorful = coloritos.OKBLUE + url + coloritos.ENDC
mime_colorful = coloritos.WARNING + le[-1] + coloritos.ENDC
le = le[:-1]
l = " ".join(li) + " " + url_colorful + " " + " ".join(le) + " " + mime_colorful + " " + '\n'
ip_mas_users = "%s %s[%s]%s" % (ip, coloritos.FAIL, ", ".join(dips[ip]), coloritos.ENDC)
l = l.replace(ip, ip_mas_users)
except Exception, msg:
print "Excepción", msg, ":\n", l
sys.exit(1)
sys.stdout.write(l)
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment