-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
58 lines (44 loc) · 1.78 KB
/
main.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
import os.path
import sqlite3
from datetime import *
import summary
LOGS_FROM_DAYS = -2 # Go back X number of days
LOGDIRECTORY = "//DCVM-WEB/c$/inetpub/logs/LogFiles/W3SVC1/"
DTSTRING = datetime.now().strftime("%y%m%d")
CONNECTIONSTRING = os.path.realpath(r"\Users\chollinger\iisparser") + "\\" + DTSTRING + str(datetime.now().second) + ".db"
connection = sqlite3.connect(CONNECTIONSTRING) # TODO holy crap lol this sucks
connection.execute("CREATE TABLE logtable (dt TEXT, uri TEXT, status INTEGER, timereq INTEGER);")
currentdate = datetime.now()
filenames = []
# Create filenames for the log files to be loaded
i = 0
while i > LOGS_FROM_DAYS:
dt = datetime.now() + timedelta(days=i)
filenames.append(os.path.join(LOGDIRECTORY, ("u_ex%s_x.log" % dt.strftime("%y%m%d"))))
i -= 1
del i
filenames.reverse() # Reverse so the oldest logs are displayed last
cursor = connection.cursor()
for filename in filenames:
try:
print("Opening " + filename + "...")
file = open(filename)
lines = file.readlines()
for line in lines:
if line.startswith('#'):
continue
values = line.split(' ')
sqlcommand = ("""INSERT INTO logtable VALUES("%s", "%s", %s, %s);""" %
(values[0] + " " + values[1], values[4], values[11], values[14])).replace('\n', '')
#print(sqlcommand)
cursor.execute(sqlcommand)
summary.total_requests += 1
if int(values[11]) >= 400:
summary.total_errors += 1
except IOError:
print("Could not load log file \"" + filename + "\", skipping.")
except:
print("Could not execute SQL command. Skipping...")
summary.initialize(connection.cursor(), LOGS_FROM_DAYS)
connection.commit()
connection.close()