forked from kscarlett/nginx-log-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
179 lines (149 loc) · 4.54 KB
/
main.go
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
package main
import (
"fmt"
"log"
"math/rand"
"os"
"strings"
"time"
"github.com/brianvoe/gofakeit/v6"
"github.com/caarlos0/env/v6"
)
type config struct {
Rate float32 `env:"RATE" envDefault:"1"`
IPv4Percent int `env:"IPV4_PERCENT" envDefault:"100"`
SameIpAddresses int `env:"SAME_IP_ADDRESSES" envDefault:"0"`
STDOUT string `env:"STDOUT" envDefault:"terminal"`
TruncateFile int `env:"TRUNCATE_FILE" envDefault:"0"`
StatusOkPercent int `env:"STATUS_OK_PERCENT" envDefault:"80"`
PathMinLength int `env:"PATH_MIN" envDefault:"1"`
PathMaxLength int `env:"PATH_MAX" envDefault:"5"`
PercentageGet int `env:"GET_PERCENT" envDefault:"60"`
PercentagePost int `env:"POST_PERCENT" envDefault:"30"`
PercentagePut int `env:"PUT_PERCENT" envDefault:"0"`
PercentagePatch int `env:"PATCH_PERCENT" envDefault:"0"`
PercentageDelete int `env:"DELETE_PERCENT" envDefault:"0"`
}
func main() {
cfg := config{}
if err := env.Parse(&cfg); err != nil {
panic(err)
}
checkMinMax(&cfg.PathMinLength, &cfg.PathMaxLength)
ticker := time.NewTicker(time.Second / time.Duration(cfg.Rate))
gofakeit.Seed(time.Now().UnixNano())
var ip, httpMethod, path, httpVersion, referrer, userAgent string
var statusCode, bodyBytesSent int
var timeLocal time.Time
httpVersion = "HTTP/1.1"
referrer = "-"
sameIpAddresses := []string{}
if cfg.SameIpAddresses > 0 {
for i := 0; i <= cfg.SameIpAddresses; i++ {
sameIpAddresses = append(sameIpAddresses, weightedIPVersion(cfg.IPv4Percent))
}
}
var (
fileFlags int
file *os.File
err error
)
if cfg.STDOUT != "terminal" {
fileFlags = os.O_WRONLY | os.O_CREATE
if cfg.TruncateFile == 1 {
fmt.Println("NGINX LOG GENERATOR [INFO]: open file with truncate")
fileFlags += os.O_TRUNC
} else {
fileFlags += os.O_APPEND
}
file, err = os.OpenFile(cfg.STDOUT, fileFlags, 0644)
if err != nil {
log.Fatal(err)
}
defer file.Close()
fmt.Printf("NGINX LOG GENERATOR [INFO]: started writing to %s\n", cfg.STDOUT)
}
for range ticker.C {
timeLocal = time.Now()
if len(sameIpAddresses) == 0 {
ip = weightedIPVersion(cfg.IPv4Percent)
} else {
ip = sameIpAddresses[rand.Intn(len(sameIpAddresses))]
}
httpMethod = weightedHTTPMethod(cfg.PercentageGet, cfg.PercentagePost, cfg.PercentagePut, cfg.PercentagePatch, cfg.PercentageDelete)
path = randomPath(cfg.PathMinLength, cfg.PathMaxLength)
statusCode = weightedStatusCode(cfg.StatusOkPercent)
bodyBytesSent = realisticBytesSent(statusCode)
userAgent = gofakeit.UserAgent()
logLine := fmt.Sprintf("%s - - [%s] \"%s %s %s\" %v %v \"%s\" \"%s\"\n", ip, timeLocal.Format("02/Jan/2006:15:04:05 -0700"), httpMethod, path, httpVersion, statusCode, bodyBytesSent, referrer, userAgent)
if cfg.STDOUT == "terminal" {
fmt.Print(logLine)
} else {
file.WriteString(logLine)
}
}
}
func realisticBytesSent(statusCode int) int {
if statusCode != 200 {
return gofakeit.Number(30, 120)
}
return gofakeit.Number(800, 3100)
}
func weightedStatusCode(percentageOk int) int {
roll := gofakeit.Number(0, 100)
if roll <= percentageOk {
return 200
}
return gofakeit.HTTPStatusCodeSimple()
}
func weightedHTTPMethod(percentageGet, percentagePost, percentagePut, percentagePatch, percentageDelete int) string {
if percentageGet+percentagePost >= 100 {
panic("HTTP method percentages add up to more than 100%")
}
roll := gofakeit.Number(0, 100)
if roll <= percentageGet {
return "GET"
} else if roll <= percentagePost {
return "POST"
} else if roll <= percentagePut {
return "PUT"
} else if roll <= percentagePatch {
return "PATCH"
} else if roll <= percentageDelete {
return "DELETE"
}
return gofakeit.HTTPMethod()
}
func weightedIPVersion(percentageIPv4 int) string {
roll := gofakeit.Number(0, 100)
if roll <= percentageIPv4 {
return gofakeit.IPv4Address()
} else {
return gofakeit.IPv6Address()
}
}
func randomPath(min, max int) string {
var path strings.Builder
length := gofakeit.Number(min, max)
path.WriteString("/")
for i := 0; i < length; i++ {
if i > 0 {
path.WriteString(gofakeit.RandomString([]string{"-", "-", "_", "%20", "/", "/", "/"}))
}
path.WriteString(gofakeit.BuzzWord())
}
path.WriteString(gofakeit.RandomString([]string{".hmtl", ".php", ".htm", ".jpg", ".png", ".gif", ".svg", ".css", ".js"}))
result := path.String()
return strings.Replace(result, " ", "%20", -1)
}
func checkMinMax(min, max *int) {
if *min < 1 {
*min = 1
}
if *max < 1 {
*max = 1
}
if *min > *max {
*min, *max = *max, *min
}
}