Skip to content

Instantly share code, notes, and snippets.

@kanazux
Last active October 23, 2015 16:38
Show Gist options
  • Save kanazux/9975300 to your computer and use it in GitHub Desktop.
Save kanazux/9975300 to your computer and use it in GitHub Desktop.
print logs for webfitler, teste signals control c
#!/usr/local/bin/python
#
# Written by kanazuchi
#
import os
import re
import sys
import time
import signal
import argparse
c_time = int(time.time())
def controlc(signal, frame):
os.unlink(logfile)
sys.exit(0)
def getlogs(logfile):
if not os.path.exists(logfile):
open(logfile, "a")
if opts.filter == "all":
pattern = ".*"
else:
pattern = opts.filter
readLog = open(logfile, "r", 0)
while 1:
if int(time.time()) - c_time == 120:
os.unlink(logfile)
sys.exit(0)
lineLog = readLog.readline().rstrip()
if re.match('.*{}.*'.format(pattern), lineLog) and lineLog != "" and lineLog != "\n" and lineLog != "\r":
lineLog = lineLog.split()
if opts.log_file == "access":
print >> sys.stdout, " ".join([lineLog[1],lineLog[0],lineLog[3],lineLog[4]])
elif opts.log_file == "netfilter":
print >> sys.stdout, " ".join(lineLog[1],lineLog[4],lineLog[2],lineLog[3],lineLog[5],lineLog[6])
def set_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
'-l', dest='log_file',
action='store',
choices=('access','netfilter'),
default='access',
help='Log file can be netfilter or access log. Default is access')
parser.add_argument(
'-f', dest='filter',
action='store',
default='all',
help='Filter can be an ip or user/group. Default is all')
return parser.parse_args()
if __name__ == "__main__":
opts = set_parser()
signal.signal(signal.SIGINT, controlc)
logfile = "/var/tmp/{}".format(opts.log_file)
getlogs(logfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment