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