quarta-feira, 27 de novembro de 2013

Mini-Script para gerenciar o acesso do Squid

#!/bin/bash

# ------------------------------------------------- #
# Mini-mini-Script para gerenciar o acesso do Squid #
# em tempo real em um terminal utilizando o arquivo #
# access.log #
# ------------------------------------------------- #
# tail -f no arquivo
# | (pipe)
# awk nos campos
# $3 - endereco IP para formato 255.255.255.255 = %15s
# $4 - codigo de resposta para format %27s
# $6 - comandos (get,post,connect) para %7s
# $8 - Login do Usuario
# $7 - url
#
# Cores (36)-> Cyan  (32)-> Verde  (33)-> Amarelo  (30)-> Preto  (37)-> Branco


###################################
####        Cabeçalho          ####  
###################################
cabecalho()
{
clear
echocolorido 1 34 "+------------------------------------------------------------------------------------------------------------------------------>"
echocolorido 1 34 "|    Horario            |     STATUS                |      USUARIO    |  Metodo  |            SITE                              "
echocolorido 1 34 "+------------------------------------------------------------------------------------------------------------------------------>"
}

###################################
###     Echo com colorido      ####
###################################
echocolorido()
 {
  echo $4 -e "\e[$1;$2m $3";tput sgr0;
 }

#####################################################
####  A função monta log filtra do log do squid  ####
#####################################################
monta_log()
{
tail -f /var/log/squid/access.log | grep $LOGIN |awk '{
printf("%-5s|",strftime("%x - %X ") )
if ($4 ==  "TCP_MISS/000")           printf("\033[1;40;33m %-25s \033[1;0m|","TCP_MISS/000");
  else if($4 ==  "TCP_MISS/100")         printf("\033[1;40;32m %-25s \033[1;0m|","Continue ");
  else if($4 ==  "TCP_MISS/200")     printf("\033[1;40;32m %-25s \033[1;0m|","Permitido");
  else if($4 ==  "TCP_MISS/301")     printf("\033[1;40;32m %-25s \033[1;0m|","Movido");
  else if($4 ==  "TCP_MISS/302")     printf("\033[1;40;32m %-25s \033[1;0m|","Encontrado");
  else if($4 ==  "TCP_CLIENT_REFRESH_MISS/200") printf("\033[1;40;32m %-25s \033[1;0m|","Copia do Cache do Browser");
  else if($4 ==  "TCP_HIT/200")     printf("\033[1;40;32m %-25s \033[1;0m|","Copia do Cache");
  else if($4 ==  "TCP_MISS/204")     printf("\033[1;40;32m %-25s \033[1;0m|","Sem Conteudo");
  else if($4 ==  "TCP_MISS/304")     printf("\033[1;40;33m %-25s \033[1;0m|","TCP_MISS/304");
  else if($4 ==  "TCP_MISS/404")     printf("\033[1;40;32m %-25s \033[1;0m|","Nao Encontrado");
  else if($4 ==  "TCP_MISS/503")     printf("\033[1;40;32m %-25s \033[1;0m|","Servico Indisponivel");
  else if($4 ==  "NONE/400")         printf("\033[1;40;33m %-25s \033[1;0m|","NONE/400");
  else if($4 ==  "TCP_NEGATIVE_HIT/204")   printf("\033[1;40;33m %-25s \033[1;0m|","TCP_NEGATIVE_HIT/204");
  else if($4 ==  "TCP_REFRESH_UNMODIFIED/200")  printf("\033[1;40;33m %-25s \033[1;0m|","TCP_REFRESH_UNMODIFIED/200");
  else if($4 ==  "TCP_IMS_HIT/304")   printf("\033[1;40;33m %-25s \033[1;0m|","TCP_IMS_HIT/304");
  else if($4 ==  "TCP_DENIED/302")   printf("\033[1;40;33m %-25s \033[1;0m|","TCP_DENIED/302");
  else if($4 ==  "TCP_DENIED/400")   printf("\033[1;40;31m %-25s \033[1;0m|","Resposta ruim");
  else if($4 ==  "TCP_DENIED/401")   printf("\033[1;40;31m %-25s \033[1;0m|","Nao Autorizado");
  else if($4 ==  "TCP_DENIED/402")   printf("\033[1;40;31m %-25s \033[1;0m|","Pagamento Requisitado");
  else if($4 ==  "TCP_DENIED/403")   printf("\033[1;40;31m %-25s \033[1;0m|","Requisicao Negada");
  else if($4 ==  "TCP_DENIED/404")   printf("\033[1;40;31m %-25s \033[1;0m|","Nao Encontrado");
  else if($4 ==  "TCP_DENIED/405")   printf("\033[1;40;31m %-25s \033[1;0m|","Metodo nao Encontrado");
  else if($4 ==  "TCP_DENIED/406")   printf("\033[1;40;31m %-25s \033[1;0m|","Nao Aceito");
  else if($4 ==  "TCP_DENIED/407")   printf("\033[1;40;31m %-25s \033[1;0m|","Autenticacao no Proxy");
  else if($4 ==  "TCP_DENIED/408")   printf("\033[1;40;31m %-25s \033[1;0m|","Tempo Exedido");
  else if($4 ==  "TCP_DENIED/409")   printf("\033[1;40;31m %-25s \033[1;0m|","Conflito");
else    
  printf( "\033[1;40;32m %-19s \033[m|", $4) ;
  printf( "\033[1;40;1m\033[1;36m %-15s \033[1;0m|",$8);
  printf( "\033[1;40;1m\033[1;30m %-8s \033[1;0m|",$6);
  printf(" %-5s \n ",substr($7,0,67));
}'
}


printf "Nome de Login ou IP: "
read LOGIN
cabecalho;
monta_log;

Nenhum comentário:

Postar um comentário