Shell para extrair informações de impressoras Brother

Publicado por Incrivel Monstro Verde Cabeludo (última atualização em 03/09/2018)

[ Hits: 2.406 ]

Download le_estado_impressoras_brother.sh




Este script, muito simples, extrai informações de impressoras Brother, como nível do toner (com nivel de alerta), contador de páginas, páginas impressas no periodo, e monta um página html para consulta.

Foi testado com vários modelos de Brother (mas não todos) e deve rodar no cron do linux, para buscar dados da impressora e gerar estatísticas. Opcionalmente, pode fazer FTP para algum site, também.

Mude as variáveis no inicio do script, de acordo com a sua preferência, e informe o IP das impressoras a serem consultadas,  conforme o modelo.

Utilizo na empresa em que trabalho, que possui mais de 30 impressoras e serve para acompanhar uso dos usuários e o nível dos toners, para substituição.

P.S.: para que o script pegue o nome/contato da impressora, é necessário, na configuração da impressora, feita via browser, configurar as opções "Contact" e "Location".

  



Esconder código-fonte

#!/bin/bash 
# (C) serginho was here 20out2016 - IncrivelMonstroVerdeCabeludo@hotmail.com
#
# Script simples para gerar informacoes (Impressora, nivel de toner, paginas impressas...)
# a informacao de paginas impressas, sera sempre a partir do primeiro dia do mes (que houver leitura da impressora)
# o script precisa rodar no cron
# ex: 30 8,10,12,16 * * 1-5   /usr/local/bin/le_estado_impressoras_brother.sh
# 
# Paginas impressoras BROTHER - gera estatistica e envia por ftp ao site 
# http://localhost/status_impressoras.html 
#
# Variaveis configuraveis
FILE_TEMP1="/tmp/x1"
FILE_TEMP2="/tmp/x2"
# onde ficara pagina html gerada
DIR_HTML="/tmp"
# nome do arquivo html
HTML="status_impressoras.html"
ARQ_HTML="$DIR_HTML/$HTML"
# arquivo que guardara estatisticas das impressoras
ARQ_CSV="/tmp/toner_level.txt"
# Percentual de alerta toner. Se < que ...
NIVEL_ALERTA=20
#
HORA=`date +%H`;
CONTGERAL=0
CONTA_PAGINAS_TOTAL=0
# Coloque aqui os IPs das impressoras segundo o modelo da mesma
# para modelos DCP-L5652DN e compativeis
arrayIP=('192.168.77.80' '192.168.77.88' '192.168.77.224' '192.168.77.225' '192.168.77.115' '192.168.77.146' '192.168.77.144' '192.168.77.86' '192.168.77.162');
# para modelos MFC-J6520DW e compativeis (color comunicacao)
arrayIP2=('192.168.77.168');
# para modelos 7440 e 8085 e 8080  -  admin/access
arrayIP3=('192.168.77.128' '192.168.77.244' '192.168.77.214' '192.168.77.161' '192.168.77.67' '192.168.77.179' '192.168.77.141' )
arrayIP3nome=('Almoxarifado Tipo4' 'Suporte Tipo4' 'Tecnica Tipo2' 'Vendas Tipo4' 'RH Tipo2' 'Atendimento' 'Compras Color Tipo 1')
# para lexmark
arrayIP4=('192.168.77.149')
# para modelo DCP-8157DN
arrayIP5=('192.168.77.215' '192.168.77.222' '192.168.77.211')
# -------------------------

#------ FUNCOES --------
guarda_em_arquivo () {
   DATE=`date +%d-%m-%Y`
   TL=$(($4 * 100/50))
   #soh guarda no arquivo quando hora <= 10
#   if [ $HORA -le 8 ]; then echo "$DATE, $1, $2, $3, $TL" >> $ARQ_CSV; fi
   # guarda sempre
   echo "$DATE, $1, $2, $3, $TL" >> $ARQ_CSV
}

CONTA_PAGINA() {
   ip=$1
   LAST_CONT_PAG=$2
   mes=`date +%m`
   ano=`date +%Y`
   FILE=$ARQ_CSV
      
        pattern="$mes-$ano";
        result=`grep $ip $FILE | grep $pattern | head -1`

   DT_INI=`echo $result | cut -d\, -f1`
   PAG_INI=`echo $result | cut -d\, -f4`

        result=`grep $ip $FILE | grep $pattern | tail -1`
   DT_FIM=`echo $result | cut -d, -f1`
   PAG_FIM=`echo $result | cut -d, -f4`
   # pega contagem atual, senao busca do arquivo (pq nem todas consultas sao gravadas)
   if [ $LAST_CONT_PAG > 0 ]; then PAG_FIM=$LAST_CONT_PAG; fi
        CONT=$((PAG_FIM-PAG_INI))
   CONTA_PAGINAS_TOTAL=$((CONTA_PAGINAS_TOTAL+CONT))
}

