Coleta de utilização de rede utilizando SAR (systat package)

Publicado por Hertz Raya Zocolan Silva (última atualização em 11/07/2018)

[ Hits: 1.563 ]

Download coleta_network.sh




Script para coleta de informações de rede. Informações coletadas:

Data
Hora
Colisao
Pacotes RX (ifconfig)
Pacotes TX (ifconfig)
Pacotes RX (sar)
Pacotes TX (sar)
Active Connections Openings (sar)
Passive Connection Openings (sar)
Failed Connection Attempts (sar)
Connection Resets Received (sar)
Connections Established (sar)
Segments Received (sar)
Segments Out (sar)
Segments Retransmited (sar)
Bad Segments Received (sar)

Formato do arquivo de saída: CSV com (;) separando os campos

  



Esconder código-fonte

#!/bin/bash
#
# Script : /usr/local/scripts/coleta_network.sh
# Feito  : Hertz S. ( 11/04/2016 )
# Obs    : Script para coleta de informacoes de utilizacao
#          de espaco em network
#

#exec 1>/dev/null
#exec 2>&1

# 
# Rotina de Inclusao de Variaveis Globais -------------------------------------
#

PROCESS_NAME="Coleta de Informacoes de Rede"
      DEVICE="$1"

if [ -z ${DEVICE} ]
then
   echo -e "\nSintaxe: $0 [NIC_NAME}\n"
   exit 23
else
   ifconfig ${DEVICE} 1>/dev/null 2>&1
   ERRO="$?"
      if [ ${ERRO} -gt 0 ]
      then
         echo -e "\nNIC ${DEVICE} invalida ! ...\n"
         echo -e "\nNIC valida(s) : `ifconfig -a  | grep flag | grep -v LOOP | cut -d\: -f1,1  | sed 'N ; s/\n/ , /g'`\n"
         exit 23
      else
         conf_nic="`ifconfig ${DEVICE} | grep inet | grep -v prefi | wc -l`" ; conf_nic="`echo ${conf_nic}`"
            if [ "${conf_nic}" -eq 0 ]
            then
               echo -e "NIC ${DEVICE} sem IP configurado !\n"
               exit 23
             fi
      fi
fi

export PATH=$PATH:/usr/bin
SERVER="`hostname`"
IP_LOCAL="`/sbin/ifconfig | grep \"cast\" | head -1 | awk '{print $2}' | cut -d\: -f2,2`"
DT_FULL="`date +%d/%m/%Y`"
  DT_DT="`date +%d%m%Y`"
DT_HORA="`date +%H:%M:%S`"
  DT_HR="`date +%H%M%S`"
 DT_MES="`date +%m_%Y`"
 DT_ano="`date +%Y`"
 DT_mes="`date +%m`"
 DT_DIA="`date +%a`"

if [ "${DT_DIA}" = "Sun" ] ; then DT_DIA="Domingo" ; fi
if [ "${DT_DIA}" = "Mon" ] ; then DT_DIA="Segunda" ; fi
if [ "${DT_DIA}" = "Tue" ] ; then DT_DIA="Terca"   ; fi
if [ "${DT_DIA}" = "Wed" ] ; then DT_DIA="Quarta"  ; fi
if [ "${DT_DIA}" = "Thu" ] ; then DT_DIA="Quinta"  ; fi
if [ "${DT_DIA}" = "Fri" ] ; then DT_DIA="Sexta"   ; fi
if [ "${DT_DIA}" = "Sat" ] ; then DT_DIA="Sabado"  ; fi

if [ "${DT_mes}" = "01" ] ; then DT_MES_NAME="Janeiro"   ; fi
if [ "${DT_mes}" = "02" ] ; then DT_MES_NAME="Fevereiro" ; fi
if [ "${DT_mes}" = "03" ] ; then DT_MES_NAME="Marco"     ; fi
if [ "${DT_mes}" = "04" ] ; then DT_MES_NAME="Abril"     ; fi
if [ "${DT_mes}" = "05" ] ; then DT_MES_NAME="Maio"      ; fi
if [ "${DT_mes}" = "06" ] ; then DT_MES_NAME="Junho"     ; fi
if [ "${DT_mes}" = "07" ] ; then DT_MES_NAME="Julho"     ; fi
if [ "${DT_mes}" = "08" ] ; then DT_MES_NAME="Agosto"    ; fi
if [ "${DT_mes}" = "09" ] ; then DT_MES_NAME="Setembro"  ; fi
if [ "${DT_mes}" = "10" ] ; then DT_MES_NAME="Outubro"   ; fi
if [ "${DT_mes}" = "11" ] ; then DT_MES_NAME="Novembro"  ; fi
if [ "${DT_mes}" = "12" ] ; then DT_MES_NAME="Dezembro"  ; fi

