Created
December 20, 2014 20:11
-
-
Save plcosta/f496c4b12b242a6cee04 to your computer and use it in GitHub Desktop.
Linux Derrubar conexões ociosas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################################### | |
# # | |
# Script para matar conexoes ociosas # | |
# # | |
# Nome: LIMPANDO_CONEXAO Versao 0.2 # | |
# by Guilherme Moura de Souza # | |
# Colaboradores: Tulio.Hoffimann # | |
# stack_of # | |
# Diede # | |
# # | |
# Necessario criar o diretorio /u/log # | |
# # | |
# Recomendo rodar atraves do cron do root da # | |
# seguinte forma: */1 * * * * "caminho do script" # | |
# # | |
################################################### | |
LOG=/u/logs/"LIM_CON-"`date +%m%d` | |
LIMPANDO_CONEXAO() { | |
# Digite na frente do "=" o limete de tempo que sera permitido que a | |
# conexao fique ociosa "EM MINUTOS" | |
LIMITE=30 | |
# Identificando o numero de conexoes | |
conexoes=`w -h | grep -c " "` | |
for L in $(seq 1 $conexoes) | |
do | |
#Definindo o PTS/* ou TTY* | |
PTSocioso=`w -h | grep -m $L " " | tail -1 | tr -s " " | cut -d " " -f2` | |
#Buscando o tempo OCIOSO | |
Tempocioso=`w -h | grep -m $L " " | tail -1 | tr -s " " | cut -d " " -f5` | |
#Verificando se alguma conexao esta acima do limite | |
H1=$(echo $Tempocioso | grep "m") | |
S=$(echo $Tempocioso | grep "s") | |
if [ -z $H1 ]; then | |
if [ -z $S ]; then | |
M=$(echo $Tempocioso | cut -d ":" -f1) | |
if [ $M -gt $LIMITE ]; then | |
skill -kill -v $PTSocioso >> $LOG | |
touch /u/COMECA_DE_NOVO | |
fi | |
fi; else | |
H2=$(echo $H1 | cut -d ":" -f1) | |
H3=$(echo $H1 | cut -d ":" -f2 | cut -d "m" -f1) | |
H=`awk 'BEGIN {print '$H2'*60+'$H3'}'` | |
if [ $H -gt $LIMITE ]; then | |
skill -kill -v $PTSocioso >> $LOG | |
touch /u/COMECA_DE_NOVO | |
fi | |
fi | |
done | |
if [ -e /u/COMECA_DE_NOVO ] | |
then rm -f /u/COMECA_DE_NOVO | |
LIMPANDO_CONEXAO | |
fi | |
} | |
# Iniciando Rotina e gerando LOGS | |
echo " ==================================== " >> $LOG | |
echo " Inicio da Varredura as "`date +%H:%M:%S` >> $LOG | |
rm -f /u/COMECA_DE_NOVO | |
LIMPANDO_CONEXAO | |
echo " Fim da Varredura as "`date +%H:%M:%S` >> $LOG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment