Beispiel #1
0
def main(ip, log_file, top, tests):
    """Main function to parse web log file"""
    if tests:
        suite = unittest.TestLoader().discover(os.path.dirname(__file__)+'/tests')
        # Run
        unittest.TextTestRunner(verbosity=2).run(suite)
        sys.exit()

    lp = LogParser(ip, log_file, top)
    matched_lines = None
    top_lines = None
    try:
        if top > 0:
            matched_lines, top_lines = lp.parse()
        else:
            matched_lines = lp.parse()
    except Exception as e:
        logger.error(e)

    if top > 0:
        if matched_lines is not None and top_lines is not None:
            lp.print_output(matched_lines, top_lines)
    else:
        if matched_lines is not None:
            lp.print_output(matched_lines)
 def test_top_10(self):
     lp = LogParser('180.76.15.135', 'tests/public_access.log', 10)
     matched_lines, top_lines = lp.parse()
     # test the right number of lines in output
     self.assertEqual(10, len(top_lines))
 def test_no_found_subnet_addr(self):
     lp = LogParser('180.76.16.0/24', 'tests/public_access.log', 0)
     matched_lines = lp.parse()
     self.assertEqual(0, len(matched_lines['requested_ip']))
 def test_found_ip_addr(self):
     lp = LogParser('180.76.15.135', 'tests/public_access.log', 0)
     matched_lines = lp.parse()
     self.assertEqual(2, len(matched_lines['requested_ip']))
 def test_wrong_log_file_name(self):
     lp = LogParser('127.0.0.1', 'dummy.log', 0)
     self.assertRaises(RuntimeError, lp.parse)
 def test_no_log_file_name(self):
     lp = LogParser('127.0.0.1', None, 0)
     self.assertRaises(RuntimeError, lp.parse)
 def test_wrong_ip_addr(self):
     lp = LogParser('ewrwerwrw', None, 0)
     self.assertRaises(RuntimeError, lp.parse)
 def test_no_ip_addr(self):
     lp = LogParser(None, None, 0)
     self.assertRaises(RuntimeError, lp.parse)