# Declaracao de Variaveis -----------------------------------------------------

DIRCOL="/usr/local/coleta_`hostname`/network/${DT_MES}"
mkdir -p ${DIRCOL}

#
# Inicio do Script ------------------------------------------------------------
#

FSTXT="${DIRCOL}/${DT_DT}.txt"

/usr/bin/sar -n DEV 1 2 | grep ${DEVICE} > /tmp/sarn.$$
netstat --statistic > /tmp/net.$$

  COLISAO_NIC="`/sbin/ifconfig ${DEVICE} | grep -e col | awk '{print $11}'`"
       RX_NIC="`/sbin/ifconfig ${DEVICE} | grep RX | grep -v err | cut -d\( -f2,2 | cut -d\) -f1,1`"
       TX_NIC="`/sbin/ifconfig ${DEVICE} | grep TX | grep -v err | cut -d\( -f2,2 | cut -d\) -f1,1`"
   NIC_RX_PCK="`cat /tmp/sarn.$$ | tail -1 | grep ${DEVICE} | awk '{print $3}'`"
   NIC_TX_PCK="`cat /tmp/sarn.$$ | tail -1 | grep ${DEVICE} | awk '{print $4}'`" ; yes | rm /tmp/sarn.$$ 2>/dev/null
 ACTIVE_CONNECTIONS_OPENINGS="`cat /tmp/net.$$ | grep \"active connections openings\" | awk '{print $1}'`"
 PASSIVE_CONNECTION_OPENINGS="`cat /tmp/net.$$ | grep \"passive connection openings\" | awk '{print $1}'`"
  FAILED_CONNECTION_ATTEMPTS="`cat /tmp/net.$$ | grep \"failed connection attempts\" | awk '{print $1}'`"
  CONNECTION_RESETS_RECEIVED="`cat /tmp/net.$$ | grep \"connection resets received\" | awk '{print $1}'`"
     CONNECTIONS_ESTABLISHED="`cat /tmp/net.$$ | grep \"connections established\" | awk '{print $1}'`"
           SEGMENTS_RECEIVED="`cat /tmp/net.$$ | grep \"segments received\" | grep -v \"bad\" | awk '{print $1}'`"
           SEGMENTS_SEND_OUT="`cat /tmp/net.$$ | grep \"segments send out\" | awk '{print $1}'`"
       SEGMENTS_RETRANSMITED="`cat /tmp/net.$$ | grep \"segments retransmited\" | awk '{print $1}'`"
       BAD_SEGMENTS_RECEIVED="`cat /tmp/net.$$ | grep \"bad segments received\" | awk '{print $1}'`"

