| 1 |
package sysloggen |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math/rand" |
| 6 |
"os" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/juju/errors" |
| 10 |
) |
| 11 |
|
| 12 |
var facilities = []string{ |
| 13 |
"kern", "user", "mail", "daemon", "auth", "syslog", |
| 14 |
"lpr", "news", "uucp", "cron", "authpriv", "ftp", |
| 15 |
} |
| 16 |
|
| 17 |
var severities = []string{ |
| 18 |
"emerg", "alert", "crit", "err", "warning", "notice", "info", "debug", |
| 19 |
} |
| 20 |
|
| 21 |
var messages = []string{ |
| 22 |
"Connection established", |
| 23 |
"User login successful", |
| 24 |
"Disk space low", |
| 25 |
"System rebooted", |
| 26 |
"Configuration updated", |
| 27 |
"Unauthorized access attempt", |
| 28 |
"Service started", |
| 29 |
"File not found", |
| 30 |
"Network interface down", |
| 31 |
"Memory usage high", |
| 32 |
"Process terminated", |
| 33 |
"CPU temperature critical", |
| 34 |
"New device connected", |
| 35 |
"Package installation completed", |
| 36 |
"Database query failed", |
| 37 |
"Service stopped", |
| 38 |
"Error reading file", |
| 39 |
"Permission denied", |
| 40 |
"Disk write error", |
| 41 |
"Failed login attempt", |
| 42 |
"Configuration load failed", |
| 43 |
"Authentication failure", |
| 44 |
"Server shutting down", |
| 45 |
"Session expired", |
| 46 |
"Process started", |
| 47 |
"Timeout occurred", |
| 48 |
"Network unreachable", |
| 49 |
"Port unreachable", |
| 50 |
"Insufficient privileges", |
| 51 |
"DNS resolution failed", |
| 52 |
"Database connection error", |
| 53 |
"Configuration reload successful", |
| 54 |
"File system full", |
| 55 |
"System performance degraded", |
| 56 |
"Kernel panic", |
| 57 |
"Backup completed", |
| 58 |
"Backup failed", |
| 59 |
"File copied successfully", |
| 60 |
"Process crashed", |
| 61 |
"Memory leak detected", |
| 62 |
"New update available", |
| 63 |
"Update failed", |
| 64 |
"Service restart requested", |
| 65 |
"Database schema updated", |
| 66 |
"Invalid input detected", |
| 67 |
"Request timed out", |
| 68 |
"Resource allocation failed", |
| 69 |
"Firewall rule added", |
| 70 |
"Firewall rule deleted", |
| 71 |
"User session started", |
| 72 |
"User session ended", |
| 73 |
"Disk format completed", |
| 74 |
"SSH connection established", |
| 75 |
"SSH connection closed", |
| 76 |
"Unexpected error occurred", |
| 77 |
"Request successfully processed", |
| 78 |
"Service restart completed", |
| 79 |
"Cache cleared", |
| 80 |
"Cache update completed", |
| 81 |
"Configuration applied successfully", |
| 82 |
"Service health check failed", |
| 83 |
"System running low on resources", |
| 84 |
"File system check completed", |
| 85 |
"Software version updated", |
| 86 |
"Memory usage normal", |
| 87 |
"Disk usage critical", |
| 88 |
"Network congestion detected", |
| 89 |
"Network speed reduced", |
| 90 |
"Service unavailable", |
| 91 |
"Hardware failure detected", |
| 92 |
"Service dependency failure", |
| 93 |
"System time updated", |
| 94 |
"Invalid credentials provided", |
| 95 |
"Login attempt locked out", |
| 96 |
"User account disabled", |
| 97 |
"User account enabled", |
| 98 |
"Service request queued", |
| 99 |
"Service request completed", |
| 100 |
"User permissions updated", |
| 101 |
"Certificate expiration warning", |
| 102 |
"Application crash reported", |
| 103 |
"Security patch applied", |
| 104 |
"Backup restoration completed", |
| 105 |
"Maintenance mode enabled", |
| 106 |
"Maintenance mode disabled", |
| 107 |
"Security alert raised", |
| 108 |
"Security breach detected", |
| 109 |
"Resource utilization warning", |
| 110 |
"High CPU usage detected", |
| 111 |
"High memory usage detected", |
| 112 |
"Disk error occurred", |
| 113 |
"Data corruption detected", |
| 114 |
"API request failed", |
| 115 |
"API response received", |
| 116 |
"File checksum mismatch", |
| 117 |
"Hardware upgrade completed", |
| 118 |
"System configuration backed up", |
| 119 |
"User session timed out", |
| 120 |
"System configuration restored", |
| 121 |
"Service initialization failed", |
| 122 |
"Server started successfully", |
| 123 |
"Server stopped unexpectedly", |
| 124 |
"Logging level changed", |
| 125 |
"Error handling request", |
| 126 |
"Out of memory error", |
| 127 |
"Service dependency initialized", |
| 128 |
"Scheduled task executed", |
| 129 |
"Scheduled task failed", |
| 130 |
"User authentication successful", |
| 131 |
"User authentication failed", |
| 132 |
"Session token expired", |
| 133 |
"File upload completed", |
| 134 |
"File download started", |
| 135 |
"File upload failed", |
| 136 |
"File download failed", |
| 137 |
"File transfer completed", |
| 138 |
"File transfer failed", |
| 139 |
"System clock synchronized", |
| 140 |
"System time drift detected", |
| 141 |
"Software upgrade completed", |
| 142 |
"System reboot required", |
| 143 |
"Disk space reclaimed", |
| 144 |
"IP address conflict detected", |
| 145 |
"Application configuration error", |
| 146 |
"System health check completed", |
| 147 |
"System health check failed", |
| 148 |
"Log file rotated", |
| 149 |
"Log file archived", |
| 150 |
"User password changed", |
| 151 |
"Invalid password attempt", |
| 152 |
"SMTP server connection error", |
| 153 |
"Database migration completed", |
| 154 |
"Database migration failed", |
| 155 |
"Network link restored", |
| 156 |
"Network interface reset", |
| 157 |
} |
| 158 |
|
| 159 |
func randomElement(list []string) string { |
| 160 |
return list[rand.Intn(len(list))] |
| 161 |
} |
| 162 |
|
| 163 |
func generateSyslogEntry(ts time.Time, layout string, parts SyslogParts) string { |
| 164 |
timestamp := ts.Format(layout) |
| 165 |
hostname := "myhost" |
| 166 |
|
| 167 |
if parts.Tag == "" { |
| 168 |
parts.Tag = randomElement(facilities) |
| 169 |
} |
| 170 |
|
| 171 |
if parts.Severity == "" { |
| 172 |
parts.Severity = randomElement(severities) |
| 173 |
} |
| 174 |
|
| 175 |
if parts.Pid == 0 { |
| 176 |
parts.Pid = rand.Intn(9000) + 100 |
| 177 |
} |
| 178 |
|
| 179 |
if parts.Message == "" { |
| 180 |
parts.Message = randomElement(messages) |
| 181 |
} |
| 182 |
|
| 183 |
return fmt.Sprintf( |
| 184 |
"%s %s %s[%d]: <%s> %s", |
| 185 |
timestamp, hostname, parts.Tag, parts.Pid, parts.Severity, parts.Message, |
| 186 |
) |
| 187 |
} |
| 188 |
|
| 189 |
func randomStep(params *DelayCfg, lastDelay, curDelayDelta time.Duration) time.Duration { |
| 190 |
min := -100 * int64(time.Millisecond) |
| 191 |
max := 100 * int64(time.Millisecond) |
| 192 |
curDelayDelta += time.Duration(rand.Int63n(max-min) + min) |
| 193 |
//curDelayDelta += -time.Duration(rand.Int63n(max)) |
| 194 |
//_ = min |
| 195 |
|
| 196 |
ret := lastDelay + curDelayDelta |
| 197 |
if ret < time.Duration(params.MinDelayMS)*time.Millisecond { |
| 198 |
ret = time.Duration(params.MinDelayMS) * time.Millisecond |
| 199 |
} |
| 200 |
if ret > time.Duration(params.MaxDelayMS)*time.Millisecond { |
| 201 |
//ret = time.Duration(params.MaxDelayMS) * time.Millisecond |
| 202 |
} |
| 203 |
|
| 204 |
return ret |
| 205 |
|
| 206 |
//min := int64(params.MinDelayMS) * int64(time.Millisecond) |
| 207 |
//max := int64(params.MaxDelayMS) * int64(time.Millisecond) |
| 208 |
//return time.Duration(rand.Int63n(max-min) + min) |
| 209 |
} |
| 210 |
|
| 211 |
type Params struct { |
| 212 |
// If TimeLayout is empty, "Jan _2 15:04:05" will be used. |
| 213 |
TimeLayout string |
| 214 |
|
| 215 |
StartTime time.Time |
| 216 |
SecondLogTime time.Time |
| 217 |
|
| 218 |
// LogBasename is a name like "mylog", can be "/path/to/mylog" as well. |
| 219 |
// The first (older) log will have the ".1" appended to it. |
| 220 |
LogBasename string |
| 221 |
|
| 222 |
// NumLogs specifies the max amount of logs to generate. If 0, we never stop |
| 223 |
// generating logs, and once caught up with the current time (time.Now()), it |
| 224 |
// switches to the real-time mode and continues there forever, waiting for |
| 225 |
// appropriate durations before printing every line. |
| 226 |
NumLogs int |
| 227 |
|
| 228 |
MinDelayMS int |
| 229 |
MaxDelayMS int |
| 230 |
|
| 231 |
RandomSeed int |
| 232 |
|
| 233 |
// SkipIfPrevLogSizeIs and SkipIfLastLogSizeIs are an optimization: |
| 234 |
// if the log files already exist and are of these exact sizes, don't |
| 235 |
// generate anything. |
| 236 |
SkipIfPrevLogSizeIs int64 |
| 237 |
SkipIfLastLogSizeIs int64 |
| 238 |
|
| 239 |
Spikes []Spike |
| 240 |
} |
| 241 |
|
| 242 |
type DelayCfg struct { |
| 243 |
MinDelayMS int |
| 244 |
MaxDelayMS int |
| 245 |
} |
| 246 |
|
| 247 |
type SpikePhase struct { |
| 248 |
// If EndTime is zero, the phase will never end |
| 249 |
EndTime time.Time |
| 250 |
|
| 251 |
MinDelayMS int |
| 252 |
MaxDelayMS int |
| 253 |
|
| 254 |
Trend func(phasePercentage float64, minDelayMS, maxDelayMS int) DelayCfg |
| 255 |
} |
| 256 |
|
| 257 |
type SyslogParts struct { |
| 258 |
Tag string |
| 259 |
Severity string |
| 260 |
Pid int |
| 261 |
Message string |
| 262 |
} |
| 263 |
|
| 264 |
type Spike struct { |
| 265 |
StartTime time.Time |
| 266 |
// All of SyslogParts fields are optional; what is not specified, will be random |
| 267 |
SyslogParts SyslogParts |
| 268 |
Phases []SpikePhase |
| 269 |
} |
| 270 |
|
| 271 |
type streamCtx struct { |
| 272 |
spikeCfg Spike |
| 273 |
|
| 274 |
lastDelay time.Duration |
| 275 |
curDelayDelta time.Duration |
| 276 |
|
| 277 |
nextMsgTime time.Time |
| 278 |
} |
| 279 |
|
| 280 |
func GenerateSyslog(params Params) error { |
| 281 |
if params.TimeLayout == "" { |
| 282 |
params.TimeLayout = "Jan _2 15:04:05" |
| 283 |
} |
| 284 |
|
| 285 |
sCtxs := []*streamCtx{ |
| 286 |
// Main stream |
| 287 |
&streamCtx{ |
| 288 |
spikeCfg: Spike{ |
| 289 |
Phases: []SpikePhase{ |
| 290 |
SpikePhase{ |
| 291 |
MinDelayMS: params.MinDelayMS, |
| 292 |
MaxDelayMS: params.MaxDelayMS, |
| 293 |
}, |
| 294 |
}, |
| 295 |
}, |
| 296 |
}, |
| 297 |
} |
| 298 |
|
| 299 |
for _, spike := range params.Spikes { |
| 300 |
sCtxs = append(sCtxs, &streamCtx{ |
| 301 |
spikeCfg: spike, |
| 302 |
}) |
| 303 |
} |
| 304 |
|
| 305 |
type nextDelayAndMsg struct { |
| 306 |
time time.Time |
| 307 |
syslogParts SyslogParts |
| 308 |
} |
| 309 |
|
| 310 |
getNextDelayAndMsg := func(curTime time.Time) nextDelayAndMsg { |
| 311 |
// Generate nextMsgTime for all active spikes |
| 312 |
for _, sc := range sCtxs { |
| 313 |
if sc.nextMsgTime.IsZero() { |
| 314 |
if curTime.Before(sc.spikeCfg.StartTime) { |
| 315 |
continue |
| 316 |
} |
| 317 |
|
| 318 |
phaseStartTime := sc.spikeCfg.StartTime |
| 319 |
|
| 320 |
if len(sc.spikeCfg.Phases) == 0 { |
| 321 |
panic("no phases") |
| 322 |
} |
| 323 |
var d *DelayCfg |
| 324 |
for _, phase := range sc.spikeCfg.Phases { |
| 325 |
if phase.EndTime.IsZero() || curTime.Before(phase.EndTime) { |
| 326 |
// Found the phase |
| 327 |
percentage := 0.0 |
| 328 |
if !phase.EndTime.IsZero() { |
| 329 |
totalDur := phase.EndTime.Sub(phaseStartTime) |
| 330 |
elapsedDur := curTime.Sub(phaseStartTime) |
| 331 |
percentage = float64(elapsedDur) / float64(totalDur) * 100.0 |
| 332 |
} |
| 333 |
|
| 334 |
var curDelayCfg DelayCfg |
| 335 |
if phase.Trend != nil { |
| 336 |
curDelayCfg = phase.Trend(percentage, phase.MinDelayMS, phase.MaxDelayMS) |
| 337 |
} else { |
| 338 |
curDelayCfg = DelayCfg{ |
| 339 |
MinDelayMS: phase.MinDelayMS, |
| 340 |
MaxDelayMS: phase.MaxDelayMS, |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
d = &curDelayCfg |
| 345 |
break |
| 346 |
} |
| 347 |
|
| 348 |
phaseStartTime = phase.EndTime |
| 349 |
} |
| 350 |
|
| 351 |
if d == nil { |
| 352 |
// the spike is over (all phases of it are over) |
| 353 |
continue |
| 354 |
} |
| 355 |
|
| 356 |
if sc.lastDelay == 0 { |
| 357 |
sc.lastDelay = time.Duration(d.MinDelayMS + (d.MaxDelayMS-d.MinDelayMS)/2) |
| 358 |
} |
| 359 |
|
| 360 |
dur := randomStep(d, sc.lastDelay, sc.curDelayDelta) |
| 361 |
newDelayDelta := sc.lastDelay - dur |
| 362 |
|
| 363 |
sc.curDelayDelta = newDelayDelta |
| 364 |
sc.lastDelay = dur |
| 365 |
sc.nextMsgTime = curTime.Add(dur) |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
var earliestTime *time.Time |
| 370 |
var syslogParts *SyslogParts |
| 371 |
// Find the earliest time |
| 372 |
for _, sc := range sCtxs { |
| 373 |
if sc.nextMsgTime.IsZero() { |
| 374 |
continue |
| 375 |
} |
| 376 |
|
| 377 |
if earliestTime == nil || sc.nextMsgTime.Before(*earliestTime) { |
| 378 |
tmpTime := sc.nextMsgTime |
| 379 |
tmpParts := sc.spikeCfg.SyslogParts |
| 380 |
|
| 381 |
earliestTime = &tmpTime |
| 382 |
syslogParts = &tmpParts |
| 383 |
|
| 384 |
// Reset it so we'll generate it again next time |
| 385 |
sc.nextMsgTime = time.Time{} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
if earliestTime == nil { |
| 390 |
panic("earliestTime must not be nil at this point, since we have the 'main' spike") |
| 391 |
} |
| 392 |
|
| 393 |
return nextDelayAndMsg{ |
| 394 |
time: *earliestTime, |
| 395 |
syslogParts: *syslogParts, |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
rand.Seed(int64(params.RandomSeed)) |
| 400 |
|
| 401 |
curTime := params.StartTime |
| 402 |
|
| 403 |
prevlogFname := params.LogBasename + ".1" |
| 404 |
lastlogFname := params.LogBasename |
| 405 |
|
| 406 |
if params.SkipIfPrevLogSizeIs != 0 && params.SkipIfLastLogSizeIs != 0 { |
| 407 |
var prevlogSize int64 |
| 408 |
var lastlogSize int64 |
| 409 |
|
| 410 |
prevlogStat, err := os.Stat(prevlogFname) |
| 411 |
if err == nil { |
| 412 |
prevlogSize = prevlogStat.Size() |
| 413 |
} |
| 414 |
|
| 415 |
lastlogStat, err := os.Stat(lastlogFname) |
| 416 |
if err == nil { |
| 417 |
lastlogSize = lastlogStat.Size() |
| 418 |
} |
| 419 |
|
| 420 |
if params.SkipIfPrevLogSizeIs == prevlogSize && |
| 421 |
params.SkipIfLastLogSizeIs == lastlogSize { |
| 422 |
// Nothing to do |
| 423 |
//fmt.Println("Log files already exist and are of expected sizes, skipping generation") |
| 424 |
return nil |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
fmt.Printf("Creating first log file %s\n", prevlogFname) |
| 429 |
|
| 430 |
file, err := os.Create(prevlogFname) |
| 431 |
if err != nil { |
| 432 |
return errors.Annotatef(err, "creating first log file") |
| 433 |
} |
| 434 |
defer file.Close() |
| 435 |
|
| 436 |
numLogFile := 0 |
| 437 |
numLogsWritten := 0 |
| 438 |
|
| 439 |
isRealtime := false |
| 440 |
|
| 441 |
for { |
| 442 |
next := getNextDelayAndMsg(curTime) |
| 443 |
if curTime.After(next.time) { |
| 444 |
panic(fmt.Sprintf("curTime %v can't be after next.time %v", curTime, next.time)) |
| 445 |
} |
| 446 |
|
| 447 |
step := next.time.Sub(curTime) |
| 448 |
curTime = curTime.Add(step) |
| 449 |
|
| 450 |
now := time.Now() |
| 451 |
if params.NumLogs > 0 && numLogsWritten >= params.NumLogs { |
| 452 |
break |
| 453 |
} |
| 454 |
|
| 455 |
// Real-time transition: we've caught up to current time |
| 456 |
if params.NumLogs == 0 && curTime.After(now) { |
| 457 |
if !isRealtime { |
| 458 |
fmt.Printf("Switching to realtime mode\n") |
| 459 |
isRealtime = true |
| 460 |
} |
| 461 |
|
| 462 |
time.Sleep(step) |
| 463 |
curTime = time.Now() |
| 464 |
} |
| 465 |
|
| 466 |
if !curTime.Before(params.SecondLogTime) && numLogFile == 0 { |
| 467 |
numLogFile += 1 |
| 468 |
file.Close() |
| 469 |
|
| 470 |
fmt.Printf("Switching to the next log file %s\n", lastlogFname) |
| 471 |
|
| 472 |
var err error |
| 473 |
file, err = os.Create(lastlogFname) |
| 474 |
if err != nil { |
| 475 |
return errors.Annotatef(err, "creating second log file") |
| 476 |
} |
| 477 |
defer file.Close() |
| 478 |
} |
| 479 |
|
| 480 |
logEntry := generateSyslogEntry(curTime, params.TimeLayout, next.syslogParts) |
| 481 |
_, err := file.WriteString(logEntry + "\n") |
| 482 |
if err != nil { |
| 483 |
return errors.Annotatef(err, "writing to log file") |
| 484 |
} |
| 485 |
|
| 486 |
numLogsWritten++ |
| 487 |
} |
| 488 |
|
| 489 |
return nil |
| 490 |
} |