Agregado identidad del agente y exportado a HTML. Se guardan ahora un HTML y un PNG como resultado del proceso
This commit is contained in:
@@ -1,83 +1,84 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Cargar configuración
|
# Cargar configuración
|
||||||
|
if [ ! -f config.conf ]; then echo "Error: config.conf no encontrado"; exit 1; fi
|
||||||
source ./config.conf
|
source ./config.conf
|
||||||
|
|
||||||
# 1. Procesamiento eficiente de logs
|
# Inicializar JSON si no existe
|
||||||
echo "Procesando logs..."
|
if [ ! -f "$OUTPUT_FILE" ]; then
|
||||||
|
echo '{"datos": [], "resumen": {}}' > "$OUTPUT_FILE"
|
||||||
|
fi
|
||||||
|
|
||||||
# Extraemos: IP, Fecha(DD/Mon/YYYY), Hora(HH), Agente, URL
|
echo "Procesando logs..."
|
||||||
# Formato Combined: $1=IP, $4=[DD/Mon/YYYY:HH:mm:ss, $7=URL, $12+=Agente
|
# Extraer: IP | Fecha | Hora | Agente (IP + Agente definen al único)
|
||||||
awk -v url="$URL_A_MONITORIZAR" '
|
awk -v url="$URL_A_MONITORIZAR" '
|
||||||
$7 == url || $7 ~ url {
|
$7 == url || $7 ~ url {
|
||||||
split($4, t, /[:/]/);
|
split($4, t, /[:/]/);
|
||||||
fecha=t[2]"/"t[3]"/"t[4];
|
fecha=t[1]"/"t[2]"/"t[3];
|
||||||
hora=t[5];
|
hora=t[4]":"t[5];
|
||||||
# Extraer User Agent (todo lo que sigue después de la columna 11)
|
# Extraer User Agent
|
||||||
ua=""; for(i=12; i<=NF; i++) ua=(ua=="" ? $i : ua" "$i);
|
ua=""; for(i=12; i<=NF; i++) ua=(ua=="" ? $i : ua" "$i);
|
||||||
gsub(/"/, "", ua);
|
gsub(/"/, "", ua);
|
||||||
print $1 "|" fecha "|" hora "|" ua
|
print $1 "|" fecha "|" hora "|" ua
|
||||||
}' "$LOG_FILE" | sort -u > temp_data.txt
|
}' "$LOG_FILE" | sort -u > temp_data.txt
|
||||||
|
|
||||||
# 2. Actualizar JSON (Estructura optimizada)
|
echo "Actualizando base de datos JSON..."
|
||||||
echo "Actualizando JSON..."
|
# Usamos un archivo temporal para construir la nueva lista de datos
|
||||||
if [ ! -f "$OUTPUT_FILE" ]; then echo "[]" > "$OUTPUT_FILE"; fi
|
TMP_JSON="tmp_db.json"
|
||||||
|
|
||||||
# Usamos jq para fusionar datos únicos de forma masiva (más rápido que línea a línea)
|
|
||||||
while IFS="|" read -r ip fecha hora ua; do
|
while IFS="|" read -r ip fecha hora ua; do
|
||||||
is_rss=$([[ "$ua" =~ (RSS|Feed|Reader|SimplePie|W3C_Validator) ]] && echo "true" || echo "false")
|
# Identificador único para evitar duplicados en el histórico
|
||||||
NEW_ENTRY=$(jq -n --arg ip "$ip" --arg f "$fecha" --arg h "$hora" --arg ua "$ua" --arg rss "$is_rss" \
|
ID_UNICO=$(echo "${ip}${ua}" | md5sum | cut -d' ' -f1)
|
||||||
'{ip: $ip, fecha: $f, hora: $h, agente: $ua, es_rss: $rss}')
|
|
||||||
|
|
||||||
# Solo añadir si no existe la combinación exacta
|
# Determinar si es RSS
|
||||||
jq --argjson new "$NEW_ENTRY" 'if any(.[]; .ip == $new.ip and .agente == $new.agente and .fecha == $new.fecha) then . else . + [$new] end' \
|
IS_RSS=$([[ "$ua" =~ (RSS|Feed|Reader|SimplePie|W3C_Validator|TinyPRSS) ]] && echo "true" || echo "false")
|
||||||
"$OUTPUT_FILE" > "$OUTPUT_FILE.tmp" && mv "$OUTPUT_FILE.tmp" "$OUTPUT_FILE"
|
|
||||||
|
# Insertar solo si no existe el ID_UNICO
|
||||||
|
jq --arg id "$ID_UNICO" --arg ip "$ip" --arg f "$fecha" --arg h "$hora" --arg ua "$ua" --arg rss "$IS_RSS" \
|
||||||
|
'if .datos | any(.[]; .uid == $id) then .
|
||||||
|
else .datos += [{"uid": $id, "ip": $ip, "fecha": $f, "hora": $h, "agente": $ua, "es_rss": ($rss=="true")}] end' \
|
||||||
|
"$OUTPUT_FILE" > "$TMP_JSON" && mv "$TMP_JSON" "$OUTPUT_FILE"
|
||||||
done < temp_data.txt
|
done < temp_data.txt
|
||||||
|
|
||||||
# 3. Preparar datos para Gnuplot
|
echo "Recalculando resumen y gráficas..."
|
||||||
|
# Actualizar el bloque de resumen dentro del JSON
|
||||||
|
jq '.resumen = {
|
||||||
|
"total_visitantes_unicos": (.datos | length),
|
||||||
|
"usuarios_rss": ([.datos[] | select(.es_rss == true)] | length),
|
||||||
|
"usuarios_estandar": ([.datos[] | select(.es_rss == false)] | length),
|
||||||
|
"ultima_actualizacion": "'$(date +%Y-%m-%d\ %H:%M:%S)'"
|
||||||
|
}' "$OUTPUT_FILE" > "$TMP_JSON" && mv "$TMP_JSON" "$OUTPUT_FILE"
|
||||||
|
|
||||||
# Frecuencia por Hora
|
# 3. Datos para Gnuplot (Frecuencia semanal)
|
||||||
jq -r '.[].hora' "$OUTPUT_FILE" | sort | uniq -c | awk '{print $2, $1}' > hourly.dat
|
jq -r '.datos[].fecha' "$OUTPUT_FILE" | sort | uniq -c | awk '{print $2, $1}' > freq.dat
|
||||||
|
|
||||||
# Frecuencia por Agente (Top 10)
|
|
||||||
jq -r '.[].agente' "$OUTPUT_FILE" | sort | uniq -c | sort -rn | head -10 | awk '{$1=""; print "\""$0"\"", $1}' > agents.dat
|
|
||||||
|
|
||||||
# 4. Generar Gráficas
|
|
||||||
echo "Generando gráficas..."
|
|
||||||
gnuplot <<EOF
|
gnuplot <<EOF
|
||||||
set terminal pngcairo size 800,400
|
set terminal pngcairo size 800,400
|
||||||
set output 'grafico_horas.png'
|
set output 'grafica_semanal.png'
|
||||||
set title "Visitas por Hora (Total Histórico)"
|
set title "Frecuencia de Visitas Únicas"
|
||||||
set style fill solid
|
|
||||||
plot 'hourly.dat' using 2:xtic(1) with boxes title "Visitas"
|
|
||||||
|
|
||||||
set output 'grafico_agentes.png'
|
|
||||||
set title "Top 10 Agentes"
|
|
||||||
set xtics rotate by -45
|
set xtics rotate by -45
|
||||||
plot 'agents.dat' using 2:xtic(1) with boxes title "Agentes"
|
set style fill solid
|
||||||
|
plot 'freq.dat' using 2:xtic(1) with boxes title "Visitas"
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# 5. Generar Informe HTML
|
# 4. Generar Informe HTML
|
||||||
echo "Generando informe HTML..."
|
RSS_PERC=$(jq '.resumen.usuarios_rss * 100 / .resumen.total_visitantes_unicos' "$OUTPUT_FILE")
|
||||||
UNIQUE_AGENTS=$(jq '[.[].agente] | unique | length' "$OUTPUT_FILE")
|
|
||||||
RSS_COUNT=$(jq '[.[] | select(.es_rss == "true")] | length' "$OUTPUT_FILE")
|
|
||||||
|
|
||||||
cat <<HTML > informe.html
|
cat <<HTML > informe.html
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html lang="es">
|
||||||
<head><title>Reporte de Accesos</title></head>
|
<head><meta charset="UTF-8"><title>Reporte de Audiencia</title></head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Reporte para: $URL_A_MONITORIZAR</h1>
|
<h1>Informe para $URL_A_MONITORIZAR</h1>
|
||||||
<p><b>Agentes Únicos:</b> $UNIQUE_AGENTS</p>
|
<ul>
|
||||||
<p><b>Lectores RSS detectados:</b> $RSS_COUNT</p>
|
<li><b>Total Visitantes Únicos:</b> $(jq '.resumen.total_visitantes_unicos' "$OUTPUT_FILE")</li>
|
||||||
<hr>
|
<li><b>Lectores RSS:</b> $(jq '.resumen.usuarios_rss' "$OUTPUT_FILE") ($RSS_PERC%)</li>
|
||||||
<h2>Frecuencia Horaria</h2>
|
<li><b>Última ejecución:</b> $(jq -r '.resumen.ultima_actualizacion' "$OUTPUT_FILE")</li>
|
||||||
<img src="grafico_horas.png">
|
</ul>
|
||||||
<h2>Top Agentes</h2>
|
<img src="grafica_semanal.png">
|
||||||
<img src="grafico_agentes.png">
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
HTML
|
HTML
|
||||||
|
|
||||||
rm temp_data.txt hourly.dat agents.dat
|
rm temp_data.txt freq.dat
|
||||||
echo "Completado. Revisa informe.html"
|
echo "Proceso terminado exitosamente."
|
||||||
|
|||||||
Reference in New Issue
Block a user