Skip to content

Instantly share code, notes, and snippets.

@polsala
Created April 18, 2024 13:03
Show Gist options
  • Select an option

  • Save polsala/5cf2f09d2fef16d5bda11b53da7c3292 to your computer and use it in GitHub Desktop.

Select an option

Save polsala/5cf2f09d2fef16d5bda11b53da7c3292 to your computer and use it in GitHub Desktop.
Revisar las llamadas y numero de veces de cada llamada para los logs de la restapi en un intervalo de tiempo
#!/bin/bash
#./check_restapi_log_calls.sh /home/erp/var/log/restapi.log 'Apr 18 12:11:00 2024' 'Apr 18 12:25:00 2024'
# Input variables
file_path=$1
start_date="$2"
end_date="$3"
# Convertir las fechas de inicio y fin a formato 'date' para comparar
start_timestamp=$(date -d "$start_date" '+%s')
end_timestamp=$(date -d "$end_date" '+%s')
# Usar awk para filtrar y contar solicitudes
awk -v start="$start_timestamp" -v end="$end_timestamp" '
BEGIN {
# Definir nombres de meses para conversión a números
split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month_names);
for (i = 1; i <= 12; i++) {
month_nums[month_names[i]] = i;
}
}
function parse_datetime(str, parts, date, time, Y, M, D, h, m, s) {
# Extraer la fecha y la hora
if (match(str, /\[(\w{3} \w{3} [0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} 20[0-9]{2})\]/, parts)) {
split(parts[1], date, " ");
split(date[4], time, ":");
Y = date[5]; M = month_nums[date[2]]; D = date[3];
h = time[1]; m = time[2]; s = time[3];
# Crear timestamp
return mktime(sprintf("%d %d %d %d %d %d", Y, M, D, h, m, s));
}
return -1;
}
{
# Procesar solo líneas con POST o GET
if ($0 ~ /(POST|GET) [^ ]+/ && $0 ~ /\[pid:/) {
timestamp = parse_datetime($0);
if (timestamp >= start && timestamp <= end) {
match($0, /(POST|GET) [^ ]+/, m);
count[m[0]]++;
}
}
}
END {
# Ordenar los elementos por el número de llamadas
for (req in count) {
if (count[req]) {
printf "%-d %s\n", count[req], req;
}
}
}
' "$file_path" | sort -nr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment