| 1 | package XAS::Apps::Rotate; | ||
| 2 | |||
| 3 | our $VERSION = '0.04'; | ||
| 4 | |||
| 5 | use Try::Tiny; | ||
| 6 | |||
| 7 | use XAS::Class | ||
| 8 | debug => 0, | ||
| 9 | version => $VERSION, | ||
| 10 | base => 'XAS::Lib::App', | ||
| 11 | utils => 'compress glob2regex', | ||
| 12 | constants => 'TRUE FALSE WILDCARD', | ||
| 13 | filesystem => 'Dir File', | ||
| 14 | constant => { | ||
| 15 | X2SECS => { | ||
| 16 | 's' => 1, | ||
| 17 | 'm' => 60, | ||
| 18 | 'h' => 60 * 60, | ||
| 19 | 'd' => 60 * 60 * 24, | ||
| 20 | 'w' => 60 * 60 * 24 * 7 | ||
| 21 | }, | ||
| 22 | X2BYTES => { | ||
| 23 | b => 1, | ||
| 24 | k => 1024, | ||
| 25 | m => 1024 * 1024, | ||
| 26 | g => 1024 * 1024 * 1024, | ||
| 27 | t => 1024 * 1024 * 1204 * 1024 | ||
| 28 | } | ||
| 29 | }, | ||
| 30 | messages => { | ||
| 31 | processing => "processing %s in %s", | ||
| 32 | age_params => "age parameter '%s' should match \\d+\\s+[smhdw]", | ||
| 33 | size_params => "size parameter '%s' should match \\d+\\s+[bkmg]", | ||
| 34 | compressing => "compressing %s", | ||
| 35 | copied => "copied %s to %s", | ||
| 36 | moved => "moved %s to %s", | ||
| 37 | nocompress => "compression failed for %s", | ||
| 38 | norecreate => "unable to recreate %s", | ||
| 39 | notail => "unable to copy %s lines into %s", | ||
| 40 | noexist => "%s did not exist", | ||
| 41 | nomatch => "nothing in %s to match %s", | ||
| 42 | nochange => "unable to change to %s" | ||
| 43 | }, | ||
| 44 | ; | ||
| 45 | |||
| 46 | # ---------------------------------------------------------------------- | ||
| 47 | # Global Variables | ||
| 48 | # ---------------------------------------------------------------------- | ||
| 49 | |||
| 50 | my $compressor; | ||
| 51 | my $modify_age; | ||
| 52 | my $create_age; | ||
| 53 | my $compress; | ||
| 54 | my $ifempty; | ||
| 55 | my $method; | ||
| 56 | my $missingok; | ||
| 57 | my $file_size; | ||
| 58 | my $create_new; | ||
| 59 | my $prolog; | ||
| 60 | my $epilog; | ||
| 61 | my $lines_or_files; | ||
| 62 | my $line_count; | ||
| 63 | my $file_count; | ||
| 64 | my $pause; | ||
| 65 | |||
| 66 | my $zipcmd; | ||
| 67 | my $gzipcmd; | ||
| 68 | my $bzipcmd; | ||
| 69 | my $compcmd; | ||
| 70 | my $tailcmd; | ||
| 71 | my $logfile; | ||
| 72 | |||
| 73 | my @wanted_files; | ||
| 74 | |||
| 75 | # ---------------------------------------------------------------------- | ||
| 76 | # Public Methods | ||
| 77 | # ---------------------------------------------------------------------- | ||
| 78 | |||
| 79 | sub run_cmd { | ||
| 80 | my $self = shift; | ||
| 81 | my $cmd = shift; | ||
| 82 | |||
| 83 | # | ||
| 84 | # Runs a command and places the output into the logfile. | ||
| 85 | # | ||
| 86 | |||
| 87 | $self->log->info("running '$cmd'"); | ||
| 88 | |||
| 89 | my @output = `$cmd 2>&1`; | ||
| 90 | foreach my $line (@output) { | ||
| 91 | |||
| 92 | $self->log->info(compress($line)); | ||
| 93 | |||
| 94 | } | ||
| 95 | |||
| 96 | } | ||
| 97 | |||
| 98 | sub get_extension { | ||
| 99 | my $self = shift; | ||
| 100 | |||
| 101 | # | ||
| 102 | # Return the compressor's extension. | ||
| 103 | # | ||
| 104 | |||
| 105 | my $ext; | ||
| 106 | |||
| 107 | # Try to guess what the right extension would be | ||
| 108 | |||
| 109 | $ext = '.gz' if ($compressor =~ /gzip/i); | ||
| 110 | $ext = '.zip' if ($compressor =~ /zip/i); | ||
| 111 | $ext = '.bz2' if ($compressor =~ /bzip2/i); | ||
| 112 | $ext = '.Z' if ($compressor =~ /compress/i); | ||
| 113 | |||
| 114 | return($ext); | ||
| 115 | |||
| 116 | } | ||
| 117 | |||
| 118 | sub is_true { | ||
| 119 | my $self = shift; | ||
| 120 | my $parm = shift; | ||
| 121 | |||
| 122 | # | ||
| 123 | # Checks to see if the parameter is the string 't', 'true' or the number 1. | ||
| 124 | # | ||
| 125 | |||
| 126 | my @truth = qw(yes true t 1 0e0); | ||
| 127 | |||
| 128 | return scalar(grep {lc($parm) eq $_} @truth); | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | sub is_false { | ||
| 134 | my $self = shift; | ||
| 135 | my $parm = shift; | ||
| 136 | |||
| 137 | # | ||
| 138 | # Checks to see if the parameter is the string 'f' or 'false' or the number 0. | ||
| 139 | # | ||
| 140 | |||
| 141 | my @truth = qw(no false f 0); | ||
| 142 | |||
| 143 | return scalar(grep {lc($parm) eq $_} @truth); | ||
| 144 | |||
| 145 | } | ||
| 146 | |||
| 147 | sub is_oldder { | ||
| 148 | my $self = shift; | ||
| 149 | my $age_diff = shift; | ||
| 150 | my $age = shift; | ||
| 151 | |||
| 152 | # | ||
| 153 | # Parses $age_diff and checks to see if the $age is newer than | ||
| 154 | # the current time minus $age_diff. That is we return | ||
| 155 | # TRUE if a file is too new based on the criteria $age_diff and $age | ||
| 156 | # and the implicit parameter: the current time. | ||
| 157 | # | ||
| 158 | |||
| 159 | return FALSE unless $age_diff; | ||
| 160 | $self->log->debug("age_diff = $age_diff, age = $age"); | ||
| 161 | |||
| 162 | # Parse age string, e.g. 20 hours, 3 days, 4 weeks, etc. | ||
| 163 | |||
| 164 | if ( $age_diff !~ /(\d+)\s+([smhdw])/i ) { | ||
| 165 | |||
| 166 | $self->log->warn_msg('age_params', $age_diff); | ||
| 167 | |||
| 168 | return FALSE; | ||
| 169 | |||
| 170 | } else { | ||
| 171 | |||
| 172 | my $sec_diff = $1 * X2SECS->{lc $2}; | ||
| 173 | my $diff = time() - $sec_diff; | ||
| 174 | $self->log->debug("age = $age; diff = $diff"); | ||
| 175 | my $ret = $age <= $diff; | ||
| 176 | $self->log->debug("is_oldder: $ret"); | ||
| 177 | |||
| 178 | return $ret; | ||
| 179 | |||
| 180 | } | ||
| 181 | |||
| 182 | } | ||
| 183 | |||
| 184 | sub is_bigger { | ||
| 185 | my $self = shift; | ||
| 186 | my $size_spec = shift; | ||
| 187 | my $size = shift; | ||
| 188 | |||
| 189 | # | ||
| 190 | # Parses $size_spec and checks to see if the $size is bigger than | ||
| 191 | # that. We return TRUE if a file is too big. | ||
| 192 | # | ||
| 193 | |||
| 194 | return FALSE unless $size_spec; | ||
| 195 | $self->log->debug("size_spec = $size_spec, size = $size"); | ||
| 196 | |||
| 197 | # Parse size string, e.g. 20 MB, 3 bytes, 4 GB, etc. | ||
| 198 | |||
| 199 | if ($size_spec !~ /(\d+)\s+([bkmgt])/i ) { | ||
| 200 | |||
| 201 | $self->log->warn_msg('size_params', $size_spec); | ||
| 202 | |||
| 203 | return FALSE; | ||
| 204 | |||
| 205 | } else { | ||
| 206 | |||
| 207 | my $max_size = $1 * X2BYTES->{lc $2}; | ||
| 208 | my $ret = $size >= $max_size; | ||
| 209 | |||
| 210 | $self->log->debug("is_bigger: $ret"); | ||
| 211 | |||
| 212 | return $ret; | ||
| 213 | |||
| 214 | } | ||
| 215 | |||
| 216 | } | ||
| 217 | |||
| 218 | sub compress_file { | ||
| 219 | my $self = shift; | ||
| 220 | my $filename = shift; | ||
| 221 | |||
| 222 | # | ||
| 223 | # Compress the .0th version of this file. | ||
| 224 | # | ||
| 225 | |||
| 226 | my $cmd; | ||
| 227 | my $ext; | ||
| 228 | |||
| 229 | $self->log->debug("entering compress_file()"); | ||
| 230 | |||
| 231 | if ($self->is_true($compress)) { | ||
| 232 | |||
| 233 | my $file = $filename->path . ".0"; | ||
| 234 | $ext = get_extension(); | ||
| 235 | $self->log->info_msg('compressing', $file); | ||
| 236 | |||
| 237 | if ($compressor =~ /zip/i) { | ||
| 238 | |||
| 239 | $cmd = $zipcmd . " -m " . $file . $ext . " " . $file; | ||
| 240 | $self->log->debug("ZIP command = $cmd"); | ||
| 241 | |||
| 242 | } elsif ($compressor =~ /gzip/i) { | ||
| 243 | |||
| 244 | $cmd = $gzipcmd . " " . $file; | ||
| 245 | $self->log->debug("GZIP command = $cmd"); | ||
| 246 | |||
| 247 | } elsif ($compressor =~ /bzip2/i) { | ||
| 248 | |||
| 249 | $cmd = $bzipcmd . " " . $file; | ||
| 250 | $self->log->debug("BZIP2 command = $cmd"); | ||
| 251 | |||
| 252 | } elsif ($compressor =~ /compress/i) { | ||
| 253 | |||
| 254 | $cmd = $compcmd . " " . $file; | ||
| 255 | $self->log->debug("COMPRESS command = $cmd"); | ||
| 256 | |||
| 257 | } | ||
| 258 | |||
| 259 | $self->run_cmd($cmd); | ||
| 260 | $self->log->error_msg('nocompress', $file) if $?; | ||
| 261 | |||
| 262 | } | ||
| 263 | |||
| 264 | $self->log->debug("leaving compress_file()"); | ||
| 265 | |||
| 266 | } | ||
| 267 | |||
| 268 | sub recreate_file { | ||
| 269 | my $self = shift; | ||
| 270 | my $filename = shift; | ||
| 271 | |||
| 272 | # | ||
| 273 | # Recreate the file if needed. | ||
| 274 | # | ||
| 275 | |||
| 276 | my $cmd; | ||
| 277 | my $tmpname; | ||
| 278 | |||
| 279 | $self->log->debug("entering recreate_file()"); | ||
| 280 | $self->log->debug("filename = " . $filename->path); | ||
| 281 | |||
| 282 | if ($self->is_true($create_new)) { | ||
| 283 | |||
| 284 | if ($lines_or_files =~ /files/i) { | ||
| 285 | |||
| 286 | try { | ||
| 287 | |||
| 288 | # thanks MS, Windows doesn't delete files in a timely | ||
| 289 | # manner, so the below code is needed to ensure file | ||
| 290 | # deletion... | ||
| 291 | |||
| 292 | for (1..20) { | ||
| 293 | |||
| 294 | last unless ($filename->exists); | ||
| 295 | $filename->delete(); | ||
| 296 | sleep($pause); | ||
| 297 | |||
| 298 | } | ||
| 299 | |||
| 300 | $filename->open('w'); | ||
| 301 | |||
| 302 | } catch { | ||
| 303 | |||
| 304 | my $ex = $_; | ||
| 305 | my $ref = ref($ex); | ||
| 306 | |||
| 307 | $self->log->warn_msg('norecreate', $filename->path); | ||
| 308 | |||
| 309 | }; | ||
| 310 | |||
| 311 | } else { | ||
| 312 | |||
| 313 | $tmpname = $filename->path . '.0'; | ||
| 314 | |||
| 315 | if ($^O eq 'MSWin32') { | ||
| 316 | |||
| 317 | # | ||
| 318 | # The Tail command from the Windows 2003 Resource Kit | ||
| 319 | # is broken. The -n switch does not work, so the default | ||
| 320 | # number of lines is used. | ||
| 321 | # | ||
| 322 | |||
| 323 | $cmd = $tailcmd . " " . $tmpname . ' > ' . $filename->path; | ||
| 324 | |||
| 325 | } else { | ||
| 326 | |||
| 327 | $cmd = $tailcmd . ' -n ' . $line_count . ' ' . $tmpname . ' > ' . $filename->path; | ||
| 328 | |||
| 329 | } | ||
| 330 | |||
| 331 | $self->run_cmd($cmd); | ||
| 332 | $self->log->error_msg(('notail', $line_count, $filename) if $?; | ||
| 333 | |||
| 334 | } | ||
| 335 | |||
| 336 | } | ||
| 337 | |||
| 338 | $self->log->debug("leaving recreate_file()"); | ||
| 339 | |||
| 340 | } | ||
| 341 | |||
| 342 | sub rotate_file { | ||
| 343 | my $self = shift; | ||
| 344 | my $filename = shift; | ||
| 345 | |||
| 346 | # | ||
| 347 | # Rotate the files. | ||
| 348 | # | ||
| 349 | |||
| 350 | my $x; | ||
| 351 | my $count; | ||
| 352 | my $tmpname; | ||
| 353 | my $prvname; | ||
| 354 | my $first = TRUE; | ||
| 355 | my $ext = $self->get_extension(); | ||
| 356 | |||
| 357 | $self->log->debug("entering rotate_file(); filename = $filename"); | ||
| 358 | |||
| 359 | for ($count = $file_count - 1; $count > 0; $count--) { | ||
| 360 | |||
| 361 | $tmpname = File($filename->path . '.' . $count); | ||
| 362 | $tmpname = File($tmpname->path . $ext) if ($self->is_true($compress)); | ||
| 363 | $self->log->debug("tmpname = $tmpname"); | ||
| 364 | |||
| 365 | if ($first) { | ||
| 366 | |||
| 367 | unlink($tmpname->path); | ||
| 368 | $self->log->debug("removed $tmpname"); | ||
| 369 | $first = FALSE; | ||
| 370 | |||
| 371 | } | ||
| 372 | |||
| 373 | $x = $count - 1; | ||
| 374 | $prvname = File($filename->path . '.' . $x); | ||
| 375 | $prvname = File($prvname->path . $ext) if ($self->is_true($compress)); | ||
| 376 | $self->log->debug("prvname = $prvname"); | ||
| 377 | |||
| 378 | if ($prvname->exists) { | ||
| 379 | |||
| 380 | if ($method =~ /copy/i) { | ||
| 381 | |||
| 382 | $prvname->copy($tmpname->path); | ||
| 383 | $self->log->info_msg('copied', $prvname, $tmpname); | ||
| 384 | |||
| 385 | } else { | ||
| 386 | |||
| 387 | $prvname->move($tmpname->path); | ||
| 388 | $self->log->info_msg('moved', $prvname, $tmpname); | ||
| 389 | |||
| 390 | } | ||
| 391 | |||
| 392 | } else { | ||
| 393 | |||
| 394 | $self->log->warn_msg('noexist', $prvname); | ||
| 395 | |||
| 396 | } | ||
| 397 | |||
| 398 | } | ||
| 399 | |||
| 400 | if ($method =~ /copy/i) { | ||
| 401 | |||
| 402 | $filename->copy($filename->path . '.0'); | ||
| 403 | $self->log->info_msg('copied', $filename, $filename. '.0'); | ||
| 404 | |||
| 405 | } else { | ||
| 406 | |||
| 407 | $filename->move($filename->path . '.0'); | ||
| 408 | $self->log->info_msg('moved', $filename, $filename . '.0'); | ||
| 409 | |||
| 410 | } | ||
| 411 | |||
| 412 | $self->log->debug("leaving rotate_file()"); | ||
| 413 | |||
| 414 | } | ||
| 415 | |||
| 416 | sub process_files { | ||
| 417 | my $self = shift; | ||
| 418 | |||
| 419 | # | ||
| 420 | # Process the files. | ||
| 421 | # | ||
| 422 | |||
| 423 | my $ran_prolog = FALSE; | ||
| 424 | |||
| 425 | $self->log->debug("entering process_files()"); | ||
| 426 | |||
| 427 | if (($prolog) && (scalar(@wanted_files) > 0)) { | ||
| 428 | |||
| 429 | |||
| 430 | $ran_prolog = TRUE; | ||
| 431 | $self->run_cmd($prolog); | ||
| 432 | sleep $pause; | ||
| 433 | |||
| 434 | } | ||
| 435 | |||
| 436 | while (my $filename = pop(@wanted_files)) { | ||
| 437 | |||
| 438 | $self->log->debug("processing $filename->path"); | ||
| 439 | |||
| 440 | $self->rotate_file($filename); | ||
| 441 | $self->recreate_file($filename); | ||
| 442 | $self->compress_file($filename); | ||
| 443 | |||
| 444 | } | ||
| 445 | |||
| 446 | if (($epilog) && ($ran_prolog)) { | ||
| 447 | |||
| 448 | $self->run_cmd($epilog); | ||
| 449 | sleep $pause; | ||
| 450 | |||
| 451 | } | ||
| 452 | |||
| 453 | $self->log->debug("leaving process_files()"); | ||
| 454 | |||
| 455 | } | ||
| 456 | |||
| 457 | sub find_files { | ||
| 458 | my $self = shift; | ||
| 459 | my $from = shift; | ||
| 460 | |||
| 461 | # | ||
| 462 | # Scan the local directory looking for files to rotate. | ||
| 463 | # | ||
| 464 | |||
| 465 | $self->log->debug("entering find_files()"); | ||
| 466 | |||
| 467 | my @files; | ||
| 468 | my $fdir = Dir($from->volume, $from->dir); | ||
| 469 | |||
| 470 | $self->log->info_msg('processing', $from->name, $fdir->path); | ||
| 471 | |||
| 472 | if ($from->name =~ WILDCARD) { | ||
| 473 | |||
| 474 | my $regex = glob2regex($from->name); | ||
| 475 | my $pattern = qr/$regex/; | ||
| 476 | |||
| 477 | @files = grep ( $_->path =~ /$pattern/, $fdir->files() ); | ||
| 478 | |||
| 479 | } else { | ||
| 480 | |||
| 481 | push(@files, $from); | ||
| 482 | |||
| 483 | } | ||
| 484 | |||
| 485 | foreach my $file (@files) { | ||
| 486 | |||
| 487 | next unless($file->exists); | ||
| 488 | |||
| 489 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev, | ||
| 490 | $size,$atime,$mtime,$ctime,$blksize,$blocks) = $file->stat; | ||
| 491 | |||
| 492 | $self->log->debug("file = $file, size = $size, ctime = $ctime, mtime = $mtime"); | ||
| 493 | |||
| 494 | if ($self->is_oldder($modify_age, $mtime)) { | ||
| 495 | |||
| 496 | if (($size != 0) && ($self->is_false($ifempty))) { | ||
| 497 | |||
| 498 | push(@wanted_files, $file); | ||
| 499 | $self->log->debug("added $file; modify-age"); | ||
| 500 | |||
| 501 | } | ||
| 502 | |||
| 503 | } elsif ($self->is_oldder($create_age, $ctime)) { | ||
| 504 | |||
| 505 | if (($size != 0) && ($self->is_false($ifempty))) { | ||
| 506 | |||
| 507 | push(@wanted_files, $file); | ||
| 508 | $self->log->debug("added $file; create-age"); | ||
| 509 | |||
| 510 | } | ||
| 511 | |||
| 512 | } elsif ($self->is_bigger($file_size, $size)) { | ||
| 513 | |||
| 514 | if (($size != 0) && ($self->is_false($ifempty))) { | ||
| 515 | |||
| 516 | push(@wanted_files, $file); | ||
| 517 | $self->log->debug("added $file; file-size"); | ||
| 518 | |||
| 519 | } | ||
| 520 | |||
| 521 | } | ||
| 522 | |||
| 523 | } | ||
| 524 | |||
| 525 | $self->log->debug("the wanted_files array has $#wanted_files elements"); | ||
| 526 | |||
| 527 | if ($#wanted_files < 0) { | ||
| 528 | |||
| 529 | $self->log->warn_msg('nomatch', $fdir->path, $from->name) if ($self->is_true($missingok)); | ||
| 530 | |||
| 531 | } | ||
| 532 | |||
| 533 | $self->log->debug("leaving find_files()"); | ||
| 534 | |||
| 535 | } | ||
| 536 | |||
| 537 | sub setup { | ||
| 538 | my $self = shift; | ||
| 539 | |||
| 540 | $compressor = $self->cfg->val('settings', 'compressor', 'zip'); | ||
| 541 | $zipcmd = $self->cfg->val('settings', 'zip-command', 'c:\bin\zip.exe'); | ||
| 542 | $gzipcmd = $self->cfg->val('settings', 'gzip-command', 'c:\bin\gzip.exe'); | ||
| 543 | $bzipcmd = $self->cfg->val('settings', 'bzip2-command', 'c:\bin\bzip2.exe'); | ||
| 544 | $compcmd = $self->cfg->val('settings', 'compress-command', 'c:\bin\compress.exe'); | ||
| 545 | $tailcmd = $self->cfg->val('settings', 'tail-command', 'c:\bin\tail.exe'); | ||
| 546 | |||
| 547 | } | ||
| 548 | |||
| 549 | sub main { | ||
| 550 | my $self = shift; | ||
| 551 | |||
| 552 | $self->setup(); | ||
| 553 | |||
| 554 | my $directory; | ||
| 555 | my @sections = $self->cfg->Sections(); | ||
| 556 | |||
| 557 | $self->log->info('start run'); | ||
| 558 | $self->log->debug("found $#sections Sections"); | ||
| 559 | |||
| 560 | foreach my $section (@sections) { | ||
| 561 | |||
| 562 | $self->log->debug("section = $section"); | ||
| 563 | |||
| 564 | next if ($section =~ /settings/i); | ||
| 565 | |||
| 566 | $directory = File($section); | ||
| 567 | |||
| 568 | $modify_age = $self->cfg->val($section, 'modify-age'); | ||
| 569 | $create_age = $self->cfg->val($section, 'create-age'); | ||
| 570 | $file_size = $self->cfg->val($section, 'file-size'); | ||
| 571 | $compress = $self->cfg->val($section, 'compress', 'false'); | ||
| 572 | $create_new = $self->cfg->val($section, 'create-new', 'false'); | ||
| 573 | $ifempty = $self->cfg->val($section, 'ifempty', 'false'); | ||
| 574 | $file_count = $self->cfg->val($section, 'file-count', '5'); | ||
| 575 | $method = $self->cfg->val($section, 'method', 'copy'); | ||
| 576 | $missingok = $self->cfg->val($section, 'missingok', 'true'); | ||
| 577 | $line_count = $self->cfg->val($section, 'line-count', '10'); | ||
| 578 | $prolog = $self->cfg->val($section, 'prolog'); | ||
| 579 | $epilog = $self->cfg->val($section, 'epilog'); | ||
| 580 | $pause = $self->cfg->val($section, 'pause', '10'); | ||
| 581 | $lines_or_files = $self->cfg->val($section, 'lines-or-files', 'files'); | ||
| 582 | |||
| 583 | $self->find_files($directory); | ||
| 584 | $self->process_files(); | ||
| 585 | |||
| 586 | } | ||
| 587 | |||
| 588 | $self->log->info('stop run'); | ||
| 589 | |||
| 590 | } | ||
| 591 | |||
| 592 | sub options { | ||
| 593 | my $self = shift; | ||
| 594 | |||
| 595 | return { | ||
| 596 | 'cfgfile=s' => sub { | ||
| 597 | $cfgfile} = File($_[1]); | ||
| 598 | $self->env->cfgfile($cfgfile); | ||
| 599 | }, | ||
| 600 | }; | ||
| 601 | |||
| 602 | } | ||
| 603 | |||
| 604 | # ---------------------------------------------------------------------- | ||
| 605 | # Private Methods | ||
| 606 | # ---------------------------------------------------------------------- | ||
| 607 | |||
| 608 | sub init { | ||
| 609 | my $class = shift; | ||
| 610 | |||
| 611 | my $self = $class->SUPER::init(@_); | ||
| 612 | |||
| 613 | $self->{cfg} = Config::IniFiles->new( | ||
| 614 | -file => $self->cfgfile->path, | ||
| 615 | -default => 'settings', | ||
| 616 | ) or $self->throw_msg( | ||
| 617 | 'xas.apps.rotate.init.badini', | ||
| 618 | 'badini', | ||
| 619 | $self->cfgfile->path | ||
| 620 | ); | ||
| 621 | |||
| 622 | return $self; | ||
| 623 | |||
| 624 | } | ||
| 625 | |||
| 626 | 1; | ||
| 627 | |||
| 628 | __END__ | ||
| 629 | |||
| 630 | =head1 NAME | ||
| 631 | |||
| 632 | XAS::Apps::Rotate - Rotate files based on a config file | ||
| 633 | |||
| 634 | =head1 SYNOPSIS | ||
| 635 | |||
| 636 | use XAS::Apps::Rotate; | ||
| 637 | |||
| 638 | my $app = XAS::Apps::Rotate->new( | ||
| 639 | -throws => 'rotate', | ||
| 640 | -facility => 'systems', | ||
| 641 | -priority => 'warn', | ||
| 642 | ); | ||
| 643 | |||
| 644 | exit $app->run; | ||
| 645 | |||
| 646 | =head1 DESCRIPTION | ||
| 647 | |||
| 648 | This module is used to rotate files. | ||
| 649 | |||
| 650 | =head1 CONFIGURATION | ||
| 651 | |||
| 652 | The configuration file uses the standard windows .ini file. It has the | ||
| 653 | following format. | ||
| 654 | |||
| 655 | [settings] | ||
| 656 | compressor = zip - default file compressor | ||
| 657 | zip-command - zip command defaults to 'c:\bin\zip.exe' | ||
| 658 | gzip-command - gzip command defaults to 'c:\bin\gzip.exe' | ||
| 659 | bzip2-command - bzip2 command defaults to 'c:\bin\bzip2.exe' | ||
| 660 | compress-command - compress command defaults to 'c:\bin\compress.exe' | ||
| 661 | tail-command - tail command defaults to 'c:\bin\tail.exe' | ||
| 662 | |||
| 663 | [log\test.log] - file to process, may have DOS wildcards | ||
| 664 | compress = true - wither to compress the file | ||
| 665 | method = move - how to handle the file | ||
| 666 | missingok = false - whither a missing file is OK | ||
| 667 | lines-or-files = files - | ||
| 668 | create-new = false - create a new file after rotation | ||
| 669 | modify-age - process based on last modification | ||
| 670 | create-age - process based in creation time | ||
| 671 | file-size - process based on file size | ||
| 672 | ifempty - process if file is empty | ||
| 673 | file-count - number of rotated files kept | ||
| 674 | line-count - how many line to transfer to new file | ||
| 675 | prolog - command to process before rotation | ||
| 676 | epilog - command to process after rotation | ||
| 677 | pause - number of seconds to pause after prolog and epilog | ||
| 678 | |||
| 679 | =head1 METHODS | ||
| 680 | |||
| 681 | =head2 main | ||
| 682 | |||
| 683 | Process the configuration file. | ||
| 684 | |||
| 685 | =head2 run_cmd($cmd) | ||
| 686 | |||
| 687 | Run a command and capture the output to the log file. | ||
| 688 | |||
| 689 | =over 4 | ||
| 690 | |||
| 691 | =item B<$cmd> | ||
| 692 | |||
| 693 | The command to run. | ||
| 694 | |||
| 695 | =back | ||
| 696 | |||
| 697 | =head2 get_extension | ||
| 698 | |||
| 699 | Return the file extension base on compressor type. | ||
| 700 | |||
| 701 | =head2 is_true($param) | ||
| 702 | |||
| 703 | Return TRUE or FALSE based on $param. If $param is 'true', or 1 then TRUE. | ||
| 704 | |||
| 705 | =head2 is_false($param) | ||
| 706 | |||
| 707 | Return TRUE or FALSE based on $param. If $param is 'false' or 0 then TRUE. | ||
| 708 | |||
| 709 | =head2 is_older($age_diff, $age) | ||
| 710 | |||
| 711 | Parses $age_diff and checks to see if the $age is newer than | ||
| 712 | the current time minus $age_diff. That is we return | ||
| 713 | TRUE if a file is too new based on the criteria $age_diff and $age | ||
| 714 | and the implicit parameter: the current time. | ||
| 715 | |||
| 716 | =head2 is_bigger($size_spec, $size) | ||
| 717 | |||
| 718 | Parses $size_spec and checks to see if the $size is bigger than | ||
| 719 | that. We return TRUE if a file is too big. | ||
| 720 | |||
| 721 | =head2 compress_file($filename) | ||
| 722 | |||
| 723 | Compress the file based on chosen compression type. | ||
| 724 | |||
| 725 | =over 4 | ||
| 726 | |||
| 727 | =item B<$filename> | ||
| 728 | |||
| 729 | The file to compress | ||
| 730 | |||
| 731 | =back | ||
| 732 | |||
| 733 | =head2 recreate_file($filename) | ||
| 734 | |||
| 735 | Recreate the file if needed. This checks the file-or-line config item. | ||
| 736 | If it is 'file' then a new file is created, if it is 'lines' then | ||
| 737 | the config item 'line-count' is used to copy that number of lines from | ||
| 738 | the end of the file into the beginning of the new file. | ||
| 739 | |||
| 740 | =over 4 | ||
| 741 | |||
| 742 | =item B<$filename> | ||
| 743 | |||
| 744 | The file to recreate. | ||
| 745 | |||
| 746 | =back | ||
| 747 | |||
| 748 | =head2 rotate_file($filename) | ||
| 749 | |||
| 750 | Perform the basic file rotation. | ||
| 751 | |||
| 752 | =over 4 | ||
| 753 | |||
| 754 | =item B<$filename> | ||
| 755 | |||
| 756 | The file to rotate. | ||
| 757 | |||
| 758 | =back | ||
| 759 | |||
| 760 | =head2 process_files | ||
| 761 | |||
| 762 | Process the selected files. It preforms the following actions: | ||
| 763 | |||
| 764 | =over 4 | ||
| 765 | |||
| 766 | =item 1 Process the prolog command. | ||
| 767 | |||
| 768 | =item 2 Rotate the file. | ||
| 769 | |||
| 770 | =item 3 Recreate the new file. | ||
| 771 | |||
| 772 | =item 4 Compress the rotated file | ||
| 773 | |||
| 774 | =item 5 Process the epilog command. | ||
| 775 | |||
| 776 | =back | ||
| 777 | |||
| 778 | =head2 find_files | ||
| 779 | |||
| 780 | Scan the local directory looking for files to rotate. | ||
| 781 | |||
| 782 | =head1 SEE ALSO | ||
| 783 | |||
| 784 | L< Rotate|Rotate> | ||
| 785 | |||
| 786 | =head1 AUTHOR | ||
| 787 | |||
| 788 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 789 | |||
| 790 | =head1 COPYRIGHT AND LICENSE | ||
| 791 | |||
| 792 | Copyright (C) 2014 Kevin L. Esteb | ||
| 793 | |||
| 794 | This library is free software; you can redistribute it and/or modify | ||
| 795 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 796 | at your option, any later version of Perl 5 you may have available. | ||
| 797 | |||
| 798 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 799 | |||
| 800 | =cut |