lilalo

diff l3-agent @ 155:8ee5e59f1bd3

Локальное хранение и анализ данных с помощью SQlite

Очень много изменений, касающихся работы с sqlite
и локального использования результатов записи.
Подробнее:
README.l3text
author Igor Chubin <igor@chub.in>
date Tue Mar 16 20:05:30 2010 +0200 (2010-03-16)
parents 822b36252d7f
children
line diff
     1.1 --- a/l3-agent	Tue Jun 23 01:15:02 2009 +0300
     1.2 +++ b/l3-agent	Tue Mar 16 20:05:30 2010 +0200
     1.3 @@ -10,6 +10,7 @@
     1.4  use Text::Iconv;
     1.5  use Time::Local 'timelocal_nocheck';
     1.6  use IO::Socket;
     1.7 +use DBI;
     1.8  
     1.9  use lib "/etc/lilalo";
    1.10  use l3config;
    1.11 @@ -176,8 +177,7 @@
    1.12  {
    1.13      my $cline = $_[0];
    1.14      my @lists = split /\;/, $cline;
    1.15 -    
    1.16 -    
    1.17 +
    1.18      my @commands = ();
    1.19      for my $list (@lists) {
    1.20          push @commands, split /\|/, $list;
    1.21 @@ -344,7 +344,7 @@
    1.22          my $tty = $1;
    1.23          my %cl;
    1.24          my $last_output_length=0;
    1.25 -	my $saved_output;
    1.26 +        my $saved_output;
    1.27          while (<FILE>) {
    1.28              $commandlines_processed++;
    1.29  
    1.30 @@ -357,7 +357,8 @@
    1.31                  $commandlines_loaded++;
    1.32                  $last_output_length=0;
    1.33  
    1.34 -                # Previous command
    1.35 +                # Сохраняем часть выполненного ранее разбора в переменной last_cl,
    1.36 +                # которую мы дополним некоторой информацией и сохраним позже
    1.37                  my %last_cl = %cl;
    1.38                  my $this_line = $1;
    1.39                  my $err = $2 || "";
    1.40 @@ -425,7 +426,6 @@
    1.41                  $last_cl{"err"}=$err;
    1.42                  $last_cl{"err"}=130 if $err eq "^C";
    1.43  
    1.44 -
    1.45                  # Output
    1.46                  if (!$last_cl{"suppress_output"} || $last_cl{"err"}) {
    1.47                      for (my $i=0; $i<$Config{"terminal_height"}; $i++) {
    1.48 @@ -570,8 +570,19 @@
    1.49                  }
    1.50  
    1.51                  $vt{$local_session_id}->reset();
    1.52 -		$saved_output="";
    1.53 +                $saved_output="";
    1.54  
    1.55 +# Обработка команд с одинаковым временем
    1.56 +# Скорее всего они набраны с помощью tab-completion
    1.57 +                if ((defined($last_cl{time}) 
    1.58 +                     && defined($cl{time})
    1.59 +                     && $last_cl{time} == $cl{time}) 
    1.60 +                    && 
    1.61 +                    (defined($last_cl{nonce}) 
    1.62 +                     && defined($cl{nonce}) 
    1.63 +                     && $last_cl{nonce} == $cl{nonce})) {
    1.64 +                    $last_cl{"tab"}=1;
    1.65 +                };
    1.66  
    1.67                  # Changing encoding 
    1.68                  for (keys %last_cl) {
    1.69 @@ -675,21 +686,109 @@
    1.70      $text =~ s/&/&amp;/g;
    1.71      $text =~ s/</&lt;/g;
    1.72      $text =~ s/>/&gt;/g;
    1.73 +    $text =~ s/"/&quot;/g;
    1.74      print $TO $text;
    1.75  }
    1.76  
    1.77 +sub strq
    1.78 +{
    1.79 +    my $text = join "", @_;
    1.80 +    $text =~ s/&/&amp;/g;
    1.81 +    $text =~ s/</&lt;/g;
    1.82 +    $text =~ s/>/&gt;/g;
    1.83 +    $text =~ s/"/&quot;/g;
    1.84 +    return $text;
    1.85 +}
    1.86  
    1.87  =cut 
    1.88  Вывести результат обработки журнала.
    1.89  =cut
    1.90  
    1.91 +sub print_commands_to_sqlite_cache
    1.92 +{
    1.93 +    my $output_filename=$_[0];
    1.94 +    my $db = DBI->connect("dbi:SQLite:$output_filename", "", "",
    1.95 +                          {RaiseError => 1, AutoCommit => 1});
    1.96 +
    1.97 +    my $cl;
    1.98 +    for my $i (@Command_Lines_Index) {
    1.99 +        my $val;
   1.100 +        my $var_list;
   1.101 +        my $values_list;
   1.102 +        my $create_var_list;
   1.103 +        my $sql;
   1.104 +        $cl = $Command_Lines[$i];
   1.105 +
   1.106 +        $cl->{l3cd}=$Config{l3cd} if $Config{"l3cd"};
   1.107 +        for my $element (qw(
   1.108 +            local_session_id
   1.109 +            history
   1.110 +            uid
   1.111 +            pid
   1.112 +            time
   1.113 +            pwd
   1.114 +            raw_start
   1.115 +            raw_output_start
   1.116 +            raw_end
   1.117 +            raw_file
   1.118 +            tty
   1.119 +            err
   1.120 +            last_command
   1.121 +            nonce
   1.122 +            l3cd
   1.123 +            tab
   1.124 +            )) {
   1.125 +            if (defined($cl->{"$element"})) {
   1.126 +                $val = $cl->{"$element"};
   1.127 +            }
   1.128 +            else {
   1.129 +                $val = ""
   1.130 +            };
   1.131 +            $var_list .= "$element,";
   1.132 +            $values_list .= '"'.$val."\",";
   1.133 +            $create_var_list .= ", $element TEXT";
   1.134 +        }
   1.135 +        for my $element (qw(
   1.136 +            prompt
   1.137 +            cline
   1.138 +            output
   1.139 +            )) {
   1.140 +            if (defined($cl->{"$element"})) {
   1.141 +                $val = $cl->{"$element"};
   1.142 +            }
   1.143 +            else {
   1.144 +                $val = ""
   1.145 +            };
   1.146 +            $var_list .= "$element,";
   1.147 +            $values_list .= '"'.strq($val)."\",";
   1.148 +            $create_var_list .= ", $element TEXT";
   1.149 +        }
   1.150 +        if ($cl->{"diff"}) {
   1.151 +            $val = strq(${$Diffs{$cl->{"diff"}}}{"text"});
   1.152 +        }
   1.153 +        else {
   1.154 +            $val="";
   1.155 +        }
   1.156 +        $var_list .= "diff,";
   1.157 +        $create_var_list .= ", diff TEXT";
   1.158 +        $values_list .= '"'.$val."\",";
   1.159 +
   1.160 +        $db->do("CREATE TABLE IF NOT EXISTS commands (id INTEGER PRIMARY KEY $create_var_list)");
   1.161 +        #print "CREATE TABLE commands (id INTEGER PRIMARY KEY $create_var_list)\n";
   1.162 +        $values_list =~ s/,$//;
   1.163 +        $var_list =~ s/,$//;
   1.164 +        $sql = "INSERT INTO commands ($var_list) VALUES($values_list) ";
   1.165 +        print "$sql\n";
   1.166 +        $db->do($sql);
   1.167 +    }
   1.168 +}
   1.169 +
   1.170  sub print_command_lines
   1.171  {
   1.172      my $output_filename=$_[0];
   1.173      open(OUT, ">>", $output_filename)
   1.174          or die "Can't open $output_filename for writing\n";
   1.175  
   1.176 -
   1.177      my $cl;
   1.178      my $in_range=0;
   1.179      for my $i (@Command_Lines_Index) {
   1.180 @@ -704,17 +803,12 @@
   1.181              next;
   1.182          }
   1.183          next if ($Config{"from"} && $Config{"to"} && !$in_range) 
   1.184 -            ||
   1.185 -                ($Config{"skip_empty"} =~ /^y/i && $cl->{"cline"} =~ /^\s*$/ )
   1.186 -            ||
   1.187 -            ($Config{"skip_wrong"} =~ /^y/i && $cl->{"err"} != 0)
   1.188 -            ||
   1.189 -            ($Config{"skip_interrupted"} =~ /^y/i && $cl->{"err"} == 130);
   1.190 -        
   1.191 +             || ($Config{"skip_empty"} =~ /^y/i && $cl->{"cline"} =~ /^\s*$/ )
   1.192 +             || ($Config{"skip_wrong"} =~ /^y/i && $cl->{"err"} != 0)
   1.193 +             || ($Config{"skip_interrupted"} =~ /^y/i && $cl->{"err"} == 130);
   1.194 +
   1.195          # Вырезаем из вывода только нужное количество строк
   1.196 -
   1.197          my $output="";
   1.198 -
   1.199          if (!grep ($_ eq $cl->{"last_command"}, @{$Config{"full_output_commands"}})
   1.200              && ($Config{"head_lines"} 
   1.201              || $Config{"tail_lines"})) { 
   1.202 @@ -730,20 +824,20 @@
   1.203              if ($start < 0) {
   1.204                  $start=0;
   1.205                  $mark=0;
   1.206 -            }   
   1.207 +            }
   1.208              if ($start < $Config{"cache_head_lines"}) {
   1.209                  $start=$Config{"cache_head_lines"};
   1.210                  $mark=0;
   1.211 -            }   
   1.212 +            }
   1.213              $output .= $Config{"skip_text"}."\n" if $mark;
   1.214              for ($i=$start; $i<= $#lines; $i++) {
   1.215                  $output .= $lines[$i]."\n";
   1.216              }
   1.217 -        } 
   1.218 +        }
   1.219          else {
   1.220              # Full output
   1.221              $output .= $cl->{"output"};
   1.222 -        }   
   1.223 +        }
   1.224  
   1.225          # Совместимость с labmaker
   1.226  
   1.227 @@ -759,7 +853,6 @@
   1.228          # timelocal(            $sec,      $min,      $hour,      $mday,$mon,$year);
   1.229          $cl->{time} ||= timelocal_nocheck($cl->{sec},$cl->{min},$cl->{hour},$cl->{day},0,$year);
   1.230  
   1.231 -
   1.232          # Начинаем вывод команды
   1.233          print OUT "<command>\n";
   1.234          print OUT "<l3cd>$Config{l3cd}</l3cd>\n" if $Config{"l3cd"};
   1.235 @@ -917,6 +1010,7 @@
   1.236          load_command_lines($Config{"input"}, $Config{"input_mask"});
   1.237          sort_command_lines;
   1.238          #process_command_lines;
   1.239 +        print_commands_to_sqlite_cache($Config{"cache_sqlite"});
   1.240          print_command_lines($Config{"cache"});
   1.241      } 
   1.242      else {
   1.243 @@ -975,6 +1069,7 @@
   1.244              if (@Command_Lines) {
   1.245                  sort_command_lines;
   1.246                  #process_command_lines;
   1.247 +                print_commands_to_sqlite_cache($Config{"cache_sqlite"});
   1.248                  print_command_lines($Config{"cache"});
   1.249              }
   1.250              save_cache_stat();