MONTA_PAGINA() {
   i=$1
   LOCATION=$2
   PAGINAS_IMPRESSAS=$3
   TN=$4
   
   echo "<tr><td>(<a href=http://$i>$i</a>) - <b>$LOCATION</b></td>" >> $ARQ_HTML
   echo "<td align=right><b>$PAGINAS_IMPRESSAS</b></td>" >> $ARQ_HTML
   echo -n "<td>"  >> $ARQ_HTML

   # -- CALC toner level
   if [ $TN -le $NIVEL_ALERTA ]; then 
      TXT_TN="<font color=red>$TN %</font>" 
   else 
      TXT_TN="$TN %"
   fi

   echo  "$TXT_TN </td>"  >> $ARQ_HTML
   guarda_em_arquivo "$LOCATION" $i $PAGINAS_IMPRESSAS $TONER_LEVEL

   # chama contador
   CONTA_PAGINA $i $PAGINAS_IMPRESSAS
   echo "<td><font face=arial size=1>Entre $DT_INI ($PAG_INI) e $DT_FIM ($PAG_FIM):</font> $CONT </td></tr>" >> $ARQ_HTML

   if [ -f $FILE_TEMP1 ]; then rm $FILE_TEMP1; fi
   if [ -f $FILE_TEMP2 ]; then rm $FILE_TEMP2; fi

   ((CONTGERAL++))
}

# end FUNCTION
#----------------------------------------------------
# monta cabecalho html
echo "<html><title>Status Impressoras</title>
 <head>
  <meta charset=\"UTF-8\">
</head> 
<style> /* unvisited link */
a:link {
     color: black;
     text-decoration: none;
}
a:visited {
         color: black;
     text-decoration: none;
}
a:hover {
             color: black;
     text-decoration: underline;
}
a:active {
                 color: black;
     text-decoration: none;
}
table-hover thead tr:hover th, .table-hover tbody tr:hover td {
    background-color: #6699ff;
}
</style>

<body>
<font face=arial size=5>Relatorio gerado em `date` <font size=3>
<table class=\"table table-hover\" style=\"border:1px solid black;\">
   <tr style=\"border:1px solid black; background-color=#eaeded;\">
      <td>Local</td><td>P&aacute;ginas impressas</td><td>Nivel Toner</td> <TD>Consumo</TD></tr>" > $ARQ_HTML

# IMPRESSORA TIPO 1
for i in "${arrayIP[@]}"
do
   if fping -c 2 $i; then 
      wget --tries=3  http://$i/general/information.html?kind=item  -O $FILE_TEMP1
      PAGINAS_IMPRESSAS=`grep Counter $FILE_TEMP1 | sed 's/<!/\n/g' | grep -A1 Counter | tail -1 | cut -d\> -f4 | cut -d\< -f1`
      wget --tries=3  http://$i/general/status.html  -O $FILE_TEMP2
      TONER_LEVEL=`grep black $FILE_TEMP2 | sed 's/</\n/g' | grep bla -A3 | head -1 | cut -d= -f5 | sed 's/[^0-9]//g'`
      LOCATION=`grep black $FILE_TEMP2 | sed 's/</\n/g' | grep Contac -A3 | tail -1  | cut  -d\>  -f2`

      TN=$(($TONER_LEVEL * 100/56)) 
      MONTA_PAGINA $i $LOCATION $PAGINAS_IMPRESSAS $TN
   
   else
      echo "<tr><td>$i <font color=red>desligada</font></b></td><td><b> - </td><td></td></tr>"  >> $ARQ_HTML
   fi
done

# IMPRESSORA TIPO 2

for i in "${arrayIP2[@]}"
do
   if fping -c 2 $i; then 
      wget --tries=3  http://$i/general/information.html?kind=item  -O $FILE_TEMP1
      PAGINAS_IMPRESSAS=`grep Counter $FILE_TEMP1 | sed 's/</\n/g' | grep -A2  Counter  | tail -1 | cut -d\> -f2`
   
      wget --tries=3  http://$i/general/status.html  -O $FILE_TEMP2
      TONER_LEVEL=`grep black $FILE_TEMP2 | sed 's/</\n/g' | grep bla -A3 | head -1 | cut -d= -f5 | sed 's/[^0-9]//g'`
      LOCATION=`grep black $FILE_TEMP2 | sed 's/</\n/g' | grep Contac -A3 | tail -2  | cut  -d\>  -f2`
   
      TN=$(($TONER_LEVEL * 100/56)) 
      MONTA_PAGINA $i $LOCATION $PAGINAS_IMPRESSAS $TN

   else
      echo "<tr><td>$i <font color=red>desligada</font></b></td><td><b> - </td><td></td></tr>"  >> $ARQ_HTML
   fi
