#!/usr/bin/env python

import os, sys


LOGS = "%s/data/logs"%os.environ['SALVUS_ROOT']

def count(f, s, lines):
    fname = os.path.join(LOGS, f)
    if not os.path.exists(fname):
        return -1
    if lines:
        c = "tail -n %s %s"%(lines, fname)
    else:
        c = "cat %s"%fname
    # exclude 'Error: read ' since those are caused by db driver and aren't worth worrying about (they don't seem to cause trouble)
    c += " | grep '%s' | wc -l"%s
    #print c
    n = int(os.popen(c).read())
    #print n
    return n

def check(num):
    strings = [('cql ERROR:',num), ('PoolConnectionError',num), ('debug: Uncaught exception:',num), ('There may have been an error creating your account',num), ("here was an error signing you in",num)]

    for f in sorted(os.listdir(LOGS)):
        if f.startswith('hub-') and f.endswith('.log'):
            for s,lines in strings:
                i = count(f, s, lines)
                if i > 0:
                    print 'last %s:'%lines, f, i, "'%s'"%s

if len(sys.argv) == 1:
    sys.argv.append('50000')
    sys.argv.append('5000')
    sys.argv.append('500')

for num in sys.argv[1:]:
    check(int(num))
