|
| 1 | +""" |
| 2 | +:created: 15 Jan 2016 |
| 3 | +:author: bleerodgers blr@nw3weather.co.uk |
| 4 | +""" |
| 5 | + |
| 6 | +import random |
| 7 | + |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | + |
| 11 | +class ApacheLogEmulator(object): |
| 12 | + """ |
| 13 | + Very basic emulator for logging http traffic from an Apache web server |
| 14 | + """ |
| 15 | + |
| 16 | + def __init__(self, log_path): |
| 17 | + self.log_path = log_path |
| 18 | + |
| 19 | + def emulate(self, count): |
| 20 | + lines = [] |
| 21 | + for i in xrange(count): |
| 22 | + lines.append( |
| 23 | + self._get_record() |
| 24 | + ) |
| 25 | + self._write_lines(lines) |
| 26 | + |
| 27 | + def _get_record(self): |
| 28 | + fields = [ |
| 29 | + self._random_ip(), # host |
| 30 | + '-', # ident |
| 31 | + '-', # authuser |
| 32 | + self._current_date(), # date |
| 33 | + self._random_request(), # request |
| 34 | + self._random_status(), # status |
| 35 | + str(random.randint(0, 10000)), # bytes |
| 36 | + '"http://www.google.com"', # referrer |
| 37 | + '"Mozilla/5.0 (compatible; ApacheLogBot/0.1;)"' # user agent string |
| 38 | + ] |
| 39 | + return ' '.join(fields) + '\n' |
| 40 | + |
| 41 | + def _write_lines(self, data): |
| 42 | + with open(self.log_path, 'a') as f: |
| 43 | + f.writelines(data) |
| 44 | + |
| 45 | + def _random_ip(self): |
| 46 | + return random.choice([ |
| 47 | + '192.168.1.52', |
| 48 | + '10.10.10.10', |
| 49 | + '22.22.2.222', |
| 50 | + '255.25.55.25', |
| 51 | + '16.2.33.45', |
| 52 | + '1.22.333.444' |
| 53 | + ]) |
| 54 | + |
| 55 | + def _current_date(self): |
| 56 | + return '[{0} +0000]'.format(datetime.now().strftime('%d/%b/%Y:%H:%M:%S')) |
| 57 | + |
| 58 | + def _random_request(self): |
| 59 | + method = random.choice(['GET', 'PUT', 'POST', 'DELETE', 'HEAD']) |
| 60 | + protocol = 'HTTP/' + random.choice(['1.0', '1.1', '2']) |
| 61 | + |
| 62 | + page = '{0}.html'.format(random.choice(('edit', 'make', 'scrap', 'grab'))) |
| 63 | + query_str = random.choice(['?randomise', '']) |
| 64 | + page += query_str |
| 65 | + |
| 66 | + sections = [] |
| 67 | + for i in xrange(random.randint(1, 5)): |
| 68 | + sections.append(random.choice([chr(i) for i in xrange(65, 80)])) |
| 69 | + sections.append(page) |
| 70 | + |
| 71 | + url = '/' |
| 72 | + root_weight = 0.1 |
| 73 | + if random.random() > root_weight: |
| 74 | + url += '/'.join(sections) |
| 75 | + |
| 76 | + return '"{0}"'.format(' '.join((method, url, protocol))) |
| 77 | + |
| 78 | + def _random_status(self): |
| 79 | + return str(random.choice([ |
| 80 | + 100, 101, |
| 81 | + 200, 201, 206, |
| 82 | + 301, 302, 304, |
| 83 | + 401, 403, 404, 418, 451, |
| 84 | + 500, 502, 503 |
| 85 | + ])) |
| 86 | + |
| 87 | + |
| 88 | +def main(): |
| 89 | + import time |
| 90 | + import argparse |
| 91 | + |
| 92 | + parser = argparse.ArgumentParser(description='Apache http access log emulator') |
| 93 | + parser.add_argument('-p', '--path', help='path to write to [apache_log.txt]') |
| 94 | + parser.add_argument('-f', '--frequency', type=float, help='how often to write records, in s [3.3]') |
| 95 | + parser.add_argument('-c', '--count', type=int, help='max count of records to write [1000]') |
| 96 | + |
| 97 | + args = parser.parse_args() |
| 98 | + emulator = ApacheLogEmulator(args.path or 'apache_log.txt') |
| 99 | + |
| 100 | + while True: |
| 101 | + count = random.randint(0, args.count or 1000) |
| 102 | + print 'Generating {0} random http requests'.format(count) |
| 103 | + emulator.emulate(count) |
| 104 | + time.sleep(args.frequency or 3.3) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + main() |
0 commit comments