diff --git a/call.cgi b/call.cgi new file mode 100755 index 0000000..ad3ee29 --- /dev/null +++ b/call.cgi @@ -0,0 +1,49 @@ +#!/usr/bin/env python +import os +import sys +import cgi +import re +import json +import pickle +from datetime import datetime +from datetime import timedelta + +form = cgi.FieldStorage() + +print "Content-Type: application/json" +print "" + +fr = datetime.strptime(form["date"].value + " " + form["from"].value, "%Y %m %d %H:%M") +to = datetime.strptime(form["date"].value + " " + form["to"].value, "%Y %m %d %H:%M") + +logdir = "/var/log/httpd/" + +l = fr + timedelta(7) # a week after +higher = "access_log-%04d%02d%02d"%(l.year, l.month, l.day) +lower = "access_log-%04d%02d%02d"%(fr.year, fr.month, fr.day) +logfiles = [fname for fname in os.listdir(logdir) if fname.startswith("access_log-") and lower <= fname and fname <=higher] + +#logfiles.sort() +if not logfiles: logfiles.append('access_log') + +#ipaddr = "172.29.119." + str(int(name)+150) + +errlog = [] +error_count = 0 +lines = [] +maildb = pickle.load(open("name.pickle")) + +for fname in logfiles[-2:]: + with open(logdir + fname) as f: + line = [l for l in f.readlines() if "logonoff" in l] + line = [re.match(r'^([0-9.]*).*\[(\d+/\w+/\d+:\d+:\d+:\d+).*logonoff\?(o.*) HTTP', l) for l in line] + line = [l.groups() for l in line if l] + line = [(ip, datetime.strptime(date, '%d/%b/%Y:%H:%M:%S'), action) + for ip, date, action in line] + line = [[ip, date.strftime("%Y/%m/%d %H:%M")]+action.split(':') + for ip, date, action in line if fr <= date and date <= to] + line = [[ip, date, action, id_, maildb[id_]["no"], maildb[id_]["name"], + maildb[id_]["reading"], maildb[id_]["dep"]] for ip, date, action, id_ in line if id_ in maildb] + lines += line + +json.dump(lines, sys.stdout) diff --git a/makepickle.py b/makepickle.py new file mode 100644 index 0000000..430b7c9 --- /dev/null +++ b/makepickle.py @@ -0,0 +1,43 @@ +from os import path +import pickle + +outdb = {} +dbpath = path.expanduser("~tutimura/public_html/cgi-bin/passwd/name.txt") + +with open(dbpath, "r") as f: + for line in f.readlines(): + if not line: continue + no_, reading, name = line.split("\t") + if name.count(" ") == 2: + first_n, family_n, note = name.split(" ") + name = first_n + " " + family_n + name = name.strip() + outdb[no_] = {"name": name, "reading": reading} + +maildb = {} +mailpath = path.expanduser("~tutimura/public_html/cgi-bin/passwd/mail.txt") +with open(mailpath, "r") as f: + for line in f.readlines(): + if not line: continue + line = line.strip() + tpl = line.split("\t") + if len(tpl) == 2: + no_, mailadr = tpl + dep = note = "" + elif len(tpl) == 3: + no_, mailadr, dep = tpl + note = "" + elif len(tpl) == 4: + no_, mailadr, dep, note = tpl + id_ = mailadr.split("@")[0] + if no_ in outdb: + name = outdb[no_]["name"] + reading = outdb[no_]["reading"] + else: + name = "" + reading = "" + maildb[id_] = { "name": name, "reading": reading, + "no": no_, "dep": dep, "note": note} + +with open("name.pickle", "wb") as out: + pickle.dump(maildb, out)