Seleccionar página

Vía: emerge world

Últimamente andaba buscando la forma de colorear la salida de tail para poder leer más fácilmente los logs de mis máquinas. Ayer encontré esta página de"trucos sucios y rápidos" la solución. Para usar el script, copiarlo a algún lugar del PATH de vuestra máquina (p.e. /usr/local/bin) dadle permiso de ejecución y ejecutrarlo de la siguiente manera:

tail -f /var/log/messages | colorlog.pl

Enseguida notaréis la diferencia 😉

#!/usr/bin/perl

$black    = "�33[30m";
$red      = "�33[31m";
$green    = "�33[32m";
$yellow   = "�33[33m";
$blue     = "�33[34m";
$magenta  = "�33[35m";
$purple   = "�33[35m";
$cyan     = "�33[36m";
$white    = "�33[37m";
$darkgray = "�33[30m";
@word_good=("startingn", "Freeing", "Detected", "starting.", "accepted.n", "authenticated.n", "Ready", "active", "reloading", "saved;", "restarting", "ONLINEn");
@word_warn=("dangling", "closed.n", "Assuming", "root", "rootn", "exitingn", "missing", "Ignored", "adminalert:", "deleting", "OFFLINEn");
@word_bad=("bad");
@word_note=("LOGIN", "DHCP_OFFER", "optimized", "reset:", "unloaded", "disconnected", "connect", "Successful", "registeredn");
@line_good=("up", "DHCP_ACK", "Cleaned", "Initializing", "Starting", "success", "successfully", "alive", "found", "ONLINEn");
@line_warn=("warning:", "WARNING:", "invalid", "obsolete", "bad", "Password", "detected", "timeout", "timeout:", "attackalert:", "wrong", "Lame", "FAILED", "failing", "unknown", "obsolete", "stopped.n", "terminating.", "disabledn", "disabled", "Lost");
@line_bad=("DENY", "lost", "shutting", "dead", "DHCP_NAK", "failure;", "Unable", "inactive", "terminating", "refused", "rejected", "down", "OFFLINEn", "errorn", "ERRORn", "ERROR:", "error", "ERROR", "error:", "failed:");
@daemons=("named");
$col_good = $green;
$col_warn = $yellow;
$col_bad = $red;
$col_note = $purple;
$col_norm =  "�33[00m";
$col_background = "�33[07m";
$col_brighten =  "�33[01m";
$col_underline =  "�33[04m";
$col_blink =   "�33[05m";
$col_default = "$col_norm$white";
print "$col_norm$cyan";
$datespace=0;
mainloop: while (<>)
{
 $thisline = $_;
 $timestamp = substr($_,0,15);
 s/................//;
 @rec = split (/ /, $_);
 $output="$col_brighten$cyan$timestamp";
 $output.=" $col_brighten$blue$rec[0]";
 if ($rec[1] eq "last")
 {
  $output.="$col_norm$green last message repeated ";
  $output.="$col_brighten$rec[4]$col_norm$green timesn";
  print "$output$col_default";
  next mainloop;
 }
 if ($rec[1] =~ /[(d+)]:/)
 {
  my($pid) = $1;
  $rec[1]=~s/[$1]:// ;
  $output .= "$col_norm$green $rec[1]" .
             "$col_brighten$green[";
  $output .= "$col_brighten$white$pid" .
             "$col_brighten$green]: ";
 }
 else {
  $output .= "$col_norm$green $rec[1] ";
 }
 $restcolor="$col_norm$cyan";
 $restoftheline="";
 for ($therest=(2); $therest<=$#rec; $therest++)
 { $highlight=0;
  for ($i=0; $i<=$#word_good; $i++)
  { if ($word_good[$i] eq $rec[$therest])
   { $restoftheline.="$col_brighten$col_good"; $highlight=1; }
  }
  for ($i=0; $i<=$#word_warn; $i++)
  { if ($word_warn[$i] eq $rec[$therest])
   { $restoftheline.="$col_brighten$col_warn"; $highlight=1; }
  }
  for ($i=0; $i<=$#word_bad; $i++)
  { if ($word_bad[$i] eq $rec[$therest])
   { $restoftheline.="$col_brighten$col_bad"; $highlight=1; }
  }
  for ($i=0; $i<=$#word_note; $i++)
  { if ($word_note[$i] eq $rec[$therest])
   { $restoftheline.="$col_brighten$col_note"; $highlight=1; }
  }
  for ($i=0; $i<=$#line_good; $i++)
  { if ($line_good[$i] eq $rec[$therest])
   { $restcolor="$col_norm$col_good";
     $restoftheline.="$col_brighten$col_good"; $highlight=1; }
  }
  for ($i=0; $i<=$#line_warn; $i++)
  { if ($line_warn[$i] eq $rec[$therest])
   { $restcolor="$col_norm$col_warn";
     $restoftheline.="$col_brighten$col_warn"; $highlight=1; }
  }
  for ($i=0; $i<=$#line_bad; $i++)
  { if ($line_bad[$i] eq $rec[$therest])
   { $restcolor="$col_norm$col_bad";
     $restoftheline.="$col_brighten$col_bad"; $highlight=1; }
  }
  $restoftheline.="$rec[$therest] ";
  if ($highlight == 1)
  { $restoftheline.=$restcolor; }
 }
 $output.="$restcolor$restoftheline";
 print "$output$col_default�33[1G";
}
exit(0);