-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript_auditoria_v2.4.py
executable file
·319 lines (291 loc) · 9.64 KB
/
script_auditoria_v2.4.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/python
import psycopg2
########################## Explicacion #################################
# Este script crea una base datos de replica de otra, para usarla como
# BD de auditoria. Este script genera un archivo "auditoria.sql"
### Datos de Configuracion de Conexion de la Base de Datos Fuente ###
# RECUERDE MODIFICAR LOS PARAMETROS DE CONEXION
database='f2015-des' #cambiar por el que corresponda
host='localhost'
user='postgres'
password='123456'
###################################################
#### Datos de conexion a bases de datos destino (auditoria) (INCOMPLETO)
#dbname_destino = 'aud_%s' % (database)
#host_destino = 'localhost'
user_destino = 'auditoria' #este rol debe estar creado
password_destino = '4ud1t0r14'
########################################################################
listbs = ['base_language_export',
'base_language_import',
'base_language_install',
'base_module_configuration',
'base_module_import',
'base_module_update',
'base_module_upgrade',
'base_setup_terminology',
'base_update_translations',
'board_board',
'board_board_line',
'board_menu_create',
'ir_act_client',
'ir_actions',
'ir_actions_configuration_wizard',
'ir_actions_todo',
'ir_actions_todo_category',
'ir_act_report_custom',
'ir_act_report_xml',
'ir_act_server',
'ir_act_url',
'ir_act_window',
'ir_act_window_group_rel',
'ir_act_window_view',
'ir_act_wizard',
'ir_attachment',
'ir_config_parameter',
'ir_cron',
'ir_default',
'ir_exports',
'ir_exports_line',
'ir_filters',
'ir_mail_server',
'ir_model',
'ir_model_access',
'ir_model_data',
'ir_model_fields',
'ir_model_fields_group_rel',
'ir_module_category',
'ir_module_module',
'ir_module_module_dependency',
'ir_property',
'ir_rule',
'ir_sequence',
'ir_sequence_type',
'ir_server_object_lines',
'ir_translation',
'ir_ui_menu',
'ir_ui_menu_group_rel',
'ir_ui_view',
'ir_ui_view_custom',
'ir_ui_view_sc',
'ir_values',
'ir_wizard_screen',
'maintenance_contract',
'migrade_application_installer_modules',
'multi_company_default',
'osv_memory_autovacuum',
'partner_clear_ids',
'partner_massmail_wizard',
'partner_sms_send',
'partner_wizard_ean_check',
'product_installer',
'publisher_warranty_contract',
'publisher_warranty_contract_wizard',
'rel_modules_langexport',
'rel_server_actions',
'res_bank',
'res_company',
'res_company_users_rel',
'res_config',
'res_config_installer',
'res_country',
'res_country_state',
'res_currency',
'res_currency_rate',
'res_currency_rate_type',
'res_groups',
'res_groups_action_rel',
'res_groups_implied_rel',
'res_groups_report_rel',
'res_groups_users_rel',
'res_groups_wizard_rel',
'res_lang',
'res_log',
'res_log_report',
'res_partner',
'res_partner_address',
'res_partner_bank',
'res_partner_bank_type',
'res_partner_bank_type_field',
'res_partner_category',
'res_partner_category_rel',
'res_partner_event',
'res_partner_title',
'res_payterm',
'res_request',
'res_request_history',
'res_request_link',
#'res_users',
'res_widget',
'res_widget_user',
'res_widget_wizard',
'rule_group_rel',
'user_preferences_config',
'wizard_ir_model_menu_create',
'wizard_ir_model_menu_create_line',
'wkf',
'wkf_activity',
'wkf_instance',
'wkf_logs',
'wkf_transition',
'wkf_triggers',
'wkf_witm_trans',
'wkf_workitem',
#Excluir vistas
'task_by_days']
omite = ''
for listb in listbs:
exluye = " AND table_name<>'%s'" % listb
omite+=exluye
########################################################################
conn = psycopg2.connect("dbname=%s host=%s user=%s password=%s" % (database, host, user, password))
cur_tabla = conn.cursor()
###Consulta que permite obtener los nombres de las tables de la base de datos especificada
cur_tabla.execute("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'" + omite + "ORDER BY table_name")
###conexion con la base de datos de auditoria, recuerde que ya debe estar creada (de forma manual)
#aud_conn = psycopg2.connect("dbname=aud_%s host=%s user=%s password=%s" % (database, host, user, password))
###Se abre el archivo con el que se va a trabajar
sAuditable = open('script_bd_auditoria.sql', 'w')
sTrigger = open('script_trigger_auditoria.sql', 'w')
sBash = open('audiBash.sh', 'w')
sAuditable.write("""CREATE DATABASE aud_%s
WITH OWNER = openerp
ENCODING = 'UTF8'
TABLESPACE = pg_default
LC_COLLATE = 'es_VE.UTF-8'
LC_CTYPE = 'es_VE.UTF-8'
CONNECTION LIMIT = -1;""" % (database)) # se crea la nueva base de datos con el prefijo 'aud_'
sAuditable.write("""\n\c aud_%s \n CREATE EXTENSION dblink;"""% (database))
sTrigger.write("""\c %s \n CREATE EXTENSION dblink;"""% (database))
for tabla in cur_tabla:
###====================================== Creacion de las Tablas ======================================
sAuditable.write("""
CREATE TABLE %s
(
audit_id serial NOT NULL, -- PK: Identificador autoincremental del registro historico de auditoria
audit_datetime timestamp without time zone, -- Fecha y Hora de la creacion del registro
audit_user character varying(50), -- Codigo SISR del usuario que realizo la operacion
audit_oper character varying(50), -- Codigo del tipo de operacion (Insert, Update, Delete)
""" % (tabla))
cur_columna = conn.cursor()
### Consulta que permite obtener el nombre, tipo y longitud de cada campo de la tabla en revision.
cur_columna.execute("select column_name, data_type, character_maximum_length from information_schema.columns where table_name='%s' ORDER BY ordinal_position ASC" % (tabla))
i = 0
cols = []
for columna in cur_columna:
cols.append(columna)
for columna in cols:
i = i + 1
if(i == len(cols)):
if columna[2] == None:
sAuditable.write(""" %s %s
""" % (columna[0], columna[1]))
else:
sAuditable.write(""" %s %s(%s)
""" % (columna[0], columna[1], columna[2]))
else:
if columna[2] == None:
sAuditable.write(""" %s %s,
""" % (columna[0], columna[1]))
else:
sAuditable.write(""" %s %s(%s),
""" % (columna[0], columna[1], columna[2]))
sAuditable.write(""");""")
###==================================== Creacion del StoreProcedure ===================================
cur_columna.execute("SELECT column_name FROM information_schema.columns WHERE table_name='%s' ORDER BY ordinal_position ASC" % (tabla))
columnas = ""
new_columnas = ""
old_columnas = ""
i = 0
cols = []
for columna in cur_columna:
cols.append(columna)
for columna in cols:
i = i + 1
if (columnas == ""):
columnas = columna[0]
new_columnas = "'''||NEW." + columna[0]+"||'''"
old_columnas = "'''||OLD." + columna[0]+"||'''"
else:
if (i % 4 == 0):
if (i < len(cols)):
columnas = columnas + ", " + columna[0] + ", " + '\n\t\t'
new_columnas = new_columnas + ", " + "'''||NEW." + columna[0] + "||'''" + ", " + '\n\t\t'
old_columnas = old_columnas + ", " + "'''||OLD." + columna[0] + "||'''" + ", " + '\n\t\t'
else:
columnas = columnas + ", " + columna[0] + '\n\t\t'
new_columnas = new_columnas + ", " + "'''||NEW." + columna[0] +"||'''" + '\n\t\t'
old_columnas = old_columnas + ", " + "'''||OLD." + columna[0] + "||'''" + '\n\t\t'
else:
if (i % 4 == 1):
columnas = columnas + columna[0]
new_columnas = new_columnas + "'''||NEW." + columna[0] + "||'''"
old_columnas = old_columnas + "'''||OLD." + columna[0] + "||'''"
else:
columnas = columnas + ", " + columna[0]
new_columnas = new_columnas + ", " + "'''||NEW." + columna[0] + "||'''"
old_columnas = old_columnas + ", " + "'''||OLD." + columna[0] + "||'''"
sTrigger.write("""
-- Function: rau_%s()
-- DROP FUNCTION rau_%s();
CREATE OR REPLACE FUNCTION rau_%s()
RETURNS TRIGGER AS $rau_%s$
DECLARE
BEGIN
PERFORM dblink_connect('dbname=aud_%s host=127.0.0.1 user=%s password=%s');
-- Funcion para conservar un registro historico (auditoria) de todos los
-- cambios realizados a los datos en un tabla (sean por INSERT, UPDATE o DELETE)
IF (TG_OP = 'INSERT') THEN
IF NEW.write_date IS NULL THEN NEW.write_date = NEW.create_date; END IF;
IF NEW.write_uid IS NULL THEN NEW.write_uid = NEW.create_uid; END IF;
PERFORM dblink_exec('INSERT INTO %s (audit_datetime, audit_user, audit_oper,
%s)
VALUES (
NOW(), '''||current_user||''', '''||TG_OP||''',
%s);');
PERFORM dblink_disconnect();
END IF;
IF (TG_OP = 'UPDATE') THEN
PERFORM dblink_exec('INSERT INTO %s (audit_datetime, audit_user, audit_oper,
%s)
VALUES (
NOW(), '''||current_user||''', '''||TG_OP||''',
%s);');
PERFORM dblink_disconnect();
END IF;
IF (TG_OP = 'DELETE') THEN
IF OLD.write_date IS NULL THEN OLD.write_date = OLD.create_date; END IF;
IF OLD.write_uid IS NULL THEN OLD.write_uid = OLD.create_uid; END IF;
PERFORM dblink_exec('INSERT INTO %s (audit_datetime, audit_user, audit_oper,
%s)
VALUES (
NOW(), '''||current_user||''', '''||TG_OP||''',
%s);');
PERFORM dblink_disconnect();
END IF;
RETURN NEW;
END;
$rau_%s$ LANGUAGE plpgsql;
""" % (tabla[0], tabla[0], tabla[0],tabla[0],
database,user_destino, password_destino,tabla[0], columnas, new_columnas,
tabla[0], columnas, new_columnas,
tabla[0], columnas, old_columnas,
tabla[0]))
###====================================== Creacion del Triger =====================================
sTrigger.write("""
-- Trigger: rau_%s on %s
-- DROP TRIGGER rau_%s ON %s;
CREATE TRIGGER rau_%s
AFTER INSERT OR UPDATE OR DELETE
ON %s
FOR EACH ROW
EXECUTE PROCEDURE rau_%s();""" % (tabla[0], tabla[0], tabla[0], tabla[0], tabla[0], tabla[0], tabla[0]))
sBash.write("""#!/bin/bash
if [ $(whoami) = "postgres" ];then
psql -f `dirname $0`/script_bd_auditoria.sql && psql -f `dirname $0`/script_trigger_auditoria.sql ; echo "Ingrese la clave de root para reiniciar openerp" && su root /etc/init.d/openerp restart
else
su postgres
fi""")
cur_columna.close
cur_tabla.close
conn.close