1#! @PERL@ 2eval 'exec @PERL@ -S $0 ${1+"$@"}' 3 if 0; 4 5# -*- Perl -*- 6# Copyright (c) 1997 - 2003 7# Motoyuki Kasahara 8# 9# This program is free software; you can redistribute it and/or modify 10# it under the terms of the GNU General Public License as published by 11# the Free Software Foundation; either version 2, or (at your option) 12# any later version. 13# 14# This program is distributed in the hope that it will be useful, 15# but WITHOUT ANY WARRANTY; without even the implied warranty of 16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17# GNU General Public License for more details. 18# 19 20# Set output record separator to ' '. 21$, = ' '; 22 23# Set umask 24umask(022); 25 26# PATH 27$ENV{'PATH'} = '/usr/bin:/bin'; 28$ENV{'PATH'} .= ':/usr/ucb' if (-d '/usr/ucb'); 29 30# Program name, program version and mailing address. 31$program_name = 'ebndaily'; 32$program_version = '@VERSION@'; 33$mailing_address = '@MAILING_ADDRESS@'; 34 35# Statistics commands. 36$ebnstat = '@libexecdir@/@EBNSTAT@'; 37$ndtpstat = '@libexecdir@/@NDTPSTAT@'; 38$ebhtstat = '@libexecdir@/@EBHTSTAT@'; 39 40# Log file. 41$log_file = '@logdir@/ebnetd.log'; 42 43# How many generataions of syslog files are kept. 44$ages = 7; 45 46# `commpress' and `gzip' Command. 47$compress = '@COMPRESS@'; 48$gzip = '@GZIPCMD@'; 49$bzip2 = '@BZIP2@'; 50$gzip_options = ''; 51 52# How to compress old logs; `gzip', `compress', or `none'. 53$compressor = 'none'; 54 55# Compression programs and suffixes they usually add to the filename. 56%compressors = ('gzip', '.gz', 57 'bzip2', '.bz2', 58 'compress', '.Z', 59 'none', ''); 60 61# Command to send mail which accepts "-s subject" option. 62$mailx = '@MAILX@'; 63 64# Whether to send a report mail or not. 65$report_mail_flag = 1; 66 67# Usage 68$help_message = "Usage: $program_name [option...] mail-address... 69Options: 70 -a INTEGER --ages INTEGER 71 keep INTEGER ages of old syslog files 72 (default: $ages) 73 -c TYPE --compressor TYPE 74 how to compress old syslog files; compress, gzip, 75 bzip2 or none (default: $compressor) 76 -h --help display this help, then exit 77 -l FILE --log-file FILE specify a syslog file 78 (default: $log_file) 79 -n --no-mail do not send report mail 80 -v --version display version number, then exit 81 -1 --fast compress faster when using gzip or bzip2 82 -9 --best compress better when using gzip or bzip2 83 84Arguemnts: 85 mail-address receipient of a report from $program_name. 86 87Report bugs to $mailing_address. 88"; 89 90# Copyright message. 91$copyright = "Copyright (c) 1997 - 2003 92 Motoyuki Kasahara 93"; 94 95# `try ...' message. 96$try_help_message = "try \`$0 --help\' for more information\n"; 97 98# Command line options. 99@long_options = ('-a --ages required-argument', 100 '-c --compressor required-argument', 101 '-h --help no-argument', 102 '-l --log-file required-argument', 103 '-n --no-mail no-argument', 104 '-v --version no-argument', 105 '-1 --fast no-argument', 106 '-2 no-argument', 107 '-3 no-argument', 108 '-4 no-argument', 109 '-5 no-argument', 110 '-6 no-argument', 111 '-7 no-argument', 112 '-8 no-argument', 113 '-9 --best no-argument'); 114 115# 116# Parse command line options. 117# 118&getopt_initialize(@long_options); 119while (($option_name, $option_argument) = &getopt_long) { 120 if ($option_name eq '-a') { 121 if ($option_argument !~ /^\d+$/ || $option_argument <= 0) { 122 warn "$0: ages must be an integral number and greater than 0.\n"; 123 exit(1); 124 } 125 $ages = $option_argument; 126 } elsif ($option_name eq '-c') { 127 if ($option_argument !~ /^(compress|gzip|bzip2|none)$/i) { 128 warn "$0: unknown compressor \`$option_argument\'\n"; 129 warn $try_help_message; 130 exit(1); 131 } 132 $compressor = "\L$option_argument"; 133 } elsif ($option_name eq '-h') { 134 print $help_message; 135 exit(0); 136 } elsif ($option_name eq '-l') { 137 $log_file = $option_argument; 138 139 } elsif ($option_name eq '-n') { 140 $report_mail_flag = 0; 141 142 } elsif ($option_name eq '-v') { 143 print "$program_name (EBNETD) version $program_version\n"; 144 print $copyright; 145 exit(0); 146 } elsif ($option_name =~ /^-[1-9]$/) { 147 $gzip_options = $option_argument; 148 } else { 149 warn $try_help_message; 150 exit(1); 151 } 152} 153 154if (@ARGV == 0) { 155 warn "$0: too few argument\n"; 156 warn $try_help_message; 157 exit(1); 158} 159 160die "$0: gzip not found.\n" if ($compressor eq 'gzip' && $gzip eq 'no'); 161die "$0: bzip2 not found.\n" if ($compressor eq 'bzip2' && $bzip2 eq 'no'); 162 163# 164# Set signal handlers. 165# 166$SIG{'HUP'} = 'signal_handler'; 167$SIG{'INT'} = 'signal_handler'; 168$SIG{'QUIT'} = 'signal_handler'; 169$SIG{'TERM'} = 'signal_handler'; 170 171sub signal_handler { 172 close(SAVEOUT); 173 close(SAVEERR); 174 if (-f $errfile && !unlink("$errfile")) { 175 warn "$0: cannot unlink the file, $!: $errfile\n"; 176 } 177 if (-f $statfile && !unlink("$statfile")) { 178 warn "$0: cannot unlink the file, $!: $statfile\n"; 179 } 180 exit(1); 181} 182 183# 184# Redirect STDOUT and STDERR to temporary files. 185# 186$error_file = "/tmp/ebndaye$$"; 187$stat_file = "/tmp/ebndays$$"; 188 189if (-f $error_file && !unlink("$error_file")) { 190 warn "$0: cannot unlink the file, $!: $error_file\n"; 191} 192if (-f $stat_file && !unlink("$stat_file")) { 193 warn "$0: cannot unlink the file, $!: $stat_file\n"; 194} 195 196open(SAVEERR, ">&STDERR"); 197open(SAVEOUT, ">&STDOUT"); 198if (!open(STDERR, ">$error_file")) { 199 print SAVEERR "cannot open the file $error_file, $!\n"; 200 exit 1; 201} 202if (!open(STDOUT, ">$stat_file")) { 203 print SAVEERR "cannot open the file $stat_file, $!\n"; 204 exit 1; 205} 206select(STDOUT); 207$| = 1; 208 209# 210# Get statistics. 211# 212warn "failed to execute \`$ebnstat $log_file\'\n" 213 if (system("$ebnstat $log_file") >> 8 != 0); 214warn "failed to execute \`$ndtpstat $log_file\'\n" 215 if (system("$ndtpstat $log_file") >> 8 != 0); 216warn "failed to execute \`$ebhtstat $log_file\'\n" 217 if (system("$ebhtstat $log_file") >> 8 != 0); 218 219# 220# Rotate old logs. 221# 222$suffix = $compressors{$compressor}; 223$i = $ages - 1; 224warn "$0: cannot remove $log_file.$i$suffix\n" 225 if (-f "$log_file.$i$suffix" && !unlink("$log_file.$i$suffix")); 226 227while (0 < $i) { 228 $j = $i--; 229 warn "$0: cannot move $log_file.$i$suffix to $log_file.$j$suffix\n" 230 if (-f "$log_file.$i$suffix" 231 && !rename("$log_file.$i$suffix", "$log_file.$j$suffix")); 232} 233 234warn "$0: cannot remove $log_file.0\n" 235 if (-f "$log_file.0" && !unlink("$log_file.0")); 236 237# 238# Copy `ebnetd.log' to `ebnetd.log.0'. 239# 240warn "$0: failed to execute \`cp $log_file $log_file.0\'\n" 241 if (system("cp $log_file $log_file.0") >> 8 != 0); 242 243# 244# Make a new log file. 245# 246warn "$0: cannot create new $log_file\n" 247 unless (open(LOGFILE, ">$log_file") && close(LOGFILE)); 248 249# 250# Compress the last log. 251# 252if ($compressor eq 'gzip') { 253 if (system("$gzip $gzip_options $log_file.0") >> 8 != 0) { 254 warn "$0: failed to execute \`$gzip $gzip_options $log_file.0\'\n"; 255 } 256} elsif ($compressor eq 'bzip2') { 257 if (system("$bzip2 $gzip_options $log_file.0") >> 8 != 0) { 258 warn "$0: failed to execute \`$bzip2 $gzip_options $log_file.0\'\n"; 259 } 260} elsif ($compressor eq 'compress') { 261 # It doesn't examine exit code. In some systems, `compress' returns 262 # with exit code 1 when an file is larger after compression. 263 system("$compress -c $log_file.0 > $log_file.0$suffix"); 264 warn "$0: cannot compress $log_file.0\n" if (! -f "$log_file.0$suffix"); 265 warn "$0: cannot remove $log_file.0\n" 266 if (-f "$log_file.0" && !unlink("$log_file.0")); 267} 268 269# 270# Output log sizes. 271# 272print "Log Size:\n"; 273warn "$0: failed to execute \`ls -l $log_file*\'\n" 274 if (system("ls -l $log_file*") >> 8 != 0); 275print "\n\n\n"; 276 277# 278# Error messages at executing ebndaily. 279# 280print "Error Messages at Executing $0:\n" 281 if (-f $error_file && -s $error_file); 282 283# 284# Send the report via mail. 285# 286if ($report_mail_flag 287 && system("cat $stat_file $error_file | $mailx -s \'ebnetd/ndtpd/ebhttpd daily log\' @ARGV") >> 8 != 0) { 288 print SAVEERR "failed to execute \`$mailx -s ... @ARGV\'\n"; 289 exit 1; 290} 291 292# 293# Remove temorary files. 294# 295unlink($stat_file, $error_file); 296 297exit; 298 299# Local Variables: 300# mode: perl 301# End: 302