85 lines
3.0 KiB
Bash
85 lines
3.0 KiB
Bash
#!/bin/bash
|
|
|
|
# Cargar configuración
|
|
if [ ! -f config.conf ]; then echo "Error: config.conf no encontrado"; exit 1; fi
|
|
source ./config.conf
|
|
|
|
# Inicializar JSON si no existe
|
|
if [ ! -f "$OUTPUT_FILE" ]; then
|
|
echo '{"datos": [], "resumen": {}}' > "$OUTPUT_FILE"
|
|
fi
|
|
|
|
echo "Procesando logs..."
|
|
# Extraer: IP | Fecha | Hora | Agente (IP + Agente definen al único)
|
|
awk -v url="$URL_A_MONITORIZAR" '
|
|
$7 == url || $7 ~ url {
|
|
split($4, t, /[:/]/);
|
|
fecha=t[1]"/"t[2]"/"t[3];
|
|
hora=t[4]":"t[5];
|
|
# Extraer User Agent
|
|
ua=""; for(i=12; i<=NF; i++) ua=(ua=="" ? $i : ua" "$i);
|
|
gsub(/"/, "", ua);
|
|
print $1 "|" fecha "|" hora "|" ua
|
|
}' "$LOG_FILE" | sort -u > temp_data.txt
|
|
|
|
echo "Actualizando base de datos JSON..."
|
|
# Usamos un archivo temporal para construir la nueva lista de datos
|
|
TMP_JSON="tmp_db.json"
|
|
|
|
while IFS="|" read -r ip fecha hora ua; do
|
|
# Identificador único para evitar duplicados en el histórico
|
|
ID_UNICO=$(echo "${ip}${ua}" | md5sum | cut -d' ' -f1)
|
|
|
|
# Determinar si es RSS
|
|
IS_RSS=$([[ "$ua" =~ (RSS|Feed|Reader|SimplePie|W3C_Validator|TinyPRSS) ]] && echo "true" || echo "false")
|
|
|
|
# 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
|
|
|
|
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"
|
|
|
|
# 3. Datos para Gnuplot (Frecuencia semanal)
|
|
jq -r '.datos[].fecha' "$OUTPUT_FILE" | sort | uniq -c | awk '{print $2, $1}' > freq.dat
|
|
|
|
gnuplot <<EOF
|
|
set terminal pngcairo size 800,400
|
|
set output 'grafica_semanal.png'
|
|
set title "Frecuencia de Visitas Únicas"
|
|
set xtics rotate by -45
|
|
set style fill solid
|
|
plot 'freq.dat' using 2:xtic(1) with boxes title "Visitas"
|
|
EOF
|
|
|
|
# 4. Generar Informe HTML
|
|
RSS_PERC=$(jq '.resumen.usuarios_rss * 100 / .resumen.total_visitantes_unicos' "$OUTPUT_FILE")
|
|
|
|
cat <<HTML > informe.html
|
|
<!DOCTYPE html>
|
|
<html lang="es">
|
|
<head><meta charset="UTF-8"><title>Reporte de Audiencia</title></head>
|
|
<body>
|
|
<h1>Informe para $URL_A_MONITORIZAR</h1>
|
|
<ul>
|
|
<li><b>Total Visitantes Únicos:</b> $(jq '.resumen.total_visitantes_unicos' "$OUTPUT_FILE")</li>
|
|
<li><b>Lectores RSS:</b> $(jq '.resumen.usuarios_rss' "$OUTPUT_FILE") ($RSS_PERC%)</li>
|
|
<li><b>Última ejecución:</b> $(jq -r '.resumen.ultima_actualizacion' "$OUTPUT_FILE")</li>
|
|
</ul>
|
|
<img src="grafica_semanal.png">
|
|
</body>
|
|
</html>
|
|
HTML
|
|
|
|
rm temp_data.txt freq.dat
|
|
echo "Proceso terminado exitosamente."
|