diff --git a/README b/README new file mode 100644 index 0000000..e69de29 diff --git a/barcharter.py b/barcharter.py new file mode 100644 index 0000000..47f2ca4 --- /dev/null +++ b/barcharter.py @@ -0,0 +1,59 @@ +#!/usr/bin/python + +import sys +skip_ips = ["93.97.158.26"] +html_template_head = """ + + + + + + +
+ + +""" +cols_html = """ + data.addColumn('string', 'Source'); + data.addColumn('number', 'Count');""" +row_html = """ + data.setValue(%d, 0, '%s'); + data.setValue(%d, 1, %d); +""" +data_html = "%(count)s, undefined, undefined" + +def get_data(file_handle): + name = file_handle.name.split("/")[-1].split(".")[0] + count = 0 + for row in file_handle.readlines(): + ip = row.split()[0] + if ip in skip_ips: + continue + count += 1 + skip_ips.append(ip) + return name, count + +def plot_data(sources): + html = html_template_head + html += cols_html + html += " data.addRows(%d);" % len(sources) + idx = 0 + for name, count in sources: + html += row_html % (idx, name, idx, count); + idx += 1 + html += html_template_foot + print html + +sources = [get_data(open(x, "r")) for x in sys.argv[1:]] +plot_data(sources) diff --git a/customlogcharter.py b/customlogcharter.py new file mode 100755 index 0000000..945cb95 --- /dev/null +++ b/customlogcharter.py @@ -0,0 +1,97 @@ +#!/usr/bin/python + +import sys +import re +from datetime import datetime, timedelta +skip_ips = ["93.97.158.26"] +html_template_head = """ + + + + + + +
+ + + +""" +cols_html = """ + data.addColumn('number', '%s'); + data.addColumn('string', 'title1'); + data.addColumn('string', 'text1');""" +row_html = """ +data.addRows([ + [new Date(%(year)s,%(month)s ,%(day)s, %(hour)s, 0, 0), %(value_seq)s], + ]); + """ +data_html = "%(count)s, undefined, undefined" + +def get_data(file_handle): + rows = [] + times = [] + buckets = {} + start_date = None + local_skip_ips = skip_ips[:] + for line in file_handle: + if not line.strip(): + continue + ip = line.split()[0] + if ip in local_skip_ips: + continue + local_skip_ips.append(ip) + match = re.match(r".*\[([^\]]+).*", line) + if match: + when = datetime.strptime(match.group(1), + "%d/%b/%Y:%H:%M:%S +0000") + rounded = when.strftime("%d/%b/%Y:%H") + when = datetime.strptime(rounded, "%d/%b/%Y:%H") + if not start_date: + start_date = when + end_date = when + val = buckets.setdefault(when, 0) + buckets[when] = val + 1 + name = file_handle.name.split("/")[-1].split(".")[0] + return name, start_date, end_date, buckets + +def plot_data(sources): + html = html_template_head + for name, _, _, _ in sources: + html += cols_html % name + times = [] + start_date = min([x for _, x, _, _ in sources]) + end_date = max([y for _, _, y, _ in sources]) + when = start_date + while when < end_date: + times.append(when) + when += timedelta(0,0,0,0,0,1) + rows = [] + for when in times: + value_seq = [] + for _, _, _, buckets in sources: + values = {'year':when.year, + 'month':when.month, + 'day':when.day, + 'hour':when.hour, + 'count':buckets.get(when, 0)} + value_seq.append(data_html % values) + values['value_seq'] = ",".join(value_seq) + rows.append(row_html % values) + html += "\n".join(rows) + html += html_template_foot + print html + +sources = [get_data(open(x, "r")) for x in sys.argv[1:]] +plot_data(sources)