-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_log_v3.py
451 lines (399 loc) · 18.7 KB
/
random_log_v3.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/usr/bin/python
#coding=utf-8
# random generate log
# by zhaozf
# 20170905
"""
log_format main '$time_iso8601 $server_name $remote_addr "$request" '
'$status $body_bytes_sent $request_time "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
2017-09-22T16:59:36+08:00 tm.pzrx.com 45.255.135.41 "GET /demo/index.html HTTP/1.1" 200 612 0.000 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"
"""
import linecache
import random
import time
import datetime
import os,shutil,sys
import platform
#t = time.strftime("%d/%m/%Y:%H:%M:%S", time.localtime())
##时间转时间戳
def StrToTimestamp(dt):
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
timestamp = int(time.mktime(timeArray))
return timestamp
##时间转时间戳
def TimestampToTime(timestamp):
timeArray = time.localtime(timestamp)
timestyle = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
return timestyle
def recordLog(msg):
with open(output_log,'a') as f:
f.write(msg+"\n")
class RandomGenLog(object):
##初始化参数
def __init__(self,init_log_filename):
self.file_ist = fileList
##统计对应文件的行数,用于随机取行
self.mydict = self.fileLineCount()
self.ip_file = "ip.txt"
self.domain_file = "domain.txt"
self.agent_file = "agent.txt"
##初始文件名
self.log_filename = init_log_filename
##循环写入日志总条数
self.write_num = writelog_by_min
self.initfun()
self.timeDefine()
self.random_define()
def initfun(self):
self.domain_dict = {}
self.domain_list = []
with open(self.domain_file) as f:
for i in f.readlines():
domain = i.strip()
self.domain_dict[domain] = {'bw':0,'bw_5min':0,'hits':0,'hits_err':0,'hits_5min':0,'hits_err_5min':0,'hits_5min_last':0,'hits_err_5min_last':0}
self.count_line = 0
self.count_line_second = 0
self.hits = 0
self.hits_error = 0
self.hist_5min_last = 0
self.hist_error_5min_last = 0
self.bandwidth = 0
def timeDefine(self):
##1秒最大写入记录数
self.max_seconds = max_seconds
## 初始开始时间戳
self.start_time_stamp_record = int(time.time())
self.time_second_record = self.start_time_stamp_record
##开始时间记录
start_time_record = TimestampToTime(self.start_time_stamp_record)
##开始时间列表
start_time_record_list = start_time_record.split(':')
##开始时间:分
get_min = start_time_record_list[-2]
if 0 <= int(get_min)<5:
start_time_record_list[-2] = "00"
elif 5<=int(get_min)<10:
start_time_record_list[-2] = "05"
elif 10<=int(get_min)<15:
start_time_record_list[-2] = "10"
elif 15<=int(get_min)<20:
start_time_record_list[-2] = "15"
elif 20<=int(get_min)<25:
start_time_record_list[-2] = "20"
elif 25<=int(get_min)<30:
start_time_record_list[-2] = "25"
elif 30<=int(get_min)<35:
start_time_record_list[-2] = "30"
elif 35<=int(get_min)<40:
start_time_record_list[-2] = "35"
elif 40<=int(get_min)<45:
start_time_record_list[-2] = "40"
elif 45<=int(get_min)<50:
start_time_record_list[-2] = "45"
elif 50<=int(get_min)<55:
start_time_record_list[-2] = "50"
elif int(get_min)>=55:
start_time_record_list[-2] = "55"
##开始时间:秒
start_time_record_list[-1] = "00"
## 重新再组合
start_time_record_int = ":".join(start_time_record_list)
print "起始时间:",start_time_record_int
recordLog("起始时间:%s" % start_time_record_int)
self.start_time_stamp_record_int = StrToTimestamp(start_time_record_int)
print "起始时间戳",self.start_time_stamp_record_int
recordLog("起始时间戳:%s" % self.start_time_stamp_record_int)
self.start_time_stamp_record_5_min = self.start_time_stamp_record
self.date_hour = time.strftime('%Y%m%d%H',time.localtime(time.time()))
##预定义随机的字符
def random_define(self):
#self.fileBodySizeRange = fileBodySizeRange
##状态码
self.status_list = ['200','206','502','503','404','403','304','499']
self.status_error_list = ['404','403','499','502','503']
##状态码出现的权重比列
self.weight_status = [85,11.5,0.5,0.2,0.8,0.3,1.5,0.2]
##随机字母
self.alph_str = "abcdefghhijklmnopqrsvwz"
self.sleep_time = [0.1]*2 + [0.01]*2 + [1]*5 + [2] * 2
##阿随机数字
self.num_str = "0123456789"
##混合或字母或数字
self.mix_alph_num = self.alph_str + self.num_str
##请求URL文件的扩展名文件类型及对应的size 大小范围,及请求时间(后面会除以1000)
self.fileBodySizeRange = {
'mp4':[(1024000,204800000),(1000,100000)],
'flv':[(1024000,204800000),(1000,100000)],
'rar':[(1024000,81920000),(1000,100000)],
'zip':[(1024000,81920000),(1000,100000)],
'm3u8':[(1000,1500),(0,1000)],
'ts':[(1024000,5120000),(1,10000)],
'jpg':[(10240,3072000),(1,6000)],
'ico':[(500,5120),(1,1000)],
'png':[(10240,2048000),(1,6000)],
'gif':[(102400,10240000),(1,10000)],
'css':[(500,20480),(1,1000)],
'js':[(500,20480),(1,1000)],
'json':[(30000,80263),(1,1000)],
'xml':[(10263,60000),(1,1000)],
'apk':[(5120000,51200000),(1000,200000)],
'exe':[(2120000,31200000),((1000,100000))]
}
##返回随机的字符串
def random_fun(self,chars):
str = ''
#chars = "abcdefghijklmnopqrstuvwyz0123456789"
length = len(chars) - 1
for i in range(random.randint(2,7)):
random_str = chars[random.randint(0, length)]
str += random_str
return str
##统计对应文件的行数,用于随机取行
def fileLineCount(self):
mydict = {}
for i in self.file_ist:
count = len(linecache.getlines(i))
mydict[i] = count
return mydict
##从文件中随机取行
def choiceRomLine(self,filename):
count = self.mydict[filename]
lineno = random.randrange(0,count)
line = linecache.getline(filename,lineno).rstrip("\n")
return line
def RandomGenFild(self):
# reqfname_alph = self.random_fun(self.alph_str)
# print "随机字母:",reqfname_alph
# reqfname_num = self.random_fun(self.num_str)
# print "随机数字:",reqfname_num
# reqfname_mix_alph_num = self.random_fun(self.mix_alph_num)
# print "混合字母或数字:",reqfname_mix_alph_num
## 同时m3u8 的文件名
reqfname_m3u8 = random.choice(["index","360p","540p","720p","1024p"])
##html 的文件名
reqfname_html = random.choice(["index","home",self.random_fun(self.alph_str),self.random_fun(self.num_str),
self.random_fun(self.mix_alph_num)])
reqpath_list = [self.random_fun(self.alph_str),self.random_fun(self.num_str),self.random_fun(self.mix_alph_num),
self.random_fun(self.alph_str),self.random_fun(self.num_str),self.random_fun(self.mix_alph_num),
self.random_fun(self.alph_str),self.random_fun(self.num_str)]
##路径长度范围随机组合成的列表
slice_reqpath = random.sample(reqpath_list,random.randint(0,5))
ext_list = self.fileBodySizeRange.keys()
ext_name = random.choice(ext_list)
ext_name_body = self.fileBodySizeRange[ext_name][0]
ext_name_request_time = self.fileBodySizeRange[ext_name][1]
## 取IP
ipd =self.choiceRomLine(self.ip_file).split('.')
##print ipd
##防止取IP程序出错
if len(ipd) != 0:
try:
ip = ".".join([ipd[0],ipd[1],ipd[2],str(random.randint(1,254))])
except:
ip = '1.1.1.1'
else:
ip = "1.1.1.1"
## 取域名
domain = self.choiceRomLine(self.domain_file)
if len(domain) == 0:
domain = 'www.fang.com'
## 取 user agent
agent = self.choiceRomLine(self.agent_file)
if len(agent) == 0:
agent = '-'
##随机状态码
status = self.status_list[self.weightChoice()]
##非正常状态码的 body大小
if status in self.status_error_list:
body = random.randint(100,512)
else:
body = random.randint(ext_name_body[0],ext_name_body[1])
request_time = float(random.randint(ext_name_request_time[0],ext_name_request_time[1])/1000.000)
if len(slice_reqpath) == 0:
request_url = "/" + reqfname_html + ".html"
elif ext_name == 'm3u8':
request_url = "/" + self.random_fun(self.alph_str) + "/" + reqfname_m3u8 + "." + ext_name
elif ext_name == "ts":
request_url = "/" + "/".join(slice_reqpath) + "/" + self.random_fun(self.num_str) + "." + ext_name
elif ext_name in ['jpg','gif','png']:
request_url = "/" + random.choice(["upload","uploadfile","image","img"]) + "/" + "/".join(slice_reqpath) \
+ random.choice([self.random_fun(self.alph_str),self.random_fun(self.num_str)]) + "." + ext_name
elif ext_name in ['js','css','ico']:
request_url = "/" + "static" + "/" \
+ random.choice([self.random_fun(self.alph_str),self.random_fun(self.num_str)]) + "." + ext_name
else:
request_url = "/" + "/".join(slice_reqpath) + "." + ext_name
##当前的时间戳
self.this_time_stamp = int(time.time())
##当前时间
self.this_time = TimestampToTime(self.this_time_stamp)
#print request_url
return ip,domain,agent,body,status,request_url,self.this_time,request_time
def logFormat(self):
##取得各字段
genFild = self.RandomGenFild()
ip = genFild[0]
domain = genFild[1]
agent = genFild[2]
self.body = genFild[3]
status = genFild[4]
request_url = genFild[5]
##时间字段
log_date_time = genFild[6]
##请求时间字段
log_request_time = genFild[7]
##分隔时间字段
log_time_str_list = log_date_time.split(' ')
##年月日时间字段
log_date = log_time_str_list[0]
##时分秒时间字段
log_time = log_time_str_list[1]
##年
log_year = log_date.split('-')[0]
##月
log_month = log_date.split('-')[1]
##日
log_day = log_date.split('-')[2]
##时
log_hour = log_time.split(':')[0]
#log_min = log_time_str_list[2].split(':')[2]
## ex: 2017070710 格式
self.nowdate_by_hour = log_year + log_month + log_day + log_hour
#self.logMsgFormat = '''{0} - - {1}T{2}+08:00 "{3}" "GET {4} HTTP/1.1" {5} {6} "-" "{7}" "-" "-"'''.format(ip,log_date,log_time,domain,request_url,status,self.body,agent)
##组合成日志格式
self.logMsgFormat = '''{0}T{1}+08:00 {2} {3} "GET {4} HTTP/1.1" {5} {6} {7} "-" "{8}" "-"'''.format(log_date,log_time,domain,ip,request_url,status,self.body,log_request_time,agent)
#status_line = 1
##错误状态码的计数
if status in self.status_error_list:
self.domain_dict[domain]["hits_err"] = self.domain_dict[domain]["hits_err"] + 1
self.hits_error = self.hits_error + 1
##正常状态码的计数
else:
##点击数
self.hits = self.hits + 1
##总累计带宽
self.bandwidth = self.bandwidth + self.body
##域名累计带宽
self.domain_dict[domain]['bw'] = self.domain_dict[domain]['bw'] + self.body
##域名点击数
self.domain_dict[domain]['hits'] = self.domain_dict[domain]['hits'] + 1
return self.logMsgFormat
def WriteLog(self):
## 这里未通过外部调用类来循环写入日志,是为了防止不断的重复多次打开和关闭文件,浪费系统资源影响效率
with open(self.log_filename,"a") as f:
## 循环的写入条数
for i in xrange(1,self.write_num):
#print "写第几条日志: %s" %i
msg = self.logFormat()
f.write(msg +"\n")
if(self.this_time_stamp - self.start_time_stamp_record_int) == 299:
start_t = TimestampToTime(self.start_time_stamp_record_int)
end_t = TimestampToTime(self.this_time_stamp)
self.start_time_stamp_record_int = self.start_time_stamp_record_int + 300
hits_5min = self.hits - self.hist_5min_last
hits_error_5min = self.hits_error - self.hist_error_5min_last
bw_5min = float((self.bandwidth/1024/1024/300) * 8)
time_msg = "{0:15}~{1:15}".format(start_t,end_t)
print time_msg
#recordLog(time_msg)
recordLog("\n ############################# %s #################################\n" %time_msg)
msg_normal = "5分钟正常请求日志(包含所有域名)写入行数: {0:12} ".format(hits_5min)
msg_error = "5分钟错误请求日志(包含所有域名)写入行数:{0:12} 行".format(hits_error_5min)
msg_bw = "5分钟请求平均带宽是(包含所有域名):{0:12.2f} Mbps".format(bw_5min)
msg_hits = "到目前为止正常请求日志(包含所有域名)写入行数: {0:12}".format(self.hits)
msg_hits_error = "到目前为止错误请求日志(包含所有域名)写入行数: {0:12} ".format(self.hits_error)
print msg_normal
recordLog(msg_normal)
print msg_error
recordLog(msg_error)
print msg_bw
recordLog(msg_bw)
print msg_hits
recordLog(msg_hits)
print msg_hits_error
recordLog(msg_hits_error)
for domain in self.domain_dict.keys():
d_hits_5min = self.domain_dict[domain]['hits_5min'] = self.domain_dict[domain]['hits'] - self.domain_dict[domain]['hits_5min_last']
d_hits_err_5min = self.domain_dict[domain]['hits_err_5min'] = self.domain_dict[domain]['hits_err'] - self.domain_dict[domain]['hits_err_5min_last']
d_bw = self.domain_dict[domain]['bw_5min'] = float((self.domain_dict[domain]['bw']/1024/1024/300) * 8)
domain_msg = "域名:{0:20}, 带宽:{1:12.2f} mbps, 点击数:{2:8}, 错误点击数:{3:8}".format(domain,d_bw,d_hits_5min,d_hits_err_5min)
print domain_msg
recordLog(domain_msg)
self.domain_dict[domain]['hits_5min_last'] = self.domain_dict[domain]['hits']
self.domain_dict[domain]['hits_err_5min_last'] = self.domain_dict[domain]['hits_err']
self.domain_dict[domain]['bw'] = 0
#self.domain_dict[domain]['hits'] = 0
#self.domain_dict[domain]['hits_err'] = 0
self.hist_5min_last = self.hits
self.hist_error_5min_last = self.hits_error
self.bandwidth = 0
#self.hits_error = 0
#self.hits = 0
##控制写入速度
#print self.this_time_stamp
#print self.time_second_record
if (self.this_time_stamp - self.time_second_record) == 1:
self.time_second_record = self.this_time_stamp
j = i - self.count_line_second
if j > self.max_seconds:
sleep_t = random.choice(self.sleep_time)
#print "1秒内大于 %s 行,sleep %s 秒" % (self.max_seconds,sleep_t)
time.sleep(sleep_t)
self.count_line_second = i
#print self.date_hour
#print self.nowdate_by_hour
##按小时循切换日志文件
if self.date_hour != self.nowdate_by_hour:
print "切换日志文件"
if 'linux' in (platform.platform()).lower():
old_log_filename = log_file_path + str(self.date_hour) + '.log'
else:
old_log_filename = str(self.date_hour) + '.log'
self.date_hour = self.nowdate_by_hour
f.flush()
f.close()
try:
shutil.move(self.log_filename,old_log_filename)
except:
pass
self.WriteLog()
##状态码的概率函数
def weightChoice(self):
randnum = random.uniform(0.0,float(sum(self.weight_status)))
start = 0.0
for schoice, item in enumerate(self.weight_status):
#print index,item
start += item
if randnum <= start:
break
return schoice
def main():
if 'linux' in (platform.platform()).lower():
if not os.path.exists(log_file_path):
os.makedirs(log_file_path)
log_full_filename = os.sep.join([log_file_path,filename_log])
else:
log_full_filename = filename_log
if os.path.exists(log_full_filename):
try:
os.remove(log_full_filename)
except:
print "无法移除文件"
sys.exit(1)
F = RandomGenLog(log_full_filename)
F.WriteLog()
if __name__ == '__main__':
fileList = ["ip.txt","domain.txt","agent.txt"]
log_file_path = "/opt/cdn/nginx/logs/"
filename_log = "access.log"
output_log = "output.log"
##要写入多少行的日志
writelog_by_min = 20000000
##每秒最大写入多少行,超过就sleep
max_seconds = 100
##日志格式的月份有是字母,这里是为了映射: 数字 -》字母
#month_num_to_letter = {'01':'Jan','02':'Feb','03':'Mar','04':'Apr','05':'May','06':'Jun','07':'Jul','08':'Aug','09':'Sep','10':'Oct','11':'Nov','12':'Dec'}
## 月份 字母--》数字
#month_letter_to_num = dict([(v,k) for k,v in month_num_to_letter.iteritems()])
main()