def readem(request_log_file, queue): """Tail given file and add parsed lines to queue""" f = subprocess.Popen(['tail','-F', '-n0', request_log_file],\ stdout=subprocess.PIPE,stderr=subprocess.PIPE) while True: line = f.stdout.readline() values = parse_line(line) queue.put(values)
def main(): if len(sys.argv) != 2: print "Usage: python last_five.py <file>" sys.exit(2) now = int(time.time()) five_ago = now - FIVE_MINUTES count_not_ok = 0 with open(sys.argv[1], 'r') as f: for line in filerev(f): vals = parse_line(line) code = vals['status_code'] ts = vals['epoch'] if ts > five_ago and ts <= now and code not in OK: count_not_ok += 1 print "Found %s non-200 status codes" % (count_not_ok, )
def main(): if len(sys.argv) != 2: print "Usage: python last_five.py <file>" sys.exit(2) now = int(time.time()) five_ago = now - FIVE_MINUTES count_not_ok = 0 with open(sys.argv[1], 'r') as f: for line in filerev(f): vals = parse_line(line) code = vals['status_code'] ts = vals['epoch'] if ts > five_ago and ts <= now and code not in OK: count_not_ok += 1 print "Found %s non-200 status codes" % (count_not_ok,)
def test_parse_line_good(self): line = '127.0.0.1 - - [16/Aug/2014 04:45:18] "GET / HTTP/1.1" 200 32 0.0003' vals = parse_line(line) self.assertEqual(vals['status_code'], 200)
def test_parse_line_bad(self): line = "\n\n\n" vals = parse_line(line) self.assertEqual(vals, None)