python生产nginx日志

文章描述了一段Python代码,该脚本用于在生产环境中生成模拟的Nginx访问日志,以测试ELK(Elasticsearch,Logstash,Kibana)框架的部署。脚本包含了随机生成IP、时间戳、请求方法、协议、状态码和用户代理等日志关键信息。
摘要由CSDN通过智能技术生成

python生产nginx日志

最近在生产中使用到了elk框架,为了测试部署编写此脚本生成假数据。

#!/usr/bin/python3.6
# -*- coding:utf-8 -*-
# www.tomlong.cn
# 推荐python3.6

import os
import random
import datetime
import sys
import time

print(sys.platform)
print(sys.argv[0])
# 固定的日志条数
LOG_SUM = 200_000_0
BACK_CODES_LIST = ["200", "302", "304", "400", "403", "404", "502"]
TIME_ZONE_LIST = ["-1200", "-1100", "-1000", "-0900", "-0800", "-0700", "-0600",
                  "-0500", "-0400", "-0300", "-0200", "-0100", "0000", "+0100",
                  "+0200", "+0300", "+0400", "+0500", "+0600", "+0700", "+0800",
                  "+0900", "+1000", "+1100"]
REQUEST_PROTOCOL = ["HTTP/1.0", "HTTP/1.1", "HTTP/2.0", "HTTP/3.0"]
REQUEST_METHOD = ["GET", "POST", "PUT", "HEAD", "DELETE", "OPTIONS", "TRACE"]
USER_AGENTS = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 "
    "Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 "
    "Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"]


def generate_random_ip():
    def get_rand_sum():
        return random.randint(1, 239)

    # 为了方便elk分析ip地址,使用公网ip定位
    # 对内网ip主机位10 192 172 240进行判断排除
    num1 = random.randint(1, 239)  # 仅控制A类网主机位
    while num1 == 10 or num1 == 192 or num1 == 172:
        num1 = get_rand_sum()
    num2 = random.randint(0, 255)
    num3 = random.randint(0, 255)
    num4 = random.randint(0, 254)
    return "{}.{}.{}.{}".format(num1, num2, num3, num4)


# 测试获取当前时间戳
# print(datetime.datetime.now().timestamp())
def get_random_time():
    timestamp = datetime.datetime.now().timestamp() - random.randint(0, 100_000_00)
    # 将时间戳转换为datetime格式的时间
    date_time = datetime.datetime.fromtimestamp(timestamp)
    # 格式化输出date_time [14/Aug/2023:18:18:23 +0800]
    date_time_str = date_time.strftime("%d/%b/%Y:%H:%M:%S")
    return '[' + date_time_str + ']'


# 下部分
 with open('nginx_assess_log.log', 'a') as f:
     flag = 0
     for flag in range(0, LOG_SUM):
         f.write((generate_random_ip() + ' -- ' + get_random_time() + '\"'
                  + random.choice(REQUEST_METHOD) + ' ' + '/' + ' '
                  + random.choice(REQUEST_PROTOCOL) + '"' + " " +
                  random.choice(BACK_CODES_LIST) + " " +
                  random.randint(0, 2000_00).__str__() + ' "-"' + " "
                  + random.choice(USER_AGENTS) + " " + '"-"' + '\n'))

     flag = flag + 1
     if flag == LOG_SUM:
         print(flag.__str__() + " " + "pieces of data were generated in ./")
         f.close()
         sys.exit()
     else:
         print("erorr!!!")
         print("Rollbacking")
         f.close()
         os.remove("./nginx_assess_log.log")
         sys.exit(-1)



# 可以置于后台持续生产日志,将上面模块注释
# with open('nginx_assess_log.log', 'a') as f:
#     while True:
#         f.write((generate_random_ip() + ' -- ' + get_random_time() + '\"'
#                  + random.choice(REQUEST_METHOD) + ' ' + '/' + ' '
#                  + random.choice(REQUEST_PROTOCOL) + '"' + " " +
#                  random.choice(BACK_CODES_LIST) + " " +
#                  random.randint(0, 2000_00).__str__() + ' "-"' + " "
#                  + random.choice(USER_AGENTS) + " " + '"-"' + '\n'))
#         time.sleep(0.5)



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值