-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrnd_syslog
More file actions
executable file
·140 lines (102 loc) · 3.76 KB
/
rnd_syslog
File metadata and controls
executable file
·140 lines (102 loc) · 3.76 KB
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
#!/usr/bin/python3
import os
import random
import requests
import re
import time
import MySQLdb
#Debug
#from pprint import pprint
#from code import interact
class rndsyslogEntries():
data_dir = "rndData"
ietf_rfc_link = "https://tools.ietf.org/rfc/rfc{}.txt"
ietf_rfc_min_max = (1, 8450)
def __init__(self, VHostCount):
self.SentenceObjects = []
self.currentIterIdx = 0
random.seed()
try:
os.mkdir(self.data_dir)
except FileExistsError:
pass
self.FileList = [file.path for file in os.scandir(self.data_dir) if file.is_file()]
while(len(self.FileList) < VHostCount):
rfc_id = random.randint(*self.ietf_rfc_min_max)
url = ietf_rfc_link.format(rfc_id)
filename = data_dir + os.sep + "rfc{}.txt".format(rfc_id)
try:
rfc_fd = open(filename, "x")
except FileExistsError:
continue
resp = requests.get(url, headers = {
"Accept": "text/plain",
"Connection": "close" })
rfc_fd.write(resp.text)
self.FileList.append(filename)
for filename in self.FileList:
self.SentenceObjects.append((os.path.basename(filename), rndsyslogEntries.Sentences(filename)))
def __len__(self):
return len(self.SentenceObjects)
def __getitem__(self, idx):
if(idx < 0 or len(self) < idx):
raise IndexError
return (self.SentenceObjects[idx][0], self.SentenceObjects[idx][1].getRndLine())
def __iter__(self):
self.currentIterIdx = 0
return self
def __next__(self):
try:
retTuple = self.__getitem__(self.currentIterIdx)
self.currentIterIdx += 1
except IndexError:
raise StopIteration
return(retTuple)
class Sentences():
def getRndLine(self):
if(len(self.bufferdLines) > 0):
idx = random.randint(0, len(self.bufferdLines))
return(self.bufferdLines[idx])
else:
return("")
def __init__(self, filename):
self.bufferdLines = []
file_fd = open(filename, "r")
while True:
buf = file_fd.readline()
if(len(buf) <= 0):
break
buf = buf.strip()
if len(buf) > 0:
self.bufferdLines.append(buf)
def getValueByChance(Chances, Divergence):
summedChances = sum((Chance[1] for Chance in Chances))
rnd = random.random() * summedChances
for Chance in Chances:
if(Chance[1] > rnd):
break
else:
rnd -= Chance[1]
rnd = random.random() * Divergence - (Divergence/2.0)
return(Chance[0] + Chance[0]*rnd)
if __name__ == "__main__":
db_name = "syslog"
db_user = "syslog"
db_pass = "7f1$GRQ;MMGfhNXyElm"
db_con = MySQLdb.Connect(
user=db_user,
passwd=db_pass,
db=db_name)
db_cur = db_con.cursor()
rndEntries = rndsyslogEntries(6)
WaiterDivergence = 0.2
WaiterChances = [(120, 0.1), (60, 0.3), (12, 0.3), (2, 0.2), (0.5, 0.1)]
LevelChances = [(0, 0.01), (1, 0.04), (2, 0.06), (3, 0.1), (4, 0.4), (5, 0.2), (6, 0.2)]
while True:
wait = getValueByChance(WaiterChances, WaiterDivergence)
time.sleep(wait)
rndData = random.choice(rndEntries)
db_cur.execute("INSERT INTO syslog (timestamp, host, tag, level, msg) VALUES (%s, %s, %s, %s, %s)",
(MySQLdb.Timestamp.now(), rndData[0], "", getValueByChance(LevelChances, 0.0), rndData[1]))
db_con.commit()
#interact(local={**globals(), **locals()})