diff --git a/call.cgi b/call.cgi index 71e9ced..1353894 100755 --- a/call.cgi +++ b/call.cgi @@ -7,12 +7,17 @@ import pickle from datetime import datetime from datetime import timedelta +#import openpyxl + +if sys.argv[-1] == "--test": + logdir="./" +else: + logdir = "/var/log/httpd/" + +datefmt = "%Y/%m/%d %H:%M" form = cgi.FieldStorage() -print "Content-Type: application/json" -print "" - def machine_name(ip_str): ip = [int(v) for v in ip_str.split(".")] if ip_str.startswith("172.29.11"): @@ -31,8 +36,6 @@ 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) @@ -55,20 +58,36 @@ db[host].append([date,action]) else: db[host] = [[date,action]] -for mac in db.keys(): +for machine in db.keys(): candidate = [] - for date, action in db[mac]: + active = False + for date, action in db[machine]: if action.startswith("on:"): if date < to: _, id_ = action.split(":") if id_ in maildb: - date = date.strftime("%Y/%m/%d %H:%M") - candidate.append([mac, date, action, id_, - maildb[id_]["no"], maildb[id_]["name"], - maildb[id_]["reading"], maildb[id_]["dep"]]) + date = date.strftime(datefmt) + active = True + candidate.append( + # 0 1 2 3 4 + [machine, date, "---", action, id_, + # 5 6 + maildb[id_]["no"], maildb[id_]["name"], + # 7 8 + maildb[id_]["reading"], maildb[id_]["dep"]]) if action.startswith("off:") or action.startswith("shutdown"): - if not fr < date and candidate: - candidate.pop() + if candidate: + if not fr < date: + candidate.pop() + elif active and id_ == candidate[-1][4]: + active = False + candidate[-1][2] = date.strftime(datefmt) lines.extend(candidate) -json.dump(lines, sys.stdout) +if not "excel" in form: + print "Content-Type: application/json" + print "" + + json.dump(lines, sys.stdout) +else: + pass diff --git a/sampletest.txt b/sampletest.txt new file mode 100644 index 0000000..5bd641f --- /dev/null +++ b/sampletest.txt @@ -0,0 +1 @@ +QUERY_STRING="date=2018 7 10&from=9:00&to=12:00" ./call.cgi --test | python testjson.py | less