Pra quem não usa WhatsUp e Nagios mas tem um *inx.

Publicado por Henrique Aschenbrenner 10/03/2006

[ Hits: 7.296 ]

Homepage: NONE

Download monitor




Pessoal, primeiramente uma boa noite, este é o meu primeiro Script publicado na VOL e eu gostaria que me reportasem seus experimentos com o script que desenvolvi. Bom básicamente é um script que monitora hosts através de ping e envia e-mail para notificar o "host down", mas ele é um pouquinho inteligente. Ele é composto por 3 arquivos, o script própriamente dito, um arquivo onde irá conter os nomes dos hosts e IP`s e um arquivo temporário. Bom vamos lá ...

  



Esconder código-fonte

*****arquivo monitor********

#!/bin/ksh
#Script de monitoracao dos servidores do datacenter
#Criado por Henrique Aschenbrenner em 13/02/2003
#Versao 0.1.1.1.1.1.1.1.1.0

#________________________________________________
#Declaracao de variaveis

#arqivos de log

tela="/var/tmp/operador/henrique/logs/tela.log"
infor="/var/tmp/operador/henrique/logs/infor.log"
monit="/var/tmp/operador/henrique/logs/monit.log"
monit1="/var/tmp/operador/henrique/logs/monit1.log"
warning="/var/tmp/operador/henrique/logs/warning.log"

#arquivos de informacao

lista="/var/tmp/operador/henrique/progs/lista.inf"
lista1="/var/tmp/operador/henrique/progs/lista1.inf"

count=0
limit=1
hosts=79
a=2
message=""

#_______________________________________________
#Criando arquivos

cp /var/tmp/operador/henrique/progs/listaB.inf /var/tmp/operador/henrique/progs/lista.inf
cp /var/tmp/operador/henrique/progs/listaB.inf /var/tmp/operador/henrique/progs/lista1.inf

#_____________________________________________
#Laco de repeticao infinito

while [ $count -le $limit ]
do
   {
   
   a=2
   clear
   echo >$tela
   echo >$monit1
   echo >$monit
   echo >$lista1
#___________________________________________
#Enquanto a nao for host fassa

   while [ $a -le $hosts ]
   do
      for i in "`awk -F: 'BEGIN { b='$a' } { linha[NR]=$1} END { print  linha[b] }' $lista`"
      do
#__________________________________________
#variaveis de tempo

         time1=`date '+%H:%M'`
         time=`date '+%d/%m/%y'`
#___________________________________________
#filtrando arquivo e criando variaveis

         t="`awk -F: 'BEGIN {b='$a'}{ linha[NR]=$1}END{print linha[b]}' $lista`"
         var="`awk -F: 'BEGIN {b='$a'}{ linha[NR]=$2}END{print linha[b]}' $lista`"
         u="`awk -F: 'BEGIN {b='$a'}{ linha[NR]=$3}END{print linha[b]}' $lista`"
         v="`awk -F: 'BEGIN {b='$a'}{ linha[NR]=$4}END{print linha[b]}' $lista`"

         p="`ping  $var > /dev/null ; echo $?`"
         hora=`date +%H`
         min=`date +%M`
      if [ $p -eq $v ]
         then
             {
              if [ $p -eq 1 ]
            then
                {
                  p=`expr $v + 1`
                  echo "${t}:${var}:${u}:${p}">> $lista1
                }
            else
                {
                                 echo "${t}:${var}:${u}:${p}">> $lista1
                }
              fi
                    }
         else
             {
               if [ $p -eq 0 ]
               then
                   {
                echo "${t}:${var}:${u}:${p}">> $lista1
                   }
               else
                   {
               if [ $p -eq 1 -a $v -eq 2 ]
                  then
                       {
                         p=2
                         echo "${t}:${var}:${u}:${v}">> $lista1
                       }
                  else
                       {
                         p=`expr $v + 1`
                         echo "${t}:${var}:${u}:${p}">> $lista1
                       }
               fi
                   }
            fi
             }
      fi

         message="${time} ${time1} | HOST: $i | STATUS:     "
#___________________________________________
#Testando as condicoes entre p e v e comparando com a log 

         if [ $p -eq $v ]
            then
               {
                  if [ $p -eq 0 ]
                     then
                        {
                           message="${message}  OK "
                        }
                     else
                        {
                           message="${message} Ainda fora do ar..."
                        }
                     fi
               }
            else
               {
                  if [ $p -eq 0 ]
                     then
                        {
                           if [ $v -eq 1 ]
                                then
                                {
                             message="${message}  OK "
                                }
                           else
                                {
                           message="${message} OK"
                           echo $message >> $monit1
                           mailx -s "Server Up - $i" -r DATACENTER@elektro.com.br $u < $monit1
                           sleep 1
                           echo $message >> $infor
                           echo > $monit1
                                }
                           fi
                        }
                     else
                        {
                           if [ $p -eq 2 ]
                             then
                            {
                           message="${message} Fora do ar..."
                           echo $message >> $monit1
                           mailx -s "Server Down - $i" -r DATACENTER@elektro.com.br $u < $monit1
                           sleep 1
                           echo $message >> $infor
                           echo > $monit1
                            }
                             else
                            {
                           message="${message} Atencao, possivel desconecxao..."
                           echo $message >> $warning
                        #   mailx -s "Server Warning - $i" -r DATACENTER@elektro.com.br $u < $monit1
                           
                           sleep 1
                           echo $message >> $infor
                           echo > $monit1
                            }
                           fi
                        }
                  fi
               }
         fi
         echo $message 
         done 
         a=`expr $a + 1`
         done
#______________________________________
#parada de 3 minutos
      sleep 180
#____________________________________________________
#Mesma coisa que a parte de cima

   clear
   a=2
   echo >$tela
   echo >$monit1
   echo >$monit
   echo >$lista
   while [ $a -le $hosts ]
   do
      for i in "`awk -F: 'BEGIN { b='$a' } { linha[NR]=$1} END { print  linha[b] }' $lista1`"
                do
                        time1=`date '+%H:%M'`
                        time=`date '+%d/%m/%y'`

         t="`awk -F: 'BEGIN {b='$a'}{ linha[NR]=$1}END{print linha[b]}' $lista1`"
                        var="`awk -F: 'BEGIN {b='$a'}{ linha[NR]=$2}END{print linha[b]}' $lista1`"
                        u="`awk -F: 'BEGIN {b='$a'}{ linha[NR]=$3}END{print linha[b]}' $lista1`"
                        v="`awk -F: 'BEGIN {b='$a'}{ linha[NR]=$4}END{print linha[b]}' $lista1`"


         p="`ping -c 2 $var > /dev/null ; echo $?`"
         hora=`date +%H`
         min=`date +%M`
      if [ $p -eq $v ]
         then
             {
              if [ $p -eq 1 ]
            then
                {
                  p=`expr $v + 1`
                  echo "${t}:${var}:${u}:${p}">> $lista
                }
            else
                {
                                 echo "${t}:${var}:${u}:${p}">> $lista
                }
              fi
                    }
         else
             {
               if [ $p -eq 0 ]
               then
                   {
                echo "${t}:${var}:${u}:${p}">> $lista
                   }
               else
                   {
               if [ $p -eq 1 -a $v -eq 2 ]
                  then
                       {
                         p=2
                         echo "${t}:${var}:${u}:${v}">> $lista
                       }
                  else
                       {
                         p=`expr $v + 1`
                         echo "${t}:${var}:${u}:${p}">> $lista
                       }
               fi
                   }
             fi
             }
      fi

         message="${time} ${time1} | HOST: $i | STATUS:     "


         if [ $p -eq $v ]
            then
               {
                  if [ $p -eq 0 ]
                     then
                        {
                           message="${message}  OK "
                        }
                     else
                        {
                           message="${message} Ainda fora do ar..."
                        }
                  fi
               }
            else
               {
                  if [ $p -eq 0 ]
                     then
                        {
                               if [ $v -eq 1 ]
                          then
                           {
                              message="{message}  OK "
                           }
                          else
                           {
                           message="${message} OK"
                           echo $message >> $monit
                           mailx -s "Server Up - $i" -r DATACENTER@elektro.com.br $u < $monit
                           sleep 1
                           echo $message >> $infor
                           echo > $monit
                           }
                           fi
                        }
                     else
                        {
                           if [ $p -eq 2 ]
                             then
                            {
                           message="${message} Fora do ar..."
                           echo $message >> $monit
                           mailx -s "Server Down - $i" -r DATACENTER@elektro.com.br $u < $monit
                           sleep 1
                           echo $message >> $infor
                           echo > $monit
                            }
                             else
                            {
                           message="${message} Atencao, possivel desconecxao..."
                           echo $message >> $warning 
                        #   mailx -s "Server Warning - $i" -r DATACENTER@elektro.com.br $u < $monit 
                           
                           sleep 1
                           echo $message >> $infor
                           echo $message > $monit
                            }
                            fi
                        }
                  fi
               }
         fi
         echo $message 
         done 
         a=`expr $a + 1`
         done
      sleep 180
         um
   }
done



*****arquivo listaB.inf************

nome_do_servidor:IP_do_servidor:e_mail_para_notificacao:0 

********************************

O Zero no final da linha mensurada acima é uma flag que corresponde :

0 host up
1 host não respondeu ao primeiro ping
2 host down

Scripts recomendados

RSS para TAGS do CVS

Script para atualizar o sistema

Editor de configurações para Tint2

Bashblog v3.0 - cria um microblog em HTML5

Monitoramento de usuário via screenshots


  

Comentários
[1] Comentário enviado por aroliveira em 20/05/2007 - 11:07h

Não entendi o formato do arquivo de hosts. Podes explicar ? Poderias colocar uma explicação mais detalhada ?


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts