-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess_log-en.py
More file actions
170 lines (145 loc) · 5.32 KB
/
access_log-en.py
File metadata and controls
170 lines (145 loc) · 5.32 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
168
169
170
import os
from pg import DB
import sys
import time
def conectar_db():
try:
banco_nome = input('Enter the name of the database that will be used: ')
host = input('Enter the host ip, if it is local enter "localhost": ')
porta = int(input('Enter the port "default port is 5432": '))
usuario = input('Enter the user: ')
senha = input('Enter the password: ')
db = DB(dbname=banco_nome, host=host,
port=porta, user=usuario, passwd=senha)
print('Connected!')
input('Press any key to continue ...')
os.system('cls')
return db
except KeyboardInterrupt:
print('Parando...')
sys.exit()
except Exception as e:
os.system('cls')
print('The described error was: ', e)
print('Let\'s try again...')
input('Press any key to continue ...')
conectar_db()
def criar_tabelas(banco):
try:
print('Creating tables')
banco.query("""CREATE TABLE public.access_log (
id serial,
ip VARCHAR(70) NOT NULL,
identify_check VARCHAR(50) NOT NULL,
userid VARCHAR(30) NOT NULL,
horario TIMESTAMP NOT NULL,
zona_h VARCHAR NOT NULL,
request VARCHAR NOT NULL,
status_code VARCHAR(5) NOT NULL,
size_object VARCHAR NOT NULL,
CONSTRAINT access_log_pk PRIMARY KEY (id)
)""")
print('Tables created!')
except Exception:
print('Make sure the tables are already created. If not, check your database.')
def inserir_dados(db, ip, id_check, user_id, horar, zona_h, com, status_code, size_object):
db.insert('access_log', ip=ip, identify_check=id_check,
userid=user_id, horario=horar, zona_h=zona_h, request=com,
status_code=status_code, size_object=size_object)
def arq_controle(arq):
if not os.path.exists(arq):
if 'control_accesslog' == arq:
file = open(arq, 'w')
file.write('Last accessed logs\n\n')
elif 'logs_error' == arq:
file = open(arq, 'w')
file.write('Latest logs with error\n\n')
try:
global cont
cont = file.read()
except IOError:
arq_controle(arq)
file.close()
return cont
else:
file = open(arq)
cont = file.read()
file.close()
return cont
def esc_controle(f_log, arq):
file = open(arq, 'a')
file.write(f_log + '\n')
file.close()
def acessarAccessLog(banco, logs):
with open(logs) as access:
arq = arq_controle('control_accesslog')
access = reversed(list(access))
j = 0
for i in access:
if i not in arq:
try:
i = i.split()
ip = i[0]
id_check = i[1]
user_id = i[2]
horar = i[3]
zona_h = i[4]
com = i[5] + ' ' + i[6] + ' ' + i[7]
status_code = i[8]
size_object = i[9]
inserir_dados(banco, ip, id_check, user_id,
horar, zona_h[0:5], com, status_code, size_object)
except IndexError:
esc_controle(' '.join(i), 'logs_error')
print('Some logs have errors to insert.')
print('Check in logs_error.')
else:
return i
if j == 0:
esc_controle(' '.join(i), 'control_accesslog')
j = 1
try:
print('Creating program\'s control structure...')
arq_controle('control_accesslog')
arq_controle('logs_error')
print('Make sure you have a database created to receive the logs!')
print('Before let\'s connect in the database...\n\n')
banco = conectar_db()
logs = input('Enter the path of access_log: ')
while True:
print('1 - Create tables')
print('2 - Search for data and insert in database')
print('3 - Leave him searching for logs every 30s')
print('0 - Quit')
opcao = input('\nEnter your option:')
if opcao == '1':
criar_tabelas(banco)
input('Press any key to continue ...')
os.system('cls')
elif opcao == '2':
acessarAccessLog(banco, logs)
print('Logs inserted!')
input('Press any key to continue ...')
os.system('cls')
elif opcao == '3':
try:
seg = int(input(
'Enter the seconds for the program to search logs: '))
except ValueError:
print('Enter in numbers. Let\'s try again.\n\n\n')
print('To stop use CTRL+C')
while True:
try:
acessarAccessLog(banco, logs)
time.sleep(seg)
except KeyboardInterrupt:
print('Stopping....\n')
input('Press any key to continue ...')
os.system('cls')
break
elif opcao == '0':
sys.exit('Closing program.')
else:
print('Enter one of the options.')
except KeyboardInterrupt:
print('Closing program.')