done

# IMPRESSORA TIPO 3
# cheio=&#x25a0;   vazio=&#x25a1
# &#x25a0;&#x25a0;&#x25a0;&#x25a0;&#x25a0;&#x25a0;&#x25a0;&#x25a1;&#x25a1;&#x25a1;
z=0
for i in "${arrayIP3[@]}"
do
   if fping -c 2 $i; then 
      LOCATION=${arrayIP3nome[$z]}
      wget --tries=3  http://$i/etc/mnt_info.html?kind=item  -O $FILE_TEMP1
      PAGINAS_IMPRESSAS=`grep -A2 "Page Counter" $FILE_TEMP1 | tail -1`
      TONER_LEVEL=`grep -A2 Toner $FILE_TEMP1 | head -3 | tail -1 | sed 's/&#x25a0;/@\n/g'  | grep @ | wc -l`

      TN=$(($TONER_LEVEL * 10)) 
      MONTA_PAGINA $i "$LOCATION" $PAGINAS_IMPRESSAS $TN
   else
      echo "<tr><td>$i <font color=red>desligada ${arrayIP3nome[$z]}</font></b></td><td><b> - </td><td></td></tr>"  >> $ARQ_HTML
   fi
   ((z++))
done

# IMPRESSORA TIPO 4
for i in "${arrayIP4[@]}"
do
   if fping -c 2 $i; then 
      wget --tries=3  http://$i/cgi-bin/dynamic/printer/config/reports/devicestatistics.html  -O $FILE_TEMP1
      PAGINAS_IMPRESSAS=`grep -A4 "Folhas impressas" $FILE_TEMP1 | tail -1 | cut -d\> -f8 | cut -d\< -f1 | sed 's/ //g'`
           wget --tries=3  http://$i/cgi-bin/dynamic/topbar.html -O $FILE_TEMP2
           LOCATION=`grep contato $FILE_TEMP2 | cut -d: -f2 | cut -d\< -f1`
           
#      wget --tries=3 http://$i/cgi-bin/dynamic/printer/PrinterStatus.html -O $FILE_TEMP1
#      TONER_LEVEL=`grep Preto $FILE_TEMP1 | cut -d~ -f2 | sed 's/[^0-9]//g'`   

      TN=0
      MONTA_PAGINA $i $LOCATION $PAGINAS_IMPRESSAS $TN
   else
      echo "<tr><td>$i <font color=red>desligada</font></b></td><td><b> - </td><td></td></tr>"  >> $ARQ_HTML
   fi
done

# IMPRESSORA TIPO 5
for i in "${arrayIP5[@]}"
do
   if fping -c 2 $i; then 
      wget --tries=3  http://$i/general/information.html?kind=item  -O $FILE_TEMP1
      PAGINAS_IMPRESSAS=`grep Counter $FILE_TEMP1 | sed 's/</\n/g' | grep -A2  Counter  | tail -1 | cut -d\> -f2`
      TN=`grep Life  $FILE_TEMP1 |  sed 's/</\n/g' | grep -A8 "Drum&#32;Unit" | grep -A2 Remain | tail -1 |  cut -d"." -f1 |  sed 's/[^0-9]//g'`
   
      wget --tries=3  http://$i/general/status.html  -O $FILE_TEMP2
      LOCATION=`grep Contac $FILE_TEMP2 | sed 's/</\n/g' | grep Contac -A2 | tail -1 | cut -d\> -f2`

      MONTA_PAGINA $i $LOCATION $PAGINAS_IMPRESSAS $TN

   else
      echo "<tr><td>$i <font color=red>desligada</font></b></td><td><b> - </td><td></td></tr>"  >> $ARQ_HTML
   fi
done
# ----  ------ #

echo "</table><br><br>" >> $ARQ_HTML

echo "<br><font face=arial size=1>his</font></a> $CONTGERAL $CONTA_PAGINAS_TOTAL" >> $ARQ_HTML

# Quer fazer FTP para algum site ?
# descomente e preencha abaixo
#ftp -v -n >> /tmp/ftpb.log <<END_SESSION
#     open ftp.xxx.com.br
#     user USUARIO SENHA
#     ascii
#     lcd $DIR_HTML
#     put $HTML
#     bye
#END_SESSION

# eof

Scripts recomendados

ddp - dd com porcentagem

Compilação do kernel linux-libre 4.4.6 com cflags -march=native + -Ofast

Simples Verificador de E-mail Gmail

varrer.sh

Faz uma busca no Google e abre o primeiro hit (Estou com Sorte)


  

Comentários
[1] Comentário enviado por Apolo_Linux em 03/11/2018 - 22:42h

Achei interessante e irei testar, onde trabalho todas as impressoras são brother;
Grato por compartilhar.
Att,
Izaquiel Melo.
Suporte em T.I
Viva o Linux.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts