Actualizacion de codigo
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1 @@
|
|||||||
.conf
|
config.conf
|
||||||
@@ -4,5 +4,5 @@ LOG_FILE="/var/log/nginx/interlan.access.log" # Ruta a tu archivo de log princip
|
|||||||
|
|
||||||
URL_A_MONITORIZAR="/feed" # La URL que quieres analizar
|
URL_A_MONITORIZAR="/feed" # La URL que quieres analizar
|
||||||
|
|
||||||
OUTPUT_JSON="visitantes_unicos.json" # Archivo JSON de salida
|
OUTPUT_FILE="visitantes_unicos.json" # Archivo JSON de salida
|
||||||
# --- Fin de Configuración ---
|
# --- Fin de Configuración ---
|
||||||
|
|||||||
@@ -4,5 +4,5 @@ LOG_FILE="/var/log/nginx/access.log" # Ruta a tu archivo de log principal
|
|||||||
|
|
||||||
URL_A_MONITORIZAR="/feed" # El script es principalmente para ver datos de RSS, pero lo puedes modificar tu gusto
|
URL_A_MONITORIZAR="/feed" # El script es principalmente para ver datos de RSS, pero lo puedes modificar tu gusto
|
||||||
|
|
||||||
OUTPUT_JSON="visitantes_unicos.json" # Archivo JSON de salida
|
OUTPUT_FILE="visitantes_unicos.json" # Archivo JSON de salida
|
||||||
# --- Fin de Configuración ---
|
# --- Fin de Configuración ---
|
||||||
|
|||||||
102
control-rss.sh
102
control-rss.sh
@@ -1,45 +1,83 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Cargar configuración
|
# Cargar configuración
|
||||||
source config.conf
|
source ./config.conf
|
||||||
|
|
||||||
# Creacion del JSON si no existe
|
# 1. Procesamiento eficiente de logs
|
||||||
if [ ! -f "$OUTPUT_FILE" ]; then
|
echo "Procesando logs..."
|
||||||
echo '{"total_unicos": 0, "visitas": []}' > "$OUTPUT_FILE"
|
|
||||||
fi
|
|
||||||
|
|
||||||
zgrep "$TARGET_URL" $LOG_PATH | awk -F'"' '{
|
# Extraemos: IP, Fecha(DD/Mon/YYYY), Hora(HH), Agente, URL
|
||||||
split($1, a, " ");
|
# Formato Combined: $1=IP, $4=[DD/Mon/YYYY:HH:mm:ss, $7=URL, $12+=Agente
|
||||||
ip = a[1]; fecha = substr(a[4], 2, 11); ua = $6;
|
awk -v url="$URL_A_MONITORIZAR" '
|
||||||
print ip "|" fecha "|" ua
|
$7 == url || $7 ~ url {
|
||||||
}' | sort -u > temp_agents.txt
|
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
|
||||||
|
|
||||||
while IFS="|" read -r ip fecha ua; do
|
# 2. Actualizar JSON (Estructura optimizada)
|
||||||
if ! jq -e --arg ua "$ua" --arg date "$fecha" '.visitas[] | select(.user_agent == $ua and .fecha == $date)' "$OUTPUT_FILE" > /dev/null; then
|
echo "Actualizando JSON..."
|
||||||
jq --arg ip "$ip" --arg date "$fecha" --arg ua "$ua" \
|
if [ ! -f "$OUTPUT_FILE" ]; then echo "[]" > "$OUTPUT_FILE"; fi
|
||||||
'.visitas += [{"ip": $ip, "fecha": $date, "user_agent": $ua}] | .total_unicos = (.visitas | map(.user_agent) | unique | length)' \
|
|
||||||
"$OUTPUT_FILE" > "$OUTPUT_FILE.tmp" && mv "$OUTPUT_FILE.tmp" "$OUTPUT_FILE"
|
|
||||||
fi
|
|
||||||
done < temp_agents.txt
|
|
||||||
|
|
||||||
# 2. Generar datos para Gnuplot (Frecuencia por fecha)
|
# Usamos jq para fusionar datos únicos de forma masiva (más rápido que línea a línea)
|
||||||
# Extraemos del JSON: "Fecha Cuenta"
|
while IFS="|" read -r ip fecha hora ua; do
|
||||||
jq -r '.visitas[].fecha' "$OUTPUT_FILE" | sort | uniq -c | awk '{print $2, $1}' > datos_grafica.dat
|
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}')
|
||||||
|
|
||||||
# 3. Generar la gráfica con Gnuplot
|
# Solo añadir si no existe la combinación exacta
|
||||||
echo "Generando gráfica con Gnuplot..."
|
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
|
gnuplot <<EOF
|
||||||
set terminal pngcairo size 800,600 font "sans,10"
|
set terminal pngcairo size 800,400
|
||||||
set output 'frecuencia_visitas.png'
|
set output 'grafico_horas.png'
|
||||||
set title "Frecuencia de Visitas a $TARGET_URL"
|
set title "Visitas por Hora (Total Histórico)"
|
||||||
set xlabel "Fecha"
|
|
||||||
set ylabel "Visitas"
|
|
||||||
set style fill solid
|
set style fill solid
|
||||||
set boxwidth 0.5
|
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 "datos_grafica.dat" using 2:xtic(1) with boxes title "Visitas por día"
|
plot 'agents.dat' using 2:xtic(1) with boxes title "Agentes"
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# Limpieza
|
# 5. Generar Informe HTML
|
||||||
rm temp_agents.txt datos_grafica.dat
|
echo "Generando informe HTML..."
|
||||||
echo "Proceso completado. Gráfica guardada en frecuencia_visitas.png"
|
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"
|
||||||
|
|||||||
Reference in New Issue
Block a user