Skip to content

Instantly share code, notes, and snippets.

@Esl1h
Created February 1, 2021 02:13
Show Gist options
  • Save Esl1h/5943c6309249d7758af5e13b70ec6961 to your computer and use it in GitHub Desktop.
Save Esl1h/5943c6309249d7758af5e13b70ec6961 to your computer and use it in GitHub Desktop.
Solução simples para o desafio: extraia 2 resultados a partir de um arquivo de log - quantidade de logs e quantidade de solicitações
#!/bin/bash
: '
DESAFIO:
Dado um arquivo de log do apache (acess.log) com diversas linhas semelhantes a abaixo:
172.19.0.100 - - [18/Feb/2020:22:35:10 +0000] "GET /index HTTP/1.1" 200 31067 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)" "172.19.0.2"
172.19.0.103 - - [15/Feb/2020:22:32:02 +0000] "GET /index HTTP/1.1" 200 14034 "-" "Mozilla/5.0 (compatible; SemrushBot/6~bl; +http://www.semrush.com/bot.html)" "172.19.0.4"
172.19.0.104 - - [16/Feb/2020:22:31:32 +0000] "GET /site HTTP/1.1" 200 36565 "https://command-not-found.com/curl" "Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)" "172.19.0.3"
172.19.0.105 - - [16/Feb/2020:22:30:10 +0000] "GET /credits HTTP/1.1" 200 31067 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)" "172.19.0.2"
172.19.0.106 - - [17/Feb/2020:22:30:10 +0000] "GET /index HTTP/1.1" 200 31067 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)" "172.19.0.2"
172.19.0.107 - - [18/Feb/2020:22:35:10 +0000] "GET /index HTTP/1.1" 200 31067 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)" "172.19.0.2"
172.19.0.108 - - [15/Feb/2020:22:32:02 +0000] "GET /index HTTP/1.1" 200 14034 "-" "Mozilla/5.0 (compatible; SemrushBot/6~bl; +http://www.semrush.com/bot.html)" "172.19.0.4"
172.19.0.109 - - [16/Feb/2020:22:31:32 +0000] "GET /site HTTP/1.1" 200 36565 "https://command-not-found.com/curl" "Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)" "172.19.0.3"
172.19.0.110 - - [16/Feb/2020:22:30:10 +0000] "GET /credits HTTP/1.1" 200 31067 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)" "172.19.0.2"
172.19.0.111 - - [17/Feb/2020:22:30:10 +0000] "GET /index HTTP/1.1" 200 31067 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)" "172.19.0.2"
172.19.0.112 - - [18/Feb/2020:22:35:10 +0000] "GET /index HTTP/1.1" 200 31067 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)" "172.19.0.2"
Crie um script que ao indicar o arquivo de log, deve retornar 2 resultados:
# 1. Primeiro resultado: "QUANTITY FIELD" quantidade de logs e a data (os 10 primeiros)
# 2. Segundo resultado: quantidade de solicitações por cada endereço de IP (os 10 primeiros)
'
# RESOLUÇÃO PARA A PROPOSTA:
# executar este script com argumento do arquivo de log, ex.: script.sh acess.log
LOG_FILE=$1
function erroarg() {
echo "$0: $*" >&2;
echo "Erro - informe o arquivo de log do apache"
exit 1
}
# checa se o argumento (path do arquivo) não é null
if [ -z "$1" ];
then
erroarg
else
echo "iniciando..."
fi
echo "Primeiro relatório:"
cat $1 | grep -Eo '[0-9]{2}\/[a-Z]{3}\/[0-9]{4}' | sort | uniq -c | head -n 10 | sort
echo
echo "Segundo relatorio:"
cat $1 | cut -d ' ' -f 1 | sort | uniq -c | head -n 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment