定时任务脚本
一 先启动crontab服务
service crond restart
二 写定时器(time.txt)
crontab -e
*/1 * * * * /opt/log_generator.sh
三 运行脚本
运行Python脚本的脚本
python /opt/generate_log.py
下面是Python脚本:
#coding=UTF-8
import random
import time
url_paths = [
"class/112.html",
"class/128.html",
"class/145.html",
"class/146.html",
"class/131.html",
"class/130.html",
"learn/821",
"course/list"
]
http_referers = [
"http://www.baidu.com/s?wd={query}",
"http://www.sogou.com/web?wd={query}",
"http://cn.bing.com/search?wd={query}",
"http://search.yahoo.com/search?wd={query}"
]
search_keyword = [
"Spark Sql实战",
"Hadoop 基础",
"Storm 实战",
"Spark Streaming 实战",
"大数据面试"
]
ip_slices = [123, 156, 124, 10, 31, 29, 12, 167, 143, 187, 30, 45, 55, 63, 72, 87]
status_codes = ["200" , "404" , "500"]
def sample_url():
return random.sample(url_paths, 1)[0]
def sample_ip():
slice = random.sample(ip_slices, 4)
return ".".join([str(item) for item in slice])
def sample_status():
return random.sample(status_codes , 1)[0]
def sample_referer():
if random.uniform(0,1) > 0.2:
return "-"
refer_str = random.sample(http_referers,1)
query_str = random.sample(search_keyword,1)
return refer_str[0].format(query=query_str[0])
def generate_log(count=10):
time_str = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
f = open("/opt/access.log","w+")
while count >= 1:
query_log = "{ip}\t{local_time}\t\"GET /{url} HTTP/1.1 \"\t{referer}\t{status}".format(url=sample_url(), ip=sample_ip(),referer=sample_referer(),status=sample_status(),local_time=time_str)
print(query_log)
f.write(query_log + '\n')
count = count - 1
if __name__ == '__main__':
generate_log()