Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add argument repeat for create multiple set of logs #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 52 additions & 47 deletions apache-fake-log-gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,18 @@ def match(self, *args):
parser.add_argument("--num", "-n", dest='num_lines', help="Number of lines to generate (0 for infinite)", type=int, default=1)
parser.add_argument("--prefix", "-p", dest='file_prefix', help="Prefix the output file name", type=str)
parser.add_argument("--sleep", "-s", help="Sleep this long between lines (in seconds)", default=0.0, type=float)
parser.add_argument("--repeat", "-r", dest='repeat', help="Repeat the fake log function 1 to N", type=int, default=1)

args = parser.parse_args()

log_lines = args.num_lines
file_prefix = args.file_prefix
output_type = args.output_type
log_format = args.log_format
repeat = args.repeat

faker = Faker()

timestr = time.strftime("%Y%m%d-%H%M%S")
otime = datetime.datetime.now()

outFileName = 'access_log_'+timestr+'.log' if not file_prefix else file_prefix+'_access_log_'+timestr+'.log'

for case in switch(output_type):
if case('LOG'):
f = open(outFileName,'w')
break
if case('GZ'):
f = gzip.open(outFileName+'.gz','w')
break
if case('CONSOLE'): pass
if case():
f = sys.stdout

response=["200","404","500","301"]

verb=["GET","POST","DELETE","PUT"]
Expand All @@ -78,34 +64,53 @@ def match(self, *args):

ualist = [faker.firefox, faker.chrome, faker.safari, faker.internet_explorer, faker.opera]

flag = True
while (flag):
if args.sleep:
increment = datetime.timedelta(seconds=args.sleep)
else:
increment = datetime.timedelta(seconds=random.randint(30, 300))
otime += increment

ip = faker.ipv4()
dt = otime.strftime('%d/%b/%Y:%H:%M:%S')
tz = datetime.datetime.now(local).strftime('%z')
vrb = numpy.random.choice(verb,p=[0.6,0.1,0.1,0.2])

uri = random.choice(resources)
if uri.find("apps")>0:
uri += str(random.randint(1000,10000))

resp = numpy.random.choice(response,p=[0.9,0.04,0.02,0.04])
byt = int(random.gauss(5000,50))
referer = faker.uri()
useragent = numpy.random.choice(ualist,p=[0.5,0.3,0.1,0.05,0.05] )()
if log_format == "CLF":
f.write('%s - - [%s %s] "%s %s HTTP/1.0" %s %s\n' % (ip,dt,tz,vrb,uri,resp,byt))
elif log_format == "ELF":
f.write('%s - - [%s %s] "%s %s HTTP/1.0" %s %s "%s" "%s"\n' % (ip,dt,tz,vrb,uri,resp,byt,referer,useragent))
f.flush()

log_lines = log_lines - 1
flag = False if log_lines == 0 else True
if args.sleep:
time.sleep(args.sleep)
def create_log(log_lines):
timestr = time.strftime("%Y%m%d-%H%M%S")
otime = datetime.datetime.now()

outFileName = 'access_log_'+timestr+'.log' if not file_prefix else file_prefix+'_access_log_'+timestr+'.log'

for case in switch(output_type):
if case('LOG'):
f = open(outFileName,'w')
break
if case('GZ'):
f = gzip.open(outFileName+'.gz','w')
break
if case('CONSOLE'): pass
if case():
f = sys.stdout
flag = True
while (flag):
if args.sleep:
increment = datetime.timedelta(seconds=args.sleep)
else:
increment = datetime.timedelta(seconds=random.randint(30, 300))
otime += increment

ip = faker.ipv4()
dt = otime.strftime('%d/%b/%Y:%H:%M:%S')
tz = datetime.datetime.now(local).strftime('%z')
vrb = numpy.random.choice(verb,p=[0.6,0.1,0.1,0.2])

uri = random.choice(resources)
if uri.find("apps")>0:
uri += str(random.randint(1000,10000))

resp = numpy.random.choice(response,p=[0.9,0.04,0.02,0.04])
byt = int(random.gauss(5000,50))
referer = faker.uri()
useragent = numpy.random.choice(ualist,p=[0.5,0.3,0.1,0.05,0.05] )()
if log_format == "CLF":
f.write('%s - - [%s %s] "%s %s HTTP/1.0" %s %s\n' % (ip,dt,tz,vrb,uri,resp,byt))
elif log_format == "ELF":
f.write('%s - - [%s %s] "%s %s HTTP/1.0" %s %s "%s" "%s"\n' % (ip,dt,tz,vrb,uri,resp,byt,referer,useragent))
f.flush()

log_lines = log_lines - 1
flag = False if log_lines == 0 else True
if args.sleep:
time.sleep(args.sleep)

for x in range(repeat):
create_log(log_lines)