Files
control-rss/control-rss.sh
2026-01-13 10:33:27 -05:00

84 lines
2.8 KiB
Bash

#!/bin/bash
# Cargar configuración
source ./config.conf
# 1. Procesamiento eficiente de logs
echo "Procesando logs..."
# Extraemos: IP, Fecha(DD/Mon/YYYY), Hora(HH), Agente, URL
# Formato Combined: $1=IP, $4=[DD/Mon/YYYY:HH:mm:ss, $7=URL, $12+=Agente
awk -v url="$URL_A_MONITORIZAR" '
$7 == url || $7 ~ url {
split($4, t, /[:/]/);
fecha=t[2]"/"t[3]"/"t[4];
hora=t[5];
# Extraer User Agent (todo lo que sigue después de la columna 11)
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
# 2. Actualizar JSON (Estructura optimizada)
echo "Actualizando JSON..."
if [ ! -f "$OUTPUT_FILE" ]; then echo "[]" > "$OUTPUT_FILE"; fi
# 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
is_rss=$([[ "$ua" =~ (RSS|Feed|Reader|SimplePie|W3C_Validator) ]] && echo "true" || echo "false")
NEW_ENTRY=$(jq -n --arg ip "$ip" --arg f "$fecha" --arg h "$hora" --arg ua "$ua" --arg rss "$is_rss" \
'{ip: $ip, fecha: $f, hora: $h, agente: $ua, es_rss: $rss}')
# Solo añadir si no existe la combinación exacta
jq --argjson new "$NEW_ENTRY" 'if any(.[]; .ip == $new.ip and .agente == $new.agente and .fecha == $new.fecha) then . else . + [$new] end' \
"$OUTPUT_FILE" > "$OUTPUT_FILE.tmp" && mv "$OUTPUT_FILE.tmp" "$OUTPUT_FILE"
done < temp_data.txt
# 3. Preparar datos para Gnuplot
# Frecuencia por Hora
jq -r '.[].hora' "$OUTPUT_FILE" | sort | uniq -c | awk '{print $2, $1}' > hourly.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
set terminal pngcairo size 800,400
set output 'grafico_horas.png'
set title "Visitas por Hora (Total Histórico)"
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
plot 'agents.dat' using 2:xtic(1) with boxes title "Agentes"
EOF
# 5. Generar Informe HTML
echo "Generando informe HTML..."
UNIQUE_AGENTS=$(jq '[.[].agente] | unique | length' "$OUTPUT_FILE")
RSS_COUNT=$(jq '[.[] | select(.es_rss == "true")] | length' "$OUTPUT_FILE")
cat <<HTML > informe.html
<!DOCTYPE html>
<html>
<head><title>Reporte de Accesos</title></head>
<body>
<h1>Reporte para: $URL_A_MONITORIZAR</h1>
<p><b>Agentes Únicos:</b> $UNIQUE_AGENTS</p>
<p><b>Lectores RSS detectados:</b> $RSS_COUNT</p>
<hr>
<h2>Frecuencia Horaria</h2>
<img src="grafico_horas.png">
<h2>Top Agentes</h2>
<img src="grafico_agentes.png">
</body>
</html>
HTML
rm temp_data.txt hourly.dat agents.dat
echo "Completado. Revisa informe.html"