-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgetLogs.py
More file actions
167 lines (134 loc) · 4.61 KB
/
getLogs.py
File metadata and controls
167 lines (134 loc) · 4.61 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
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
from ftplib import FTP
from stopwatch import Stopwatch
import io
import re
import requests
import mysql.connector
from datetime import datetime
import json
# host and credentials
host = "107.155.124.138"
port = 8821
username = ""
password = ""
# update dynamic dns
requests.get('')
#connect to db
mydb = mysql.connector.connect(
host="",
user="",
password="",
database=""
)
mycursor = mydb.cursor()
# source time of last run top avoid duplicates
if "lastRun" not in locals():
mycursor.execute(f"""SELECT EventDate FROM RunLog WHERE ServiceType = 'LOGS' ORDER BY EventDate DESC LIMIT 1""")
lastRun = mycursor.fetchall()[0][0]
# establing conneciton
timer = Stopwatch()
timer.start()
ftp = FTP()
ftp.connect(host, port)
ftp.login(username, password)
ftp.cwd("/107.155.124.138_7000/")
timer.stop()
print(f"CONNECTING TO FTP: {timer.elapsedTime()} \n")
# regex patterns
loginPattern = re.compile(r"(?P<timestamp>\d{4}.\d{2}.\d\d-\d\d.\d\d.\d\d): '(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}) (?P<steamid>\d*):(?P<playername>.*)\((?P<playerid>\d+)\)' (?P<status>logged in)")
logoutPattern = re.compile(r"(?P<timestamp>\d{4}.\d{2}.\d\d-\d\d.\d\d.\d\d): '(?P<playerid>\d+)' (?P<status>logging out)")
chatPattern = re.compile(r"(?P<timestamp>\d{4}.\d{2}.\d\d-\d\d.\d\d.\d\d): '(?P<steamid>\d*):(?P<playername>.*)\((?P<playerid>\d+)\)' '(?P<room>\w+): (?P<message>.*)'")
deathPattern = re.compile(r"(?P<timestamp>\d{4}.\d{2}.\d\d-\d\d.\d\d.\d\d): Died: (?P<victim>.+) \((?P<victimid>\d+)\), Killer: (?P<killer>.+) \((?P<killerid>\d+)\) Weapon: (?P<weapon>.+) \[\w+\]")
killObjectPattern = re.compile(r"{.+[:,].+}|\[.+[,:].+\]")
# start buffers
adminLog = io.BytesIO()
chatLog = io.BytesIO()
deathLog = io.BytesIO()
loginLog = io.BytesIO()
minesLog = io.BytesIO()
violationsLog = io.BytesIO()
# loop over files in directory
for file in ftp.nlst():
if re.match("admin", file):
ftp.retrbinary('RETR ' + file, adminLog.write)
elif re.match("chat", file):
ftp.retrbinary('RETR ' + file, chatLog.write)
elif re.match("kill", file):
ftp.retrbinary('RETR ' + file, deathLog.write)
elif re.match("login", file):
ftp.retrbinary('RETR ' + file, loginLog.write)
elif re.match("mines", file):
ftp.retrbinary('RETR ' + file, minesLog.write)
elif re.match("violations", file):
ftp.retrbinary('RETR ' + file, violationsLog.write)
# print buffers
for match in re.finditer(loginPattern, loginLog.getvalue().decode('utf-16-le')):
if datetime.strptime(match.group('timestamp'), '%Y.%m.%d-%H.%M.%S') < lastRun:
continue
print(match.groupdict())
mycursor.execute(
f"""INSERT INTO Login (
EventDate,
IP,
SteamID,
PlayerName,
PlayerID
) VALUES (
"{match.group('timestamp')}",
"{match.group('ip')}",
{match.group('steamid')},
"{match.group('playername')}",
{match.group('playerid')}
)"""
)
for match in re.finditer(logoutPattern, loginLog.getvalue().decode('utf-16')):
if datetime.strptime(match.group('timestamp'), '%Y.%m.%d-%H.%M.%S') < lastRun:
continue
print(match.groupdict())
mycursor.execute(
f"""INSERT INTO Logout (
EventDate,
PlayerID
) VALUES (
"{match.group('timestamp')}",
{match.group('playerid')}
)"""
)
for match in re.finditer(chatPattern, chatLog.getvalue().decode('utf-16-le')):
if datetime.strptime(match.group('timestamp'), '%Y.%m.%d-%H.%M.%S') < lastRun:
continue
print(match.groupdict())
for match in re.finditer(deathPattern, deathLog.getvalue().decode('utf-16-le')):
if datetime.strptime(match.group('timestamp'), '%Y.%m.%d-%H.%M.%S') < lastRun:
continue
print(match.groupdict())
mycursor.execute(
f"""INSERT INTO Death (
EventDate,
KillerID,
Killer,
VictimID,
Victim,
Weapon
) VALUES (
"{match.group('timestamp')}",
{match.group('killerid')},
"{match.group('killer')}",
{match.group('victimid')},
"{match.group('victim')}",
"{match.group('weapon')}"
)"""
)
# post update cleanup
lastRun = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
mycursor.execute(f"""UPDATE RunLog SET EventDate = '{lastRun}' WHERE ServiceType = 'LOGS'""")
mydb.commit()
# close buffers
adminLog.close()
chatLog.close()
deathLog.close()
loginLog.close()
minesLog.close()
violationsLog.close()
# close db connection
mydb.close()