#!/opt/vdops/bin/perl # Runs from cron every night under 'tocops'; saves apager.txt, nodewatch.txt, # and ops.txt to 'yesterday' versions and then wipes *.txt, all in # /home/tocops/logs # V Who When What # --------------------------------------------------------------------------- # 1.3.1 skendric 03-09-2011 Write yesterday's date # 1.3.0 skendric 03-09-2011 Save yesterday's logs # 1.2.0 skendric 09-17-2009 Add 'emergency' to file list # 1.1.9 kkawakub 12-29-2008 Add 'ipsec' to file list # 1.1.8 skendric 05-28-2008 Add 'vpn' back to log list # 1.1.7 skendric 04-29-2008 Update log list # 1.1.6 skendric 04-23-2008 Remove dialin; check for file existence # 1.1.5 skendric 01-25-2008 Syntax clean-up # 1.1.4 kkawakub 10-24-2007 Add WebVPN # 1.1.3 skendric 06-04-2005 Add ips and nessus # 1.1.2 skendric 04-17-2005 Remove 'packet' from @logs # 1.1.1 skendric 07-28-2004 Add 'packet' to @logs # 1.1.0 skendric 02-27-2004 Add 'use strict', remove html printing # 1.0.0 skendric 12-13-2002 First Version # Load modules use strict; use warnings; use feature 'say'; use Date::Simple; use Fcntl qw(:DEFAULT :flock); use File::Copy 'cp'; # Declare variables my $date_file; my $dir; my @logs; my $yesterday; my @yesterday; # Define variables $date_file = '/var/www/html/toc/yesterday/yesterday.txt'; $dir = '/home/tocops/logs'; @logs = qw /apager emergency ips ipsec nodewatch ops ups vpn webvpn webvpn_detailed wireless/; @yesterday = qw/apager nodewatch ops/; # Save logs for yesterday LOG: for my $log (@yesterday) { my ($log_now, $log_old); $log_now .= $log . '.txt'; $log_old .= $log . '-yesterday.txt'; next LOG unless -w "$dir/$log_now"; cp("$dir/$log_now", "$dir/$log_old") or warn "Cannot copy $dir/$log_now to $dir/$log_old"; } # Write yesterday's date $yesterday = Date::Simple->today() - 1; open my $handle, '>', $date_file or warn "Cannot open $date_file: $!"; printf {$handle} "$yesterday\n"; close $handle or warn "Cannot close $date_file: $!"; # Wipe logs FILE: for my $log (@logs) { # Open and wipe current file $log .= '.txt'; # Skip if unless we can write to it next FILE unless -w "$dir/$log"; # Open and wipe sysopen(LOG, "$dir/$log", O_RDWR | O_CREAT) or die "Cannot open $log: $!\n"; flock(LOG, LOCK_EX) or die "Cannot write-lock $log: $!\n"; truncate(LOG, 0) or warn "Cannot truncate $log: $!\n"; # Close file close LOG or warn "Cannot close $log: $!\n"; }