diff --git a/.gitignore b/.gitignore index b4a01fb..269fefe 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ maps/map_200.svg maps/map_300.svg maps/map_all.svg test/ +log/ *.db config.cfg TODO diff --git a/iplocate.py b/iplocate.py index 0c06361..474dda8 100755 --- a/iplocate.py +++ b/iplocate.py @@ -12,6 +12,7 @@ from rich import box from rich.console import Console from rich.table import Table import consultas.querys_sqlite as querys +import logging selfpath = os.path.abspath(os.path.dirname(__file__)) ownip = requests.get('https://ifconfig.me/').text @@ -20,13 +21,19 @@ parser.read(f'{selfpath}/config.cfg') token = parser.get('iplocate','token') token = token.strip("'") muevelog = f'{selfpath}/muevelog.sh ' +log_file = f'{selfpath}/log/iplocate.log' console = Console() #tkn=True - # IP validate https://stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python ip_regx = "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$" ip_local_regx = "^192\.168\.0\.([0-9]|[0-9][0-9]|[0-9][0-9][0-9])$" + +def log_usage(proceso, info): + usage_log = f"Proceso:[{proceso}] - Info: [{info}]" + logging.info(usage_log) + + def filtro_ip_propia(ip): return True if ip != ownip and not re.search(ip_local_regx, ip) else False @@ -39,10 +46,8 @@ def print_ipinfo(ip, tkn=True): print(f'Exception sql_alch.consulta_ip({ip})\n',ex) ip_info = None if isinstance(ip_info, dict): - #print('es dict') print_tabla_ip(ip_info) elif isinstance(ip_info, list): - #print('es lista') lista_visitas=[] contad=0 for tupla in ip_info: @@ -90,7 +95,6 @@ def print_ipinfo(ip, tkn=True): def print_tabla_ip(ip_info): - # color dodger_blue2 tbl_ip = Table(box = box.ROUNDED, highlight=True, border_style="dim plum1") tbl_ip.add_column("IP", justify="left", style="bold #005fff", no_wrap=True) tbl_ip.add_column(f"{ip_info['ip']}", justify="left", style="#00ff5f") @@ -138,8 +142,6 @@ def print_tabla_ip(ip_info): print('Exception print(tabla_ip): ', ex) def print_tabla_visita(lista_visitas): - # color dodger_blue2 - #tbl_v = Table(title=f"[bold][yellow]Visitas registradas:[/yellow] [green]{lista_visitas[0]}[/bold][/green]", tbl_v = Table(box = box.ROUNDED, show_lines = False,row_styles=["dim", ""], border_style="dark_orange3") tbl_v.add_column("Fecha visita", justify="center", style="bright_yellow", no_wrap=True) tbl_v.add_column("Codigo", justify="center", style="bold dodger_blue2") @@ -317,4 +319,10 @@ def uso(): console.print(ayuda) if __name__ == "__main__": + logging.basicConfig( + filename = log_file, + format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level = logging.INFO + ) + logging.info("iplocate inicio") main() diff --git a/maps/map_thumb.svg b/maps/map_thumb.svg index 1915770..040bc8e 100644 --- a/maps/map_thumb.svg +++ b/maps/map_thumb.svg @@ -13167,6 +13167,456 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -15219,6 +15669,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Maps & Data (C) OpenStreetMap.org contributors diff --git a/sql_alch.py b/sql_alch.py index 1121d6f..8622b8d 100644 --- a/sql_alch.py +++ b/sql_alch.py @@ -1,7 +1,7 @@ import os import time import subprocess -from iplocate import re, requests, token, filtro_ip_propia, selfpath, parser +from iplocate import re, requests, token, filtro_ip_propia, selfpath, parser, log_usage from json import loads from datetime import datetime from sqlalchemy import create_engine @@ -147,7 +147,7 @@ def ip_registrada(ip): def carga_access_log(log): if os.path.exists(log): nombre_log = log.split('/')[-1] - console.print(f'[yellow]Registrando [[/yellow]{nombre_log}[yellow]][/yellow]') + console.print(f'[yellow]Procesando [[/yellow]{nombre_log}[yellow]][/yellow]') try: with open(log, 'r') as lista: try: @@ -223,9 +223,10 @@ def carga_access_log(log): def carga_error_logs(log): if os.path.exists(log): nombre_log = log.split('/')[-1] - console.print(f'[yellow]Registrando [[/yellow]{nombre_log}[yellow]][/yellow]') + console.print(f'[yellow]Procesando [[/yellow]{nombre_log}[yellow]][/yellow]') try: with open(log, 'r') as lista: + ip, fecha, url, metodo = None, None, None, None try: largo = subprocess.run(['wc', '-l', log], capture_output=True, text=True) largo = int(largo.stdout.split(' ')[0]) @@ -236,7 +237,8 @@ def carga_error_logs(log): try: ip = linea.split('client: ')[1].split(',')[0] except Exception as ex: - print('Exception Ip error_log: ', ex) + #print('Exception Ip error_log: ', ex) + log_usage('Exception Ip error_log {crit}', ex) ip = None try: fecha = ' '.join(linea.split(' ')[0:2]) @@ -257,7 +259,8 @@ def carga_error_logs(log): try: ip = linea.split('client: ')[1].split(',')[0] except Exception as ex: - print('Exception Ip error_log: ', ex) + #print('Exception Ip error_log: ', ex) + log_usage('Exception Ip error_log {notice}', ex) ip = None try: fecha = ' '.join(linea.split(' ')[0:2]) @@ -273,24 +276,32 @@ def carga_error_logs(log): url = url[:252]+'...' except Exception: url = '---' - if filtro_ip_propia(ip): - fecha = int(fecha_error_to_epoch(fecha)) - codigo = 0 - if ip_registrada(ip): - session.add(Visita(ip=ip, - cod_html=codigo, - fecha=fecha, - consulta=url, - metodo=metodo, - registro=1)) - else: - session.add(Visita(ip=ip, - cod_html=codigo, - fecha=fecha, - consulta=url, - metodo=metodo)) + if ip != None: + if filtro_ip_propia(ip): + fecha = int(fecha_error_to_epoch(fecha)) + codigo = 0 + if ip_registrada(ip): + session.add(Visita(ip=ip, + cod_html=codigo, + fecha=fecha, + consulta=url, + metodo=metodo, + registro=1)) + else: + session.add(Visita(ip=ip, + cod_html=codigo, + fecha=fecha, + consulta=url, + metodo=metodo)) + else: + log_usage('carga error.log', linea) except Exception as ex: - print('Exception: ', ex) + print('[Procesando *Error.log] Exception: ', ex) + try: + info_error = f'IP:[{ip}] - FECHA:[{fecha}] - METODO:[{metodo}] - URL:[{url}]' + log_usage('Exception error.log', info_error) + except: + pass try: with Progress() as prog, session: task1=prog.add_task("[yellow bold]Guardando[/yellow bold]", total=len(session.new)) @@ -299,20 +310,22 @@ def carga_error_logs(log): prog.update(task1, advance=0.1) time.sleep(0.05) except Exception as ex: - print('Exception Progress: ', ex) + #print('Exception error.log - Progress session commit', ex) + log_usage('Exception error.log - Progress session commit', ex) console.print(f'[magenta] - Carga completa.. borrando log[/magenta]\n') os.remove(log) return True except: console.print(f'[red]Error al intentar abrir/cargar: [{log}[/red]]\n') + log_usage(f'Error al abrir/cargar', log) return False else: console.print(f'[bold red]Log: [[/bold red]{log}[bold red]] inexistente.[/bold red]\n') + log_usage(f'Log inexistente', log) return False def carga_logs(): - #print(f'[bold green]Carga de logs en base de datos:[/bold green]\n') logpath = logs_dir+'/access.log' if os.path.exists(logpath): carga_access_log(logpath) @@ -355,7 +368,8 @@ def carga_registro_ip(ip_info): )) session.commit() except Exception as ex: - print('Exception: ', ex) + print('[session.commit(ADD REGISTRO)] Exception: ', ex) + print('Datos-Dic: ', info_dic) stmt = update(Visita).where(Visita.ip == ip_info['ip']).values(registro=1).\ execution_options(synchronize_session="fetch") #result = session.execute(stmt) @@ -363,7 +377,7 @@ def carga_registro_ip(ip_info): session.execute(stmt) session.commit() except Exception as ex: - print('Exception: ', ex) + print('[session.commit(UPDT VISITA)] Exception: ', ex) def consulta_ip(ip_consulta, tkn=True):