summaryrefslogtreecommitdiff
path: root/monitor/josef_logreader.py
blob: d261fd4a06867b10efad00c99f64256bc9f2fb57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python
# -*- coding: utf-8 -*-     

import sys
import time
import datetime
import os

from monitor_conf import *

TIME_LEN = 21
NEW_STH_STR = "STH updated"
START_STR = "Starting monitor"

def get_logs():
    logs = []
    for file in os.listdir("./" + OUTPUT_DIR):
        if file.endswith(".log"):
            logs.append(file)
    return logs

def get_age_from_line(line):
    past = datetime.datetime.strptime(line[:20], '%Y-%m-%d, %H:%M:%S')
    present = datetime.datetime.now()
    return present - past

def read_loglog(filename):
    with open(OUTPUT_DIR + filename) as f:
        content = f.readlines()
    return content

def print_log_stats(l):
    log = read_loglog(l)
    rev_log = list(reversed(log))
    last = rev_log[0]

    # Stats from last STH update
    for item in rev_log:
        line = item[TIME_LEN:]
        # if line[:len(START_STR)] == START_STR:
        #     break
        if line[:len(NEW_STH_STR)] == NEW_STH_STR:
            timestamp = datetime.datetime.strptime(line[-20:-1], '%Y-%m-%d %H:%M:%S')
            age = datetime.datetime.utcnow() - timestamp
            size = line.split("Size: ")[1].split(",")[0]
            # print line[:-1]
            print "STH age: " + str(age)[:-7]
            print "Size: " + size
            break
    else:
        print "No STH update found in log."


def print_average_age(l):
    log = read_loglog(l)
    rev_log = list(reversed(log))
    # last = rev_log[0]

    prev_timestamp = None
    ages = []
    for item in rev_log:
        line = item[TIME_LEN:]
        if line[:len(START_STR)] == START_STR:
            # break
            prev_timestamp = None # Don't count over restarts
        if line[:len(NEW_STH_STR)] == NEW_STH_STR:
            timestamp = datetime.datetime.strptime(line[-20:-1], '%Y-%m-%d %H:%M:%S')
            if prev_timestamp is not None:
                ages.append(prev_timestamp - timestamp)
            prev_timestamp = timestamp

    if len(ages) == 0:
        print "No timedeltas found.."
    else:
        average_timedelta = sum(ages, datetime.timedelta(0)) / len(ages)
        print "Average update time: " + str(average_timedelta) + " (" + str(len(ages)) + " values)"

def print_errors(l):
    # print errors since last restart
    log = read_loglog(l)
    rev_log = list(reversed(log))

    prev_timestamp = None
    ages = []
    for item in rev_log:
        line = item[TIME_LEN:]
        if "ERROR" in line:
            print item[:-1]
        if line[:len(START_STR)] == START_STR:
            # break # comment this line to print all errors ever
            pass


if __name__ == "__main__":
    logs = get_logs()
    for log in logs:
        if log == "monitor.log":
            pass
        else:
            print log
            print_log_stats(log)
            print_average_age(log)
            print_errors(log)
            print ""