lilalo

annotate l3-agent @ 120:42d9af3c851c

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