lilalo
annotate l3-agent @ 155:8ee5e59f1bd3
Локальное хранение и анализ данных с помощью SQlite
Очень много изменений, касающихся работы с sqlite
и локального использования результатов записи.
Подробнее:
README.l3text
Очень много изменений, касающихся работы с sqlite
и локального использования результатов записи.
Подробнее:
README.l3text
author | Igor Chubin <igor@chub.in> |
---|---|
date | Tue Mar 16 20:05:30 2010 +0200 (2010-03-16) |
parents | 822b36252d7f |
children |
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; |
igor@155 | 13 use DBI; |
devi@23 | 14 |
igor@115 | 15 use lib "/etc/lilalo"; |
devi@23 | 16 use l3config; |
devi@23 | 17 |
devi@23 | 18 our @Command_Lines; |
devi@23 | 19 our @Command_Lines_Index; |
devi@28 | 20 our %Diffs; |
devi@27 | 21 our %Sessions; |
devi@23 | 22 |
devi@62 | 23 our %Script_Files; # Информация о позициях в скрипт-файлах, |
devi@62 | 24 # до которых уже выполнен разбор |
devi@62 | 25 # и информация о времени модификации файла |
devi@62 | 26 # $Script_Files{$file}->{size} |
devi@62 | 27 # $Script_Files{$file}->{tell} |
devi@23 | 28 |
devi@62 | 29 our $Killed =0; # В режиме демона -- процесс получил сигнал о завершении |
devi@23 | 30 |
devi@23 | 31 sub init_variables; |
devi@23 | 32 sub main; |
devi@23 | 33 |
devi@23 | 34 sub load_diff_files; |
devi@23 | 35 sub bind_diff; |
devi@62 | 36 sub extract_commands_from_cline; |
devi@23 | 37 sub load_command_lines; |
devi@23 | 38 sub sort_command_lines; |
devi@23 | 39 sub print_command_lines; |
devi@23 | 40 sub printq; |
devi@23 | 41 |
devi@25 | 42 sub save_cache_stat; |
devi@25 | 43 sub load_cache_stat; |
devi@27 | 44 sub print_session; |
devi@25 | 45 |
devi@23 | 46 sub load_diff_files |
devi@23 | 47 { |
devi@62 | 48 my @pathes = @_; |
devi@62 | 49 |
devi@62 | 50 for my $path (@pathes) { |
devi@62 | 51 my $template = "*.diff"; |
devi@62 | 52 my @files = <$path/$template>; |
devi@62 | 53 my $i=0; |
devi@62 | 54 for my $file (@files) { |
devi@28 | 55 |
devi@62 | 56 next if defined($Diffs{$file}); |
devi@62 | 57 my %diff; |
devi@23 | 58 |
devi@80 | 59 # Старый формат имени diff-файла |
devi@80 | 60 # DEPRECATED |
devi@80 | 61 if ($file=~m@/(D?[0-9][0-9]?[0-9]?)[^/]*?([0-9]*):([0-9]*):?([0-9]*)@) { |
devi@80 | 62 $diff{"day"}=$1 || ""; |
devi@80 | 63 $diff{"hour"}=$2; |
devi@80 | 64 $diff{"min"}=$3; |
devi@80 | 65 $diff{"sec"}=$4 || 0; |
devi@80 | 66 |
devi@80 | 67 $diff{"uid"} = 0 if $path =~ m@/root/@; |
devi@23 | 68 |
devi@62 | 69 print "diff loaded: $diff{day} $diff{hour}:$diff{min}:$diff{sec}\n"; |
devi@80 | 70 |
devi@80 | 71 } |
devi@80 | 72 # Новый формат имени diff-файла |
devi@80 | 73 elsif ($file =~ m@.*/([^_]*)_([0-9]+)(.*)@) { |
devi@80 | 74 $diff{"local_session_id"} = $1; |
devi@80 | 75 $diff{"time"} = $2; |
devi@80 | 76 $diff{"filename"} = $3; |
devi@80 | 77 $diff{"filename"} =~ s@_@/@g; |
devi@80 | 78 $diff{"filename"} =~ s@//@_@g; |
devi@80 | 79 |
devi@80 | 80 print "diff loaded: $diff{filename} (time=$diff{time},session=$diff{local_session_id})\n"; |
devi@80 | 81 } |
devi@80 | 82 else { |
devi@80 | 83 next; |
devi@80 | 84 } |
devi@80 | 85 |
devi@80 | 86 # Чтение и изменение кодировки содержимого diff-файла |
devi@62 | 87 local $/; |
devi@62 | 88 open (F, "$file") |
devi@62 | 89 or return "Can't open file $file ($_[0]) for reading"; |
devi@62 | 90 my $text = <F>; |
devi@62 | 91 if ($Config{"encoding"} && $Config{"encoding"} !~ /^utf-8$/i) { |
devi@62 | 92 my $converter = Text::Iconv->new($Config{"encoding"}, "utf-8"); |
devi@62 | 93 $text = $converter->convert($text); |
devi@62 | 94 } |
devi@62 | 95 close(F); |
devi@62 | 96 $diff{"text"}=$text; |
devi@23 | 97 |
devi@80 | 98 $diff{"path"}=$path; |
devi@80 | 99 $diff{"bind_to"}=""; |
devi@80 | 100 $diff{"time_range"}=-1; |
devi@80 | 101 $diff{"index"}=$i; |
devi@80 | 102 |
devi@62 | 103 $Diffs{$file} = \%diff; |
devi@62 | 104 $i++; |
devi@62 | 105 } |
devi@62 | 106 } |
devi@23 | 107 } |
devi@23 | 108 |
devi@23 | 109 |
devi@23 | 110 sub bind_diff |
devi@23 | 111 { |
devi@62 | 112 print "Trying to bind diff...\n"; |
devi@23 | 113 |
devi@62 | 114 my $cl = shift; |
devi@62 | 115 my $hour = $cl->{"hour"}; |
devi@62 | 116 my $min = $cl->{"min"}; |
devi@62 | 117 my $sec = $cl->{"sec"}; |
devi@23 | 118 |
devi@62 | 119 my $min_dt = 10000; |
devi@23 | 120 |
igor@119 | 121 if (defined($cl->{"diff"})) { |
igor@119 | 122 print STDERR "Command ".$cl->{time}." is already bound"; |
igor@119 | 123 return; |
igor@119 | 124 } |
igor@119 | 125 |
igor@119 | 126 # Загружаем новые diff-файлы |
igor@119 | 127 # Это нужно делать непосредственно перед привязкой, поскольку diff'ы могли образоваться только что |
igor@119 | 128 for my $lab_log (split (/\s+/, $Config{"diffs"} || $Config{"input"})) { |
igor@119 | 129 load_diff_files($lab_log); |
igor@119 | 130 } |
igor@119 | 131 |
igor@119 | 132 my $diff_to_bind; |
devi@62 | 133 for my $diff_key (keys %Diffs) { |
igor@119 | 134 my $diff = $Diffs{$diff_key}; |
igor@119 | 135 next if ($diff->{"local_session_id"} |
igor@119 | 136 && $cl->{"local_session_id"} |
igor@119 | 137 && ($cl->{"local_session_id"} ne $diff->{"local_session_id"})); |
devi@80 | 138 |
igor@119 | 139 next if ($diff->{"day"} && $cl->{"day"} && ($cl->{"day"} ne $diff->{"day"})); |
devi@80 | 140 |
igor@119 | 141 my $dt; |
igor@119 | 142 if (not $diff->{"time"}) { |
igor@119 | 143 print STDERR "diff time is 0"; |
igor@119 | 144 print STDERR join(" ", keys(%$diff)); |
igor@119 | 145 print STDERR $diff->{text}; |
igor@119 | 146 } |
igor@119 | 147 if (not $cl->{"time"}) { |
igor@119 | 148 print STDERR "cl time is 0"; |
igor@119 | 149 } |
igor@119 | 150 if ($diff->{"time"} && $cl->{"time"}) { |
igor@119 | 151 $dt = $diff->{"time"} - $cl->{"time"} |
igor@119 | 152 } |
igor@119 | 153 else { |
igor@119 | 154 $dt=($diff->{"hour"}-$hour)*3600 +($diff->{"min"}-$min)*60 + ($diff->{"sec"}-$sec); |
igor@119 | 155 } |
igor@119 | 156 if ($dt >=0 && $dt < $min_dt && !$diff->{"bind_to"}) { |
igor@119 | 157 $min_dt = $dt; |
igor@119 | 158 $diff_to_bind = $diff_key; |
igor@119 | 159 } |
igor@119 | 160 } |
igor@119 | 161 if ($diff_to_bind) { |
igor@119 | 162 print "Approppriate diff found: dt=$min_dt\n"; |
igor@119 | 163 $Diffs{$diff_to_bind}->{"bind_to"}=$cl; |
igor@119 | 164 $cl->{"diff"} = $diff_to_bind; |
igor@119 | 165 } |
igor@119 | 166 else { |
igor@119 | 167 print STDERR "Diff not found\n"; |
igor@119 | 168 print STDERR "cl{time}",$cl->{time},"\n"; |
devi@62 | 169 } |
devi@23 | 170 } |
devi@23 | 171 |
devi@23 | 172 |
devi@62 | 173 sub extract_commands_from_cline |
devi@23 | 174 # Разобрать командную строку $_[1] и возвратить хэш, содержащий |
devi@23 | 175 # номер первого появление команды в строке: |
devi@62 | 176 # команда => первая позиция |
devi@23 | 177 { |
devi@62 | 178 my $cline = $_[0]; |
devi@62 | 179 my @lists = split /\;/, $cline; |
igor@155 | 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; |
igor@155 | 347 my $saved_output; |
devi@62 | 348 while (<FILE>) { |
devi@62 | 349 $commandlines_processed++; |
devi@23 | 350 |
devi@62 | 351 next if s/^Script started on.*?\n//s; |
devi@23 | 352 |
devi@62 | 353 if (/[0-9][0-9]:[0-9][0-9]:[0-9][0-9].\[[0-9][0-9]D.\[K/ && m/$cline_re/) { |
devi@62 | 354 s/.*\x0d(?!\x0a)//; |
devi@62 | 355 m/$cline_re2/gs; |
devi@23 | 356 |
devi@62 | 357 $commandlines_loaded++; |
devi@62 | 358 $last_output_length=0; |
devi@23 | 359 |
igor@155 | 360 # Сохраняем часть выполненного ранее разбора в переменной last_cl, |
igor@155 | 361 # которую мы дополним некоторой информацией и сохраним позже |
devi@62 | 362 my %last_cl = %cl; |
igor@119 | 363 my $this_line = $1; |
devi@62 | 364 my $err = $2 || ""; |
devi@23 | 365 |
devi@62 | 366 $cl{"local_session_id"} = $local_session_id; |
devi@62 | 367 # Parse new command |
devi@62 | 368 $cl{"uid"} = $3; |
devi@74 | 369 #$cl{"euid"} = $cl{"uid"}; # Если в команде обнаружится sudo, euid поменяем на 0 |
devi@62 | 370 $cl{"pid"} = $4; |
devi@62 | 371 $cl{"day"} = $5; |
devi@62 | 372 $cl{"lab"} = $6; |
devi@62 | 373 $cl{"hour"} = $7; |
devi@62 | 374 $cl{"min"} = $8; |
devi@62 | 375 $cl{"sec"} = $9; |
devi@72 | 376 #$cl{"fullprompt"} = $10; |
devi@62 | 377 $cl{"prompt"} = $11; |
devi@62 | 378 $cl{"raw_cline"} = $12; |
devi@23 | 379 |
devi@62 | 380 { |
devi@62 | 381 use bytes; |
igor@119 | 382 $cl{"raw_start"} = tell (FILE) - length($this_line); |
devi@62 | 383 $cl{"raw_output_start"} = tell FILE; |
devi@62 | 384 } |
devi@62 | 385 $cl{"raw_file"} = $file; |
devi@23 | 386 |
devi@62 | 387 $cl{"err"} = 0; |
devi@62 | 388 $cl{"output"} = ""; |
devi@62 | 389 $cl{"tty"} = $tty; |
devi@23 | 390 |
devi@62 | 391 $cline_vt->process($cl{"raw_cline"}."\n"); |
devi@62 | 392 $cl{"cline"} = $cline_vt->row_plaintext (1); |
devi@62 | 393 $cl{"cline"} =~ s/\s*$//; |
igor@119 | 394 $cl{"cline"} =~ s/.*?[\#\$]\s*//; |
devi@62 | 395 $cline_vt->reset(); |
devi@23 | 396 |
devi@62 | 397 my %commands = extract_commands_from_cline($cl{"cline"}); |
devi@74 | 398 #$cl{"euid"}=0 if defined $commands{"sudo"}; |
devi@62 | 399 my @comms = sort { $commands{$a} cmp $commands{$b} } keys %commands; |
devi@62 | 400 $cl{"last_command"} = $comms[$#comms] || ""; |
devi@23 | 401 |
devi@62 | 402 if ( |
devi@62 | 403 $Config{"suppress_editors"} =~ /^y/i |
devi@62 | 404 && grep ($_ eq $cl{"last_command"}, @{$Config{"editors"}}) |
devi@62 | 405 || $Config{"suppress_pagers"} =~ /^y/i |
devi@62 | 406 && grep ($_ eq $cl{"last_command"}, @{$Config{"pagers"}}) |
devi@62 | 407 || $Config{"suppress_terminal"}=~ /^y/i |
devi@62 | 408 && grep ($_ eq $cl{"last_command"}, @{$Config{"terminal"}}) |
devi@62 | 409 ) { |
devi@62 | 410 $cl{"suppress_output"} = "1"; |
devi@62 | 411 } |
devi@62 | 412 else { |
devi@62 | 413 $cl{"suppress_output"} = "0"; |
devi@62 | 414 } |
devi@62 | 415 $skip_info = 0; |
devi@23 | 416 |
devi@23 | 417 |
devi@62 | 418 print " ",$cl{"last_command"}; |
devi@23 | 419 |
igor@119 | 420 if (grep ($_ eq $last_cl{"last_command"}, @{$Config{"editors"}})) { |
igor@119 | 421 bind_diff(\%last_cl); |
igor@119 | 422 } |
igor@119 | 423 |
devi@62 | 424 # Error code |
devi@62 | 425 $last_cl{"raw_end"} = $cl{"raw_start"}; |
devi@62 | 426 $last_cl{"err"}=$err; |
devi@62 | 427 $last_cl{"err"}=130 if $err eq "^C"; |
devi@23 | 428 |
devi@62 | 429 # Output |
devi@62 | 430 if (!$last_cl{"suppress_output"} || $last_cl{"err"}) { |
devi@62 | 431 for (my $i=0; $i<$Config{"terminal_height"}; $i++) { |
igor@115 | 432 my $line= $vt{$local_session_id}->row_plaintext($i); |
devi@62 | 433 next if !defined ($line) ; #|| $line =~ /^\s*$/; |
devi@62 | 434 $line =~ s/\s*$//; |
devi@62 | 435 $line .= "\n" unless $line =~ /^\s*$/; |
devi@62 | 436 $last_cl{"output"} .= $line; |
devi@62 | 437 } |
devi@62 | 438 } |
devi@62 | 439 else { |
devi@62 | 440 $last_cl{"output"}= ""; |
devi@62 | 441 } |
devi@23 | 442 |
igor@115 | 443 $vt{$local_session_id}->reset(); |
devi@23 | 444 |
devi@23 | 445 |
devi@62 | 446 # Save |
devi@62 | 447 if (!$Config{"lab"} || $cl{"lab"} eq $Config{"lab"}) { |
devi@62 | 448 # Changing encoding |
devi@62 | 449 for (keys %last_cl) { |
devi@62 | 450 next if /raw/; |
devi@62 | 451 $last_cl{$_} = $converter->convert($last_cl{$_}) |
devi@62 | 452 if ($Config{"encoding"} && |
devi@62 | 453 $Config{"encoding"} !~ /^utf-8$/i); |
devi@62 | 454 } |
devi@62 | 455 push @Command_Lines, \%last_cl; |
devi@23 | 456 |
devi@62 | 457 # Сохранение позиции в файле, до которой выполнен |
devi@62 | 458 # успешный разбор |
devi@62 | 459 $Script_Files{$file}->{tell} = $last_cl{raw_end}; |
devi@62 | 460 } |
devi@62 | 461 next; |
devi@62 | 462 } |
devi@74 | 463 |
igor@114 | 464 elsif (m/$cline_re_v2/ || m/$cline_re_v3/) { |
devi@74 | 465 # Разбираем командную строку версии 2 |
igor@119 | 466 my $before=$_; |
igor@119 | 467 s/.*\x0d(?!\x0a)//; |
devi@74 | 468 |
igor@114 | 469 my $re; |
igor@114 | 470 if (m/$cline_re_v2/) { |
igor@114 | 471 $re=$cline_re2_v2; |
igor@114 | 472 } |
igor@114 | 473 else { |
igor@114 | 474 s/.\[1K.\[10D//gs; |
igor@114 | 475 $re=$cline_re2_v3; |
igor@119 | 476 print STDERR "... $_ ...\n"; |
igor@114 | 477 } |
igor@121 | 478 m/$re/gs; |
devi@74 | 479 |
devi@74 | 480 $commandlines_loaded++; |
devi@74 | 481 $last_output_length=0; |
devi@74 | 482 |
devi@74 | 483 # Previous command |
devi@74 | 484 my %last_cl = %cl; |
devi@74 | 485 |
devi@74 | 486 $cl{"local_session_id"} = $local_session_id; |
devi@74 | 487 # Parse new command |
igor@119 | 488 my $this_line = $1; |
devi@74 | 489 $cl{"history"} = $2; |
devi@74 | 490 my $err = $3; |
devi@74 | 491 $cl{"uid"} = $4; |
devi@74 | 492 #$cl{"euid"} = $cl{"uid"}; # Если в команде обнаружится sudo, euid поменяем на 0 |
devi@74 | 493 $cl{"pid"} = $5; |
devi@74 | 494 $cl{"time"} = $6; |
devi@74 | 495 $cl{"pwd"} = $7; |
igor@119 | 496 $cl{"nonce"} = $8; |
devi@74 | 497 #$cl{"fullprompt"} = $8; |
igor@119 | 498 $cl{"prompt"} = $10; |
igor@119 | 499 #$cl{"raw_cline"}= $10; |
igor@119 | 500 $cl{"raw_cline"}= $before; |
devi@74 | 501 |
devi@74 | 502 { |
devi@74 | 503 use bytes; |
igor@119 | 504 $cl{"raw_start"} = tell (FILE) - length($before); |
devi@74 | 505 $cl{"raw_output_start"} = tell FILE; |
devi@74 | 506 } |
devi@74 | 507 $cl{"raw_file"} = $file; |
devi@74 | 508 |
devi@74 | 509 $cl{"err"} = 0; |
devi@74 | 510 $cl{"output"} = ""; |
devi@74 | 511 #$cl{"tty"} = $tty; |
devi@74 | 512 |
devi@74 | 513 $cline_vt->process($cl{"raw_cline"}."\n"); |
devi@74 | 514 $cl{"cline"} = $cline_vt->row_plaintext (1); |
devi@74 | 515 $cl{"cline"} =~ s/\s*$//; |
igor@119 | 516 $cl{"cline"} =~ s/.*?[\#\$]\s*//; |
devi@74 | 517 $cline_vt->reset(); |
igor@119 | 518 print STDERR "cline=".$cl{"cline"}."<<\n"; |
devi@74 | 519 |
devi@74 | 520 my %commands = extract_commands_from_cline($cl{"cline"}); |
devi@74 | 521 #$cl{"euid"} = 0 if defined $commands{"sudo"}; |
devi@74 | 522 my @comms = sort { $commands{$a} cmp $commands{$b} } keys %commands; |
devi@74 | 523 $cl{"last_command"} |
devi@74 | 524 = $comms[$#comms] || ""; |
devi@74 | 525 |
igor@119 | 526 print STDERR "last_command=".$cl{"last_command"}."<<\n"; |
igor@119 | 527 |
devi@74 | 528 if ( |
devi@74 | 529 $Config{"suppress_editors"} =~ /^y/i |
devi@74 | 530 && grep ($_ eq $cl{"last_command"}, @{$Config{"editors"}}) |
devi@74 | 531 || $Config{"suppress_pagers"} =~ /^y/i |
devi@74 | 532 && grep ($_ eq $cl{"last_command"}, @{$Config{"pagers"}}) |
devi@74 | 533 || $Config{"suppress_terminal"}=~ /^y/i |
devi@74 | 534 && grep ($_ eq $cl{"last_command"}, @{$Config{"terminal"}}) |
devi@74 | 535 ) { |
devi@74 | 536 $cl{"suppress_output"} = "1"; |
devi@74 | 537 } |
devi@74 | 538 else { |
devi@74 | 539 $cl{"suppress_output"} = "0"; |
devi@74 | 540 } |
devi@74 | 541 $skip_info = 0; |
devi@74 | 542 |
devi@74 | 543 if ($Config{verbose} =~ /y/i) { |
devi@83 | 544 print "\n| " if $commandlines_loaded % 5 == 1; |
devi@74 | 545 print " ",$cl{"last_command"}; |
devi@74 | 546 } |
devi@74 | 547 |
igor@119 | 548 if (defined($last_cl{time}) |
igor@119 | 549 && grep ($_ eq $last_cl{"last_command"}, @{$Config{"editors"}})) { |
igor@119 | 550 bind_diff(\%last_cl); |
devi@74 | 551 } |
devi@74 | 552 |
devi@74 | 553 # Error code |
devi@74 | 554 $last_cl{"err"}=$err; |
devi@74 | 555 $last_cl{"raw_end"} = $cl{"raw_start"}; |
devi@74 | 556 |
devi@74 | 557 # Output |
devi@74 | 558 if (!$last_cl{"suppress_output"} || $last_cl{"err"}) { |
igor@150 | 559 $last_cl{"output"}=$saved_output; |
devi@74 | 560 for (my $i=0; $i<$Config{"terminal_height"}; $i++) { |
igor@115 | 561 my $line= $vt{$local_session_id}->row_plaintext($i); |
devi@74 | 562 next if !defined ($line) ; #|| $line =~ /^\s*$/; |
devi@74 | 563 $line =~ s/\s*$//; |
devi@74 | 564 $line .= "\n" unless $line =~ /^\s*$/; |
devi@74 | 565 $last_cl{"output"} .= $line; |
devi@74 | 566 } |
devi@74 | 567 } |
devi@74 | 568 else { |
devi@74 | 569 $last_cl{"output"}= ""; |
devi@74 | 570 } |
devi@74 | 571 |
igor@115 | 572 $vt{$local_session_id}->reset(); |
igor@155 | 573 $saved_output=""; |
devi@74 | 574 |
igor@155 | 575 # Обработка команд с одинаковым временем |
igor@155 | 576 # Скорее всего они набраны с помощью tab-completion |
igor@155 | 577 if ((defined($last_cl{time}) |
igor@155 | 578 && defined($cl{time}) |
igor@155 | 579 && $last_cl{time} == $cl{time}) |
igor@155 | 580 && |
igor@155 | 581 (defined($last_cl{nonce}) |
igor@155 | 582 && defined($cl{nonce}) |
igor@155 | 583 && $last_cl{nonce} == $cl{nonce})) { |
igor@155 | 584 $last_cl{"tab"}=1; |
igor@155 | 585 }; |
devi@74 | 586 |
devi@74 | 587 # Changing encoding |
devi@74 | 588 for (keys %last_cl) { |
devi@74 | 589 next if /raw/; |
devi@74 | 590 if ($Config{"encoding"} && |
devi@74 | 591 $Config{"encoding"} !~ /^utf-8$/i) { |
devi@74 | 592 $last_cl{$_} = $converter->convert($last_cl{$_}) |
devi@74 | 593 } |
devi@74 | 594 } |
igor@119 | 595 if (defined($last_cl{time})) { |
igor@119 | 596 print STDERR "push id=".$last_cl{time}."\n"; |
igor@119 | 597 push @Command_Lines, \%last_cl; |
igor@119 | 598 # Сохранение позиции в файле, до которой выполнен |
igor@119 | 599 # успешный разбор |
igor@119 | 600 $Script_Files{$file}->{tell} = $last_cl{raw_end}; |
igor@119 | 601 } |
devi@74 | 602 next; |
devi@74 | 603 } |
devi@74 | 604 |
igor@150 | 605 if (($commandlines_processed%100) == 0) { |
igor@150 | 606 # Каждые сто строк обнуляем терминал и переносим вывод из него в кэш |
igor@150 | 607 # Output |
igor@150 | 608 for (my $i=0; $i<$Config{"terminal_height"}; $i++) { |
igor@150 | 609 my $line= $vt{$local_session_id}->row_plaintext($i); |
igor@150 | 610 next if !defined ($line) ; #|| $line =~ /^\s*$/; |
igor@150 | 611 $line =~ s/\s*$//; |
igor@150 | 612 $line .= "\n" unless $line =~ /^\s*$/; |
igor@150 | 613 $saved_output .= $line; |
igor@150 | 614 } |
igor@150 | 615 $vt{$local_session_id}->reset(); |
igor@150 | 616 $last_output_length=0; |
igor@150 | 617 } |
igor@150 | 618 |
devi@74 | 619 # Иначе, это строка вывода |
devi@74 | 620 |
devi@62 | 621 $last_output_length+=length($_); |
devi@62 | 622 #if (!$cl{"suppress_output"} || $last_output_length < 5000) { |
devi@62 | 623 if ($last_output_length < 50000) { |
igor@115 | 624 $vt{$local_session_id}->process("$_"."\n") |
devi@62 | 625 } |
devi@62 | 626 else |
devi@62 | 627 { |
igor@119 | 628 if (!$skip_info && defined($cl{last_command})) { |
devi@62 | 629 print "($cl{last_command})"; |
devi@62 | 630 $skip_info = 1; |
devi@62 | 631 } |
devi@62 | 632 } |
devi@62 | 633 } |
devi@62 | 634 close(FILE); |
devi@23 | 635 |
devi@62 | 636 } |
devi@62 | 637 if ($Config{"verbose"} =~ /y/) { |
devi@74 | 638 print "\n`- finished.\n" ; |
devi@62 | 639 print "Lines loaded: $commandlines_processed\n"; |
devi@62 | 640 print "Command lines: $commandlines_loaded\n"; |
devi@62 | 641 } |
devi@23 | 642 } |
devi@23 | 643 |
devi@23 | 644 |
devi@23 | 645 |
devi@62 | 646 |
devi@62 | 647 sub sort_command_lines |
devi@62 | 648 { |
devi@74 | 649 print "Sorting command lines..." if $Config{"verbose"} =~ /y/; |
devi@62 | 650 |
devi@62 | 651 # Sort Command_Lines |
devi@62 | 652 # Write Command_Lines to Command_Lines_Index |
devi@62 | 653 |
devi@62 | 654 my @index; |
devi@62 | 655 for (my $i=0;$i<=$#Command_Lines;$i++) { |
devi@62 | 656 $index[$i]=$i; |
devi@62 | 657 } |
devi@62 | 658 |
devi@62 | 659 @Command_Lines_Index = sort { |
devi@86 | 660 defined($Command_Lines[$index[$a]]->{"time"}) |
devi@86 | 661 && defined($Command_Lines[$index[$b]]->{"time"}) |
devi@81 | 662 ? $Command_Lines[$index[$a]]->{"time"} <=> $Command_Lines[$index[$b]]->{"time"} |
devi@84 | 663 : defined($Command_Lines[$index[$a]]->{"day"}) |
devi@86 | 664 && defined($Command_Lines[$index[$b]]->{"day"}) |
devi@86 | 665 && defined($Command_Lines[$index[$a]]->{"hour"}) |
devi@86 | 666 && defined($Command_Lines[$index[$b]]->{"hour"}) |
devi@86 | 667 && defined($Command_Lines[$index[$a]]->{"min"}) |
devi@86 | 668 && defined($Command_Lines[$index[$b]]->{"min"}) |
devi@86 | 669 && defined($Command_Lines[$index[$a]]->{"sec"}) |
devi@86 | 670 && defined($Command_Lines[$index[$b]]->{"sec"}) |
devi@86 | 671 ? $Command_Lines[$index[$a]]->{"day"} cmp $Command_Lines[$index[$b]]->{"day"} |
devi@86 | 672 || $Command_Lines[$index[$a]]->{"hour"} <=> $Command_Lines[$index[$b]]->{"hour"} |
devi@86 | 673 || $Command_Lines[$index[$a]]->{"min"} <=> $Command_Lines[$index[$b]]->{"min"} |
devi@86 | 674 || $Command_Lines[$index[$a]]->{"sec"} <=> $Command_Lines[$index[$b]]->{"sec"} |
devi@86 | 675 : 0 |
devi@62 | 676 } @index; |
devi@62 | 677 |
devi@74 | 678 print "finished\n" if $Config{"verbose"} =~ /y/; |
devi@62 | 679 |
devi@62 | 680 } |
devi@62 | 681 |
devi@23 | 682 sub printq |
devi@23 | 683 { |
devi@62 | 684 my $TO = shift; |
devi@62 | 685 my $text = join "", @_; |
devi@62 | 686 $text =~ s/&/&/g; |
devi@62 | 687 $text =~ s/</</g; |
devi@62 | 688 $text =~ s/>/>/g; |
igor@155 | 689 $text =~ s/"/"/g; |
devi@62 | 690 print $TO $text; |
devi@23 | 691 } |
devi@23 | 692 |
igor@155 | 693 sub strq |
igor@155 | 694 { |
igor@155 | 695 my $text = join "", @_; |
igor@155 | 696 $text =~ s/&/&/g; |
igor@155 | 697 $text =~ s/</</g; |
igor@155 | 698 $text =~ s/>/>/g; |
igor@155 | 699 $text =~ s/"/"/g; |
igor@155 | 700 return $text; |
igor@155 | 701 } |
devi@23 | 702 |
devi@23 | 703 =cut |
devi@23 | 704 Вывести результат обработки журнала. |
devi@23 | 705 =cut |
devi@23 | 706 |
igor@155 | 707 sub print_commands_to_sqlite_cache |
igor@155 | 708 { |
igor@155 | 709 my $output_filename=$_[0]; |
igor@155 | 710 my $db = DBI->connect("dbi:SQLite:$output_filename", "", "", |
igor@155 | 711 {RaiseError => 1, AutoCommit => 1}); |
igor@155 | 712 |
igor@155 | 713 my $cl; |
igor@155 | 714 for my $i (@Command_Lines_Index) { |
igor@155 | 715 my $val; |
igor@155 | 716 my $var_list; |
igor@155 | 717 my $values_list; |
igor@155 | 718 my $create_var_list; |
igor@155 | 719 my $sql; |
igor@155 | 720 $cl = $Command_Lines[$i]; |
igor@155 | 721 |
igor@155 | 722 $cl->{l3cd}=$Config{l3cd} if $Config{"l3cd"}; |
igor@155 | 723 for my $element (qw( |
igor@155 | 724 local_session_id |
igor@155 | 725 history |
igor@155 | 726 uid |
igor@155 | 727 pid |
igor@155 | 728 time |
igor@155 | 729 pwd |
igor@155 | 730 raw_start |
igor@155 | 731 raw_output_start |
igor@155 | 732 raw_end |
igor@155 | 733 raw_file |
igor@155 | 734 tty |
igor@155 | 735 err |
igor@155 | 736 last_command |
igor@155 | 737 nonce |
igor@155 | 738 l3cd |
igor@155 | 739 tab |
igor@155 | 740 )) { |
igor@155 | 741 if (defined($cl->{"$element"})) { |
igor@155 | 742 $val = $cl->{"$element"}; |
igor@155 | 743 } |
igor@155 | 744 else { |
igor@155 | 745 $val = "" |
igor@155 | 746 }; |
igor@155 | 747 $var_list .= "$element,"; |
igor@155 | 748 $values_list .= '"'.$val."\","; |
igor@155 | 749 $create_var_list .= ", $element TEXT"; |
igor@155 | 750 } |
igor@155 | 751 for my $element (qw( |
igor@155 | 752 prompt |
igor@155 | 753 cline |
igor@155 | 754 output |
igor@155 | 755 )) { |
igor@155 | 756 if (defined($cl->{"$element"})) { |
igor@155 | 757 $val = $cl->{"$element"}; |
igor@155 | 758 } |
igor@155 | 759 else { |
igor@155 | 760 $val = "" |
igor@155 | 761 }; |
igor@155 | 762 $var_list .= "$element,"; |
igor@155 | 763 $values_list .= '"'.strq($val)."\","; |
igor@155 | 764 $create_var_list .= ", $element TEXT"; |
igor@155 | 765 } |
igor@155 | 766 if ($cl->{"diff"}) { |
igor@155 | 767 $val = strq(${$Diffs{$cl->{"diff"}}}{"text"}); |
igor@155 | 768 } |
igor@155 | 769 else { |
igor@155 | 770 $val=""; |
igor@155 | 771 } |
igor@155 | 772 $var_list .= "diff,"; |
igor@155 | 773 $create_var_list .= ", diff TEXT"; |
igor@155 | 774 $values_list .= '"'.$val."\","; |
igor@155 | 775 |
igor@155 | 776 $db->do("CREATE TABLE IF NOT EXISTS commands (id INTEGER PRIMARY KEY $create_var_list)"); |
igor@155 | 777 #print "CREATE TABLE commands (id INTEGER PRIMARY KEY $create_var_list)\n"; |
igor@155 | 778 $values_list =~ s/,$//; |
igor@155 | 779 $var_list =~ s/,$//; |
igor@155 | 780 $sql = "INSERT INTO commands ($var_list) VALUES($values_list) "; |
igor@155 | 781 print "$sql\n"; |
igor@155 | 782 $db->do($sql); |
igor@155 | 783 } |
igor@155 | 784 } |
igor@155 | 785 |
devi@23 | 786 sub print_command_lines |
devi@23 | 787 { |
devi@62 | 788 my $output_filename=$_[0]; |
devi@85 | 789 open(OUT, ">>", $output_filename) |
devi@62 | 790 or die "Can't open $output_filename for writing\n"; |
devi@23 | 791 |
devi@62 | 792 my $cl; |
devi@62 | 793 my $in_range=0; |
devi@62 | 794 for my $i (@Command_Lines_Index) { |
devi@62 | 795 $cl = $Command_Lines[$i]; |
devi@23 | 796 |
devi@62 | 797 if ($Config{"from"} && $cl->{"cline"} =~ /$Config{"signature"}\s*$Config{"from"}/) { |
devi@62 | 798 $in_range=1; |
devi@62 | 799 next; |
devi@62 | 800 } |
devi@62 | 801 if ($Config{"to"} && $cl->{"cline"} =~ /$Config{"signature"}\s*$Config{"to"}/) { |
devi@62 | 802 $in_range=0; |
devi@62 | 803 next; |
devi@62 | 804 } |
devi@62 | 805 next if ($Config{"from"} && $Config{"to"} && !$in_range) |
igor@155 | 806 || ($Config{"skip_empty"} =~ /^y/i && $cl->{"cline"} =~ /^\s*$/ ) |
igor@155 | 807 || ($Config{"skip_wrong"} =~ /^y/i && $cl->{"err"} != 0) |
igor@155 | 808 || ($Config{"skip_interrupted"} =~ /^y/i && $cl->{"err"} == 130); |
igor@155 | 809 |
devi@62 | 810 # Вырезаем из вывода только нужное количество строк |
devi@62 | 811 my $output=""; |
devi@73 | 812 if (!grep ($_ eq $cl->{"last_command"}, @{$Config{"full_output_commands"}}) |
devi@69 | 813 && ($Config{"head_lines"} |
devi@73 | 814 || $Config{"tail_lines"})) { |
devi@62 | 815 # Partialy output |
devi@62 | 816 my @lines = split '\n', $cl->{"output"}; |
devi@62 | 817 # head |
devi@62 | 818 my $mark=1; |
devi@62 | 819 for (my $i=0; $i<= $#lines && $i < $Config{"cache_head_lines"}; $i++) { |
devi@62 | 820 $output .= $lines[$i]."\n"; |
devi@62 | 821 } |
devi@62 | 822 # tail |
devi@62 | 823 my $start=$#lines-$Config{"cache_tail_lines"}+1; |
devi@62 | 824 if ($start < 0) { |
devi@62 | 825 $start=0; |
devi@62 | 826 $mark=0; |
igor@155 | 827 } |
devi@62 | 828 if ($start < $Config{"cache_head_lines"}) { |
devi@62 | 829 $start=$Config{"cache_head_lines"}; |
devi@62 | 830 $mark=0; |
igor@155 | 831 } |
devi@62 | 832 $output .= $Config{"skip_text"}."\n" if $mark; |
devi@73 | 833 for ($i=$start; $i<= $#lines; $i++) { |
devi@62 | 834 $output .= $lines[$i]."\n"; |
devi@62 | 835 } |
igor@155 | 836 } |
devi@62 | 837 else { |
devi@62 | 838 # Full output |
devi@62 | 839 $output .= $cl->{"output"}; |
igor@155 | 840 } |
devi@23 | 841 |
devi@62 | 842 # Совместимость с labmaker |
devi@23 | 843 |
devi@62 | 844 # Переводим в секунды Эпохи |
devi@62 | 845 # В labmaker'е данные хранились в неудобной форме: hour, min, sec, day of year |
devi@62 | 846 # Информация о годе отсутствовала |
devi@62 | 847 # Её можно внести: |
devi@62 | 848 # Декабрь 2004 год; остальные -- 2005 год. |
devi@23 | 849 |
devi@62 | 850 my $year = 2005; |
devi@62 | 851 #$year = 2004 if ( $cl->{day} > 330 ); |
devi@62 | 852 $year = $Config{year} if $Config{year}; |
devi@62 | 853 # timelocal( $sec, $min, $hour, $mday,$mon,$year); |
devi@74 | 854 $cl->{time} ||= timelocal_nocheck($cl->{sec},$cl->{min},$cl->{hour},$cl->{day},0,$year); |
devi@23 | 855 |
devi@62 | 856 # Начинаем вывод команды |
devi@62 | 857 print OUT "<command>\n"; |
devi@98 | 858 print OUT "<l3cd>$Config{l3cd}</l3cd>\n" if $Config{"l3cd"}; |
devi@62 | 859 for my $element (qw( |
devi@62 | 860 local_session_id |
devi@74 | 861 history |
devi@74 | 862 uid |
devi@74 | 863 pid |
devi@62 | 864 time |
devi@74 | 865 pwd |
devi@62 | 866 raw_start |
devi@62 | 867 raw_output_start |
devi@62 | 868 raw_end |
devi@62 | 869 raw_file |
devi@62 | 870 tty |
devi@62 | 871 err |
devi@62 | 872 last_command |
devi@74 | 873 history |
igor@119 | 874 nonce |
devi@62 | 875 )) { |
devi@65 | 876 next unless defined($cl->{"$element"}); |
devi@62 | 877 print OUT "<$element>".$cl->{$element}."</$element>\n"; |
devi@62 | 878 } |
devi@62 | 879 for my $element (qw( |
devi@62 | 880 prompt |
devi@62 | 881 cline |
devi@62 | 882 )) { |
devi@65 | 883 next unless defined($cl->{"$element"}); |
devi@62 | 884 print OUT "<$element>"; |
devi@62 | 885 printq(\*OUT,$cl->{"$element"}); |
devi@62 | 886 print OUT "</$element>\n"; |
devi@62 | 887 } |
devi@72 | 888 #note |
devi@72 | 889 #note_title |
devi@62 | 890 print OUT "<output>"; |
devi@62 | 891 printq(\*OUT,$output); |
devi@62 | 892 print OUT "</output>\n"; |
devi@62 | 893 if ($cl->{"diff"}) { |
devi@62 | 894 print OUT "<diff>"; |
devi@62 | 895 printq(\*OUT,${$Diffs{$cl->{"diff"}}}{"text"}); |
devi@62 | 896 print OUT "</diff>\n"; |
devi@62 | 897 } |
devi@62 | 898 print OUT "</command>\n"; |
devi@23 | 899 |
devi@62 | 900 } |
devi@23 | 901 |
devi@62 | 902 close(OUT); |
devi@27 | 903 } |
devi@27 | 904 |
devi@27 | 905 sub print_session |
devi@27 | 906 { |
devi@62 | 907 my $output_filename = $_[0]; |
devi@62 | 908 my $local_session_id = $_[1]; |
devi@62 | 909 return if not defined($Sessions{$local_session_id}); |
devi@27 | 910 |
devi@84 | 911 print "printing session info. session id = ".$local_session_id."\n" |
devi@84 | 912 if $Config{verbose} =~ /y/; |
devi@84 | 913 |
devi@62 | 914 open(OUT, ">>", $output_filename) |
devi@62 | 915 or die "Can't open $output_filename for writing\n"; |
devi@62 | 916 print OUT "<session>\n"; |
devi@98 | 917 print OUT "<l3cd>$Config{l3cd}</l3cd>\n" if $Config{"l3cd"}; |
devi@62 | 918 my %session = %{$Sessions{$local_session_id}}; |
devi@62 | 919 for my $key (keys %session) { |
devi@84 | 920 print OUT "<$key>".$session{$key}."</$key>\n"; |
devi@84 | 921 print " ".$key,"\n"; |
devi@62 | 922 } |
devi@62 | 923 print OUT "</session>\n"; |
devi@62 | 924 close(OUT); |
devi@27 | 925 } |
devi@27 | 926 |
devi@27 | 927 sub send_cache |
devi@27 | 928 { |
devi@62 | 929 # Если в кэше что-то накопилось, |
devi@62 | 930 # попытаемся отправить это на сервер |
devi@62 | 931 # |
devi@62 | 932 my $cache_was_sent=0; |
devi@62 | 933 |
devi@62 | 934 if (open(CACHE, $Config{cache})) { |
devi@62 | 935 local $/; |
devi@62 | 936 my $cache = <CACHE>; |
devi@62 | 937 close(CACHE); |
devi@27 | 938 |
devi@62 | 939 my $socket = IO::Socket::INET->new( |
devi@62 | 940 PeerAddr => $Config{backend_address}, |
devi@62 | 941 PeerPort => $Config{backend_port}, |
devi@62 | 942 proto => "tcp", |
devi@62 | 943 Type => SOCK_STREAM |
devi@62 | 944 ); |
devi@27 | 945 |
devi@62 | 946 if ($socket) { |
devi@62 | 947 print $socket $cache; |
devi@62 | 948 close($socket); |
devi@62 | 949 $cache_was_sent = 1; |
devi@62 | 950 } |
devi@62 | 951 } |
devi@62 | 952 return $cache_was_sent; |
devi@23 | 953 } |
devi@23 | 954 |
devi@25 | 955 sub save_cache_stat |
devi@25 | 956 { |
devi@62 | 957 open (CACHE, ">$Config{cache_stat}"); |
devi@62 | 958 for my $f (keys %Script_Files) { |
devi@62 | 959 print CACHE "$f\t",$Script_Files{$f}->{size},"\t",$Script_Files{$f}->{tell},"\n"; |
devi@62 | 960 } |
devi@62 | 961 close(CACHE); |
devi@25 | 962 } |
devi@25 | 963 |
devi@25 | 964 sub load_cache_stat |
devi@25 | 965 { |
devi@62 | 966 if (open (CACHE, "$Config{cache_stat}")) { |
devi@62 | 967 while(<CACHE>) { |
devi@62 | 968 chomp; |
devi@62 | 969 my ($f, $size, $tell) = split /\t/; |
devi@62 | 970 $Script_Files{$f}->{size} = $size; |
devi@62 | 971 $Script_Files{$f}->{tell} = $tell; |
devi@62 | 972 } |
devi@62 | 973 close(CACHE); |
devi@62 | 974 }; |
devi@25 | 975 } |
devi@23 | 976 |
devi@23 | 977 |
devi@23 | 978 main(); |
devi@23 | 979 |
devi@25 | 980 sub process_was_killed |
devi@25 | 981 { |
devi@62 | 982 $Killed = 1; |
devi@25 | 983 } |
devi@25 | 984 |
devi@98 | 985 sub reload |
devi@98 | 986 { |
devi@98 | 987 init_config; |
devi@98 | 988 } |
devi@98 | 989 |
devi@23 | 990 sub main |
devi@23 | 991 { |
devi@23 | 992 |
devi@62 | 993 $| = 1; |
devi@23 | 994 |
devi@62 | 995 init_variables(); |
devi@62 | 996 init_config(); |
devi@23 | 997 |
devi@27 | 998 |
devi@62 | 999 if ($Config{"mode"} ne "daemon") { |
devi@27 | 1000 |
devi@74 | 1001 # В нормальном режиме работы нужно |
devi@74 | 1002 # считать скрипты, обработать их и записать |
devi@74 | 1003 # результат выполнения в результирующий файл. |
devi@74 | 1004 # После этого завершить работу. |
devi@74 | 1005 |
devi@85 | 1006 # Очистим кэш-файл, если он существовал |
devi@85 | 1007 if (open (CACHE, ">", $Config{"cache"})) { |
devi@85 | 1008 close(CACHE); |
devi@85 | 1009 }; |
devi@62 | 1010 load_command_lines($Config{"input"}, $Config{"input_mask"}); |
devi@67 | 1011 sort_command_lines; |
devi@62 | 1012 #process_command_lines; |
igor@155 | 1013 print_commands_to_sqlite_cache($Config{"cache_sqlite"}); |
devi@62 | 1014 print_command_lines($Config{"cache"}); |
devi@62 | 1015 } |
devi@62 | 1016 else { |
devi@62 | 1017 if (open(PIDFILE, $Config{agent_pidfile})) { |
devi@62 | 1018 my $pid = <PIDFILE>; |
devi@62 | 1019 close(PIDFILE); |
devi@62 | 1020 if ($^O eq 'linux' && $pid &&(! -e "/proc/$pid" || !`grep $Config{"l3-agent"} /proc/$pid/cmdline && grep "uid:.*\b$<\b" /proc/$pid/status`)) { |
devi@62 | 1021 print "Removing stale pidfile\n"; |
devi@62 | 1022 unlink $Config{agent_pidfile} |
devi@62 | 1023 or die "Can't remove stale pidfile ". $Config{agent_pidfile}. " : $!"; |
devi@62 | 1024 } |
devi@108 | 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`) { |
devi@62 | 1026 print "Removing stale pidfile\n"; |
devi@62 | 1027 unlink $Config{agent_pidfile} |
devi@62 | 1028 or die "Can't remove stale pidfile ". $Config{agent_pidfile}. " : $!"; |
devi@62 | 1029 } |
devi@62 | 1030 elsif ($^O eq 'linux' || $^O eq 'freebsd' ) { |
devi@62 | 1031 print "l3-agent is already running: pid=$pid; pidfile=$Config{agent_pidfile}\n"; |
devi@62 | 1032 exit(0); |
devi@62 | 1033 } |
devi@62 | 1034 else { |
devi@62 | 1035 print "Unknown operating system"; |
devi@62 | 1036 exit(0); |
devi@62 | 1037 } |
devi@62 | 1038 } |
devi@62 | 1039 if ($Config{detach} =~ /^y/i) { |
devi@62 | 1040 #$Config{verbose} = "no"; |
devi@62 | 1041 my $pid = fork; |
devi@62 | 1042 exit if $pid; |
devi@62 | 1043 die "Couldn't fork: $!" unless defined ($pid); |
devi@27 | 1044 |
devi@62 | 1045 open(PIDFILE, ">", $Config{agent_pidfile}) |
devi@62 | 1046 or die "Can't open pidfile ". $Config{agent_pidfile}. " for wrting: $!"; |
devi@62 | 1047 print PIDFILE $$; |
devi@62 | 1048 close(PIDFILE); |
devi@27 | 1049 |
devi@62 | 1050 for my $handle (*STDIN, *STDOUT, *STDERR) { |
devi@62 | 1051 open ($handle, "+<", "/dev/null") |
devi@62 | 1052 or die "can't reopen $handle to /dev/null: $!" |
devi@62 | 1053 } |
devi@27 | 1054 |
devi@62 | 1055 POSIX::setsid() |
devi@62 | 1056 or die "Can't start a new session: $!"; |
devi@27 | 1057 |
devi@62 | 1058 $0 = $Config{"l3-agent"}; |
devi@62 | 1059 |
devi@98 | 1060 $SIG{INT} = $SIG{TERM} = \&process_was_killed; |
devi@98 | 1061 $SIG{HUP} = \&reload; |
devi@98 | 1062 |
devi@62 | 1063 } |
devi@62 | 1064 while (not $Killed) { |
devi@62 | 1065 @Command_Lines = (); |
devi@62 | 1066 @Command_Lines_Index = (); |
devi@62 | 1067 load_cache_stat(); |
devi@62 | 1068 load_command_lines($Config{"input"}, $Config{"input_mask"}); |
devi@62 | 1069 if (@Command_Lines) { |
devi@74 | 1070 sort_command_lines; |
devi@65 | 1071 #process_command_lines; |
igor@155 | 1072 print_commands_to_sqlite_cache($Config{"cache_sqlite"}); |
devi@62 | 1073 print_command_lines($Config{"cache"}); |
devi@62 | 1074 } |
devi@62 | 1075 save_cache_stat(); |
devi@62 | 1076 if (-e $Config{cache} && (stat($Config{cache}))[7]) { |
devi@62 | 1077 send_cache() && unlink($Config{cache}); |
devi@62 | 1078 } |
devi@62 | 1079 sleep($Config{"daemon_sleep_interval"} || 1); |
devi@62 | 1080 } |
devi@62 | 1081 |
devi@62 | 1082 unlink $Config{agent_pidfile}; |
devi@62 | 1083 } |
devi@23 | 1084 |
devi@23 | 1085 } |
devi@23 | 1086 |
devi@23 | 1087 sub init_variables |
devi@23 | 1088 { |
devi@23 | 1089 } |