lilalo

view 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 source
1 #!/usr/bin/perl -w
3 #
4 # (c) Igor Chubin, igor@chub.in, 2004-2008
5 #
7 use strict;
8 use POSIX;
9 use Term::VT102;
10 use Text::Iconv;
11 use Time::Local 'timelocal_nocheck';
12 use IO::Socket;
13 use DBI;
15 use lib "/etc/lilalo";
16 use l3config;
18 our @Command_Lines;
19 our @Command_Lines_Index;
20 our %Diffs;
21 our %Sessions;
23 our %Script_Files; # Информация о позициях в скрипт-файлах,
24 # до которых уже выполнен разбор
25 # и информация о времени модификации файла
26 # $Script_Files{$file}->{size}
27 # $Script_Files{$file}->{tell}
29 our $Killed =0; # В режиме демона -- процесс получил сигнал о завершении
31 sub init_variables;
32 sub main;
34 sub load_diff_files;
35 sub bind_diff;
36 sub extract_commands_from_cline;
37 sub load_command_lines;
38 sub sort_command_lines;
39 sub print_command_lines;
40 sub printq;
42 sub save_cache_stat;
43 sub load_cache_stat;
44 sub print_session;
46 sub load_diff_files
47 {
48 my @pathes = @_;
50 for my $path (@pathes) {
51 my $template = "*.diff";
52 my @files = <$path/$template>;
53 my $i=0;
54 for my $file (@files) {
56 next if defined($Diffs{$file});
57 my %diff;
59 # Старый формат имени diff-файла
60 # DEPRECATED
61 if ($file=~m@/(D?[0-9][0-9]?[0-9]?)[^/]*?([0-9]*):([0-9]*):?([0-9]*)@) {
62 $diff{"day"}=$1 || "";
63 $diff{"hour"}=$2;
64 $diff{"min"}=$3;
65 $diff{"sec"}=$4 || 0;
67 $diff{"uid"} = 0 if $path =~ m@/root/@;
69 print "diff loaded: $diff{day} $diff{hour}:$diff{min}:$diff{sec}\n";
71 }
72 # Новый формат имени diff-файла
73 elsif ($file =~ m@.*/([^_]*)_([0-9]+)(.*)@) {
74 $diff{"local_session_id"} = $1;
75 $diff{"time"} = $2;
76 $diff{"filename"} = $3;
77 $diff{"filename"} =~ s@_@/@g;
78 $diff{"filename"} =~ s@//@_@g;
80 print "diff loaded: $diff{filename} (time=$diff{time},session=$diff{local_session_id})\n";
81 }
82 else {
83 next;
84 }
86 # Чтение и изменение кодировки содержимого diff-файла
87 local $/;
88 open (F, "$file")
89 or return "Can't open file $file ($_[0]) for reading";
90 my $text = <F>;
91 if ($Config{"encoding"} && $Config{"encoding"} !~ /^utf-8$/i) {
92 my $converter = Text::Iconv->new($Config{"encoding"}, "utf-8");
93 $text = $converter->convert($text);
94 }
95 close(F);
96 $diff{"text"}=$text;
98 $diff{"path"}=$path;
99 $diff{"bind_to"}="";
100 $diff{"time_range"}=-1;
101 $diff{"index"}=$i;
103 $Diffs{$file} = \%diff;
104 $i++;
105 }
106 }
107 }
110 sub bind_diff
111 {
112 print "Trying to bind diff...\n";
114 my $cl = shift;
115 my $hour = $cl->{"hour"};
116 my $min = $cl->{"min"};
117 my $sec = $cl->{"sec"};
119 my $min_dt = 10000;
121 if (defined($cl->{"diff"})) {
122 print STDERR "Command ".$cl->{time}." is already bound";
123 return;
124 }
126 # Загружаем новые diff-файлы
127 # Это нужно делать непосредственно перед привязкой, поскольку diff'ы могли образоваться только что
128 for my $lab_log (split (/\s+/, $Config{"diffs"} || $Config{"input"})) {
129 load_diff_files($lab_log);
130 }
132 my $diff_to_bind;
133 for my $diff_key (keys %Diffs) {
134 my $diff = $Diffs{$diff_key};
135 next if ($diff->{"local_session_id"}
136 && $cl->{"local_session_id"}
137 && ($cl->{"local_session_id"} ne $diff->{"local_session_id"}));
139 next if ($diff->{"day"} && $cl->{"day"} && ($cl->{"day"} ne $diff->{"day"}));
141 my $dt;
142 if (not $diff->{"time"}) {
143 print STDERR "diff time is 0";
144 print STDERR join(" ", keys(%$diff));
145 print STDERR $diff->{text};
146 }
147 if (not $cl->{"time"}) {
148 print STDERR "cl time is 0";
149 }
150 if ($diff->{"time"} && $cl->{"time"}) {
151 $dt = $diff->{"time"} - $cl->{"time"}
152 }
153 else {
154 $dt=($diff->{"hour"}-$hour)*3600 +($diff->{"min"}-$min)*60 + ($diff->{"sec"}-$sec);
155 }
156 if ($dt >=0 && $dt < $min_dt && !$diff->{"bind_to"}) {
157 $min_dt = $dt;
158 $diff_to_bind = $diff_key;
159 }
160 }
161 if ($diff_to_bind) {
162 print "Approppriate diff found: dt=$min_dt\n";
163 $Diffs{$diff_to_bind}->{"bind_to"}=$cl;
164 $cl->{"diff"} = $diff_to_bind;
165 }
166 else {
167 print STDERR "Diff not found\n";
168 print STDERR "cl{time}",$cl->{time},"\n";
169 }
170 }
173 sub extract_commands_from_cline
174 # Разобрать командную строку $_[1] и возвратить хэш, содержащий
175 # номер первого появление команды в строке:
176 # команда => первая позиция
177 {
178 my $cline = $_[0];
179 my @lists = split /\;/, $cline;
181 my @commands = ();
182 for my $list (@lists) {
183 push @commands, split /\|/, $list;
184 }
186 my %commands;
187 my %files;
188 my $i=0;
189 for my $command (@commands) {
190 $command =~ /\s*(\S+)\s*(.*)/;
191 if ($1 && $1 eq "sudo" ) {
192 $commands{"$1"}=$i++;
193 $command =~ s/\s*sudo\s+//;
194 }
195 $command =~ /\s*(\S+)\s*(.*)/;
196 if ($1 && !defined $commands{"$1"}) {
197 $commands{"$1"}=$i++;
198 };
199 }
200 return %commands;
201 }
203 sub load_command_lines
204 {
205 my $lab_scripts_path = $_[0];
206 my $lab_scripts_mask = $_[1];
208 my $cline_re_base = qq'
209 (
210 (?:\\^?([0-9]*C?)) # exitcode
211 (?:_([0-9]+)_)? # uid
212 (?:_([0-9]+)_) # pid
213 (...?) # day
214 (.?.?) # lab
215 \\s # space separator
216 ([0-9][0-9]):([0-9][0-9]):([0-9][0-9]) # time
217 .\\[50D.\\[K # killing symbols
218 (.*?([\$\#]\\s?)) # prompt
219 (.*) # command line
220 )
221 ';
222 my $cline_re = qr/$cline_re_base/sx;
223 my $cline_re2 = qr/$cline_re_base$/sx;
225 my $cline_re_v2_base = qq'
226 (
227 v2[\#] # version
228 ([0-9]+)[\#] # history line number
229 ([0-9]+)[\#] # exitcode
230 ([0-9]+)[\#] # uid
231 ([0-9]+)[\#] # pid
232 ([0-9]+)[\#] # time
233 (.*?)[\#] # pwd
234 .\\[1024D.\\[K # killing symbols
235 (.*?([\$\#]\\s?)) # prompt
236 (.*) # command line
237 )
238 ';
240 my $cline_re_v2 = qr/$cline_re_v2_base/sx;
241 my $cline_re2_v2 = qr/$cline_re_v2_base$/sx;
243 my $cline_re_v3_base = qq'
244 (
245 v3[\#] # version
246 .*
247 )
248 ';
249 my $cline_re_v3 = qr/$cline_re_v3_base/sx;
251 my $cline_re2_v3_base = qq'
252 (
253 v3[\#] # version
254 ([0-9]+)[\#] # history line number
255 ([0-9]+)[\#] # exitcode
256 ([0-9]+)[\#] # uid
257 ([0-9]+)[\#] # pid
258 ([0-9]+)[\#] # time
259 (.*?)[\#] # pwd
260 (.*?)[\#] # nonce
261 (.*?([\$\#]\\s?)) # prompt
262 (.*) # command line
263 )
264 ';
265 my $cline_re2_v3 = qr/$cline_re2_v3_base$/sx;
268 my %vt; # Хэш виртуальных терминалов. По одному на каждый сеанс
269 my $cline_vt = Term::VT102->new (
270 'cols' => $Config{"terminal_width"},
271 'rows' => $Config{"terminal_height"});
273 my $converter = Text::Iconv->new($Config{"encoding"}, "utf-8")
274 if ($Config{"encoding"} && $Config{"encoding"} !~ /^utf-8$/i);
276 print "Parsing lab scripts...\n" if $Config{"verbose"} =~ /y/;
278 my $file;
279 my $skip_info;
281 my $commandlines_loaded =0;
282 my $commandlines_processed =0;
284 my @lab_scripts = <$lab_scripts_path/$lab_scripts_mask>;
285 for $file (@lab_scripts){
287 # Пропускаем файл, если он не изменялся со времени нашего предудущего прохода
288 my $size = (stat($file))[7];
289 next if ($Script_Files{$file} && $Script_Files{$file}->{size} && $Script_Files{$file}->{size} >= $size);
292 my $local_session_id;
293 # Начальное значение идентификатора текущего сеанса определяем из имени скрипта
294 # Впоследствии оно может быть уточнено
295 $file =~ m@.*/([^/]*)\.script$@;
296 $local_session_id = $1;
298 if (not defined($vt{$local_session_id})) {
299 $vt{$local_session_id} = Term::VT102->new (
300 'cols' => $Config{"terminal_width"},
301 'rows' => $Config{"terminal_height"});
302 }
304 #Если файл только что появился,
305 #пытаемся найти и загрузить информацию о соответствующей ему сессии
306 if (!$Script_Files{$file}) {
307 my $session_file = $file;
308 $session_file =~ s/\.script/.info/;
309 if (open(SESSION, $session_file)) {
310 local $/;
311 my $data = <SESSION>;
312 close(SESSION);
314 for my $session_data ($data =~ m@<session>(.*?)</session>@sg) {
315 my %session;
316 while ($session_data =~ m@<([^>]*?)>(.*?)</\1>@sg) {
317 $session{$1} = $2;
318 }
319 $local_session_id = $session{"local_session_id"} if $session{"local_session_id"};
320 $Sessions{$local_session_id}=\%session;
321 }
323 #Загруженную информацию сразу же отправляем в поток
324 print_session($Config{cache}, $local_session_id);
325 }
326 else {
327 die "can't open session file";
328 }
329 }
331 open (FILE, "$file");
332 binmode FILE;
334 # Переходим к тому месту, где мы окончили разбор
335 seek (FILE, $Script_Files{$file}->{tell}, 0) if $Script_Files{$file}->{tell};
336 $Script_Files{$file}->{size} = $size;
337 $Script_Files{$file}->{tell} = 0 unless $Script_Files{$file}->{tell};
339 $file =~ m@.*/(.*?)-.*@;
341 print "\n+- processing file $file\n| "
342 if $Config{"verbose"} =~/y/;
344 my $tty = $1;
345 my %cl;
346 my $last_output_length=0;
347 my $saved_output;
348 while (<FILE>) {
349 $commandlines_processed++;
351 next if s/^Script started on.*?\n//s;
353 if (/[0-9][0-9]:[0-9][0-9]:[0-9][0-9].\[[0-9][0-9]D.\[K/ && m/$cline_re/) {
354 s/.*\x0d(?!\x0a)//;
355 m/$cline_re2/gs;
357 $commandlines_loaded++;
358 $last_output_length=0;
360 # Сохраняем часть выполненного ранее разбора в переменной last_cl,
361 # которую мы дополним некоторой информацией и сохраним позже
362 my %last_cl = %cl;
363 my $this_line = $1;
364 my $err = $2 || "";
366 $cl{"local_session_id"} = $local_session_id;
367 # Parse new command
368 $cl{"uid"} = $3;
369 #$cl{"euid"} = $cl{"uid"}; # Если в команде обнаружится sudo, euid поменяем на 0
370 $cl{"pid"} = $4;
371 $cl{"day"} = $5;
372 $cl{"lab"} = $6;
373 $cl{"hour"} = $7;
374 $cl{"min"} = $8;
375 $cl{"sec"} = $9;
376 #$cl{"fullprompt"} = $10;
377 $cl{"prompt"} = $11;
378 $cl{"raw_cline"} = $12;
380 {
381 use bytes;
382 $cl{"raw_start"} = tell (FILE) - length($this_line);
383 $cl{"raw_output_start"} = tell FILE;
384 }
385 $cl{"raw_file"} = $file;
387 $cl{"err"} = 0;
388 $cl{"output"} = "";
389 $cl{"tty"} = $tty;
391 $cline_vt->process($cl{"raw_cline"}."\n");
392 $cl{"cline"} = $cline_vt->row_plaintext (1);
393 $cl{"cline"} =~ s/\s*$//;
394 $cl{"cline"} =~ s/.*?[\#\$]\s*//;
395 $cline_vt->reset();
397 my %commands = extract_commands_from_cline($cl{"cline"});
398 #$cl{"euid"}=0 if defined $commands{"sudo"};
399 my @comms = sort { $commands{$a} cmp $commands{$b} } keys %commands;
400 $cl{"last_command"} = $comms[$#comms] || "";
402 if (
403 $Config{"suppress_editors"} =~ /^y/i
404 && grep ($_ eq $cl{"last_command"}, @{$Config{"editors"}})
405 || $Config{"suppress_pagers"} =~ /^y/i
406 && grep ($_ eq $cl{"last_command"}, @{$Config{"pagers"}})
407 || $Config{"suppress_terminal"}=~ /^y/i
408 && grep ($_ eq $cl{"last_command"}, @{$Config{"terminal"}})
409 ) {
410 $cl{"suppress_output"} = "1";
411 }
412 else {
413 $cl{"suppress_output"} = "0";
414 }
415 $skip_info = 0;
418 print " ",$cl{"last_command"};
420 if (grep ($_ eq $last_cl{"last_command"}, @{$Config{"editors"}})) {
421 bind_diff(\%last_cl);
422 }
424 # Error code
425 $last_cl{"raw_end"} = $cl{"raw_start"};
426 $last_cl{"err"}=$err;
427 $last_cl{"err"}=130 if $err eq "^C";
429 # Output
430 if (!$last_cl{"suppress_output"} || $last_cl{"err"}) {
431 for (my $i=0; $i<$Config{"terminal_height"}; $i++) {
432 my $line= $vt{$local_session_id}->row_plaintext($i);
433 next if !defined ($line) ; #|| $line =~ /^\s*$/;
434 $line =~ s/\s*$//;
435 $line .= "\n" unless $line =~ /^\s*$/;
436 $last_cl{"output"} .= $line;
437 }
438 }
439 else {
440 $last_cl{"output"}= "";
441 }
443 $vt{$local_session_id}->reset();
446 # Save
447 if (!$Config{"lab"} || $cl{"lab"} eq $Config{"lab"}) {
448 # Changing encoding
449 for (keys %last_cl) {
450 next if /raw/;
451 $last_cl{$_} = $converter->convert($last_cl{$_})
452 if ($Config{"encoding"} &&
453 $Config{"encoding"} !~ /^utf-8$/i);
454 }
455 push @Command_Lines, \%last_cl;
457 # Сохранение позиции в файле, до которой выполнен
458 # успешный разбор
459 $Script_Files{$file}->{tell} = $last_cl{raw_end};
460 }
461 next;
462 }
464 elsif (m/$cline_re_v2/ || m/$cline_re_v3/) {
465 # Разбираем командную строку версии 2
466 my $before=$_;
467 s/.*\x0d(?!\x0a)//;
469 my $re;
470 if (m/$cline_re_v2/) {
471 $re=$cline_re2_v2;
472 }
473 else {
474 s/.\[1K.\[10D//gs;
475 $re=$cline_re2_v3;
476 print STDERR "... $_ ...\n";
477 }
478 m/$re/gs;
480 $commandlines_loaded++;
481 $last_output_length=0;
483 # Previous command
484 my %last_cl = %cl;
486 $cl{"local_session_id"} = $local_session_id;
487 # Parse new command
488 my $this_line = $1;
489 $cl{"history"} = $2;
490 my $err = $3;
491 $cl{"uid"} = $4;
492 #$cl{"euid"} = $cl{"uid"}; # Если в команде обнаружится sudo, euid поменяем на 0
493 $cl{"pid"} = $5;
494 $cl{"time"} = $6;
495 $cl{"pwd"} = $7;
496 $cl{"nonce"} = $8;
497 #$cl{"fullprompt"} = $8;
498 $cl{"prompt"} = $10;
499 #$cl{"raw_cline"}= $10;
500 $cl{"raw_cline"}= $before;
502 {
503 use bytes;
504 $cl{"raw_start"} = tell (FILE) - length($before);
505 $cl{"raw_output_start"} = tell FILE;
506 }
507 $cl{"raw_file"} = $file;
509 $cl{"err"} = 0;
510 $cl{"output"} = "";
511 #$cl{"tty"} = $tty;
513 $cline_vt->process($cl{"raw_cline"}."\n");
514 $cl{"cline"} = $cline_vt->row_plaintext (1);
515 $cl{"cline"} =~ s/\s*$//;
516 $cl{"cline"} =~ s/.*?[\#\$]\s*//;
517 $cline_vt->reset();
518 print STDERR "cline=".$cl{"cline"}."<<\n";
520 my %commands = extract_commands_from_cline($cl{"cline"});
521 #$cl{"euid"} = 0 if defined $commands{"sudo"};
522 my @comms = sort { $commands{$a} cmp $commands{$b} } keys %commands;
523 $cl{"last_command"}
524 = $comms[$#comms] || "";
526 print STDERR "last_command=".$cl{"last_command"}."<<\n";
528 if (
529 $Config{"suppress_editors"} =~ /^y/i
530 && grep ($_ eq $cl{"last_command"}, @{$Config{"editors"}})
531 || $Config{"suppress_pagers"} =~ /^y/i
532 && grep ($_ eq $cl{"last_command"}, @{$Config{"pagers"}})
533 || $Config{"suppress_terminal"}=~ /^y/i
534 && grep ($_ eq $cl{"last_command"}, @{$Config{"terminal"}})
535 ) {
536 $cl{"suppress_output"} = "1";
537 }
538 else {
539 $cl{"suppress_output"} = "0";
540 }
541 $skip_info = 0;
543 if ($Config{verbose} =~ /y/i) {
544 print "\n| " if $commandlines_loaded % 5 == 1;
545 print " ",$cl{"last_command"};
546 }
548 if (defined($last_cl{time})
549 && grep ($_ eq $last_cl{"last_command"}, @{$Config{"editors"}})) {
550 bind_diff(\%last_cl);
551 }
553 # Error code
554 $last_cl{"err"}=$err;
555 $last_cl{"raw_end"} = $cl{"raw_start"};
557 # Output
558 if (!$last_cl{"suppress_output"} || $last_cl{"err"}) {
559 $last_cl{"output"}=$saved_output;
560 for (my $i=0; $i<$Config{"terminal_height"}; $i++) {
561 my $line= $vt{$local_session_id}->row_plaintext($i);
562 next if !defined ($line) ; #|| $line =~ /^\s*$/;
563 $line =~ s/\s*$//;
564 $line .= "\n" unless $line =~ /^\s*$/;
565 $last_cl{"output"} .= $line;
566 }
567 }
568 else {
569 $last_cl{"output"}= "";
570 }
572 $vt{$local_session_id}->reset();
573 $saved_output="";
575 # Обработка команд с одинаковым временем
576 # Скорее всего они набраны с помощью tab-completion
577 if ((defined($last_cl{time})
578 && defined($cl{time})
579 && $last_cl{time} == $cl{time})
580 &&
581 (defined($last_cl{nonce})
582 && defined($cl{nonce})
583 && $last_cl{nonce} == $cl{nonce})) {
584 $last_cl{"tab"}=1;
585 };
587 # Changing encoding
588 for (keys %last_cl) {
589 next if /raw/;
590 if ($Config{"encoding"} &&
591 $Config{"encoding"} !~ /^utf-8$/i) {
592 $last_cl{$_} = $converter->convert($last_cl{$_})
593 }
594 }
595 if (defined($last_cl{time})) {
596 print STDERR "push id=".$last_cl{time}."\n";
597 push @Command_Lines, \%last_cl;
598 # Сохранение позиции в файле, до которой выполнен
599 # успешный разбор
600 $Script_Files{$file}->{tell} = $last_cl{raw_end};
601 }
602 next;
603 }
605 if (($commandlines_processed%100) == 0) {
606 # Каждые сто строк обнуляем терминал и переносим вывод из него в кэш
607 # Output
608 for (my $i=0; $i<$Config{"terminal_height"}; $i++) {
609 my $line= $vt{$local_session_id}->row_plaintext($i);
610 next if !defined ($line) ; #|| $line =~ /^\s*$/;
611 $line =~ s/\s*$//;
612 $line .= "\n" unless $line =~ /^\s*$/;
613 $saved_output .= $line;
614 }
615 $vt{$local_session_id}->reset();
616 $last_output_length=0;
617 }
619 # Иначе, это строка вывода
621 $last_output_length+=length($_);
622 #if (!$cl{"suppress_output"} || $last_output_length < 5000) {
623 if ($last_output_length < 50000) {
624 $vt{$local_session_id}->process("$_"."\n")
625 }
626 else
627 {
628 if (!$skip_info && defined($cl{last_command})) {
629 print "($cl{last_command})";
630 $skip_info = 1;
631 }
632 }
633 }
634 close(FILE);
636 }
637 if ($Config{"verbose"} =~ /y/) {
638 print "\n`- finished.\n" ;
639 print "Lines loaded: $commandlines_processed\n";
640 print "Command lines: $commandlines_loaded\n";
641 }
642 }
647 sub sort_command_lines
648 {
649 print "Sorting command lines..." if $Config{"verbose"} =~ /y/;
651 # Sort Command_Lines
652 # Write Command_Lines to Command_Lines_Index
654 my @index;
655 for (my $i=0;$i<=$#Command_Lines;$i++) {
656 $index[$i]=$i;
657 }
659 @Command_Lines_Index = sort {
660 defined($Command_Lines[$index[$a]]->{"time"})
661 && defined($Command_Lines[$index[$b]]->{"time"})
662 ? $Command_Lines[$index[$a]]->{"time"} <=> $Command_Lines[$index[$b]]->{"time"}
663 : defined($Command_Lines[$index[$a]]->{"day"})
664 && defined($Command_Lines[$index[$b]]->{"day"})
665 && defined($Command_Lines[$index[$a]]->{"hour"})
666 && defined($Command_Lines[$index[$b]]->{"hour"})
667 && defined($Command_Lines[$index[$a]]->{"min"})
668 && defined($Command_Lines[$index[$b]]->{"min"})
669 && defined($Command_Lines[$index[$a]]->{"sec"})
670 && defined($Command_Lines[$index[$b]]->{"sec"})
671 ? $Command_Lines[$index[$a]]->{"day"} cmp $Command_Lines[$index[$b]]->{"day"}
672 || $Command_Lines[$index[$a]]->{"hour"} <=> $Command_Lines[$index[$b]]->{"hour"}
673 || $Command_Lines[$index[$a]]->{"min"} <=> $Command_Lines[$index[$b]]->{"min"}
674 || $Command_Lines[$index[$a]]->{"sec"} <=> $Command_Lines[$index[$b]]->{"sec"}
675 : 0
676 } @index;
678 print "finished\n" if $Config{"verbose"} =~ /y/;
680 }
682 sub printq
683 {
684 my $TO = shift;
685 my $text = join "", @_;
686 $text =~ s/&/&amp;/g;
687 $text =~ s/</&lt;/g;
688 $text =~ s/>/&gt;/g;
689 $text =~ s/"/&quot;/g;
690 print $TO $text;
691 }
693 sub strq
694 {
695 my $text = join "", @_;
696 $text =~ s/&/&amp;/g;
697 $text =~ s/</&lt;/g;
698 $text =~ s/>/&gt;/g;
699 $text =~ s/"/&quot;/g;
700 return $text;
701 }
703 =cut
704 Вывести результат обработки журнала.
705 =cut
707 sub print_commands_to_sqlite_cache
708 {
709 my $output_filename=$_[0];
710 my $db = DBI->connect("dbi:SQLite:$output_filename", "", "",
711 {RaiseError => 1, AutoCommit => 1});
713 my $cl;
714 for my $i (@Command_Lines_Index) {
715 my $val;
716 my $var_list;
717 my $values_list;
718 my $create_var_list;
719 my $sql;
720 $cl = $Command_Lines[$i];
722 $cl->{l3cd}=$Config{l3cd} if $Config{"l3cd"};
723 for my $element (qw(
724 local_session_id
725 history
726 uid
727 pid
728 time
729 pwd
730 raw_start
731 raw_output_start
732 raw_end
733 raw_file
734 tty
735 err
736 last_command
737 nonce
738 l3cd
739 tab
740 )) {
741 if (defined($cl->{"$element"})) {
742 $val = $cl->{"$element"};
743 }
744 else {
745 $val = ""
746 };
747 $var_list .= "$element,";
748 $values_list .= '"'.$val."\",";
749 $create_var_list .= ", $element TEXT";
750 }
751 for my $element (qw(
752 prompt
753 cline
754 output
755 )) {
756 if (defined($cl->{"$element"})) {
757 $val = $cl->{"$element"};
758 }
759 else {
760 $val = ""
761 };
762 $var_list .= "$element,";
763 $values_list .= '"'.strq($val)."\",";
764 $create_var_list .= ", $element TEXT";
765 }
766 if ($cl->{"diff"}) {
767 $val = strq(${$Diffs{$cl->{"diff"}}}{"text"});
768 }
769 else {
770 $val="";
771 }
772 $var_list .= "diff,";
773 $create_var_list .= ", diff TEXT";
774 $values_list .= '"'.$val."\",";
776 $db->do("CREATE TABLE IF NOT EXISTS commands (id INTEGER PRIMARY KEY $create_var_list)");
777 #print "CREATE TABLE commands (id INTEGER PRIMARY KEY $create_var_list)\n";
778 $values_list =~ s/,$//;
779 $var_list =~ s/,$//;
780 $sql = "INSERT INTO commands ($var_list) VALUES($values_list) ";
781 print "$sql\n";
782 $db->do($sql);
783 }
784 }
786 sub print_command_lines
787 {
788 my $output_filename=$_[0];
789 open(OUT, ">>", $output_filename)
790 or die "Can't open $output_filename for writing\n";
792 my $cl;
793 my $in_range=0;
794 for my $i (@Command_Lines_Index) {
795 $cl = $Command_Lines[$i];
797 if ($Config{"from"} && $cl->{"cline"} =~ /$Config{"signature"}\s*$Config{"from"}/) {
798 $in_range=1;
799 next;
800 }
801 if ($Config{"to"} && $cl->{"cline"} =~ /$Config{"signature"}\s*$Config{"to"}/) {
802 $in_range=0;
803 next;
804 }
805 next if ($Config{"from"} && $Config{"to"} && !$in_range)
806 || ($Config{"skip_empty"} =~ /^y/i && $cl->{"cline"} =~ /^\s*$/ )
807 || ($Config{"skip_wrong"} =~ /^y/i && $cl->{"err"} != 0)
808 || ($Config{"skip_interrupted"} =~ /^y/i && $cl->{"err"} == 130);
810 # Вырезаем из вывода только нужное количество строк
811 my $output="";
812 if (!grep ($_ eq $cl->{"last_command"}, @{$Config{"full_output_commands"}})
813 && ($Config{"head_lines"}
814 || $Config{"tail_lines"})) {
815 # Partialy output
816 my @lines = split '\n', $cl->{"output"};
817 # head
818 my $mark=1;
819 for (my $i=0; $i<= $#lines && $i < $Config{"cache_head_lines"}; $i++) {
820 $output .= $lines[$i]."\n";
821 }
822 # tail
823 my $start=$#lines-$Config{"cache_tail_lines"}+1;
824 if ($start < 0) {
825 $start=0;
826 $mark=0;
827 }
828 if ($start < $Config{"cache_head_lines"}) {
829 $start=$Config{"cache_head_lines"};
830 $mark=0;
831 }
832 $output .= $Config{"skip_text"}."\n" if $mark;
833 for ($i=$start; $i<= $#lines; $i++) {
834 $output .= $lines[$i]."\n";
835 }
836 }
837 else {
838 # Full output
839 $output .= $cl->{"output"};
840 }
842 # Совместимость с labmaker
844 # Переводим в секунды Эпохи
845 # В labmaker'е данные хранились в неудобной форме: hour, min, sec, day of year
846 # Информация о годе отсутствовала
847 # Её можно внести:
848 # Декабрь 2004 год; остальные -- 2005 год.
850 my $year = 2005;
851 #$year = 2004 if ( $cl->{day} > 330 );
852 $year = $Config{year} if $Config{year};
853 # timelocal( $sec, $min, $hour, $mday,$mon,$year);
854 $cl->{time} ||= timelocal_nocheck($cl->{sec},$cl->{min},$cl->{hour},$cl->{day},0,$year);
856 # Начинаем вывод команды
857 print OUT "<command>\n";
858 print OUT "<l3cd>$Config{l3cd}</l3cd>\n" if $Config{"l3cd"};
859 for my $element (qw(
860 local_session_id
861 history
862 uid
863 pid
864 time
865 pwd
866 raw_start
867 raw_output_start
868 raw_end
869 raw_file
870 tty
871 err
872 last_command
873 history
874 nonce
875 )) {
876 next unless defined($cl->{"$element"});
877 print OUT "<$element>".$cl->{$element}."</$element>\n";
878 }
879 for my $element (qw(
880 prompt
881 cline
882 )) {
883 next unless defined($cl->{"$element"});
884 print OUT "<$element>";
885 printq(\*OUT,$cl->{"$element"});
886 print OUT "</$element>\n";
887 }
888 #note
889 #note_title
890 print OUT "<output>";
891 printq(\*OUT,$output);
892 print OUT "</output>\n";
893 if ($cl->{"diff"}) {
894 print OUT "<diff>";
895 printq(\*OUT,${$Diffs{$cl->{"diff"}}}{"text"});
896 print OUT "</diff>\n";
897 }
898 print OUT "</command>\n";
900 }
902 close(OUT);
903 }
905 sub print_session
906 {
907 my $output_filename = $_[0];
908 my $local_session_id = $_[1];
909 return if not defined($Sessions{$local_session_id});
911 print "printing session info. session id = ".$local_session_id."\n"
912 if $Config{verbose} =~ /y/;
914 open(OUT, ">>", $output_filename)
915 or die "Can't open $output_filename for writing\n";
916 print OUT "<session>\n";
917 print OUT "<l3cd>$Config{l3cd}</l3cd>\n" if $Config{"l3cd"};
918 my %session = %{$Sessions{$local_session_id}};
919 for my $key (keys %session) {
920 print OUT "<$key>".$session{$key}."</$key>\n";
921 print " ".$key,"\n";
922 }
923 print OUT "</session>\n";
924 close(OUT);
925 }
927 sub send_cache
928 {
929 # Если в кэше что-то накопилось,
930 # попытаемся отправить это на сервер
931 #
932 my $cache_was_sent=0;
934 if (open(CACHE, $Config{cache})) {
935 local $/;
936 my $cache = <CACHE>;
937 close(CACHE);
939 my $socket = IO::Socket::INET->new(
940 PeerAddr => $Config{backend_address},
941 PeerPort => $Config{backend_port},
942 proto => "tcp",
943 Type => SOCK_STREAM
944 );
946 if ($socket) {
947 print $socket $cache;
948 close($socket);
949 $cache_was_sent = 1;
950 }
951 }
952 return $cache_was_sent;
953 }
955 sub save_cache_stat
956 {
957 open (CACHE, ">$Config{cache_stat}");
958 for my $f (keys %Script_Files) {
959 print CACHE "$f\t",$Script_Files{$f}->{size},"\t",$Script_Files{$f}->{tell},"\n";
960 }
961 close(CACHE);
962 }
964 sub load_cache_stat
965 {
966 if (open (CACHE, "$Config{cache_stat}")) {
967 while(<CACHE>) {
968 chomp;
969 my ($f, $size, $tell) = split /\t/;
970 $Script_Files{$f}->{size} = $size;
971 $Script_Files{$f}->{tell} = $tell;
972 }
973 close(CACHE);
974 };
975 }
978 main();
980 sub process_was_killed
981 {
982 $Killed = 1;
983 }
985 sub reload
986 {
987 init_config;
988 }
990 sub main
991 {
993 $| = 1;
995 init_variables();
996 init_config();
999 if ($Config{"mode"} ne "daemon") {
1001 # В нормальном режиме работы нужно
1002 # считать скрипты, обработать их и записать
1003 # результат выполнения в результирующий файл.
1004 # После этого завершить работу.
1006 # Очистим кэш-файл, если он существовал
1007 if (open (CACHE, ">", $Config{"cache"})) {
1008 close(CACHE);
1009 };
1010 load_command_lines($Config{"input"}, $Config{"input_mask"});
1011 sort_command_lines;
1012 #process_command_lines;
1013 print_commands_to_sqlite_cache($Config{"cache_sqlite"});
1014 print_command_lines($Config{"cache"});
1016 else {
1017 if (open(PIDFILE, $Config{agent_pidfile})) {
1018 my $pid = <PIDFILE>;
1019 close(PIDFILE);
1020 if ($^O eq 'linux' && $pid &&(! -e "/proc/$pid" || !`grep $Config{"l3-agent"} /proc/$pid/cmdline && grep "uid:.*\b$<\b" /proc/$pid/status`)) {
1021 print "Removing stale pidfile\n";
1022 unlink $Config{agent_pidfile}
1023 or die "Can't remove stale pidfile ". $Config{agent_pidfile}. " : $!";
1025 elsif ($^O eq 'freebsd' && defined($pid) && $pid ne "" && not `ps axo uid,pid,command | grep '$< $pid $Config{"l3-agent"}' | grep -v grep 2> /dev/null`) {
1026 print "Removing stale pidfile\n";
1027 unlink $Config{agent_pidfile}
1028 or die "Can't remove stale pidfile ". $Config{agent_pidfile}. " : $!";
1030 elsif ($^O eq 'linux' || $^O eq 'freebsd' ) {
1031 print "l3-agent is already running: pid=$pid; pidfile=$Config{agent_pidfile}\n";
1032 exit(0);
1034 else {
1035 print "Unknown operating system";
1036 exit(0);
1039 if ($Config{detach} =~ /^y/i) {
1040 #$Config{verbose} = "no";
1041 my $pid = fork;
1042 exit if $pid;
1043 die "Couldn't fork: $!" unless defined ($pid);
1045 open(PIDFILE, ">", $Config{agent_pidfile})
1046 or die "Can't open pidfile ". $Config{agent_pidfile}. " for wrting: $!";
1047 print PIDFILE $$;
1048 close(PIDFILE);
1050 for my $handle (*STDIN, *STDOUT, *STDERR) {
1051 open ($handle, "+<", "/dev/null")
1052 or die "can't reopen $handle to /dev/null: $!"
1055 POSIX::setsid()
1056 or die "Can't start a new session: $!";
1058 $0 = $Config{"l3-agent"};
1060 $SIG{INT} = $SIG{TERM} = \&process_was_killed;
1061 $SIG{HUP} = \&reload;
1064 while (not $Killed) {
1065 @Command_Lines = ();
1066 @Command_Lines_Index = ();
1067 load_cache_stat();
1068 load_command_lines($Config{"input"}, $Config{"input_mask"});
1069 if (@Command_Lines) {
1070 sort_command_lines;
1071 #process_command_lines;
1072 print_commands_to_sqlite_cache($Config{"cache_sqlite"});
1073 print_command_lines($Config{"cache"});
1075 save_cache_stat();
1076 if (-e $Config{cache} && (stat($Config{cache}))[7]) {
1077 send_cache() && unlink($Config{cache});
1079 sleep($Config{"daemon_sleep_interval"} || 1);
1082 unlink $Config{agent_pidfile};
1087 sub init_variables