yes | rm /tmp/net.$$ 2>/dev/null

 COLISAO_NIC="`echo ${COLISAO_NIC}`"
 RX_NIC="`echo ${RX_NIC}`"
 TX_NIC="`echo ${TX_NIC}`"
 NIC_RX_PCK="`echo ${NIC_RX_PCK}`"
 NIC_TX_PCK="`echo ${NIC_TX_PCK}`"
 ACTIVE_CONNECTIONS_OPENINGS="`echo ${ACTIVE_CONNECTIONS_OPENINGS}`"
 PASSIVE_CONNECTION_OPENINGS="`echo ${PASSIVE_CONNECTION_OPENINGS}`"
 FAILED_CONNECTION_ATTEMPTS="`echo ${FAILED_CONNECTION_ATTEMPTS}`"
 CONNECTION_RESETS_RECEIVED="`echo ${CONNECTION_RESETS_RECEIVED}`"
 CONNECTIONS_ESTABLISHED="`echo ${CONNECTIONS_ESTABLISHED}`"
 SEGMENTS_RECEIVED="`echo ${SEGMENTS_RECEIVED}`"
 SEGMENTS_SEND_OUT="`echo ${SEGMENTS_SEND_OUT}`"
 SEGMENTS_RETRANSMITED="`echo ${SEGMENTS_RETRANSMITED}`"
 BAD_SEGMENTS_RECEIVED="`echo ${BAD_SEGMENTS_RECEIVED}`"

                 COLISAO_NIC_X="Colisao"
                      RX_NIC_X="Pacotes RX (ifconfig)"
                      TX_NIC_X="Pacotes TX (ifconfig)"
                  NIC_RX_PCK_X="Pacotes RX (sar)"
                  NIC_TX_PCK_X="Pacotes TX (sar)"
 ACTIVE_CONNECTIONS_OPENINGS_X="Active Connections Openings (sar)"
 PASSIVE_CONNECTION_OPENINGS_X="Passive Connection Openings (sar)"
  FAILED_CONNECTION_ATTEMPTS_X="FAiled Connection Attempts (sar)"
  CONNECTION_RESETS_RECEIVED_X="Conection Resets Received (sar)"
     CONNECTIONS_ESTABLISHED_X="Connections Established (sar)"
           SEGMENTS_RECEIVED_X="Segments Received (sar)"
           SEGMENTS_SEND_OUT_X="Segments Out (sar)"
       SEGMENTS_RETRANSMITED_X="Segments Retransmited (sar)"
       BAD_SEGMENTS_RECEIVED_X="Bad Segments Received (sar)"


if [ -f "${FSTXT}" ]
then
   echo $FSTXT existe > /dev/null
   echo "${DT_FULL};${DT_HORA};${COLISAO_NIC};${RX_NIC};${TX_NIC};${NIC_RX_PCK};${NIC_TX_PCK};${ACTIVE_CONNECTIONS_OPENINGS};${PASSIVE_CONNECTION_OPENINGS};${FAILED_CONNECTION_ATTEMPTS};${CONNECTION_RESETS_RECEIVED};${CONNECTIONS_ESTABLISHED};${SEGMENTS_RECEIVED};${SEGMENTS_SEND_OUT};${SEGMENTS_RETRANSMITED};${BAD_SEGMENTS_RECEIVED}" >> ${FSTXT}
else
   echo $FSTXT nao > /dev/null
   echo "[ ${SERVER} ${IP_LOCAL} ] - ${DT_MES_NAME} de ${DT_ano}" > ${FSTXT}
   echo "" >> ${FSTXT}
   echo "Data;Hora;${COLISAO_NIC_X};${RX_NIC_X};${TX_NIC_X};${NIC_RX_PCK_X};${NIC_TX_PCK_X};${ACTIVE_CONNECTIONS_OPENINGS_X};${PASSIVE_CONNECTION_OPENINGS_X};${FAILED_CONNECTION_ATTEMPTS_X};${CONNECTION_RESETS_RECEIVED_X};${CONNECTIONS_ESTABLISHED_X};${SEGMENTS_RECEIVED_X};${SEGMENTS_SEND_OUT_X};${SEGMENTS_RETRANSMITED_X};${BAD_SEGMENTS_RECEIVED_X}" >> ${FSTXT}

   echo "${DT_FULL};${DT_HORA};${COLISAO_NIC};${RX_NIC};${TX_NIC};${NIC_RX_PCK};${NIC_TX_PCK};${ACTIVE_CONNECTIONS_OPENINGS};${PASSIVE_CONNECTION_OPENINGS};${FAILED_CONNECTION_ATTEMPTS};${CONNECTION_RESETS_RECEIVED};${CONNECTIONS_ESTABLISHED};${SEGMENTS_RECEIVED};${SEGMENTS_SEND_OUT};${SEGMENTS_RETRANSMITED};${BAD_SEGMENTS_RECEIVED}" >> ${FSTXT}
fi

#
# Final  do Script ------------------------------------------------------------
#

Scripts recomendados

Script Player com o MPG123

A um XKill, kill grafico com GDialog

Criar pacote Debian do thunderbird de br.mozdev.org

Gerenciador Servidor de Arquivos

Bash RPG


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts