forked from bitsofinfo/log-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_generator.py
executable file
·159 lines (123 loc) · 4.26 KB
/
log_generator.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
#!/usr/bin/env python
#################################
# log-generator
#
# - small python script to generate random time series data into files
# - https://github.com/bitsofinfo/log-generator
# - uses GMT time
#
#################################
import logging
import getopt
import sys
import time
import random
import os
def main(argv):
usageInfo = '\nUSAGE:\n\nlogGenerator.py --logFile <targetFile>\n\t[--minSleepMs <int>] [--maxSleepMs <int>] \n\t[--sourceDataFile <fileWithTextData>] [--iterations <long>]\n\t[--minLines <int>] [--maxLines <int>] \n\t[--logPattern <pattern>] [--datePattern <pattern>]'
iterations = -1 # infinate
minSleep = 0.1
maxSleep = 1
minLines = 1
maxLines = 1
logFile = 'logGenerator.log'
sourceDataFile = 'defaultDataFile.txt'
sourceData = ''
logPattern = ''
datePattern = "%Y-%m-%d %H:%M:%S"
if len(argv) == 0:
print(usageInfo)
sys.exit(2)
try:
opts, args = getopt.getopt(argv,"h",["help","logFile=","minSleepMs=","maxSleepMs=","iterations=","sourceDataFile=","minLines=","maxLines="])
except:
print(usageInfo)
sys.exit(2)
for opt, arg in opts:
if opt in ('-h' , "--help"):
print(usageInfo)
sys.exit()
elif opt in ("--logFile"):
logFile = arg
elif opt in ("--minSleepMs"):
minSleep = (0.001 * float(arg))
elif opt in ("--maxSleepMs"):
maxSleep = (0.001 * float(arg))
elif opt in ("--maxLines"):
maxLines = int(arg)
elif opt in ("--minLines"):
minLines = int(arg)
elif opt in ("--sourceDataFile"):
sourceDataFile = arg
elif opt in ("--iterations"):
iterations = int(arg)
elif opt in ("--logPattern"):
logPattern = arg
elif opt in ("--datePattern"):
datePattern = arg
#check if sourcefile exists
if os.path.exists(sourceDataFile):
pass
else:
print("Please check if file " + sourceDataFile + " exists")
sys.exit()
# bring in source data
with open (sourceDataFile, "r") as fh:
sourceData=fh.read()
sourceData = sourceData.splitlines(True)
totalLines = len(sourceData)-1
if (maxLines > totalLines):
maxLines = totalLines
print("")
print("########################################")
print("### log-generator running variables: ###")
print("########################################")
print("# ")
print("# sourceDataFile: | " + sourceDataFile)
print("# sourceData lines: | " + str(totalLines))
print("# ")
print("# minSleep: | " + str(minSleep))
print("# maxSleep: | " + str(maxSleep))
print("# minLines: | " + str(minLines))
print("# maxLines: | " + str(maxLines))
print("# ")
print("# logFile: | " + logFile)
print("# logPattern: | " + logPattern)
print("# datePattern: | " + datePattern)
print("########################################")
print("")
# setup logging
logging.Formatter.converter = time.gmtime
logger = logging.getLogger("log-generator")
logger.setLevel(logging.DEBUG)
fileHandler = logging.FileHandler(logFile)
fileHandler.setLevel(logging.DEBUG)
formatter = logging.Formatter(logPattern,datePattern)
fileHandler.setFormatter(formatter)
logger.addHandler(fileHandler)
lineToStart = 0
mustIterate = True
while (mustIterate):
# sleep
time.sleep(random.uniform(minSleep, maxSleep))
# get random data
# lineToStart = random.randint(0,totalLines)
linesToGet = random.randint(minLines, maxLines)
lastLineToGet = (lineToStart + linesToGet)
if (lastLineToGet > totalLines):
lastLineToGet = totalLines
toLog = ''.join(sourceData[lineToStart:lastLineToGet])
if (toLog.startswith('\n')):
toLog = toLog[1:]
if (toLog == ''):
continue
logger.debug(toLog[:-1])
print(toLog[:-1])
if (iterations > 0):
iterations = iterations - 1
if (iterations == 0):
mustIterate = False
lineToStart += 1
sys.exit(0)
if __name__ == "__main__":
main(sys.argv[1:])