Skip to content

Instantly share code, notes, and snippets.

@SharkyRawr
Created January 8, 2016 08:49
Show Gist options
  • Save SharkyRawr/41e6f2f3f02fe4035de4 to your computer and use it in GitHub Desktop.
Save SharkyRawr/41e6f2f3f02fe4035de4 to your computer and use it in GitHub Desktop.
Python webalizer wrapper
#! /usr/bin/env python3
import os, sys, re
from subprocess import call
from os.path import join
LOGDIR = '/var/log/nginx'
OUTDIR = '/home/htdocs/internal/stats/'
WEBALIZER_OPTS = [
'/usr/local/bin/webalizer-RB30', # Binary
'-F clf', # log type
'-p', # preserve history
'-D /var/tmp/webalizer.dnscache', # DNS cache location
'-N 5', # number of DNS processes
'-w', # Enable GeoIP lookups
'-W /usr/share/GeoIP/GeoIP.dat', # external GeoIP database,
'-Q', # quiet
]
# mydomain.com.access.log, mydomain.com.error.log, ... .log.1, ... .log.2.gz, ...
my_log_fn = re.compile(r'((.*)(?:\.?(?:access|error))\.log(?:.*))')
log_id = re.compile(r'(?:(?:.*)(?:\.?(?:access|error))\.log\.?(\d+)?).*')
def parselog(m):
filename, name = m
name = name[:-1].strip()
if len(name) <= 0:
name = "_default_"
destdir = join(OUTDIR, name)
args = [
'-o ' + destdir,
'-n ' + name,
join(LOGDIR, filename),
]
if os.path.lexists(destdir) is False:
os.mkdir(destdir)
if os.path.isdir(destdir) is False:
raise Exception('outdir ' + destdir + ' is not a directory')
print (filename)
#print (name)
cmdline = " ".join(WEBALIZER_OPTS + args)
cmdargs = WEBALIZER_OPTS + args
if os.system(cmdline) is not 0:
print (" !! webalizer failed! !!\n")
def logkey(l):
m = log_id.match(l)
if m is None:
return 0
g = m.groups()[0]
if g is None:
return 0
g = int(g)
return g
if __name__ == "__main__":
logs = os.listdir(LOGDIR)
logs = sorted(logs, key=logkey, reverse=True)
for l in logs:
m = my_log_fn.match(l)
if m is not None:
filename, name = m.groups()
parselog(m.groups())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment