/
traffic_generator.py
executable file
·143 lines (121 loc) · 3.74 KB
/
traffic_generator.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
import getopt
import sys
import os
import time
import random
from datetime import datetime, timedelta
DEFAULT_LOG_SOURCE_FILE = 'sample_logs/access.log'
DEFAULT_OUTPUT_FILE = 'sample_logs/test.log'
DEFAULT_LPS = 1 # lines per second
HTTP_METHODS = [
'GET', 'POST', 'PUT', 'DELETE', 'HEAD'
]
URLS_CANDIDATES = [
'/administrator/index.php',
'/phocadocumentation',
'/item/3d-stroked-ink-actions/8037850',
'/item/archi-interior-design-joomla-template/14576483',
'/item/august/13903079',
'/item/aves-business-theme/12854860',
'/item/bigc-shop-responsive-woocommerce-theme/14279188',
'/item/cartoon-cavemen/14669255',
'/images/stories/slideshow/almhuette_raith_02.jpg',
'/templates/jp_hotel/js/moomenu.js',
'/modules/mod_bowslideshow/tmpl/images/image_shadow.png',
]
USERNAMES = [
'SMITH',
'JOHNSON',
'WILLIAMS',
'JONES',
'BROWN',
'DAVIS',
'MILLER',
'WILSON',
'MOORE',
'TAYLOR',
]
STATUS = [
200,
201,
301,
404,
500
]
class LogGenerator(object):
def __init__(self, source_file, dest_file, lps=DEFAULT_LPS):
self._source_file = source_file
self._dest_file = dest_file
self._sleep_duration = 1.0 / lps
random.seed(time.asctime())
self.timestamp = datetime.now()
self.timestamp_str = self.timestamp.strftime(
'[%d/%b/%Y:%H:%M:%S +0100]')
def generate_file(self):
src_file = open(self._source_file, 'r')
dest_file = open(self._dest_file, 'a+')
for line in src_file.readlines():
dest_file.write(line)
dest_file.flush()
os.fsync(dest_file.fileno())
time.sleep(self._sleep_duration)
def get_random_log_line(self):
host = ".".join(map(str, (int(random.random()*255) for _ in range(4))))
user = random.choice(USERNAMES)
method = random.choice(HTTP_METHODS)
url = random.choice(URLS_CANDIDATES)
status = random.choice(STATUS)
size = int(random.random()*80000)
if self.timestamp + timedelta(seconds=1) < datetime.now():
self.timestamp = datetime.now()
self.timestamp_str = self.timestamp.strftime(
'[%d/%b/%Y:%H:%M:%S +0100]')
return f'{host} - {user} {self.timestamp_str} "{method} {url}" {status} {size} "-" "Mozilla/5.0 (Windows NT 6.0; rv:34.0) Gecko/20100101 Firefox/34.0" "-"\n'
def generate_random_log(self):
dest_file = open(self._dest_file, 'a+')
while True:
line = self.get_random_log_line()
dest_file.write(line)
dest_file.flush()
os.fsync(dest_file.fileno())
time.sleep(self._sleep_duration)
def usage():
print('''
Write logs from source to destination file with a rate
-s source file
-d destination file
-l lines per second
''')
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'hs:d:l:', [
'help', 'source=', 'dest=', 'lps='])
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(2)
source = None
destination = None
lines_per_second = DEFAULT_LPS
for o, a in opts:
if o == "-v":
verbose = True
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-s", "--source"):
source = a
elif o in ("-d", "--dest"):
destination = a
elif o in ("-l", "--lps"):
lines_per_second = int(a)
else:
assert False, "unhandled option"
lg = LogGenerator(source, destination, lines_per_second)
if source:
lg.generate_file()
else:
lg.generate_random_log()
if __name__ == "__main__":
main()